Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/core/ConfigValueContainer.cc @ 677

Last change on this file since 677 was 677, checked in by landauf, 16 years ago

changed \n to std::endl

File size: 31.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include <fstream>
29#include "ConfigValueContainer.h"
30#include "../../misc/Tokenizer.h"
31#include "../../misc/String2Number.h"
32
33#define CONFIGFILEPATH "orxonox.ini"
34
35namespace orxonox
36{
37    std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; // Set the static member variable configFileLines_s to zero
38    bool ConfigValueContainer::readConfigFile_s = false;                 // Set the static member variable readConfigFile_s to false
39
40    /**
41        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
42        @param value This is only needed to determine the right type.
43        @param classname The name of the class the variable belongs to
44        @param varname The name of the variable
45        @param defvalue The default-value
46    */
47    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue)
48    {
49        this->classname_ = classname;
50        this->varname_ = varname;
51        this->type_ = Int;
52
53        this->defvalueString_ = number2String(defvalue, "0");                   // Try to convert the default-value to a string
54        this->searchConfigFileLine();                                           // Search the entry in the config-file
55
56        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
57        if (!string2Number(this->value_.value_int_, valueString, defvalue))     // Try to convert the string to a value
58            this->setConfigFileEntyToDefault();                                 // The conversion failed
59    }
60
61    /**
62        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
63        @param value This is only needed to determine the right type.
64        @param classname The name of the class the variable belongs to
65        @param varname The name of the variable
66        @param defvalue The default-value
67    */
68    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned int defvalue)
69    {
70        this->classname_ = classname;
71        this->varname_ = varname;
72        this->type_ = uInt;
73
74        this->defvalueString_ = number2String(defvalue, "0");                   // Try to convert the default-value to a string
75        this->searchConfigFileLine();                                           // Search the entry in the config-file
76
77        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
78        if (!string2Number(this->value_.value_uint_, valueString, defvalue))    // Try to convert the string to a value
79            this->setConfigFileEntyToDefault();                                 // The conversion failed
80    }
81
82    /**
83        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
84        @param value This is only needed to determine the right type.
85        @param classname The name of the class the variable belongs to
86        @param varname The name of the variable
87        @param defvalue The default-value
88    */
89    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, char defvalue)
90    {
91        this->classname_ = classname;
92        this->varname_ = varname;
93        this->type_ = Char;
94
95        this->defvalueString_ = number2String((int)defvalue, "0");              // Try to convert the default-value to a string
96        this->searchConfigFileLine();                                           // Search the entry in the config-file
97
98        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
99        // I used value_int_ instead of value_char_ to avoid number <-> char confusion in the config-file
100        if (!string2Number(this->value_.value_int_, valueString, (int)defvalue))// Try to convert the string to a value
101            this->setConfigFileEntyToDefault();                                 // The conversion failed
102    }
103
104    /**
105        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
106        @param value This is only needed to determine the right type.
107        @param classname The name of the class the variable belongs to
108        @param varname The name of the variable
109        @param defvalue The default-value
110    */
111    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned char defvalue)
112    {
113        this->classname_ = classname;
114        this->varname_ = varname;
115        this->type_ = uChar;
116
117        this->defvalueString_ = number2String((unsigned int)defvalue, "0");     // Try to convert the default-value to a string
118        this->searchConfigFileLine();                                           // Search the entry in the config-file
119
120        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
121        // I used value_uint_ instead of value_uchar_ to avoid number <-> char confusion in the config-file
122        if (!string2Number(this->value_.value_uint_, valueString, (unsigned int)defvalue)) // Try to convert the string to a value
123            this->setConfigFileEntyToDefault();                                 // The conversion failed
124    }
125
126    /**
127        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
128        @param value This is only needed to determine the right type.
129        @param classname The name of the class the variable belongs to
130        @param varname The name of the variable
131        @param defvalue The default-value
132    */
133    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, float defvalue)
134    {
135        this->classname_ = classname;
136        this->varname_ = varname;
137        this->type_ = Float;
138
139        this->defvalueString_ = number2String(defvalue, "0.000000");            // Try to convert the default-value to a string
140        this->searchConfigFileLine();                                           // Search the entry in the config-file
141
142        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
143        if (!string2Number(this->value_.value_float_, valueString, defvalue))   // Try to convert the string to a value
144            this->setConfigFileEntyToDefault();                                 // The conversion failed
145    }
146
147    /**
148        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
149        @param value This is only needed to determine the right type.
150        @param classname The name of the class the variable belongs to
151        @param varname The name of the variable
152        @param defvalue The default-value
153    */
154    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue)
155    {
156        this->classname_ = classname;
157        this->varname_ = varname;
158        this->type_ = Double;
159
160        this->defvalueString_ = number2String(defvalue, "0.000000");            // Try to convert the default-value to a string
161        this->searchConfigFileLine();                                           // Search the entry in the config-file
162
163        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
164        if (!string2Number(this->value_.value_double_, valueString, defvalue))  // Try to convert the string to a value
165            this->setConfigFileEntyToDefault();                                 // The conversion failed
166    }
167
168    /**
169        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
170        @param value This is only needed to determine the right type.
171        @param classname The name of the class the variable belongs to
172        @param varname The name of the variable
173        @param defvalue The default-value
174    */
175    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue)
176    {
177        this->classname_ = classname;
178        this->varname_ = varname;
179        this->type_ = Bool;
180
181        // Convert the default-value from bool to string
182        if (defvalue)
183            this->defvalueString_ = "true";
184        else
185            this->defvalueString_ = "false";
186
187        this->searchConfigFileLine();                                           // Search the entry in the config-file
188
189        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
190
191        // Try to parse the value-string - is it a word?
192        if (valueString.find("true") < valueString.size()
193         || valueString.find("True") < valueString.size()
194         || valueString.find("yes") < valueString.size()
195         || valueString.find("Yes") < valueString.size())
196            this->value_.value_bool_ = true;
197        else if (valueString.find("false") < valueString.size()
198              || valueString.find("False") < valueString.size()
199              || valueString.find("no") < valueString.size()
200              || valueString.find("No") < valueString.size())
201            this->value_.value_bool_ = false;
202        else
203        {
204            // Its not a known word - is it a number?
205            std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
206            if (!string2Number(this->value_.value_bool_, valueString, defvalue))    // Try to convert the string to a value
207                this->setConfigFileEntyToDefault();                                 // The conversion failed
208        }
209    }
210
211    /**
212        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
213        @param value This is only needed to determine the right type.
214        @param classname The name of the class the variable belongs to
215        @param varname The name of the variable
216        @param defvalue The default-value
217    */
218    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const std::string& defvalue)
219    {
220        this->classname_ = classname;
221        this->varname_ = varname;
222        this->type_ = String;
223
224        this->defvalueString_ = "\"" + defvalue + "\"";                         // Convert the string to a "config-file-string" with quotes
225
226        this->searchConfigFileLine();                                           // Search the entry in the config-file
227        std::string valueString = this->parseValueString(false);                // Parses the value string from the config-file-entry
228
229        // Strip the quotes
230        unsigned int pos1 = valueString.find("\"") + 1;
231        unsigned int pos2 = valueString.find("\"", pos1);
232
233        // Check if the entry was correctly quoted
234        if (pos1 < valueString.length() && pos2 < valueString.length() && !(valueString.find("\"", pos2 + 1) < valueString.length()))
235        {
236            // It was - get the string between the quotes
237            valueString = valueString.substr(pos1, pos2 - pos1);
238            this->value_string_ = valueString;
239        }
240        else
241        {
242            // It wasn't - use the default-value and restore the entry in the config-file.
243            this->value_string_ = defvalue;
244            this->setConfigFileEntyToDefault();
245        }
246    }
247
248    /**
249        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
250        @param value This is only needed to determine the right type.
251        @param classname The name of the class the variable belongs to
252        @param varname The name of the variable
253        @param defvalue The default-value
254    */
255    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue)
256    {
257        this->classname_ = classname;
258        this->varname_ = varname;
259        this->type_ = ConstChar;
260
261        this->defvalueString_ = "\"" + std::string(defvalue) + "\"";            // Convert the string to a "config-file-string" with quotes
262
263        this->searchConfigFileLine();                                           // Search the entry in the config-file
264        std::string valueString = this->parseValueString(false);                // Parses the value string from the config-file-entry
265
266        // Strip the quotes
267        unsigned int pos1 = valueString.find("\"") + 1;
268        unsigned int pos2 = valueString.find("\"", pos1);
269
270        // Check if the entry was correctly quoted
271        if (pos1 < valueString.length() && pos2 < valueString.length() && !(valueString.find("\"", pos2 + 1) < valueString.length()))
272        {
273            // It was - get the string between the quotes
274            valueString = valueString.substr(pos1, pos2 - pos1);
275            this->value_string_ = valueString;
276        }
277        else
278        {
279            // It wasn't - use the default-value and restore the entry in the config-file.
280            this->value_string_ = defvalue;
281            this->setConfigFileEntyToDefault();
282        }
283    }
284
285    /**
286        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
287        @param value This is only needed to determine the right type.
288        @param classname The name of the class the variable belongs to
289        @param varname The name of the variable
290        @param defvalue The default-value
291    */
292    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector2 defvalue)
293    {
294        this->classname_ = classname;
295        this->varname_ = varname;
296        this->type_ = Vector2;
297
298        // Try to convert the default-value from Vector2 to string
299        std::ostringstream ostream;
300        if (ostream << "(" << defvalue.x << "," << defvalue.y << ")")
301            this->defvalueString_ = ostream.str();
302        else
303            this->defvalueString_ = "(0,0)";
304
305        this->searchConfigFileLine();                                           // Search the entry in the config-file
306        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
307
308        // Strip the value-string
309        unsigned int pos1 = valueString.find("(") + 1;
310        unsigned int pos2 = valueString.find(")", pos1);
311
312        // Try to convert the stripped value-string to Vector2
313        if (pos1 < valueString.length() && pos2 < valueString.length() && pos1 < pos2)
314        {
315            valueString = valueString.substr(pos1, pos2 - pos1);
316            std::vector<std::string> tokens = tokenize(valueString, ",");
317            if (!string2Number(this->value_vector2_.x, tokens[0], defvalue.x))
318                this->setConfigFileEntyToDefault();
319            if (!string2Number(this->value_vector2_.y, tokens[1], defvalue.y))
320                this->setConfigFileEntyToDefault();
321        }
322        else
323        {
324            this->value_vector2_ = defvalue;
325            this->setConfigFileEntyToDefault();
326        }
327    }
328
329    /**
330        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
331        @param value This is only needed to determine the right type.
332        @param classname The name of the class the variable belongs to
333        @param varname The name of the variable
334        @param defvalue The default-value
335    */
336    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector3 defvalue)
337    {
338        this->classname_ = classname;
339        this->varname_ = varname;
340        this->type_ = Vector3;
341
342        // Try to convert the default-value from Vector3 to string
343        std::ostringstream ostream;
344        if (ostream << "(" << defvalue.x << "," << defvalue.y << "," << defvalue.z << ")")
345            this->defvalueString_ = ostream.str();
346        else
347            this->defvalueString_ = "(0,0,0)";
348
349        this->searchConfigFileLine();                                           // Search the entry in the config-file
350        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
351
352        // Strip the value-string
353        unsigned int pos1 = valueString.find("(") + 1;
354        unsigned int pos2 = valueString.find(")", pos1);
355
356        // Try to convert the stripped value-string to Vector3
357        if (pos1 < valueString.length() && pos2 < valueString.length() && pos1 < pos2)
358        {
359            valueString = valueString.substr(pos1, pos2 - pos1);
360            std::vector<std::string> tokens = tokenize(valueString, ",");
361            if (!string2Number(this->value_vector3_.x, tokens[0], defvalue.x))
362                this->setConfigFileEntyToDefault();
363            if (!string2Number(this->value_vector3_.y, tokens[1], defvalue.y))
364                this->setConfigFileEntyToDefault();
365            if (!string2Number(this->value_vector3_.z, tokens[2], defvalue.z))
366                this->setConfigFileEntyToDefault();
367        }
368        else
369        {
370            this->value_vector3_ = defvalue;
371            this->setConfigFileEntyToDefault();
372        }
373    }
374
375    /**
376        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
377        @param value This is only needed to determine the right type.
378        @param classname The name of the class the variable belongs to
379        @param varname The name of the variable
380        @param defvalue The default-value
381    */
382    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue)
383    {
384        this->classname_ = classname;
385        this->varname_ = varname;
386        this->type_ = ColourValue;
387
388        // Try to convert the default-value from ColourValue to string
389        std::ostringstream ostream;
390        if (ostream << "(" << defvalue.r << "," << defvalue.g << "," << defvalue.b << "," << defvalue.a << ")")
391            this->defvalueString_ = ostream.str();
392        else
393            this->defvalueString_ = "(0,0,0,0)";
394
395        this->searchConfigFileLine();                                           // Search the entry in the config-file
396        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
397
398        // Strip the value-string
399        unsigned int pos1 = valueString.find("(") + 1;
400        unsigned int pos2 = valueString.find(")", pos1);
401
402        // Try to convert the stripped value-string to Vector3
403        if (pos1 < valueString.length() && pos2 < valueString.length() && pos1 < pos2)
404        {
405            valueString = valueString.substr(pos1, pos2 - pos1);
406            std::vector<std::string> tokens = tokenize(valueString, ",");
407            if (!string2Number(this->value_colourvalue_.r, tokens[0], defvalue.r))
408                this->setConfigFileEntyToDefault();
409            if (!string2Number(this->value_colourvalue_.g, tokens[1], defvalue.g))
410                this->setConfigFileEntyToDefault();
411            if (!string2Number(this->value_colourvalue_.b, tokens[2], defvalue.b))
412                this->setConfigFileEntyToDefault();
413            if (!string2Number(this->value_colourvalue_.a, tokens[3], defvalue.a))
414                this->setConfigFileEntyToDefault();
415        }
416        else
417        {
418            this->value_colourvalue_ = defvalue;
419            this->setConfigFileEntyToDefault();
420        }
421    }
422
423    /**
424        @brief Sets the corresponding enty in the config-file back to the default value.
425    */
426    void ConfigValueContainer::setConfigFileEntyToDefault()
427    {
428        (*this->configFileLine_) = this->varname_ + "=" + this->defvalueString_;
429        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
430    }
431
432
433    /**
434        @brief Searches the corresponding entry in the config-file and creates it, if there is no entry.
435    */
436    void ConfigValueContainer::searchConfigFileLine()
437    {
438        // Read the file if needed
439        if (!ConfigValueContainer::readConfigFile_s)
440            ConfigValueContainer::readConfigFile(CONFIGFILEPATH);
441
442        // The string of the section we're searching
443        std::string section = "";
444        section.append("[");
445        section.append(this->classname_);
446        section.append("]");
447
448        // Iterate through all config-file-lines
449        bool success = false;
450        std::list<std::string>::iterator it1;
451        for(it1 = ConfigValueContainer::configFileLines_s->begin(); it1 != ConfigValueContainer::configFileLines_s->end(); ++it1)
452        {
453            // Don't try to parse comments
454            if (this->isComment(*it1))
455                continue;
456
457            if ((*it1).find(section) < (*it1).length())
458            {
459                // We found the right section
460                bool bLineIsEmpty = false;
461                std::list<std::string>::iterator positionToPutNewLineAt;
462
463                // Iterate through all lines in the section
464                std::list<std::string>::iterator it2;
465                for(it2 = ++it1; it2 != ConfigValueContainer::configFileLines_s->end(); ++it2)
466                {
467                    // Don't try to parse comments
468                    if (this->isComment(*it2))
469                        continue;
470
471                    // This if-else block is used to write a new line right after the last line of the
472                    // section but in front of the following empty lines before the next section.
473                    // (So this helps to keep a nice formatting with empty-lines between sections in the config-file)
474                    if (this->isEmpty(*it2))
475                    {
476                        if (!bLineIsEmpty)
477                        {
478                            bLineIsEmpty = true;
479                            positionToPutNewLineAt = it2;
480                        }
481                    }
482                    else
483                    {
484                        if (!bLineIsEmpty)
485                            positionToPutNewLineAt = it2;
486
487                        bLineIsEmpty = false;
488                    }
489
490                    // Look out for the beginning of the next section
491                    unsigned int open = (*it2).find("[");
492                    unsigned int close = (*it2).find("]");
493                    if ((open < (*it2).length()) && (close < (*it2).length()) && (open < close))
494                    {
495                        // The next section startet, so our line isn't yet in the file - now we add it and safe the file
496                        this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
497                        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
498                        success = true;
499                        break;
500                    }
501
502                    // Look out for the variable-name
503                    if ((*it2).find(this->varname_) < (*it2).length())
504                    {
505                        // We found the right line - safe it and return
506                        this->configFileLine_ = it2;
507                        success = true;
508                        break;
509                    }
510                }
511
512                // Check if we succeeded
513                if (!success)
514                {
515                    // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file
516                    this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
517                    ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
518                    success = true;
519                }
520                break;
521            }
522        }
523
524        // Check if we succeeded
525        if (!success)
526        {
527            // We obviously didn't found the right section, so we'll create it
528            this->configFileLines_s->push_back("[" + this->classname_ + "]");                   // Create the section
529            this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalueString_);   // Create the line
530            this->configFileLine_ = --this->configFileLines_s->end();                           // Set the pointer to the last element
531            success = true;
532            this->configFileLines_s->push_back("");                                             // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
533            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);                              // Save the changed config-file
534        }
535    }
536
537    /**
538        @brief Determines if a line in the config-file is a comment.
539        @param line The line to check
540        @return True = it's a comment
541    */
542    bool ConfigValueContainer::isComment(const std::string& line)
543    {
544        // Strip the line, whitespaces are disturbing
545        std::string teststring = getStrippedLine(line);
546
547        // There are four possible comment-symbols:
548        //  1) #comment in script-language style
549        //  2) %comment in matlab style
550        //  3) ;comment in unreal tournament config-file style
551        //  4) //comment in code style
552        if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/'))
553            return true;
554
555        return false;
556    }
557
558    /**
559        @brief Determines if a line in the config-file is empty (contains only whitespaces).
560        @param line The line to check
561        @return True = it's empty
562    */
563    bool ConfigValueContainer::isEmpty(const std::string& line)
564    {
565        return getStrippedLine(line) == "";
566    }
567
568    /**
569        @brief Removes all whitespaces from a line.
570        @param line The line to strip
571        @return The stripped line
572    */
573    std::string ConfigValueContainer::getStrippedLine(const std::string& line)
574    {
575        std::string output = line;
576        unsigned int pos;
577        while ((pos = output.find(" ")) < output.length())
578            output.erase(pos, 1);
579        while ((pos = output.find("\t")) < output.length())
580            output.erase(pos, 1);
581
582        return output;
583    }
584
585    /**
586        @brief Returns the part in the corresponding config-file-entry of the container that defines the value.
587        @param bStripped True = strip the value-string
588        @return The value-string
589    */
590    std::string ConfigValueContainer::parseValueString(bool bStripped)
591    {
592        std::string output;
593        if (bStripped)
594            output = this->getStrippedLine(*this->configFileLine_);
595        else
596            output = *this->configFileLine_;
597
598        return output.substr(output.find("=") + 1);
599    }
600
601    /**
602        @brief Reads the config-file and stores the lines in a list.
603        @param filename The name of the config-file
604    */
605    void ConfigValueContainer::readConfigFile(const std::string& filename)
606    {
607        ConfigValueContainer::readConfigFile_s = true;
608
609        // Create the list if needed
610        if (!ConfigValueContainer::configFileLines_s)
611            ConfigValueContainer::configFileLines_s = new std::list<std::string>;
612
613        // This creates the file if it's not existing
614        std::ofstream createFile;
615        createFile.open(filename.c_str(), std::fstream::app);
616        createFile.close();
617
618        // Open the file
619        std::ifstream file;
620        file.open(filename.c_str(), std::fstream::in);
621
622        char line[1024];
623
624        // Iterate through the file and add the lines into the list
625        while (file.good() && !file.eof())
626        {
627            file.getline(line, 1024);
628            ConfigValueContainer::configFileLines_s->push_back(line);
629//            std::cout << "### ->" << line << "<- : empty: " << isEmpty(line) << " comment: " << isComment(line) << std::endl;
630        }
631
632        // The last line is useless
633        ConfigValueContainer::configFileLines_s->pop_back();
634
635        // Add an empty line to the end of the file if needed
636        // this is needed for the algorithm in the searchConfigFileLine-function
637        if ((ConfigValueContainer::configFileLines_s->size() > 0) && !isEmpty(*ConfigValueContainer::configFileLines_s->rbegin()))
638        {
639//            std::cout << "### newline added" << std::endl;
640            ConfigValueContainer::configFileLines_s->push_back("");
641        }
642
643        file.close();
644    }
645
646    /**
647     *  @param Writes the content of the list, containing all lines of the config-file, into the config-file.
648     *  @param filename The name of the config-file
649     */
650    void ConfigValueContainer::writeConfigFile(const std::string& filename)
651    {
652        // Make sure we stored the config-file in the list
653        if (!ConfigValueContainer::readConfigFile_s)
654            ConfigValueContainer::readConfigFile(filename);
655
656        // Open the file
657        std::ofstream file;
658        file.open(filename.c_str(), std::fstream::out);
659
660        // Iterate through the list an write the lines into the file
661        std::list<std::string>::iterator it;
662        for(it = ConfigValueContainer::configFileLines_s->begin(); it != ConfigValueContainer::configFileLines_s->end(); ++it)
663        {
664            file << (*it) << std::endl;
665        }
666
667        file.close();
668    }
669}
Note: See TracBrowser for help on using the repository browser.