File: ofconfig.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (632 lines) | stat: -rw-r--r-- 15,464 bytes parent folder | download | duplicates (3)
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
 *
 *  Copyright (C) 1997-2021, OFFIS e.V.
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation were developed by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module:  ofstd
 *
 *  Author: Marco Eichelberg
 *
 *  Purpose:
 *    classes: OFConfigFileNode
 *
 */

#include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
#include "dcmtk/ofstd/ofconfig.h"
#include "dcmtk/ofstd/ofcast.h"

#include <cstring>

OFConfigFileNode::OFConfigFileNode(const char *keyword)
: brother_(NULL)
, son_(NULL)
, keyword_(keyword)
, value_()
{
}

OFConfigFileNode::~OFConfigFileNode()
{
  // recursively delete tree
  delete son_;
  delete brother_;
}

void OFConfigFileNode::print(STD_NAMESPACE ostream &out, unsigned int level)
{
  if (level > 0)
  {
    unsigned int i;
    for (i = 0; i < level; ++i) out << "[";
    out << keyword_;
    for (i = 0; i < level; ++i) out << "]";
    out << OFendl;

    // print subtree if present
    if (son_)
    {
      son_->print(out, level - 1);
      out << OFendl;
    }
  }
  else
  {
    out << keyword_ << " = ";
    const char *c = value_.c_str();
    while (*c)
    {
      if (*c == '\n') out << "\n    "; else out << *c;
      ++c;
    }
    out << OFendl;
  }

  // continue print on same level if present
  if (brother_) brother_->print(out, level);
}

/* ------------------------------------------------------------------------- */

OFConfigFileCursor::OFConfigFileCursor(const OFConfigFileCursor &source)
: array_(NULL)
, maxLevel_(source.maxLevel_)
{
  array_ = new OFConfigFileNodePtr[maxLevel_ + 1];
  for (unsigned int i = 0; i <= maxLevel_; i++)
    array_[i] = source.array_[i];
}

OFConfigFileCursor& OFConfigFileCursor::operator=(const OFConfigFileCursor &source)
{
  if (this != &source)
  {
    delete[] array_;
    maxLevel_ = source.maxLevel_;
    array_ = new OFConfigFileNodePtr[maxLevel_ + 1];
    for (unsigned int i = 0; i <= maxLevel_; i++)
      array_[i] = source.array_[i];
  }
  return *this;
}

void OFConfigFileCursor::clear()
{
  if (NULL == array_) array_ = new OFConfigFileNodePtr[maxLevel_ + 1];
  for (unsigned int i = 0; i <= maxLevel_; i++)
    array_[i] = NULL;
}

OFBool OFConfigFileCursor::section_valid(unsigned int level) const
{
  OFBool result=OFFalse;
  if ((level <= maxLevel_) && array_)
  {
    result = OFTrue;
    for (int i = maxLevel_; i >= OFstatic_cast(int, level); i--)
      result = result && (array_[i] != NULL);
  }
  return result;
}

void OFConfigFileCursor::set_section(
  unsigned int level,
  const char *key,
  OFConfigFileNode *anchor)
{
  int valid = (level <= maxLevel_) && array_;
  if (valid)
  {
    if (level < maxLevel_) valid = section_valid(level + 1); else valid = 1;
    if (valid)
    {
      first_section(level, anchor);
      while ((section_valid(level)) && array_[level] && (! array_[level]->match(key)))
        next_section(level);
    } else clear();
  } else clear();
}

void OFConfigFileCursor::first_section(
  unsigned int level,
  OFConfigFileNode *anchor)
{
  int valid = (level <= maxLevel_);
  if (valid)
  {
    if (level < maxLevel_) valid=section_valid(level + 1); else valid = 1;
    if (valid)
    {
      if (level < maxLevel_) array_[level] = array_[level + 1]->getSon();
      else array_[level] = anchor;
      for (int i = level - 1; i >= 0; i--) array_[i] = NULL;
    } else clear();
  } else clear();
}

void OFConfigFileCursor::next_section(unsigned int level)
{
  int valid = (level <= maxLevel_);
  if (valid)
  {
    valid = section_valid(level);
    if (valid)
    {
      array_[level] = array_[level]->getBrother();
      for (int i = level - 1; i >= 0; i--) array_[i] = NULL;
    } else clear();
  } else clear();
}

