1 #include "..\include\raMain.h" 12 this->withoutSections =
true;
14 this->buffer.push_back( std::vector<raString>() );
15 this->filename = filename;
20 std::ifstream inifile(filename);
29 this->InitGood =
true;
33 getline(inifile, iniline);
37 if(iniline[0] ==
'[' && iniline.find(
']') != raString::npos)
40 if(this->withoutSections)
41 this->withoutSections =
false;
46 pos1 = iniline.find(
"[");
47 pos2 = iniline.find(
"]", pos1+1);
48 section_tmp_name = iniline.substr(pos1+1, pos2-1);
52 this->sectionnames[0] = section_tmp_name;
54 this->sectionnames.push_back(section_tmp_name);
56 this->buffer.push_back( std::vector<raString>() );
67 if(iniline[0] !=
';' && iniline.find(
'=') != raString::npos)
71 this->buffer[section_idx-1].push_back(iniline);
73 this->buffer[0].push_back(iniline);
77 ROK(
"Config [ini] geladen");
80 this->InitGood =
false;
88 this->sectionnames.clear();
100 inifile.open(this->filename.c_str(), std::ios::out);
101 if(inifile.is_open())
103 this->InitGood =
false;
104 std::vector<std::vector <raString> >::iterator section_it;
105 std::vector<raString>::iterator sectionnames_it;
106 std::vector<raString>::iterator line_it;
108 for(section_it = this->buffer.begin(), sectionnames_it = this->sectionnames.begin(); section_it != this->buffer.end(); ++section_it, ++sectionnames_it)
110 if(sectionnames_it != this->sectionnames.end())
112 if(!this->withoutSections)
113 inifile <<
'[' << *sectionnames_it <<
']' << std::endl;
117 for(line_it = section_it->begin(); line_it != section_it->end(); ++line_it)
120 if((this->buffer.end()-section_it) == 2 && (section_it->end() - line_it) == 2)
128 inifile << *line_it << std::endl;
137 this->InitGood =
false;
143 bool returnVal = this->InitGood;
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;
159 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
161 if(!(*sectionnames_it).compare(section))
163 lsection_found =
true;
169 if(!lsection_found && !this->withoutSections)
return -1;
171 buffer_end = this->buffer[lsection].end();
172 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
174 lkey = this->getKeyFromString(*buffer_it);
176 if(!lkey.compare(key))
178 lvalue = this->getValueFromString(*buffer_it);
179 return (this->stringtonum<int>(lvalue));
189 std::vector<raString>::iterator sectionnames_it;
190 std::vector<raString>::iterator buffer_it;
191 std::vector<raString>::iterator buffer_end;
193 bool lsection_found =
false;
199 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
201 if(!(*sectionnames_it).compare(section))
203 lsection_found =
true;
209 if(!lsection_found && !this->withoutSections)
return -1;
211 buffer_end = this->buffer[lsection].end();
212 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
214 lkey = this->getKeyFromString(*buffer_it);
215 if(!lkey.compare(key))
217 lvalue = this->getValueFromString(*buffer_it);
218 return (this->stringtonum<long>(lvalue));
226 std::vector<raString>::iterator sectionnames_it;
227 std::vector<raString>::iterator buffer_it;
228 std::vector<raString>::iterator buffer_end;
230 bool lsection_found =
false;
236 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
238 if(!(*sectionnames_it).compare(section))
240 lsection_found =
true;
246 if(!lsection_found && !this->withoutSections)
return false;
248 buffer_end = this->buffer[lsection].end();
249 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
251 lkey = this->getKeyFromString(*buffer_it);
253 if(!lkey.compare(key))
256 lvalue = this->getValueFromString(*buffer_it);
257 lretval = (this->stringtonum<int>(lvalue));
270 std::vector<raString>::iterator sectionnames_it;
271 std::vector<raString>::iterator buffer_it;
272 std::vector<raString>::iterator buffer_end;
274 bool lsection_found =
false;
280 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); sectionnames_it++)
282 if(!(*sectionnames_it).compare(section))
284 lsection_found =
true;
290 if(!lsection_found && !this->withoutSections)
return "";
292 buffer_end=this->buffer[lsection].end();
293 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
295 lkey = this->getKeyFromString(*buffer_it);
297 if(!lkey.compare(key))
299 raString lstring = getValueFromString(*buffer_it);
309 return this->GetString(section, key);
315 std::vector<std::string>::iterator sectionnames_it;
316 std::vector<std::string>::iterator buffer_it;
317 std::vector<std::string>::iterator buffer_end;
319 bool lsection_found =
false;
320 bool lkey_found =
false;
326 if(!this->withoutSections)
328 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
330 if(!(*sectionnames_it).compare(section))
332 lsection_found =
true;
339 if(!lsection_found && !this->withoutSections)
340 this->CreateSection(section);
343 buffer_end = this->buffer[lsection].end();
344 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
346 lkey = this->getKeyFromString(*buffer_it);
348 if(!lkey.compare(key))
350 *buffer_it = lkey+
'=';
351 *buffer_it += newval;
364 std::string lnewentry;
365 lnewentry.append(key);
366 lnewentry.append(
"=");
368 this->buffer[lsection].push_back(lnewentry);
374 std::vector<std::string>::iterator sectionnames_it;
375 std::vector<std::string>::iterator buffer_it;
376 std::vector<std::string>::iterator buffer_end;
378 bool lsection_found =
false;
379 bool lkey_found =
false;
385 if(!this->withoutSections)
387 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
389 if(!(*sectionnames_it).compare(section))
391 lsection_found =
true;
398 if(!lsection_found && !this->withoutSections)
399 this->CreateSection(section);
402 buffer_end = this->buffer[lsection].end();
403 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
405 lkey = this->getKeyFromString(*buffer_it);
407 if(!lkey.compare(key))
409 *buffer_it = lkey+
'=';
410 *buffer_it += newval;
423 std::string lnewentry;
424 lnewentry.append(key);
425 lnewentry.append(
"=");
427 this->buffer[lsection].push_back(lnewentry);
433 std::vector<std::string>::iterator sectionnames_it;
434 std::vector<std::string>::iterator buffer_it;
435 std::vector<std::string>::iterator buffer_end;
437 bool lsection_found =
false;
438 bool lkey_found =
false;
444 if(!this->withoutSections)
446 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
448 if(!(*sectionnames_it).compare(section))
450 lsection_found =
true;
457 if(!lsection_found && !this->withoutSections)
458 this->CreateSection(section);
461 buffer_end = this->buffer[lsection].end();
462 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
464 lkey = this->getKeyFromString(*buffer_it);
466 if(!lkey.compare(key))
468 *buffer_it = lkey+
'=';
469 *buffer_it += newval;
482 std::string lnewentry;
483 lnewentry.append(key);
484 lnewentry.append(
"=");
486 this->buffer[lsection].push_back(lnewentry);
492 std::vector<std::string>::iterator sectionnames_it;
493 std::vector<std::string>::iterator buffer_it;
494 std::vector<std::string>::iterator buffer_end;
496 bool lsection_found =
false;
497 bool lkey_found =
false;
503 if(!this->withoutSections)
505 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
507 if(!(*sectionnames_it).compare(section))
509 lsection_found =
true;
516 if(!lsection_found && !this->withoutSections)
517 this->CreateSection(section);
520 buffer_end = this->buffer[lsection].end();
521 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
523 lkey = this->getKeyFromString(*buffer_it);
525 if(!lkey.compare(key))
527 *buffer_it = lkey+
'=';
528 *buffer_it += newval;
541 std::string lnewentry;
542 lnewentry.append(key);
543 lnewentry.append(
"=");
545 this->buffer[lsection].push_back(lnewentry);
548 void raIni::Set(
const char section[],
const char key[],
const char newval[])
551 std::vector<raString>::iterator sectionnames_it;
552 std::vector<raString>::iterator buffer_it;
553 std::vector<raString>::iterator buffer_end;
555 bool lsection_found =
false;
556 bool lkey_found =
false;
562 if(!this->withoutSections)
564 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
566 if(!(*sectionnames_it).compare(section))
568 lsection_found =
true;
575 if(!lsection_found && !this->withoutSections)
576 this->CreateSection(section);
579 buffer_end = this->buffer[lsection].end();
580 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
582 lkey = this->getKeyFromString(*buffer_it);
584 if(!lkey.compare(key))
586 *buffer_it = lkey+
'=';
587 *buffer_it += newval;
601 lnewentry.append(key);
602 lnewentry.append(
"=");
604 this->buffer[lsection].push_back(lnewentry);
610 bool lsecfound =
false;
611 std::vector<std::string>::iterator sectionnames_it;
613 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); sectionnames_it++)
615 if(!(*sectionnames_it).compare(section))
621 this->sectionnames.push_back(section);
622 this->buffer.push_back( std::vector<std::string>() );
628 std::vector<std::string>::iterator sectionnames_it;
631 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); sectionnames_it++)
633 if(!(*sectionnames_it).compare(section))
635 this->buffer.erase(this->buffer.begin()+lpos);
636 sectionnames_it->clear();
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;
656 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
658 if(!(*sectionnames_it).compare(section))
660 lsection_found =
true;
668 buffer_end = this->buffer[lsection].end();
669 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
671 lkey = this->getKeyFromString(*buffer_it);
673 if(!lkey.compare(key))
686 std::vector<std::string>::iterator sectionnames_it;
688 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
690 if(!sectionnames_it->compare(section))
698 std::vector<std::string>::iterator sectionnames_it;
699 std::vector<std::string>::iterator buffer_it;
700 std::vector<std::string>::iterator buffer_end;
702 bool lsection_found =
false;
707 for(sectionnames_it = this->sectionnames.begin(); sectionnames_it < this->sectionnames.end(); ++sectionnames_it)
709 if(!(*sectionnames_it).compare(section))
711 lsection_found =
true;
721 buffer_end = this->buffer[lsection].end();
722 for(buffer_it = this->buffer[lsection].begin(); buffer_it < buffer_end; ++buffer_it)
724 lkey = this->getKeyFromString(*buffer_it);
725 if(!lkey.compare(key))
733 int lsections = this->sections;
738 int lentries = this->entries;
741 std::string raIni::getKeyFromString(std::string mystring)
743 size_t i = mystring.find(
'=');
746 return mystring.substr(0, i);
757 std::string raIni::getValueFromString(std::string mystring)
759 size_t i = mystring.find_last_of(
'=');
762 return mystring.substr(i+1);
768 std::string raIni::numtostring(T num)
770 std::string mystring;
771 std::ostringstream convert;
773 mystring = convert.str();
778 T raIni::stringtonum(std::string mystring)
781 std::istringstream convert(mystring);
784 if( !(convert >> num) ) num = 0;
virtual long GetLong(const char section[], const char key[])
virtual bool ValidSection(const char section[])
virtual void SetLong(const char section[], const char key[], long newval)
virtual void RemoveEntry(const char section[], const char key[])
virtual void SetInt(const char section[], const char key[], int newval)
virtual void SetBool(const char section[], const char key[], bool newval)
static BOOL raFileExists(raString Filename)
virtual bool ValidEntry(const char section[], const char key[])
virtual bool GetBool(const char section[], const char key[])
#define INI_DEFAULT_SECTION
#define INI_LINE_DONTSAVE
virtual bool Open(const char filename[])
virtual raString Get(const char section[], const char key[])
virtual int GetInt(const char section[], const char key[])
virtual int CountSections()
void RAPI ROK(raString x)
virtual int CountEntries()
virtual void Set(const char section[], const char key[], const char newval[])
virtual void SetString(const char section[], const char key[], raString newval)
virtual void RemoveSection(const char section[])
virtual void CreateSection(const char section[])
virtual raString GetString(const char section[], const char key[])