omnetproject /omnetpp-4.1/src/sim/cdisplaystring.cc

Language C++ Lines 525
MD5 Hash f94ec88ee696baa81e82fcbea0d7ba3a Estimated Cost $10,269 (why?)
Repository https://bitbucket.org/indigopony/omnetproject View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//==========================================================================
//   CDISPLAYSTRING.CC  - part of
//                     OMNeT++/OMNEST
//            Discrete System Simulation in C++
//
//
//==========================================================================

/*--------------------------------------------------------------*
  Copyright (C) 1992-2008 Andras Varga
  Copyright (C) 2006-2008 OpenSim Ltd.

  This file is distributed WITHOUT ANY WARRANTY. See the file
  `license' for details on this and other legal matters.
*--------------------------------------------------------------*/

#include <string.h>
#include <stdio.h>
#include "opp_ctype.h"
#include "cenvir.h"
#include "cmodule.h"
#include "cchannel.h"
#include "cgate.h"
#include "cdisplaystring.h"
#include "stringutil.h"
#include "cmodelchange.h"

USING_NAMESPACE


cDisplayString::cDisplayString()
{
    dispstr = NULL;
    buffer = NULL;
    bufferend = NULL;
    tags = NULL;
    numtags = 0;
    needsassemble = false;

    ownercomponent = NULL;
}


cDisplayString::cDisplayString(const char *displaystr)
{
    dispstr = opp_strdup(displaystr);
    buffer = NULL;
    bufferend = NULL;
    tags = NULL;
    numtags = 0;
    needsassemble = false;

    ownercomponent = NULL;

    // doParse() should be the last one, as it may throw an error
    doParse();
}

cDisplayString::cDisplayString(const cDisplayString& ds)
{
    dispstr = NULL;
    buffer = NULL;
    bufferend = NULL;
    tags = NULL;
    numtags = 0;
    needsassemble = false;

    ownercomponent = NULL;

    operator=(ds);
}

cDisplayString::~cDisplayString()
{
    delete [] dispstr;
    clearTags();
}

void cDisplayString::beforeChange()
{
    // notify pre-change listeners
    if (ownercomponent && ownercomponent->hasListeners(PRE_MODEL_CHANGE)) {
        cPreDisplayStringChangeNotification tmp;
        tmp.displayString = this;
        ownercomponent->emit(PRE_MODEL_CHANGE, &tmp);
    }
}

void cDisplayString::afterChange()
{
    needsassemble = true;
    EVCB.displayStringChanged(ownercomponent);

    // notify post-change listeners
    if (ownercomponent && ownercomponent->hasListeners(POST_MODEL_CHANGE)) {
        cPostDisplayStringChangeNotification tmp;
        tmp.displayString = this;
        ownercomponent->emit(POST_MODEL_CHANGE, &tmp);
    }
}

const char *cDisplayString::str() const
{
    if (needsassemble)
        assemble();
    return dispstr ? dispstr : "";
}

void cDisplayString::parse(const char *displaystr)
{
    beforeChange();
    doParse(displaystr);
    afterChange();
}

void cDisplayString::doParse(const char *displaystr)
{
    // if it's the same, nothing to do
    if (needsassemble)
        assemble();
    if (!opp_strcmp(dispstr,displaystr))
        return;

    // parse and store new string
    delete [] dispstr;
    clearTags();
    dispstr = opp_strdup(displaystr);
    doParse();
}

void cDisplayString::updateWith(const char *s)
{
    cDisplayString ds(s);
    updateWith(ds);
}

void cDisplayString::updateWith(const cDisplayString& ds)
{
    beforeChange();
    doUpdateWith(ds);
    afterChange();
}

void cDisplayString::doUpdateWith(const cDisplayString& ds)
{
    // elements in "ds" take precedence
    int n = ds.getNumTags();
    for (int i=0; i<n; i++)
    {
        int m = ds.getNumArgs(i);
        for (int j=0; j<m; j++)
        {
            const char *arg = ds.getTagArg(i,j);
            if (arg[0]=='-' && !arg[1])  // "-" is the "antivalue"
                doSetTagArg(ds.getTagName(i), j, "");
            else if (arg[0])
                doSetTagArg(ds.getTagName(i), j, arg);
        }
    }

    // optimize storage
    doParse(str());
}

bool cDisplayString::containsTag(const char *tagname) const
{
    int t = getTagIndex(tagname);
    return t!=-1;
}

int cDisplayString::getNumArgs(const char *tagname) const
{
    return getNumArgs(getTagIndex(tagname));
}

const char *cDisplayString::getTagArg(const char *tagname, int index) const
{
    return getTagArg(getTagIndex(tagname), index);
}

bool cDisplayString::setTagArg(const char *tagname, int index, long value)
{
    char buf[32];
    sprintf(buf, "%ld", value);
    return setTagArg(tagname, index, buf);
}