void OFConfigFileCursor::orderedInsert(
  OFConfigFileNode *parent,
  OFConfigFileNode *&newnode)
{
  if (NULL == parent)
  {
    // parent must not be NULL, so this should never happen.
    // Just in case, avoid memory leak.
    delete newnode;
  }
  else
  {
    if (NULL == parent->getSon())
    {
      // parent node does not have any childs yet; we're the first one
      parent->setSon(newnode);
    }
    else
    {
      OFConfigFileNode *leftMostBrother = parent->getSon();
      if (! leftMostBrother->less(newnode->getKeyword()))
      {
        // need to insert at or before leftMostBrother
        if (leftMostBrother->match(newnode->getKeyword()))
        {
          // duplicate entry detected. Replace old entry.
          leftMostBrother->setValue(newnode->getValue());
          delete newnode;
          newnode = leftMostBrother;
        }
        else
        {
          // newnode is new leftmost entry
          newnode->setBrother(leftMostBrother);
          parent->setSon(newnode);
        }
      }
      else
      {
        // need to insert behind leftMostBrother.
        // skip all list entries where the keyword is less, except for the last one
        while (leftMostBrother->getBrother() && leftMostBrother->getBrother()->less(newnode->getKeyword()))
        {
          leftMostBrother = leftMostBrother->getBrother();
        }

        // now we're either at the last entry of the list, or the next entry is greater or equal.
        if (leftMostBrother->getBrother())
        {
          // we're not at the end of the list
          if (leftMostBrother->getBrother()->match(newnode->getKeyword()))
          {
            // duplicate entry detected. Replace old entry.
            leftMostBrother->getBrother()->setValue(newnode->getValue());
            delete newnode;
            newnode = leftMostBrother->getBrother();
          }
          else
          {
            // next entry is larger than newnode; insert here
            newnode->setBrother(leftMostBrother->getBrother());
            leftMostBrother->setBrother(newnode);
          }
        }
        else
        {
          // we're at the end of the list, i.e. newnode needs to be appended.
          leftMostBrother->setBrother(newnode);
        }
      }
    }
  }
}

void OFConfigFileCursor::insert(
  unsigned int level,
  OFConfigFileNode *&newnode,
  OFConfigFileNode *&anchor,
  OFBool orderedMode)
{
  // Implement option that causes entries to be sorted and identical entries to be replaced
  if (level == maxLevel_)
  {
    if (NULL == array_[maxLevel_])
    {
      // newnode is the first entry in the tree, store as anchor
      anchor = newnode;
    }
    else
    {
      if (orderedMode)
      {
        // we need to create the root node of the tree,
        // because orderedInsert() needs a parent.
        OFConfigFileNode rootNode("root");
        rootNode.setSon(anchor);
        orderedInsert(&rootNode, newnode);
        anchor = rootNode.getSon();  // anchor could have changed
        rootNode.setSon(NULL); // destruction of root node shall not destroy whole tree
      }
      else
      {
        array_[maxLevel_]->setBrother(newnode);
      }
    }

    // adjust cursor
    array_[maxLevel_] = newnode;
  }
  else
  {
    if (array_[level + 1])
    {
      if (NULL == array_[level + 1]->getSon())
      {
        // newnode is the first child node of the parent
        array_[level + 1]->setSon(newnode);
      }
      else
      {
        if (orderedMode)
        {
          orderedInsert(array_[level + 1], newnode);
        }
        else
        {
          if (NULL == array_[level]) array_[level] = array_[level + 1]->getSon();
          array_[level]->setBrother(newnode);
        }
      }

      // adjust cursor
      array_[level] = newnode;
    }
    else
    {
      // this should never happen unless the caller passes the wrong level, in which case
      // we at least avoid a memory leak
      delete newnode;
      newnode = NULL;
    }

  }
  if (level > 0) for (int j = level - 1; j >= 0; j--) array_[j] = NULL;
}

/* ------------------------------------------------------------------------- */

