Debian Codegraph @

rudeconfig/src/ConfigImpl.cpp

raw
1 // configimpl.cpp
2 //
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008 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 #include "../config.h"
24
25 #include "ConfigImpl.h"
26
27 #ifndef INCLUDED_Section_H
28 #include "Section.h"
29 #endif
30
31 #ifndef INCLUDED_File_H
32 #include "File.h"
33 #endif
34
35 #ifndef INCLUDED_Writer_H
36 #include "Writer.h"
37 #endif
38
39 #ifndef INPUT_RealOrganiser_h
40 #include "RealOrganiser.h"
41 #endif
42
43 #ifndef INPUT_ParserJuly2004_h
44 #include "ParserJuly2004.h"
45 #endif
46
47 #ifndef INCLUDED_BASE64ENCODER_H
48 #include "Base64Encoder.h"
49 #endif
50
51
52 #ifndef INCLUDED_CSTDIO
53 #include <cstdio>
54 #define INCLUDED_CSTDIO
55 #endif
56
57 #ifndef INCLUDED_CSTDLIB
58 #include <cstdlib>
59 #define INCLUDED_CSTDLIB
60 #endif
61
62 #ifndef INCLUDED_IOSTREAM
63 #include <iostream>
64 #define INCLUDED_IOSTREAM
65 #endif
66
67 #ifndef INCLUDED_FSTREAM
68 #include <fstream>
69 #define INCLUDED_FSTREAM
70 #endif
71
72 #include <cstring>
73
74 using namespace std;
75
76 namespace rude{
77 namespace config{
78
79 // STATIC DATA
80 //
81
82 std::string ConfigImpl::s_defaultConfigFile = "default.ini";
83 char ConfigImpl::s_defaultCommentChar = '#';
84 char ConfigImpl::s_defaultDelimiter = '=';
85 bool ConfigImpl::s_defaultPreserveDeleted = false;
86 bool ConfigImpl::s_defaultAllowDuplicate = false;
87 bool ConfigImpl::s_defaultIgnoreCase = false;
88 std::string ConfigImpl::s_returnValue="";
89
90
91 //////////////////////////////////////
92 // STATIC METHODS
93 //////////////////////////////////////
94
95 const char *ConfigImpl::version()
96 {
97 return "0";
98 }
99
100 void ConfigImpl::setDefaultConfigFile(const char *filepath)
101 {
102 s_defaultConfigFile = filepath ? filepath : "default.ini";
103 }
104 const char *ConfigImpl::getDefaultConfigFile()
105 {
106 return s_defaultConfigFile.c_str();
107 }
108
109
110 void ConfigImpl::setDefaultCommentCharacter(char c)
111 {
112 s_defaultCommentChar = c;
113 }
114
115 char ConfigImpl::getDefaultCommentCharacter()
116 {
117 return s_defaultCommentChar;
118 }
119
120
121 void ConfigImpl::setDefaultDelimiter(char c)
122 {
123 s_defaultDelimiter = c;
124 }
125
126 char ConfigImpl::getDefaultDelimiter()
127 {
128 return s_defaultDelimiter;
129 }
130
131 void ConfigImpl::setDefaultPreserveDeleted(bool shouldPreserve)
132 {
133 s_defaultPreserveDeleted = shouldPreserve;
134 }
135
136 bool ConfigImpl::getDefaultPreserveDeleted()
137 {
138 return s_defaultPreserveDeleted;
139 }
140
141
142 void ConfigImpl::setDefaultIgnoreCase(bool shouldIgnore)
143 {
144 s_defaultIgnoreCase = shouldIgnore;
145 }
146
147 void ConfigImpl::setDefaultAllowDuplicateKeys(bool shouldAllow)
148 {
149 s_defaultAllowDuplicate = shouldAllow;
150 }
151
152 // STATIC DATA CONVERSION
153 //
154 bool ConfigImpl::stringToBool(const char *value)
155 {
156 if(value)
157 {
158 // Values that mean true:
159 // yes, on, true, 1
160 // [yY]*, [oO][nN]*, O*, [Tt]*, 1
161 // Values that mean false:
162 // no, off, false, 0
163
164 switch(value[0])
165 {
166 case 't':
167 return true;
168 case 'T':
169 return true;
170 case 'y':
171 return true;
172 case 'Y':
173 return true;
174 case '1':
175 return true;
176 case 'o':
177 if(value[1] == 'n' || value[1] == 'N')
178 {
179 // since value[0] was not null, we can rest assured that there is a value[1] -
180 // it is at least the null string terminator!
181 return true;
182 }
183 break;
184 case 'O':
185 if(value[1] == 'n' || value[1] == 'N')
186 {
187 return true;
188 }
189 break;
190 default:
191 return false;
192 }
193 }
194 return false;
195 }
196
197 const char *ConfigImpl::boolToString(bool value)
198 {
199 return (value ? "true" : "false");
200 }
201
202 int ConfigImpl::stringToInt(const char *string)
203 {
204 if(string)
205 {
206 return atoi(string);
207 }
208 return 0;
209 }
210
211 const char *ConfigImpl::intToString(int value)
212 {
213 char myint[25];
214 sprintf(myint, "%i", value);
215 s_returnValue = myint;
216 return s_returnValue.c_str();
217 }
218
219 double ConfigImpl::stringToDouble(const char *string)
220 {
221 if(string)
222 {
223 return atof(string);
224 }
225 return 0;
226 }
227
228 const char *ConfigImpl::doubleToString(double value)
229 {
230 char mydouble[25];
231 sprintf(mydouble, "%g", value);
232 s_returnValue = mydouble;
233 return s_returnValue.c_str();
234 }
235
236 char * ConfigImpl::stringToBinary(const char *value, int &outlength)
237 {
238 // Base64Encoder USAGE:
239 // static char * decode(const char *data, int datalength, int &outlength);
240
241 if(value)
242 {
243 int datalength=strlen(value);
244 return Base64Encoder::decode(value, datalength, outlength);
245 }
246 outlength=0;
247 return 0;
248 }
249
250 const char *ConfigImpl::binaryToString(const char *value, int length)
251 {
252
253 // Base64Encoder USAGE:
254 // static char * encode(const char *data, int datalength, int &outlength);
255
256 if(value)
257 {
258 int outlength;
259 s_returnValue = Base64Encoder::encode(value, length, outlength);
260 }
261 else
262 {
263 s_returnValue = "";
264 }
265 return s_returnValue.c_str();
266 }
267
268
269 //////////////////////////////////////
270 // INSTANCE METHODS
271 //////////////////////////////////////
272
273
274 ///////////////////////////
275 // CONSTRUCTOR & DESTRUCTOR
276 ///////////////////////////
277
278 ConfigImpl::ConfigImpl()
279 {
280 d_file = new File();
281
282 // Create the appropriate Objects here,
283 // all other methods interact with
284 // the base class interfaces
285 //
286 d_writer = new Writer();
287
288 d_parser = new ParserJuly2004();
289
290 d_error="";
291 d_errorcode="";
292
293 d_commentcharacter = s_defaultCommentChar;
294 d_delimiter = s_defaultDelimiter;
295 d_configfile = s_defaultConfigFile;
296 d_preserveDeleted = s_defaultPreserveDeleted;
297 d_allowDuplicate = s_defaultAllowDuplicate;
298 d_ignoreCase = s_defaultIgnoreCase;
299
300
301 }
302
303 ConfigImpl::~ConfigImpl()
304 {
305 delete d_writer;
306 delete d_parser;
307 delete d_file;
308 }
309
310 ////////////
311 // STATUS
312 ////////////
313
314 void ConfigImpl::setError(const char *errorcode, const char *errorstring)
315 {
316 d_errorcode = errorcode ? errorcode : "";
317 d_error = errorstring ? errorstring : "";
318 }
319
320 const char *ConfigImpl::getError() const
321 {
322 return d_error.c_str();
323 }
324
325 const char *ConfigImpl::getErrorCode() const
326 {
327 return d_errorcode.c_str();
328 }
329
330 //////////////////////
331 // INSTANCE BEHAVIOR
332 //////////////////////
333
334 void ConfigImpl::setConfigFile(const char *filepath)
335 {
336 d_configfile = filepath ? filepath : s_defaultConfigFile.c_str();
337 }
338 const char * ConfigImpl::getConfigFile()
339 {
340 return d_configfile.c_str();
341 }
342
343 void ConfigImpl::setCommentCharacter(char c)
344 {
345 d_commentcharacter = c;
346 }
347
348 void ConfigImpl::setDelimiter(char c)
349 {
350 d_delimiter = c;
351 }
352
353 void ConfigImpl::preserveDeletedData(bool shouldPreserve)
354 {
355 d_preserveDeleted = shouldPreserve;
356 }
357
358 void ConfigImpl::ignoreCase(bool shouldIgnore)
359 {
360 d_ignoreCase = shouldIgnore;
361 }
362
363 void ConfigImpl::allowDuplicateKeys(bool shouldAllow)
364 {
365 d_allowDuplicate = shouldAllow;
366 }
367
368 //////////////////////////////
369 // LOADING & SAVING & CLEARING
370 //////////////////////////////
371
372 bool ConfigImpl::load()
373 {
374 // forward to load(filepath)
375 //
376 return load(d_configfile.c_str());
377 }
378
379 bool ConfigImpl::load(const char *filepath)
380 {
381
382 if(filepath && filepath[0])
383 {
384 d_configfile=filepath;
385 std::ifstream infile(filepath);
386
387 if(infile)
388 {
389 // forward to load(std::istream&)
390 bool retval = load(infile);
391 infile.close();
392 return retval;
393 }
394 else
395 {
396 setError("2001", "Error opening config file for reading");
397 return false;
398 }
399 }
400 else
401 {
402 return load(std::cin);
403 }
404 }
405
406 bool ConfigImpl::load(std::istream& inputstream)
407 {
408 d_parser->setCommentCharacter(d_commentcharacter);
409
410 d_parser->setDelimiter(d_delimiter);
411
412 RealOrganiser organiser(d_file);
413
414 if(d_parser->parse(inputstream, organiser))
415 {
416 setSection("");
417 return true;
418 }
419 else
420 {
421 setSection("");
422 setError(d_parser->getErrorCode(), d_parser->getError());
423 return false;
424 }
425 }
426
427 bool ConfigImpl::save()
428 {
429 // forward to save(filepath)
430 //
431 return save(d_configfile.c_str());
432 }
433
434 bool ConfigImpl::save(const char *filepath)
435 {
436 if(filepath && filepath[0] != 0)
437 {
438 ofstream outstream(filepath);
439 if(outstream)
440 {
441 // forward to save(std::ostream&)
442 //
443 bool retval = save(outstream);
444 outstream.close();
445 return retval;
446 }
447 else
448 {
449 setError("2002", "Error opening config file for writing");
450 return false;
451 }
452 }
453 else
454 {
455 return save(std::cout);
456 }
457 }
458
459 bool ConfigImpl::save(std::ostream& outstream)
460 {
461 d_writer->setOutputStream(outstream);
462 d_writer->setCommentChar(d_commentcharacter);
463 d_writer->setDelimiter(d_delimiter);
464 d_writer->preserveDeleted(d_preserveDeleted);
465
466 d_file->acceptWriter(*d_writer);
467
468 return true;
469 }
470
471 void ConfigImpl::clear()
472 {
473 d_file->clear();
474 }
475
476
477 //////////////////
478 // SECTION METHODS
479 //////////////////
480
481
482 int ConfigImpl::getNumSections() const
483 {
484 return d_file->getNumSections();
485 }
486
487 const char *ConfigImpl::getSectionNameAt(int index) const
488 {
489 return d_file->getSectionNameAt(index);
490 }
491
492 bool ConfigImpl::setSection(const char *sectionname, bool shouldCreate)
493 {
494 return d_file->setSection(sectionname, shouldCreate);
495 }
496
497 bool ConfigImpl::setSection(const char *sectionname)
498 {
499 return d_file->setSection(sectionname, true);
500 }
501
502 bool ConfigImpl::deleteSection(const char *sectionname)
503 {
504 return d_file->deleteSection(sectionname);
505 }
506
507 ////////////////
508 // DATA METHODS
509 ////////////////
510
511 int ConfigImpl::getNumSourceDestMembers() const
512 {
513 return d_file->getNumSourceDestMembers();
514 }
515
516 const char *ConfigImpl::getSrcAppAt(int index) const
517 {
518 return d_file->getSrcAppAt(index);
519 }
520
521 const char *ConfigImpl::getDestAppAt(int index) const
522 {
523 return d_file->getDestAppAt(index);
524 }
525
526 const char *ConfigImpl::getSrcObjAt(int index) const
527 {
528 return d_file->getSrcObjAt(index);
529 }
530
531 const char *ConfigImpl::getDestObjAt(int index) const
532 {
533 return d_file->getDestObjAt(index);
534 }
535
536 const char *ConfigImpl::getWidthAt(int index) const
537 {
538 return d_file->getWidthAt(index);
539 }
540 const char *ConfigImpl::getCommTypeAt(int index) const
541 {
542 return d_file->getCommTypeAt(index);
543 }
544 const char *ConfigImpl::getProcMethodAt(int index) const
545 {
546 return d_file->getProcMethodAt(index);
547 }
548
549 int ConfigImpl::getNumDataMembers() const
550 {
551 return d_file->getNumDataMembers();
552 }
553
554 const char *ConfigImpl::getDataNameAt(int index) const
555 {
556 return d_file->getDataNameAt(index);
557 }
558
559 const char *ConfigImpl::getDataValueAt(int index) const
560 {
561 return d_file->getDataValueAt(index);
562 }
563
564 bool ConfigImpl::exists(const char *name) const
565 {
566 return d_file->exists(name);
567 }
568
569 const char * ConfigImpl::getStringValue(const char *name) const
570 {
571 return d_file->getStringValue(name);
572 }
573
574 void ConfigImpl::setStringValue(const char *name, const char *value)
575 {
576 d_file->setStringValue(name, value);
577 }
578
579 bool ConfigImpl::deleteData(const char *name)
580 {
581 return d_file->deleteData(name);
582 }
583
584 ///////////////////////////////
585 // Working with Duplicate Keys
586 // NOT IMPLEMENTED YET
587 ///////////////////////////////
588
589 int ConfigImpl::getNumDataMembers(const char *key) const
590 {
591 if(exists(key))
592 {
593 return 1;
594 }
595 else
596 {
597 return 0;
598 }
599 }
600
601 const char * ConfigImpl::getStringValue(const char *name, int index) const
602 {
603 return d_file->getStringValue(name);
604 }
605
606 void ConfigImpl::addStringValue(const char *name, const char *value)
607 {
608 d_file->setStringValue(name, value);
609 }
610
611 bool ConfigImpl::deleteData(const char *name, int index)
612 {
613 return d_file->deleteData(name);
614 }
615
616 }}// end namespace rude::config