Debian Codegraph @

rudeconfig/src/config.h

raw
1 // Config.h
2 //
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Matthew Flood
4 // See file AUTHORS for contact information
5 //
6 // This file is part of RudeConfig.
7 //
8 // RudeConfig is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // RudeConfig is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with RudeConfig; (see COPYING) if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 // 02111-1307, USA.
22 //------------------------------------------------------------------------
23
24
25 #ifndef INCLUDED_RUDE_CONFIG_H
26 #define INCLUDED_RUDE_CONFIG_H
27
28 #include <istream>
29
30 namespace rude{
31
32 namespace config{
33 class ConfigImpl;
34 }
35
36 //=
37 // rude::Config is the public interface for the config/ini file reader.
38 //=
39 class Config{
40
41 // Bridge component
42 //
43 rude::config::ConfigImpl *d_implementation;
44
45 public:
46
47
48 //=
49 // Default constructor
50 // Use to obtain an instance of the rude::Config component
51 //
52 // <b>Example:</b>
53 // <code>
54 // rude::Config myconfig;
55 //
56 // //OR
57 //
58 // rude::Config *myconfig = new rude::Config();
59 // </code>
60 //=
61 Config();
62
63 //=
64 // Returns the version of the component.
65 //
66 // The version is specified by <b>X.Y</B> where:
67 // <B>X</B> is the version of the interface (this class), and
68 // <B>Y</B> is the version of the implementation (the internals).
69 //
70 // <b>Example:</b>
71 //
72 // <code>const char *version = Config::version();</code>
73 //=
74 static const char *version();
75
76 //=
77 // Sets the default filename to be used by all config objects
78 // Initially, this value is "default.ini"
79 // Methods affected include load(void) and save(void)
80 // This method does not affect already existing instances.
81 //=
82 static void setDefaultConfigFile(const char *filepath);
83
84 //=
85 // Returns the default filename to be used by all config objects
86 //=
87 static const char *getDefaultConfigFile();
88
89 //=
90 // Sets the default comment character to be used by all config objects
91 // The comment character affects how config files are parsed, and how they are written.
92 // Initially, the default comment character is set to '#'
93 // This method does not affect already existing instances.
94 //=
95 static void setDefaultCommentCharacter(char c);
96
97 //=
98 // Returns the default comment character to be used by all config objects
99 //=
100 static char getDefaultCommentCharacter();
101
102
103 //=
104 // Sets the default delimiter character to be used by all config objects
105 // The delimiter affects how config files are parsed, and how they are written.
106 // Initially, the default delimiter is set to '='
107 // This method does not affect already existing instances.
108 //=
109 static void setDefaultDelimiter(char c);
110
111 //=
112 // Returns the default delimiter character to be used by all config objects
113 //=
114 static char getDefaultDelimiter();
115
116
117 //=
118 // Sets the default behavior of all config objects in dealing with deleted values.
119 // The default behavior is for config objects to preserve deleted data as comments when
120 // the config object is saved
121 // This method does not affect already existing instances.
122 //=
123 static void setDefaultPreserveDeleted(bool shouldPreserve);
124
125 //=
126 // Returns the default deletion behavior
127 // true means deleted data will be preserved as comments when the config object is saved
128 // false means that deleted data will be discarded when the config object is saved
129 //=
130 static bool getDefaultPreserveDeleted();
131
132
133 //=
134 // Set's the default filepath to be used by the load() and save() methods
135 // The default filepath is initially set to "./default.ini"
136 // Affects the current instance only
137 //=
138 void setConfigFile(const char *filepath);
139
140 //=
141 // Returns the default filepath to be used by the load() and save() methods
142 // The default filepath is initially set to "./default.ini"
143 // Affects the current instance only
144 //=
145 const char * getConfigFile();
146
147 //=
148 // Sets whether or not deleted data will be preserved as comments.
149 // If set to true, deleted data will be converted into comments when
150 // the save() method is called. If set to false, deleted data
151 // will not be compeltely discarded. Default is false (no preserve).
152 // Affects the current instance only.
153 //=
154 void preserveDeletedData(bool shouldPreserve);
155
156 //=
157 // Set's the comment character ( initially set to '#' )
158 // If the comment character is set to NULL (0),
159 // then comments will not be written when save() is called
160 // Affects the current object only
161 //=
162 void setCommentCharacter(char commentchar);
163
164 //=
165 // Set's the delimiter for new config objects ( initially set to '=' )
166 // If the delimiter is set to NULL, then it will be assumed that
167 // the key and value are separated by whitespace.
168 // Affects the current object only
169 //=
170 void setDelimiter(char kayvaluedelimiter);
171
172 //=
173 //
174 // When called before load(), saves the configuration to the default config file.
175 // Initially, the default config file is "./default.ini".
176 //
177 // If load was called with an argument:
178 // load(const char *filename)
179 // then save() will use the same filename.
180 //
181 // The filename used by the instance can be changed by the setConfigFile() method.
182 //=
183 bool save();
184
185 //=
186 // Saves the configuration object to the specified file
187 // The default config file path for the instance is not altered...
188 // Use setConfigFile(const char *filename) or load(const char *filename) to set the default filename for the instance
189 //=
190 bool save(const char *filepath);
191
192 //=
193 // Removes the contents of the configuration file - completely wipes it out.
194 // Does not preserve anything. Afterwards, the current section is set
195 // to the empty section (""), which is the unnamed section
196 // at the beginning of the configuration file.
197 //=
198 void clear();
199
200 //=
201 // Loads the default file into the configuration object.
202 // By default, the current file is 'default.conf',
203 // but can be overridden by calling the setConfigFile(const char *filename) method.
204 // This method does not clear the config object before loading.
205 // If you load two or more configuration files that have the same sections,
206 // they are effectively merged. In this case, data members that have the same
207 // name will get the value of the last file loaded. This comes in handy if
208 // you load a global config file first, and you want to override
209 // some custom fields by subsequently loading a "preferences" file tailored to an individual.
210 // Use clear() if the config object already has data that you want to completely discard before loading
211 // a new file, or use another Config object instance.
212 //=
213 bool load();
214
215 //=
216 // Adds the file's configuration info to the current object.
217 // Does not delete any existing data in the config object.
218 // The default config file path is not altered...
219 // Use setConfigFile() to permanently set the default config file for load() and save()
220 // This method does not clear the config object before loading.
221 // Use clear() if the config object already has data that you want to discard.
222 //=
223 bool load(const char *filename);
224
225 bool load(std::istream& file);
226
227 //=
228 // Returns the most recent error string, if there is one.
229 // If no error exists, it will return the empty string ("").
230 //=
231 const char *getError();
232
233 int getNumSourceDestMembers() const;
234 const char *getSrcObjAt(int index) const;
235 const char *getDestObjAt(int index) const;
236 const char *getSrcAppAt(int index) const;
237 const char *getDestAppAt(int index) const;
238 const char *getWidthAt(int index) const;
239 /* remedius
240 */
241 const char *getCommTypeAt(int index) const;
242 /* remedius
243 */
244 const char *getProcMethodAt(int index) const;
245
246 //=
247 // Returns the number of sections in the entire configuration file
248 // This number includes the default section - "" (the empty section at the beginning of the file)
249 // Sections within the configuration file are identifed by [Square Brackets] surrounding the name of the section.
250 // Whitespace surrounding the section name is ignored. So [ This Section ] and [This Section] are identical.
251 // Section names <b>are</b> case sensitive.
252 // The default section is the empty section - the unnamed section at the beginning of the file that
253 // exists before any other [named] sections. This section exists even if the first line of the file
254 // contains a [named section]. The empty section will simply be void of any data.
255 // As such, the return value will always be >= 1.
256 //=
257 int getNumSections() const;
258
259 //=
260 // Enumeration method to discover sections
261 // Returns the section name at the given index,
262 // or NULL if the index is out of range
263 // If the section has no name, but is a valid index, then it will return the empty string ("")
264 //=
265 const char *getSectionNameAt(int index) const;
266
267 //=
268 // Sets the current working [section], possibly creating a new section
269 // The default section is "" (the untitled section at the beginning of the configuration file).
270 // If the new section cannot be found, and <b>shouldCreate</b> is <b>true</b>,
271 // then the section will be created and will exist at the end of all other sections.
272 // If the new section cannot be found, and <b>shouldCreate</b> is <b>false</b>,
273 // then the current section remains unchanged, and the method returns false.
274 // Leading and trailing whitespace is not preserved when the file is written,
275 // and as such should be avoided when naming sections
276 //=
277 bool setSection(const char *sectionname, bool shouldCreate);
278
279 //=
280 // Sets the current working section, creating the section if it does not exist.
281 // See <b><i>setSection(const char *sectionname, bool shouldCreate)</i></b> where <i>shouldCreate</i> = true.
282 //=
283 bool setSection(const char *sectionname);
284
285 //=
286 // Deletes the section identified by <i>sectionname</i> and all data associated with it
287 // Returns false if the section does not exist or has already been deleted.
288 // If the object is saved, the entire section will be commented out.
289 // If, during the lifetime of this object, the section is re-created, then the section will be revived, but all
290 // previous data members remain commented (as if deleted) unless/until recreated (see deleteData())
291 //=
292 bool deleteSection(const char *sectionname);
293
294
295 //=
296 // Returns the number of data elements for the current section
297 //
298 // <b>Example:</b>
299 //
300 // Given the following configuration:
301 //
302 //<font color=red>[Good Movies]
303 // The 5th Element
304 // Resevoir Dogs
305 // Braveheart
306 //
307 // [Bad Movies]
308 //
309 // Freddy Got Fingered
310 // CONfidential
311 //
312 // [Body Parts]
313 // arm=2
314 // head=1
315 // legs=2
316 // neck=1
317 // ears=2
318 // eyes=2
319 //
320 // </font>
321 //
322 // You'll get the following results:
323 // <code>
324 // config->setSection("Body Parts", true);
325 // config->getNumDataMembers(); // returns 6
326 //
327 // config->setSection("Bad Movies", true);
328 // config->getNumDataMembers(); // returns 2
329 //
330 // config->setSection("Good Movies", true);
331 // config->getNumDataMembers(); // returns 3
332 // </code>
333 //=
334 int getNumDataMembers() const;
335
336 //=
337 // Enumeration method to discover data members within the current section
338 // Returns the name of the data member at
339 // the specified index within the current
340 // section, or NULL if the index is out of range.
341 //
342 // <b>Example:</b>
343 //
344 // Given the following configuration:
345 //
346 //<font color=red>[Contact Info]
347 // name= Mark Twain
348 // email address = mark@twain
349 // phone = 123.456.789
350 // </font>
351 //
352 // You'll get the following results:
353 // <code>
354 // config->setSection("Contact Info");
355 // config->getDataNameAt(0); // returns "name"
356 // config->getDataNameAt(2); // returns "phone"
357 // config->getDataNameAt(3); // returns NULL (out of range)
358 //
359 // </code>
360 //=
361 const char *getDataNameAt(int index) const;
362
363 //=
364 // Returns true if a data member exists with the given
365 //name within the current section
366 //
367 //
368 // &nbsp;<b>Example:</b>
369 //
370 // Given the following configuration:
371 //
372 //<font color=red>
373 // [Strong Body Parts]
374 // arm=2
375 // head=1
376 // legs=2
377 // neck=1
378 //
379 // [Weak Body Parts]
380 // eyes=2
381 // belly=1
382 // nose=1
383 // </font>
384 //
385 // You'll get the following results:
386 // <code>
387 // config->setSection("Strong Body Parts");
388 //
389 // if(config->exists("arm"))
390 // {
391 // // yes, arm exists...
392 // }
393 //
394 // if(config->exists("belly"))
395 // {
396 // // returns false - belly isn't in that section.
397 // }
398 //
399 // config->setSection("Weak Body Parts");
400 //
401 // if(config->exists("belly"))
402 // {
403 // // yes! belly is in this section....
404 // }
405 // </code>
406 //=
407 bool exists(const char *name) const;
408
409 //=
410 // Returns the boolean value of the data identified by <i>name</i> within the current section
411 // This is a helper function that converts the string data stored in the config object to a boolean
412 // Returns true if the current value is "on" or 1,
413 // or if the value starts with the letter 't' or 'y'
414 // ( all of the above are case insensitive )
415 // returns false otherwise
416 //
417 // As such, it will interpret the following as true:
418 // Yes, yes, On, on, True, true, 1, T, t, Y, y
419 //
420 // Likewise, it will interpret the following as false:
421 // No, no, Off, off, False, false, 0, F, f, N, n
422 //
423 //
424 // <b>Example:</b>
425 //
426 // Given the following configuration:
427 //
428 //<font color=red>
429 // [Things that are and aren't]
430 // Bed Bugs= TRUE
431 // Aliens = 1
432 // Freud= ON
433 // Water= yes
434 // ducks = false
435 // carpenter = off
436 // floppy = 0
437 // sand = Nope
438 // </font>
439 // You'll get the following results:
440 // <code>
441 // config->setSection("Things that are and aren't");
442 //
443 // config->getBoolValue("Aliens"); // returns true
444 // config->getBoolValue("carpenter"); // returns false
445 // // etc...
446 // </code>
447 //=
448 bool getBoolValue(const char *name) const;
449
450 //=
451 // Returns the integer value of the data identifed by <i>name</i> within the current section
452 // This is a helper function that converts the string value stored in the config object to an int.
453 // Returns 0 if the data does not exist or if an integer value cannot be determined.
454 //=
455 int getIntValue(const char *name) const;
456
457 //=
458 // Returns the double value, or 0 if a double value cannot be determined
459 // Thie is a helper function that converts the string value stored in the config object to a double
460 //=
461 double getDoubleValue(const char *name) const;
462
463 //=
464 // Returns the string-character value.
465 // Unless the value is quoted, leading and trailing whitespace is stripped.
466 // If the value is quoted, the quotes are removed, and leading and trailing whitespace
467 // within the quotes are preserved.
468 //=
469 const char * getValue(const char *name) const;
470
471 //=
472 // Same as <code>const char *getValue(const char *name)</code>
473 // @deprecated
474 //=
475 const char * getStringValue(const char *name) const;
476
477 //=
478 // Sets the named value within the current section to "true" or "false"
479 // This is a helper function that converts a bool to the string value
480 // that the config object expects.
481 // If the value is already set for the data item, the value is changed.
482 // If the value is not already set, it is created
483 //
484 // <b>Example:</b>
485 //
486 // To create the following configuration entries:
487 //
488 // <font color=red>[On Sale]
489 // Hard Drives = true
490 // Cases = false
491 // Keyboards = false
492 // </font>
493 //
494 // Use this code:
495 // <code>
496 // config->setSection("On Sale");
497 // config->setBoolValue("Hard Drives", true);
498 // config->setBoolValue("Cases", false);
499 // config->setBoolValue("Keyboards", false);
500 // config->save();
501 // </code>
502 //
503 // @param name the identifying name of the data member
504 // @param value The integer value of the data member
505 //
506 //=
507 void setBoolValue(const char *name, bool value);
508
509 //=
510 // Sets the named value within the current section to the specified integer
511 // The config object stores values as strings- this is a helper method
512 // that converts an integer to the expected string value.
513 // If the value is already set for the data item, the value is changed.
514 // If the value is not already set, it is created
515 //
516 // <b>Example:</b>
517 //
518 // To create the following configuration entries:
519 //
520 // <font color=red>[Inventory]
521 // Hard Drives = 35
522 // Cases = 24
523 // Keyboards = 103
524 // </font>
525 //
526 // Use this code:
527 // <code>
528 // config->setSection("Inventory", true);
529 // config->setIntValue("Hard Drives", 35);
530 // config->setIntValue("Cases", 24);
531 // config->setIntValue("Keyboards", 103);
532 // config->save();
533 // </code>
534 //
535 // @param name the identifying name of the data member
536 // @param value The integer value of the data member
537 //
538 //=
539 void setIntValue(const char *name, int value);
540
541 //=
542 // Sets the value for a data item within the current section to the specified double
543 // The config object stores values as strings- this is a helper method
544 // that converts an integer to a string value to be stored.
545 // If the value is already set for the data item, the value is changed.
546 // If the value is not already set, it is created
547 //
548 // <b>Example:</b>
549 //
550 // To create the following configuration entries:
551 //
552 // <font color=red>[Prices]
553 // Hard Drives = 59.95
554 // Cases = 44.00
555 // Keyboards = 12.25
556 // </font>
557 //
558 // Use this code:
559 // <code>
560 // config->setSection("Prices", true);
561 // config->setDoubleValue("Hard Drives", 59.95);
562 // config->setDoubleValue("Cases", 44.00);
563 // config->setDoubleValue("Keyboards", 12.25);
564 // config->save();
565 // </code>
566 //
567 // @param name the identifying name of the data member
568 // @param value The double value of the data member
569 // @author Matt Flood
570 //
571 //=
572 void setDoubleValue(const char *name, double value);
573
574
575 //=
576 // Sets the value for a data item within the current section to the specified string
577 // If the value is already set for the data item, the value is changed.
578 // If the value is not already set, it is created.
579 // If leading and/or trailing whitespace exists within the value, it will be preserved in
580 // the config file. The config object will automatically surround the value with quotes.
581 // Likewise, if the value starts with a quote ("), then the value
582 // will necessarily be surrounded by quotes to preserve it. As such, programs do not have to, (and
583 // in fact should not ) perform their own quoting of values. You should provide you own
584 // quotes only when you are modifying the config/ini file using a text editor or some other means
585 // besides the RudeConfig object.
586 //
587 // Note: Leading and traling whitespace of the identifying fieldname (the key for the data item) is not preserved.
588 //
589 // <b>Example:</b>
590 //
591 // To create the following configuration entry:
592 //
593 // <font color=red>[travel]<br>Travel Agent = Mary Johnson</font>
594 //
595 // Use this code:
596 // <code>
597 // config->setSection("travel", true);
598 // config->setStringValue("Travel Agent", "Mary Johnson");
599 // config->save();
600 // </code>
601 //
602 // @param name the identifying name of the data member
603 // @param value The string value of the data member
604 // @author Matt Flood
605 //
606 //=
607 void setValue(const char *name, const char *value);
608
609 //=
610 // Same as setValue(const char *name, const char *value);
611 // @deprecated
612 //
613 //=
614 void setStringValue(const char *name, const char *value);
615
616 //=
617 // Deletes the data member identified by <i>name</i> (if it exists) in the current section.
618 // If the configuration object is saved,
619 // then the data member will be preserved by being commented out.
620 // If a new value is later assigned to this
621 // value (in the lifetime of the instance),
622 // then it becomes undeleted (uncommented).
623 // Values can only be revived this way using the same instance they were deleted with.
624 // Otherwise, they become permanent comments.
625 // See <code>preserveDeletedData(bool shouldPreserve)</code> to alter this behavior.
626 //
627 //=
628 bool deleteData(const char *name);
629
630 ~Config();
631
632 };
633
634 } // end namespace rude
635
636 #endif
637