const char *OFConfigFile::get_keyword(unsigned int level)
{
  const char *result = NULL;
  int valid = (level <= maxLevel_);
  if ((valid) && (section_valid(level)))
    result = cursor_.getKeyword(level);
  return result;
}

const char *OFConfigFile::get_value()
{
  const char *result = NULL;
  int valid = section_valid(0);
  if (valid) result = cursor_.getValue(0);
  return result;
}

OFBool OFConfigFile::get_bool_value(OFBool defaultvalue)
{
  OFBool result = defaultvalue;
  const char *val = get_value();
  if (val == NULL) return result;
  OFString pstring(val);
  OFString ostring;
  size_t len = pstring.length();
  unsigned char c;
  for (size_t i = 0; i<len; i++)
  {
    c = pstring[i];
    if ((c >= 'a') && (c <= 'z')) ostring += OFstatic_cast(char, toupper(c));
    else if ((c >= 'A') && (c <= 'Z')) ostring += c;
    else if ((c >= '0') && (c <= '9')) ostring += c;
    else if (c == '_') ostring += c;
  }

  if (ostring == "YES")  result = OFTrue; else
  if (ostring == "1")    result = OFTrue; else
  if (ostring == "TRUE") result = OFTrue; else
  if (ostring == "ON")   result = OFTrue; else
  if (ostring == "NO")   result = OFFalse; else
  if (ostring == "0")    result = OFFalse; else
  if (ostring == "FALSE")result = OFFalse; else
  if (ostring == "OFF")  result = OFFalse;
  return result;
}

void OFConfigFile::save_cursor()
{
  stack_.push(cursor_);
}

void OFConfigFile::restore_cursor()
{
  OFBool empty = stack_.empty();
  if (!empty)
  {
    cursor_ = stack_.top();
    stack_.pop();
  } else cursor_.clear();
}

void OFConfigFile::select_section(const char *key1, const char *key2, const char *key3)
{
  if (key3) set_section(3, key3);
  if (key2) set_section(2, key2);
  if (key1) set_section(1, key1);
}

const char *OFConfigFile::get_entry(const char *key0)
{
  const char *result = NULL;
  if (section_valid(1))
  {
    set_section(0, key0);
    result = get_value();
  }
  return result;
}

void OFConfigFile::store_char(char c)
{
  if (bufptr_ == OFstatic_cast(size_t, bufsize_))
  {
     char *oldbuf = buffer_;
     bufsize_ += 1024;
     buffer_ = new char[bufsize_];
     if (buffer_)
     {
       if (oldbuf)
       {
         strncpy(buffer_, oldbuf, bufptr_);
         delete[] oldbuf;
       }
       buffer_[bufptr_++] = c;
     } else {
       buffer_ = oldbuf;
       bufsize_ -= 1024;
     }
  } else buffer_[bufptr_++] = c;
}

char OFConfigFile::read_char(FILE *infile)
{
  char c;
  int done = 0;
  int handled = 0;
  int commentmode = 0;
  while (!done)
  {
    c = OFstatic_cast(char, getc(infile));
    handled = 0;
    if(!handled) if ((feof(infile)) || (ferror(infile)))
    {
      handled = 1;
      done = 1;
    }
    if(!handled) if ((c == 10) && (crfound_) && (isnewline_))
    {
      handled = 1;
      crfound_ = 0;
    }
    if(!handled) if ((c == 10) || (c == 13))
    {
      handled = 1;
      isnewline_ = 1;
      if (c == 13) crfound_ = 1; else crfound_ = 0;
      if (commentmode) commentmode = 0; else
      {
        done = 1;
        c = 10;
      }
    }
    if(!handled) if ((c == commentChar_) && (isnewline_))
    {
      handled = 1;
      commentmode = 1;
      isnewline_ = 0;
    }
    if(!handled) if (!commentmode)
    {
      handled = 1;
      done = 1;
      isnewline_ = 0;
      if (c == 0) c = ' '; // destroy 0 chars
    }
  } // done
  return c;
}

char OFConfigFile::read_keywordchar(FILE *infile)
{
  char c = 0;
  int done = 0;
  while ((!done) && (!feof(infile)) && (!ferror(infile)))
  {
    c = read_char(infile);
    if ((c != ' ') && (c != 9) && (c != 10)) done = 1;
  }
  return (c > 96) && (c < 123) ? OFstatic_cast(char, c - 32) : c;
}