bool cDisplayString::setTagArg(const char *tagname, int index, const char *value)
{
    beforeChange();
    bool result = doSetTagArg(tagname, index, value);
    afterChange();
    return result;
}

bool cDisplayString::doSetTagArg(const char *tagname, int index, const char *value)
{
    int t = getTagIndex(tagname);
    if (t==-1)
        t = doInsertTag(tagname);
    return doSetTagArg(t, index, value);
}

bool cDisplayString::removeTag(const char *tagname)
{
    return removeTag(getTagIndex(tagname));
}

int cDisplayString::getNumTags() const
{
    return numtags;
}

const char *cDisplayString::getTagName(int tagindex) const
{
    if (tagindex<0 || tagindex>=numtags) return NULL;
    return tags[tagindex].name;
}

int cDisplayString::getNumArgs(int tagindex) const
{
    if (tagindex<0 || tagindex>=numtags) return -1;
    return tags[tagindex].numargs;
}

const char *cDisplayString::getTagArg(int tagindex, int index) const
{
    if (tagindex<0 || tagindex>=numtags) return "";
    if (index<0 || index>=tags[tagindex].numargs) return "";
    return opp_nulltoempty(tags[tagindex].args[index]);
}

bool cDisplayString::setTagArg(int tagindex, int index, const char *value)
{
    beforeChange();
    bool result = doSetTagArg(tagindex, index, value);
    afterChange();
    return result;
}

bool cDisplayString::doSetTagArg(int tagindex, int index, const char *value)
{
    // check indices
    if (tagindex<0 || tagindex>=numtags) return false;
    if (index<0 || index>=MAXARGS) return false;
    Tag& tag = tags[tagindex];

    // adjust numargs if necessary
    if (index>=tag.numargs)
        tag.numargs = index+1;

    // if it's the same, nothing to do
    char *&slot = tag.args[index];
    if (!opp_strcmp(slot,value))
        return true;

    // set value
    if (slot && !pointsIntoBuffer(slot))
        delete [] slot;
    slot = opp_strdup(value);

    // get rid of possible empty trailing args, throw out tag if it became empty
    while (tag.numargs>0 && tag.args[tag.numargs-1]==NULL)
        tag.numargs--;
    if (tag.numargs==0)
        doRemoveTag(tagindex);
    return true;
}


int cDisplayString::insertTag(const char *tagname, int atindex)
{
    beforeChange();
    int result = doInsertTag(tagname, atindex);
    afterChange();
    return result;
}

int cDisplayString::doInsertTag(const char *tagname, int atindex)
{
    // check name validity
    if (!tagname || !tagname[0])
        throw cRuntimeError("Error adding a new display string tag: tag name is empty");
    for (const char *s=tagname; *s; s++)
        if (!opp_isalnum(*s) && *s!=':')
            throw cRuntimeError("Error adding a new display string tag: tag name \"%s\" "
                                "contains invalid character", tagname);

    // check uniqueness
    int t = getTagIndex(tagname);
    if (t!=-1)
        return t;

    // check index
    if (atindex<0) atindex=0;
    if (atindex>numtags) atindex=numtags;

    // create new tags[] array with hole at atindex
    Tag *newtags = new Tag[numtags+1];
    for (int s=0,d=0; s<numtags; s++,d++)
    {
       if (d==atindex) d++; // make room for new item
       newtags[d] = tags[s];
    }
    delete [] tags;
    tags = newtags;
    numtags++;

    // fill in new tag
    tags[atindex].name = opp_strdup(tagname);
    tags[atindex].numargs = 0;
    for (int i=0; i<MAXARGS; i++) tags[atindex].args[i] = NULL;

    // success
    return atindex;
}


bool cDisplayString::removeTag(int tagindex)
{
    beforeChange();
    bool result = doRemoveTag(tagindex);
    afterChange();
    return result;
}

bool cDisplayString::doRemoveTag(int tagindex)
{
    if (tagindex<0 || tagindex>=numtags) return false;

    // dealloc strings in tag
    if (!pointsIntoBuffer(tags[tagindex].name))
        delete [] tags[tagindex].name;
    for (int i=0; i<tags[tagindex].numargs; i++)
        if (!pointsIntoBuffer(tags[tagindex].args[i]))
            delete [] tags[tagindex].args[i];

    // eliminate hole in tags[] array
    for (int t=tagindex; t<numtags-1; t++)
        tags[t] = tags[t+1];
    numtags--;

    // success
    return true;
}


int cDisplayString::getTagIndex(const char *tagname) const
{
    for (int t=0; t<numtags; t++)
        if (!strcmp(tagname,tags[t].name))
            return t;
    return -1;
}

