Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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