void OFConfigFile::read_entry(FILE *infile)
{
  char c;
  int done = 0;
  int level = 0;
  int valid = 0;
  c = read_keywordchar(infile);
  if ((!feof(infile)) && (!ferror(infile)))
  {
    if (c == '[')
    {  // this is a higher level keyword
      level++;
      while (!done)
      {
        c = read_keywordchar(infile);
        valid = (!feof(infile)) && (!ferror(infile));
        if ((valid) && (c == '[')) level++; else done = 1;
      }
      if (valid)
      {  // Now we will read the keyword name
        ungetc(c, infile);
        done = 0;
        while (!done)
        {
          c = read_keywordchar(infile);
          valid = (!feof(infile)) && (!ferror(infile));
          if (valid)
          {
            if (((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '-') || (c == '_'))
            store_char(c);
            else if (c == ']') done = 1;
          } else done = 1;
        }
      }
      if (valid)
      { // Now we will read the ']'
        int declevel = level - 1;
        done = (declevel == 0);
        while (!done)
        {
          c = read_keywordchar(infile);
          valid = (!feof(infile)) && (!ferror(infile));
          if (valid)
          {
            if (c == ']') declevel--;
            done = (declevel == 0);
          } else done = 1;
        }
      }
      if (level > OFstatic_cast(int, maxLevel_)) level = maxLevel_;
    }
    else
    { // this is a level 0 keyword
      ungetc(c, infile);
      done = 0;
      while (!done)
      {
        c = read_keywordchar(infile);
        valid = (!feof(infile)) && (!ferror(infile));
        if (valid)
        {
          if (((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '-') || (c == '_')) store_char(c);
          else if (c == '=') done = 1;
        } else done = 1;
      }
    } // keyword read

    // Store the keyword.
    store_char(0);
    OFConfigFileNode *newnode = new OFConfigFileNode(buffer_);
    if (newnode) cursor_.insert(level, newnode, anchor_, orderedMode_); // new node stored
    bufptr_ = 0;
    // Read value field for level 0 keywords.
    if (level == 0)
    {
      int skipwhite = 1;
      int justnewline = 0;
      done = 0;
      while (!done)
      {
        c = read_char(infile);
        if ((!feof(infile)) && (!ferror(infile)))
        {
          if (c == 10)
          {
            if (!skipwhite) store_char(c);
            skipwhite = 1;
            justnewline = 1;
          }
          else if ((c == ' ') || (c == 9))
          {
            justnewline = 0;
            if (!skipwhite) store_char(c);
          }
          else
          {
            if (justnewline)
            {
              done = 1;
              ungetc(c, infile);
            } else {
              store_char(c);
              skipwhite = 0;
            }
          }
        } else done = 1;
      }
      store_char(0);
    }
    if ((bufptr_ > 0) && (newnode))
    {
      // remove trailing linefeeds
      while ((bufptr_ > 0) && ((buffer_[bufptr_ - 1] == 10) || (buffer_[bufptr_ - 1] == 0)))
        buffer_[--bufptr_] = 0;
      newnode->setValue(buffer_);
    }
    bufptr_ = 0;
  } // something found
}

void OFConfigFile::print(STD_NAMESPACE ostream &out)
{
  if (anchor_) anchor_->print(out, maxLevel_);
}

void OFConfigFile::loadFile(FILE *infile)
{
  if (infile) while ((!feof(infile)) && (!ferror(infile))) read_entry(infile);
}

OFConfigFile::OFConfigFile(
  FILE *infile,
  unsigned int maxLevel,
  char commentChar,
  OFBool orderedMode)
: stack_()
, cursor_(maxLevel)
, anchor_(NULL)
, isnewline_(1)
, crfound_(0)
, buffer_(NULL)
, bufptr_(0)
, bufsize_(0)
, maxLevel_(maxLevel)
, commentChar_(commentChar)
, orderedMode_(orderedMode)
{
  loadFile(infile);
}

OFConfigFile::~OFConfigFile()
{
  delete anchor_;
  delete[] buffer_;
}