raSystem  1.0 bata
raIni.cpp
Go to the documentation of this file.
1 #include "..\include\raMain.h"
2 
3 namespace System
4 {
5  bool raIni::Open(const char filename[])
6  {
7  if(!Util::raFile::raFileExists(filename))
8  return false;
9 
10  // init some variables
11  this->Closed = false;
12  this->withoutSections = true;
13  this->sectionnames.push_back(INI_DEFAULT_SECTION);
14  this->buffer.push_back( std::vector<raString>() );
15  this->filename = filename;
16  this->sections = 0;
17  this->entries = 0;
18 
19  //open the file
20  std::ifstream inifile(filename);
21  raString iniline("");
22  int section_idx = 0; // counts the section, needed for the buffer vector
23  raString section_tmp_name;
24  size_t pos1;
25  size_t pos2;
26 
27  if(inifile.is_open())
28  {
29  this->InitGood = true;
30  while(!inifile.eof())
31  {
32  // Read section and insert them to the vector
33  getline(inifile, iniline);
34  if(iniline.length() > INI_MAX_LENGTH) // too large, avoid buffer overflows
35  iniline = iniline.substr(0, INI_MAX_LENGTH);
36 
37  if(iniline[0] == '[' && iniline.find(']') != raString::npos) // a section starts
38  {
39  // change mode
40  if(this->withoutSections)
41  this->withoutSections = false;
42  // statistics
43  this->sections++;
44 
45  // Remove [ and ]
46  pos1 = iniline.find("[");
47  pos2 = iniline.find("]", pos1+1);
48  section_tmp_name = iniline.substr(pos1+1, pos2-1);
49 
50  // Overwrite default section
51  if(section_idx == 0)
52  this->sectionnames[0] = section_tmp_name;
53  else
54  this->sectionnames.push_back(section_tmp_name);
55 
56  this->buffer.push_back( std::vector<raString>() ); // create a new dimension
57 
58  section_idx++;
59 
60  // Reset pos1 and pos2
61  pos1 = 0;
62  pos2 = 0;
63  }
64  else
65  {
66  // statistics
67  if(iniline[0] != ';' && iniline.find('=') != raString::npos)
68  this->entries++;
69 
70  if(section_idx > 0)
71  this->buffer[section_idx-1].push_back(iniline);
72  else
73  this->buffer[0].push_back(iniline);
74  }
75  }
76  m_isloaded = true;
77  ROK("Config [ini] geladen");
78  }
79  else
80  this->InitGood = false;
81 
82  inifile.close();
83  return InitGood;
84  }
85  void raIni::Clear()
86  {
87  // clear buffers
88  this->sectionnames.clear();
89  this->buffer.clear();
90 
91  //statistics
92  this->entries = 0;
93  this->sections = 0;
94  }
95  void raIni::Close()
96  {
97  int emptylines = 0;
98  std::fstream inifile;
99 
100  inifile.open(this->filename.c_str(), std::ios::out);
101  if(inifile.is_open())
102  {
103  this->InitGood = false; // file is closed, so false
104  std::vector<std::vector <raString> >::iterator section_it;
105  std::vector<raString>::iterator sectionnames_it;
106  std::vector<raString>::iterator line_it;
107 
108  for(section_it = this->buffer.begin(), sectionnames_it = this->sectionnames.begin(); section_it != this->buffer.end(); ++section_it, ++sectionnames_it)
109  {
110  if(sectionnames_it != this->sectionnames.end())
111  {
112  if(!this->withoutSections) // only write the section if the section name isn't empty and if the mode "without Section" isn't true
113  inifile << '[' << *sectionnames_it << ']' << std::endl;
114  }
115 
116  // get all lines of the section
117  for(line_it = section_it->begin(); line_it != section_it->end(); ++line_it)
118  {
119  // don't add a new line if the section and entry is the last one
120  if((this->buffer.end()-section_it) == 2 && (section_it->end() - line_it) == 2) // = last entry of the last section
121  {
122  if(line_it->compare(INI_LINE_DONTSAVE)) // only write the line if it isn't marked as removed
123  inifile << *line_it;
124  }
125  else
126  {
127  if(line_it->compare(INI_LINE_DONTSAVE)) // only write the line if it isn't marked as removed
128  inifile << *line_it << std::endl;
129  }
130  }
131  }
132  inifile.close();
133  this->Closed = true;
134  }
135  else
136  {
137  this->InitGood = false;
138  this->Closed = true;
139  }
140  }
141  bool raIni::Good()
142  {
143  bool returnVal = this->InitGood;
144  return returnVal;
145  }
146 
147  int raIni::GetInt(const char section[], const char key[])
148  {
149  // Get the right dimension for accessing the buffer
150  std::vector<raString>::iterator sectionnames_it;
151  std::vector<raString>::iterator buffer_it;
152  std::vector<raString>::iterator buffer_end;
153  bool lsection_found = false;
154  int lsection = 0;
155 
156  raString lkey;
157  raString lvalue;
158 
159  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
160  {
161  if(!(*sectionnames_it).compare(section))
162  {
163  lsection_found = true;
164  break;
165  }
166  lsection++;
167  }
168 
169  if(!lsection_found && !this->withoutSections) return -1;
170 
171  buffer_end = this->buffer[lsection].end();
172  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
173  {
174  lkey = this->getKeyFromString(*buffer_it);
175 
176  if(!lkey.compare(key))
177  {
178  lvalue = this->getValueFromString(*buffer_it);
179  return (this->stringtonum<int>(lvalue));
180  }
181  }
182 
183  // key not found
184  return -1;
185  }
186  long raIni::GetLong(const char section[], const char key[])
187  {
188  // Get the right dimension for accessing the buffer
189  std::vector<raString>::iterator sectionnames_it;
190  std::vector<raString>::iterator buffer_it;
191  std::vector<raString>::iterator buffer_end;
192 
193  bool lsection_found = false;
194  int lsection = 0;
195 
196  raString lkey;
197  raString lvalue;
198 
199  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
200  {
201  if(!(*sectionnames_it).compare(section))
202  {
203  lsection_found = true;
204  break;
205  }
206  lsection++;
207  }
208 
209  if(!lsection_found && !this->withoutSections) return -1;
210 
211  buffer_end = this->buffer[lsection].end();
212  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
213  {
214  lkey = this->getKeyFromString(*buffer_it);
215  if(!lkey.compare(key))
216  {
217  lvalue = this->getValueFromString(*buffer_it);
218  return (this->stringtonum<long>(lvalue));
219  }
220  }
221  return -1;
222  }
223  bool raIni::GetBool(const char section[], const char key[])
224  {
225  // Get the right dimension for accessing the buffer
226  std::vector<raString>::iterator sectionnames_it;
227  std::vector<raString>::iterator buffer_it;
228  std::vector<raString>::iterator buffer_end;
229 
230  bool lsection_found = false;
231  int lsection = 0;
232 
233  raString lkey;
234  raString lvalue;
235 
236  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
237  {
238  if(!(*sectionnames_it).compare(section))
239  {
240  lsection_found = true;
241  break;
242  }
243  lsection++;
244  }
245 
246  if(!lsection_found && !this->withoutSections) return false;
247 
248  buffer_end = this->buffer[lsection].end();
249  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
250  {
251  lkey = this->getKeyFromString(*buffer_it);
252 
253  if(!lkey.compare(key))
254  {
255  int lretval;
256  lvalue = this->getValueFromString(*buffer_it);
257  lretval = (this->stringtonum<int>(lvalue));
258 
259  if(lretval == 1)
260  return true;
261  else
262  return false;
263  }
264  }
265  return false;
266  }
267  raString raIni::GetString(const char section[], const char key[])
268  {
269  // Get the right dimension for accessing the buffer
270  std::vector<raString>::iterator sectionnames_it;
271  std::vector<raString>::iterator buffer_it;
272  std::vector<raString>::iterator buffer_end;
273 
274  bool lsection_found = false;
275  int lsection = 0;
276 
277  raString lkey;
278  raString lvalue;
279 
280  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); sectionnames_it++)
281  {
282  if(!(*sectionnames_it).compare(section))
283  {
284  lsection_found = true;
285  break;
286  }
287  lsection++;
288  }
289 
290  if(!lsection_found && !this->withoutSections) return "";
291 
292  buffer_end=this->buffer[lsection].end();
293  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
294  {
295  lkey = this->getKeyFromString(*buffer_it);
296 
297  if(!lkey.compare(key))
298  {
299  raString lstring = getValueFromString(*buffer_it);
300  return lstring;
301  }
302  }
303 
304  // key not found
305  return "";
306  }
307  raString raIni::Get(const char section[], const char key[])
308  {
309  return this->GetString(section, key);
310  }
311 
312  void raIni::SetInt(const char section[],const char key[], int newval)
313  {
314  // Get the right dimension for accessing the buffer
315  std::vector<std::string>::iterator sectionnames_it;
316  std::vector<std::string>::iterator buffer_it;
317  std::vector<std::string>::iterator buffer_end;
318 
319  bool lsection_found = false;
320  bool lkey_found = false;
321  int lsection = 0;
322  int lkeyline = 0;
323 
324  std::string lkey;
325 
326  if(!this->withoutSections)
327  {
328  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
329  {
330  if(!(*sectionnames_it).compare(section))
331  {
332  lsection_found = true;
333  break;
334  }
335  lsection++;
336  }
337  }
338 
339  if(!lsection_found && !this->withoutSections)
340  this->CreateSection(section);
341  else
342  {
343  buffer_end = this->buffer[lsection].end();
344  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
345  {
346  lkey = this->getKeyFromString(*buffer_it);
347 
348  if(!lkey.compare(key))
349  {
350  *buffer_it = lkey+'=';
351  *buffer_it += newval;
352  lkey_found = true;
353  break;
354  }
355  lkeyline++;
356  }
357  }
358 
359  if(!lkey_found)
360  {
361  // statistics
362  this->entries++;
363 
364  std::string lnewentry;
365  lnewentry.append(key);
366  lnewentry.append("=");
367  lnewentry += newval;
368  this->buffer[lsection].push_back(lnewentry);
369  }
370  }
371  void raIni::SetLong(const char section[], const char key[], long newval)
372  {
373  // Get the right dimension for accessing the buffer
374  std::vector<std::string>::iterator sectionnames_it;
375  std::vector<std::string>::iterator buffer_it;
376  std::vector<std::string>::iterator buffer_end;
377 
378  bool lsection_found = false;
379  bool lkey_found = false;
380  int lsection = 0;
381  int lkeyline = 0;
382 
383  std::string lkey;
384 
385  if(!this->withoutSections)
386  {
387  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
388  {
389  if(!(*sectionnames_it).compare(section))
390  {
391  lsection_found = true;
392  break;
393  }
394  lsection++;
395  }
396  }
397 
398  if(!lsection_found && !this->withoutSections)
399  this->CreateSection(section);
400  else
401  {
402  buffer_end = this->buffer[lsection].end();
403  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
404  {
405  lkey = this->getKeyFromString(*buffer_it);
406 
407  if(!lkey.compare(key))
408  {
409  *buffer_it = lkey+'=';
410  *buffer_it += newval;
411  lkey_found = true;
412  break;
413  }
414  lkeyline++;
415  }
416  }
417 
418  if(!lkey_found)
419  {
420  // statistics
421  this->entries++;
422 
423  std::string lnewentry;
424  lnewentry.append(key);
425  lnewentry.append("=");
426  lnewentry += newval;
427  this->buffer[lsection].push_back(lnewentry);
428  }
429  }
430  void raIni::SetBool(const char section[], const char key[], bool newval)
431  {
432  // Get the right dimension for accessing the buffer
433  std::vector<std::string>::iterator sectionnames_it;
434  std::vector<std::string>::iterator buffer_it;
435  std::vector<std::string>::iterator buffer_end;
436 
437  bool lsection_found = false;
438  bool lkey_found = false;
439  int lsection = 0;
440  int lkeyline = 0;
441 
442  std::string lkey;
443 
444  if(!this->withoutSections)
445  {
446  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
447  {
448  if(!(*sectionnames_it).compare(section))
449  {
450  lsection_found = true;
451  break;
452  }
453  lsection++;
454  }
455  }
456 
457  if(!lsection_found && !this->withoutSections)
458  this->CreateSection(section);
459  else
460  {
461  buffer_end = this->buffer[lsection].end();
462  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
463  {
464  lkey = this->getKeyFromString(*buffer_it);
465 
466  if(!lkey.compare(key))
467  {
468  *buffer_it = lkey+'=';
469  *buffer_it += newval;
470  lkey_found = true;
471  break;
472  }
473  lkeyline++;
474  }
475  }
476 
477  if(!lkey_found)
478  {
479  // statistics
480  this->entries++;
481 
482  std::string lnewentry;
483  lnewentry.append(key);
484  lnewentry.append("=");
485  lnewentry += newval;
486  this->buffer[lsection].push_back(lnewentry);
487  }
488  }
489  void raIni::SetString(const char section[], const char key[], raString newval)
490  {
491  // Get the right dimension for accessing the buffer
492  std::vector<std::string>::iterator sectionnames_it;
493  std::vector<std::string>::iterator buffer_it;
494  std::vector<std::string>::iterator buffer_end;
495 
496  bool lsection_found = false;
497  bool lkey_found = false;
498  int lsection = 0;
499  int lkeyline = 0;
500 
501  std::string lkey;
502 
503  if(!this->withoutSections)
504  {
505  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
506  {
507  if(!(*sectionnames_it).compare(section))
508  {
509  lsection_found = true;
510  break;
511  }
512  lsection++;
513  }
514  }
515 
516  if(!lsection_found && !this->withoutSections)
517  this->CreateSection(section);
518  else
519  {
520  buffer_end = this->buffer[lsection].end();
521  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
522  {
523  lkey = this->getKeyFromString(*buffer_it);
524 
525  if(!lkey.compare(key))
526  {
527  *buffer_it = lkey+'=';
528  *buffer_it += newval;
529  lkey_found = true;
530  break;
531  }
532  lkeyline++;
533  }
534  }
535 
536  if(!lkey_found)
537  {
538  // statistics
539  this->entries++;
540 
541  std::string lnewentry;
542  lnewentry.append(key);
543  lnewentry.append("=");
544  lnewentry+=newval;
545  this->buffer[lsection].push_back(lnewentry);
546  }
547  }
548  void raIni::Set(const char section[], const char key[], const char newval[])
549  {
550  // Get the right dimension for accessing the buffer
551  std::vector<raString>::iterator sectionnames_it;
552  std::vector<raString>::iterator buffer_it;
553  std::vector<raString>::iterator buffer_end;
554 
555  bool lsection_found = false;
556  bool lkey_found = false;
557  int lsection = 0;
558  int lkeyline = 0;
559 
560  raString lkey;
561 
562  if(!this->withoutSections)
563  {
564  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
565  {
566  if(!(*sectionnames_it).compare(section))
567  {
568  lsection_found = true;
569  break;
570  }
571  lsection++;
572  }
573  }
574 
575  if(!lsection_found && !this->withoutSections)
576  this->CreateSection(section);
577  else
578  {
579  buffer_end = this->buffer[lsection].end();
580  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
581  {
582  lkey = this->getKeyFromString(*buffer_it);
583 
584  if(!lkey.compare(key))
585  {
586  *buffer_it = lkey+'=';
587  *buffer_it += newval;
588  lkey_found = true;
589  break;
590  }
591  lkeyline++;
592  }
593  }
594 
595  if(!lkey_found)
596  {
597  // statistics
598  this->entries++;
599 
600  raString lnewentry;
601  lnewentry.append(key);
602  lnewentry.append("=");
603  lnewentry += newval;
604  this->buffer[lsection].push_back(lnewentry);
605  }
606  }
607 
608  void raIni::CreateSection(const char section[])
609  {
610  bool lsecfound = false;
611  std::vector<std::string>::iterator sectionnames_it;
612 
613  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); sectionnames_it++)
614  {
615  if(!(*sectionnames_it).compare(section))
616  lsecfound = true;
617  }
618 
619  if(!lsecfound)
620  {
621  this->sectionnames.push_back(section);
622  this->buffer.push_back( std::vector<std::string>() );
623  this->sections++;
624  }
625  }
626  void raIni::RemoveSection(const char section[])
627  {
628  std::vector<std::string>::iterator sectionnames_it;
629  int lpos = 0;
630 
631  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); sectionnames_it++)
632  {
633  if(!(*sectionnames_it).compare(section))
634  {
635  this->buffer.erase(this->buffer.begin()+lpos);
636  sectionnames_it->clear();
637 
638  // statistics
639  this->sections--;
640  break;
641  }
642  lpos++;
643  }
644  }
645  void raIni::RemoveEntry(const char section[], const char key[])
646  {
647  // Get the right dimension for accessing the buffer
648  std::vector<std::string>::iterator sectionnames_it;
649  std::vector<std::string>::iterator buffer_it;
650  std::vector<std::string>::iterator buffer_end;
651  bool lsection_found = false;
652  int lsection = 0;
653 
654  std::string lkey;
655 
656  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
657  {
658  if(!(*sectionnames_it).compare(section))
659  {
660  lsection_found = true;
661  break;
662  }
663  lsection++;
664  }
665 
666  if(lsection_found)
667  {
668  buffer_end = this->buffer[lsection].end();
669  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
670  {
671  lkey = this->getKeyFromString(*buffer_it);
672 
673  if(!lkey.compare(key))
674  {
675  // statistics
676  this->entries--;
677 
678  *buffer_it = INI_LINE_DONTSAVE;
679  break;
680  }
681  }
682  }
683  }
684  bool raIni::ValidSection(const char section[])
685  {
686  std::vector<std::string>::iterator sectionnames_it;
687 
688  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
689  {
690  if(!sectionnames_it->compare(section))
691  return true;
692  }
693  return false;
694  }
695  bool raIni::ValidEntry(const char section[], const char key[])
696  {
697  // Get the right dimension for accessing the buffer
698  std::vector<std::string>::iterator sectionnames_it;
699  std::vector<std::string>::iterator buffer_it;
700  std::vector<std::string>::iterator buffer_end;
701 
702  bool lsection_found = false;
703  int lsection = 0;
704 
705  std::string lkey;
706 
707  for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
708  {
709  if(!(*sectionnames_it).compare(section))
710  {
711  lsection_found = true;
712  break;
713  }
714  lsection++;
715  }
716 
717  if(!lsection_found)
718  return false;
719  else
720  {
721  buffer_end = this->buffer[lsection].end();
722  for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
723  {
724  lkey = this->getKeyFromString(*buffer_it);
725  if(!lkey.compare(key))
726  return true;
727  }
728  }
729  return false;
730  }
732  {
733  int lsections = this->sections;
734  return lsections;
735  }
737  {
738  int lentries = this->entries;
739  return lentries;
740  }
741  std::string raIni::getKeyFromString(std::string mystring)
742  {
743  size_t i = mystring.find('=');
744  if(i>=0)
745  {
746  return mystring.substr(0, i);
747  }
748  else return "";
749  }
750 
751  /*
752  Name: getValueFromString
753  Function: Gets a value from a string, which is in the ini format
754  (key = value)
755  returns: The value as std:string
756  */
757  std::string raIni::getValueFromString(std::string mystring)
758  {
759  size_t i = mystring.find_last_of('=');
760  if(i>=0)
761  {
762  return mystring.substr(i+1);
763  }
764  else return "";
765  }
766 
767  template <class T>
768  std::string raIni::numtostring(T num)
769  {
770  std::string mystring;
771  std::ostringstream convert;
772  convert << num;
773  mystring = convert.str();
774  return mystring;
775  }
776 
777  template <class T>
778  T raIni::stringtonum(std::string mystring)
779  {
780  T num;
781  std::istringstream convert(mystring);
782  //convert.str(mystring);
783 
784  if( !(convert >> num) ) num = 0;
785 
786  return num;
787  }
788 }
virtual long GetLong(const char section[], const char key[])
Definition: raIni.cpp:186
#define INI_MAX_LENGTH
Definition: raIni.h:18
virtual bool ValidSection(const char section[])
Definition: raIni.cpp:684
virtual void SetLong(const char section[], const char key[], long newval)
Definition: raIni.cpp:371
virtual void RemoveEntry(const char section[], const char key[])
Definition: raIni.cpp:645
virtual void SetInt(const char section[], const char key[], int newval)
Definition: raIni.cpp:312
virtual void SetBool(const char section[], const char key[], bool newval)
Definition: raIni.cpp:430
virtual bool Good()
Definition: raIni.cpp:141
static BOOL raFileExists(raString Filename)
Definition: raUtility.cpp:43
virtual bool ValidEntry(const char section[], const char key[])
Definition: raIni.cpp:695
virtual bool GetBool(const char section[], const char key[])
Definition: raIni.cpp:223
#define INI_DEFAULT_SECTION
Definition: raIni.h:16
#define INI_LINE_DONTSAVE
Definition: raIni.h:21
virtual bool Open(const char filename[])
Definition: raIni.cpp:5
virtual raString Get(const char section[], const char key[])
Definition: raIni.cpp:307
virtual void Close()
Definition: raIni.cpp:95
virtual int GetInt(const char section[], const char key[])
Definition: raIni.cpp:147
virtual int CountSections()
Definition: raIni.cpp:731
std::string raString
Definition: raMain.h:107
void RAPI ROK(raString x)
Definition: raMain.cpp:114
virtual int CountEntries()
Definition: raIni.cpp:736
virtual void Set(const char section[], const char key[], const char newval[])
Definition: raIni.cpp:548
virtual void SetString(const char section[], const char key[], raString newval)
Definition: raIni.cpp:489
virtual void RemoveSection(const char section[])
Definition: raIni.cpp:626
virtual void CreateSection(const char section[])
Definition: raIni.cpp:608
virtual raString GetString(const char section[], const char key[])
Definition: raIni.cpp:267
virtual void Clear()
Definition: raIni.cpp:85