void cDisplayString::clearTags()
{
    // delete tags array. string pointers that do not point inside the
    // buffer were allocated individually via new char[] and have to be
    // deleted.
    for (int t=0; t<numtags; t++)
    {
        if (!pointsIntoBuffer(tags[t].name))
            delete [] tags[t].name;
        for (int i=0; i<tags[t].numargs; i++)
            if (!pointsIntoBuffer(tags[t].args[i]))
                delete [] tags[t].args[i];
    }
    delete [] tags;
    tags = NULL;
    numtags = 0;

    // must be done after deleting tags[] because of pointsIntoBuffer()
    delete [] buffer;
    buffer = bufferend = NULL;
}

void cDisplayString::doParse()
{
    clearTags();
    if (dispstr==NULL)
        return;

    buffer = new char[opp_strlen(dispstr)+1];
    bufferend = buffer + opp_strlen(dispstr);

    // count tags (#(';')+1) & allocate tags[] array
    int n = 1;
    for (char *s1 = dispstr; *s1; s1++)
        if (*s1==';')
            n++;
    tags = new Tag[n];

    // parse string into tags[]. To avoid several small allocations,
    // pointers in tags[] will point into the buffer.
    numtags = 1;
    tags[0].name = buffer;
    tags[0].numargs = 0;
    for (int i=0; i<MAXARGS; i++)
        tags[0].args[i] = NULL;

    char *s, *d;
    for (s=dispstr,d=buffer; *s; s++,d++)
    {
        if (*s=='\\' && *(s+1))
        {
            // allow escaping display string special chars (=,;) with backslash.
            // No need to deal with "\t", "\n" etc here, since they already got
            // interpreted by opp_parsequotedstr().
            *d = *++s;
        }
        else if (*s==';')
        {
            // new tag begins
            *d = '\0';
            numtags++;
            tags[numtags-1].name = d+1;
            tags[numtags-1].numargs = 0;
            for (int i=0; i<MAXARGS; i++)
                tags[numtags-1].args[i] = NULL;
        }
        else if (*s=='=')
        {
            // first argument of new tag begins
            *d = '\0';
            tags[numtags-1].numargs = 1;
            tags[numtags-1].args[0] = d+1;
        }
        else if (*s==',')
        {
            // new argument of current tag begins
            *d = '\0';
            if (tags[numtags-1].numargs>=MAXARGS)
                throw cRuntimeError("Error parsing display string: too many parameters for a tag, "
                                    "max %d allowed in \"%s\"", MAXARGS, dispstr);
            tags[numtags-1].numargs++;
            tags[numtags-1].args[ tags[numtags-1].numargs-1 ] = d+1;
        }
        else
        {
            *d = *s;
        }
    }
    *d = '\0';

    // check tag names are OK (matching [a-zA-Z0-9:]+)
    for (int i=0; i<numtags; i++)
    {
        if (!tags[i].name[0]) {
            if (tags[i].numargs==0)
                ; // empty tag (occurs when there're redundant semicolons, or the display string is empty) -- XXX remove it
            else
                throw cRuntimeError("Error parsing display string: missing tag name in \"%s\"", dispstr);
        }
        for (const char *s=tags[i].name; *s; s++)
            if (!opp_isalnum(*s) && *s!=':')
                throw cRuntimeError("Error parsing display string: tag name \"%s\" contains invalid character in  \"%s\"", tags[i].name, dispstr);
    }
}

void cDisplayString::assemble() const
{
    // calculate length of display string
    int size = 0;
    for (int t0=0; t0<numtags; t0++)
    {
        size += opp_strlen(tags[t0].name)+2;
        for (int i=0; i<tags[t0].numargs; i++)
            size += opp_strlen(tags[t0].args[i])+1;
    }
    size = 2*size+1;  // 2* for worst case if every char has to be escaped

    // allocate display string
    delete [] dispstr;
    dispstr = new char[size];
    dispstr[0] = '\0';

    // assemble string
    for (int t=0; t<numtags; t++)
    {
        if (t!=0)
            strcat(dispstr, ";");
        strcatescaped(dispstr, tags[t].name);
        if (tags[t].numargs>0)
            strcat(dispstr, "=");
        for (int i=0; i<tags[t].numargs; i++)
        {
            if (i!=0) strcat(dispstr, ",");
            strcatescaped(dispstr, tags[t].args[i]);
        }
    }
    needsassemble = false;
}

void cDisplayString::strcatescaped(char *d, const char *s)
{
    if (!s) return;

    d += strlen(d);
    while (*s)
    {
        // quoting \t, \n etc is the job of opp_quotestr()
        if (*s==';' || *s==',' || *s=='=')
            *d++ = '\\';
        *d++ = *s++;
    }
    *d = '\0';
}

void cDisplayString::dump() const
{
    for (int t=0; t<numtags; t++)
    {
        if (t!=0) printf("; ");
        printf("tags[%d]:\"%s\"=", t, tags[t].name);
        for (int i=0; i<tags[t].numargs; i++)
        {
            if (i!=0) printf(",");
            printf("\"%s\"", tags[t].args[i]);
        }
    }
    printf(" ==> \"%s\"\n", str());
}
Back to Top