OFFIS DCMTK  Version 3.6.0
ofconfig.h
1 /*
2  *
3  * Copyright (C) 1997-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Marco Eichelberg
17  *
18  * Purpose:
19  * classes: OFConfigFile
20  *
21  * Last Update: $Author: joergr $
22  * Update Date: $Date: 2010-10-14 13:15:50 $
23  * CVS/RCS Revision: $Revision: 1.8 $
24  * Status: $State: Exp $
25  *
26  * CVS/RCS Log at end of file
27  *
28  */
29 
30 #ifndef OFCONFIG_H
31 #define OFCONFIG_H
32 
33 #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
34 #include "dcmtk/ofstd/ofstring.h"
35 #include "dcmtk/ofstd/ofstack.h"
36 #include "dcmtk/ofstd/ofstream.h"
37 
38 #define INCLUDE_CSTDIO
39 #include "dcmtk/ofstd/ofstdinc.h"
40 
41 /*
42  * Short description of configuration file structure:
43  * - The data in a configuration file have a tree structure.
44  * The tree has a depth defined at instantiation time (by default, OFConfigFile_MaxLevel),
45  * not including the (imaginary) root node.
46  * - A level 0 entry (a leaf) has the form: KEYWORD = VALUE,
47  * where the keyword starts on row one of a line.
48  * - A level 1 entry has the form [KEYWORD]
49  * - A level 2 entry has the form [[KEYWORD]] (and so on).
50  * - Keywords may consist of:
51  * A..Z, a..z (which are converted to uppercase),
52  * 0..9,
53  * '-'
54  * - Values can be any kind of ASCII text. Values may span multiple lines.
55  * To continue a value in the next line, the next line MUST start with
56  * (any amount of) whitespace, which is discarded when reading the value.
57  * linefeeds (converted to ASCII 10 if necessary) are kept in the
58  * value string. Empty lines are discarded (and also their linefeed).
59  * - The data must have a "clean" tree structure. This means that there
60  * MUST be a level 2 keyword before any level 1 keyword etc.
61  * - lines starting with the comment char (default is "#") are interpreted
62  * as comment lines.
63  *
64  */
65 
66 #define OFConfigFile_MaxLevel 2
67 #define OFConfigFile_CommentChar '#'
68 
69 class OFConfigFile;
70 class OFConfigFileNode;
71 
73 
74 
80 {
81 public:
85  OFConfigFileNode(const char *keyword);
86 
89 
92  const char *getKeyword() const
93  {
94  return keyword_.c_str();
95  }
96 
99  const char *getValue() const
100  {
101  return value_.c_str();
102  }
103 
107  void setValue(const char *c)
108  {
109  value_ = c;
110  }
111 
116  OFBool match(const char *c) const
117  {
118  return (keyword_ == c);
119  }
120 
125  OFBool less(const char *c) const
126  {
127  return (keyword_ < c);
128  }
129 
134  {
135  return brother_;
136  }
137 
142  {
143  return son_;
144  }
145 
150  {
151  brother_ = brother;
152  }
153 
158  {
159  son_ = son;
160  }
161 
166  void print(STD_NAMESPACE ostream& out, unsigned int level);
167 
168 private:
171 
174 
177 
180 
183 
186 };
187 
190 
195 {
196 public:
199  OFConfigFileCursor(unsigned int maxLevel)
200  : array_(NULL)
201  , maxLevel_(maxLevel)
202  {
203  clear();
204  }
205 
208  OFConfigFileCursor(const OFConfigFileCursor& source);
209 
213  {
214  delete[] array_;
215  }
216 
220 
222  void clear();
223 
228  const char *getKeyword(unsigned int level) const
229  {
230  if ((level <= maxLevel_) && array_ && array_[level]) return array_[level]->getKeyword(); else return NULL;
231  }
232 
237  const char *getValue(unsigned int level) const
238  {
239  if ((level <= maxLevel_) && array_ && array_[level]) return array_[level]->getValue(); else return NULL;
240  }
241 
247  OFBool section_valid(unsigned int level) const;
248 
259  void set_section(
260  unsigned int level,
261  const char *key,
262  OFConfigFileNode *anchor);
263 
273  void first_section(
274  unsigned int level,
275  OFConfigFileNode *anchor);
276 
285  void next_section(unsigned int level);
286 
294  void insert(
295  unsigned int level,
296  OFConfigFileNode *& newnode,
297  OFConfigFileNode *& anchor,
298  OFBool orderedMode);
299 
303  OFBool operator<(const OFConfigFileCursor& /* arg */) const
304  {
305  return OFFalse;
306  }
307 
311  OFBool operator==(const OFConfigFileCursor& /* arg */) const
312  {
313  return OFTrue;
314  }
315 
316 private:
317 
325  void orderedInsert(
326  OFConfigFileNode *parent,
327  OFConfigFileNode *&newnode);
328 
331 
333  unsigned int maxLevel_;
334 };
335 
336 
343 {
344 public:
345 
353  OFConfigFile(
354  FILE *infile,
355  unsigned int maxLevel = OFConfigFile_MaxLevel,
356  char commentChar = OFConfigFile_CommentChar,
357  OFBool orderedMode = OFFalse);
358 
361  virtual ~OFConfigFile();
362 
366  void loadFile(FILE *infile);
367 
373  const char *get_keyword(unsigned int level);
374 
379  const char *get_value();
380 
391  OFBool get_bool_value(OFBool defaultvalue);
392 
398  OFBool section_valid(unsigned int level) const
399  {
400  return cursor_.section_valid(level);
401  }
402 
412  void set_section(unsigned int level, const char *key)
413  {
414  cursor_.set_section(level, key, anchor_);
415  }
416 
425  void first_section(unsigned int level)
426  {
427  cursor_.first_section(level, anchor_);
428  }
429 
438  void next_section(unsigned int level)
439  {
440  cursor_.next_section(level);
441  }
442 
445  void save_cursor();
446 
449  void restore_cursor();
450 
459  void select_section(
460  const char *key1,
461  const char *key2=NULL,
462  const char *key3=NULL);
463 
469  const char *get_entry(const char *key0);
470 
474  void print(STD_NAMESPACE ostream& out);
475 
476 private:
477 
484  char read_char(FILE *infile);
485 
491  char read_keywordchar(FILE *infile);
492 
499  void read_entry(FILE *infile);
500 
505  void store_char(char c);
506 
509  OFConfigFile(const OFConfigFile&);
510 
514 
515 
518 
521 
524 
527 
529  int crfound_;
530 
532  char *buffer_;
533 
535  size_t bufptr_;
536 
538  long bufsize_;
539 
541  unsigned int maxLevel_;
542 
545 
555  OFBool orderedMode_;
556 };
557 
558 #endif
559 
560 /*
561  * $Log: ofconfig.h,v $
562  * Revision 1.8 2010-10-14 13:15:50 joergr
563  * Updated copyright header. Added reference to COPYRIGHT file.
564  *
565  * Revision 1.7 2010-04-26 12:22:30 uli
566  * Fixed a some minor doxygen warnings.
567  *
568  * Revision 1.6 2008-04-16 09:37:27 meichel
569  * class OFConfigFile now supports an ordered mode where multiple
570  * configuration files can be loaded and can replace entries of other.
571  * Also added function to print content of configuration in reloadable format.
572  *
573  * Revision 1.5 2008-04-15 15:46:30 meichel
574  * class OFConfigFile now supports flexible tree depths and configurable
575  * comment characters and can, therefore, fully replace the equivalent
576  * code in module dcmprint.
577  *
578  * Revision 1.4 2005/12/08 16:05:51 meichel
579  * Changed include path schema for all DCMTK header files
580  *
581  * Revision 1.3 2003/06/12 13:15:59 joergr
582  * Fixed inconsistent API documentation reported by Doxygen.
583  *
584  * Revision 1.2 2003/06/04 12:31:44 meichel
585  * Added dummy comparison operators, needed by MSVC5 with STL
586  *
587  * Revision 1.1 2003/04/29 10:14:16 meichel
588  * Moved configuration file parser from module dcmpstat to ofstd and renamed
589  * class to OFConfigFile. Cleaned up implementation (no more friend declarations).
590  *
591  *
592  */
593 
void save_cursor()
puts the current cursor position on a cursor stack.
OFConfigFileNode(const char *keyword)
constructor.
OFConfigFileNodePtr * array_
the cursor is an array of pointers to OFConfigFileNode objects
Definition: ofconfig.h:330
unsigned int maxLevel_
depth of tree, i.e. number of entries in array_
Definition: ofconfig.h:333
char read_keywordchar(FILE *infile)
reads the next non-whitespace character from the input file and returns as uppercase character...
const char * getKeyword(unsigned int level) const
return keyword as C string.
Definition: ofconfig.h:228
const char * getValue() const
return current value as C string
Definition: ofconfig.h:99
this class maintains one configuration file entry (key-value pair) and the links that comprise the co...
Definition: ofconfig.h:79
OFStack< OFConfigFileCursor > stack_
stack of cursor positions that can be saved and restored
Definition: ofconfig.h:517
virtual ~OFConfigFile()
destructor
char * buffer_
buffer during file read
Definition: ofconfig.h:532
long bufsize_
buffer size during file read
Definition: ofconfig.h:538
OFConfigFile & operator=(const OFConfigFile &)
private undefined assignment operator
const char * get_value()
gets the value for the current entry (level 0 keyword).
OFConfigFileNode * getBrother() const
return pointer to next object in tree on same level
Definition: ofconfig.h:133
OFBool section_valid(unsigned int level) const
checks if the cursor points to a valid entry in the config data tree up to the the specified level...
Definition: ofconfig.h:398
const char * get_keyword(unsigned int level)
gets the name of the keyword at the specified level in the cursor path.
OFConfigFileCursor & operator=(const OFConfigFileCursor &source)
assignment operator
char read_char(FILE *infile)
reads the next character from the input file, maintains the current line number and filters out comme...
structure used by class OFConfigFile to store a cursor pointing to the currently selected section and...
Definition: ofconfig.h:194
void print(STD_NAMESPACE ostream &out, unsigned int level)
print the content of this node to an output stream
void read_entry(FILE *infile)
reads a complete entry from the config file.
void print(STD_NAMESPACE ostream &out)
print the content of the configuration to an output stream
OFBool operator<(const OFConfigFileCursor &) const
dummy comparison operator, needed by MSVC5 with STL.
Definition: ofconfig.h:303
OFString value_
configuration value
Definition: ofconfig.h:185
char commentChar_
character starting comment lines
Definition: ofconfig.h:544
const char * getValue(unsigned int level) const
return value as C string Precondition is that section_valid(level) return true.
Definition: ofconfig.h:237
OFBool less(const char *c) const
check if keyword compares "<" to given string
Definition: ofconfig.h:125
const char * get_entry(const char *key0)
sets the cursor to the given level 0 keyword and returns the string value assigned to this keyword...
OFBool match(const char *c) const
check if keyword matches given string
Definition: ofconfig.h:116
void setValue(const char *c)
set value from C string
Definition: ofconfig.h:107
OFBool section_valid(unsigned int level) const
checks if the cursor points to a valid location up to the given level
OFString keyword_
configuration keyword
Definition: ofconfig.h:182
void store_char(char c)
writes a character to the string buffer maintained in "buffer".
void loadFile(FILE *infile)
load configuration file
void next_section(unsigned int level)
sets cursor to the next entry at the given level (without changing the cursor position at higher leve...
void first_section(unsigned int level, OFConfigFileNode *anchor)
sets cursor to the first entry at the given level (without changing the cursor position at higher lev...
class for reading and evaluating configuration files.
Definition: ofconfig.h:342
~OFConfigFileCursor()
destructor
Definition: ofconfig.h:212
unsigned int maxLevel_
depth of tree, i.e. number of entries in array_
Definition: ofconfig.h:541
OFBool get_bool_value(OFBool defaultvalue)
gets the value for the current entry and interprets it as a boolean value.
void setSon(OFConfigFileNode *son)
set pointer to next object in tree on lower level
Definition: ofconfig.h:157
const char * getKeyword() const
return keyword as C string
Definition: ofconfig.h:92
void first_section(unsigned int level)
sets cursor to the first entry at the given level (without changing the cursor position at higher lev...
Definition: ofconfig.h:425
OFConfigFileCursor cursor_
current cursor position
Definition: ofconfig.h:520
OFBool operator==(const OFConfigFileCursor &) const
dummy comparison operator, needed by MSVC5 with STL.
Definition: ofconfig.h:311
OFConfigFileNode & operator=(const OFConfigFileNode &arg)
private undefined copy assignment operator
int isnewline_
flag indicating whether newline during file read
Definition: ofconfig.h:526
OFConfigFile(FILE *infile, unsigned int maxLevel=OFConfigFile_MaxLevel, char commentChar=OFConfigFile_CommentChar, OFBool orderedMode=OFFalse)
constructor.
OFConfigFileNode * getSon() const
return pointer to next object in tree on lower level
Definition: ofconfig.h:141
OFBool orderedMode_
mode flag for reading configuration file.
Definition: ofconfig.h:555
~OFConfigFileNode()
destructor, recursively deletes whole tree
void clear()
return object to default constructed state (invalid cursor)
size_t bufptr_
index into buffer during file read
Definition: ofconfig.h:535
void set_section(unsigned int level, const char *key)
sets cursor to the entry with keyword "key" at the given level.
Definition: ofconfig.h:412
void select_section(const char *key1, const char *key2=NULL, const char *key3=NULL)
sets the cursor to a different section.
a simple string class that implements a subset of std::string.
Definition: ofstring.h:86
void setBrother(OFConfigFileNode *brother)
set pointer to next object in tree on same level
Definition: ofconfig.h:149
OFConfigFileNode * anchor_
anchor to data tree
Definition: ofconfig.h:523
void insert(unsigned int level, OFConfigFileNode *&newnode, OFConfigFileNode *&anchor, OFBool orderedMode)
inserts a new node into the tree at the current cursor position
int crfound_
flag indicating whether CR was read during file read
Definition: ofconfig.h:529
const char * c_str() const
returns a pointer to the initial element of an array of length size()+1 whose first size() elements e...
Definition: ofstring.h:392
void restore_cursor()
restores a previously stored cursor position from the cursor stack.
void orderedInsert(OFConfigFileNode *parent, OFConfigFileNode *&newnode)
helper method that is called by insert() in ordered mode to insert a new node at a given tree level (...
void next_section(unsigned int level)
sets cursor to the next entry at the given level (without changing the cursor position at higher leve...
Definition: ofconfig.h:438
void set_section(unsigned int level, const char *key, OFConfigFileNode *anchor)
sets cursor to the entry with keyword "key" at the given level.
OFConfigFileNode * brother_
pointer to next object in tree on same level
Definition: ofconfig.h:176
OFConfigFileNode * son_
pointer to next object in tree on lower level
Definition: ofconfig.h:179
OFConfigFileCursor(unsigned int maxLevel)
default constructor
Definition: ofconfig.h:199


Generated on Wed Jan 4 2017 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.6