Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 9, 2007, 11:21:14 PM (17 years ago)
Author:
landauf
Message:
  • added comments and doxygen-tags to the ConfigValueContainer
  • changed some comments in the other files
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/core/ConfigValueContainer.cc

    r435 r447  
    88namespace orxonox
    99{
    10     std::list<std::string>* ConfigValueContainer::configFileLines_s = 0;
    11     bool ConfigValueContainer::readConfigFile_s = false;
    12 
     10    std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; // Set the static member variable configFileLines_s to zero
     11    bool ConfigValueContainer::readConfigFile_s = false;                 // Set the static member variable readConfigFile_s to false
     12
     13    /**
     14        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_int_.
     15        @param classname The name of the class the variable belongs to
     16        @param varname The name of the variable
     17        @param defvalue The default-value
     18    */
    1319    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue)
    1420    {
     21        // Try to convert the default-value from int to string
    1522        std::ostringstream ostream;
    16 
    1723        if (ostream << defvalue)
    1824            this->defvalue_ = ostream.str();
     
    2026            this->defvalue_ = "0";
    2127
     28        // Set the default values, then get the value-string
    2229        this->setDefaultValues(classname, varname);
    2330        std::string valueString = this->getValueString();
    2431
     32        // Try to convert the value-string to int
    2533        std::istringstream istream(valueString);
    2634        if (!(istream >> this->value_int_))
    2735        {
     36            // The conversion failed - use the default value and restore the entry in the config-file
    2837            this->value_int_ = defvalue;
    2938            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    3241    }
    3342
     43    /**
     44        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_double_.
     45        @param classname The name of the class the variable belongs to
     46        @param varname The name of the variable
     47        @param defvalue The default-value
     48    */
    3449    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue)
    3550    {
     51        // Try to convert the default-value from double to string
    3652        std::ostringstream ostream;
    37 
    3853        if (ostream << defvalue)
    3954            this->defvalue_ = ostream.str();
     
    4156            this->defvalue_ = "0.000000";
    4257
     58        // Set the default values, then get the value-string
    4359        this->setDefaultValues(classname, varname);
    4460        std::string valueString = this->getValueString();
    4561
     62        // Try to convert the value-string to double
    4663        std::istringstream istream(valueString);
    4764        if (!(istream >> this->value_double_))
    4865        {
     66            // The conversion failed - use the default value and restore the entry in the config-file
    4967            this->value_double_ = defvalue;
    5068            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    5371    }
    5472
     73    /**
     74        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_bool_.
     75        @param classname The name of the class the variable belongs to
     76        @param varname The name of the variable
     77        @param defvalue The default-value
     78    */
    5579    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue)
    5680    {
     81        // Convert the default-value from bool to string
    5782        if (defvalue)
    5883            this->defvalue_ = "true";
     
    6085            this->defvalue_ = "false";
    6186
     87        // Set the default values, then get the value-string
    6288        this->setDefaultValues(classname, varname);
    6389        std::string valueString = this->getValueString();
    6490
     91        // Try to parse the value-string - is it a word?
    6592        if (valueString.find("true") < valueString.size() || valueString.find("yes") < valueString.size())
    6693            this->value_bool_ = true;
     
    6996        else
    7097        {
     98            // Its not a known word - is it a number?
    7199            std::istringstream istream(valueString);
    72100            if (!(istream >> this->value_bool_))
    73101            {
     102                // The conversion failed - use the default value and restore the entry in the config-file
    74103                this->value_bool_ = defvalue;
    75104                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    79108    }
    80109
     110    /**
     111        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_string_.
     112        @param classname The name of the class the variable belongs to
     113        @param varname The name of the variable
     114        @param defvalue The default-value
     115    */
    81116    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue)
    82117    {
     118        // Not much to do here - just set all member-variables and check the config-file
    83119        this->defvalue_ = defvalue;
    84120        this->setDefaultValues(classname, varname);
     
    86122    }
    87123
     124    /**
     125        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_vector3_.
     126        @param classname The name of the class the variable belongs to
     127        @param varname The name of the variable
     128        @param defvalue The default-value
     129    */
    88130    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector3 defvalue)
    89131    {
     132        // Try to convert the default-value from Vector3 to string
    90133        std::ostringstream ostream;
    91134        if (ostream << "(" << defvalue.x << "," << defvalue.y << "," << defvalue.z << ")")
     
    94137            this->defvalue_ = "(0,0,0)";
    95138
     139        // Set the default values, then get the value-string
    96140        this->setDefaultValues(classname, varname);
    97141        std::string valueString = this->getValueString();
    98142
     143        // Strip the value-string
     144        valueString = this->getStrippedLine(valueString);
    99145        unsigned int pos;
    100         while ((pos = valueString.find(" ")) < valueString.length())
    101             valueString.erase(pos, 1);
    102146        while ((pos = valueString.find("(")) < valueString.length())
    103147            valueString.erase(pos, 1);
     
    107151            valueString.replace(pos, 1, " ");
    108152
     153        // Try to convert the stripped value-string to Vector3
    109154        std::istringstream istream(valueString);
    110 
    111155        if (!(istream >> this->value_vector3_.x))
    112156        {
     157            // The conversion failed - use the default value and restore the entry in the config-file
    113158            this->value_vector3_.x = defvalue.x;
    114159            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    117162        if (!(istream >> this->value_vector3_.y))
    118163        {
     164            // The conversion failed - use the default value and restore the entry in the config-file
    119165            this->value_vector3_.y = defvalue.y;
    120166            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    123169        if (!(istream >> this->value_vector3_.z))
    124170        {
     171            // The conversion failed - use the default value and restore the entry in the config-file
    125172            this->value_vector3_.z = defvalue.z;
    126173            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    129176    }
    130177
     178    /**
     179        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_colourvalue_.
     180        @param classname The name of the class the variable belongs to
     181        @param varname The name of the variable
     182        @param defvalue The default-value
     183    */
    131184    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue)
    132185    {
     186        // Try to convert the default-value from ColourValue to string
    133187        std::ostringstream ostream;
    134188        if (ostream << "(" << defvalue.r << "," << defvalue.g << "," << defvalue.b << "," << defvalue.a << ")")
     
    137191            this->defvalue_ = "(0,0,0,0)";
    138192
     193        // Set the default values, then get the value-string
    139194        this->setDefaultValues(classname, varname);
    140195        std::string valueString = this->getValueString();
    141196
     197        // Strip the value-string
     198        valueString = this->getStrippedLine(valueString);
    142199        unsigned int pos;
    143         while ((pos = valueString.find(" ")) < valueString.length())
    144             valueString.erase(pos, 1);
    145200        while ((pos = valueString.find("(")) < valueString.length())
    146201            valueString.erase(pos, 1);
     
    150205            valueString.replace(pos, 1, " ");
    151206
     207        // Try to convert the stripped value-string to Vector3
    152208        std::istringstream istream(valueString);
    153 
    154209        if (!(istream >> this->value_colourvalue_.r))
    155210        {
     211            // The conversion failed - use the default value and restore the entry in the config-file
    156212            this->value_colourvalue_.r = defvalue.r;
    157213            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    160216        if (!(istream >> this->value_colourvalue_.g))
    161217        {
     218            // The conversion failed - use the default value and restore the entry in the config-file
    162219            this->value_colourvalue_.g = defvalue.g;
    163220            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    166223        if (!(istream >> this->value_colourvalue_.b))
    167224        {
     225            // The conversion failed - use the default value and restore the entry in the config-file
    168226            this->value_colourvalue_.b = defvalue.b;
    169227            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    172230        if (!(istream >> this->value_colourvalue_.a))
    173231        {
     232            // The conversion failed - use the default value and restore the entry in the config-file
    174233            this->value_colourvalue_.a = defvalue.a;
    175234            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     
    178237    }
    179238
     239    /**
     240        @brief Sets the default values of the container and searches the coresponding entry in the config-file.
     241        @param classname The name of the class the variable belongs to
     242        @param varname The name of the variable
     243    */
    180244    void ConfigValueContainer::setDefaultValues(const std::string& classname, const std::string& varname)
    181245    {
     246        // Set the class and variable names
    182247        this->classname_ = classname;
    183248        this->varname_ = varname;
    184249
     250        // Search the entry in the config-file
    185251        this->searchConfigFileLine();
    186252
     253        // Set the values of all types to zero
    187254        this->value_int_ = 0;
    188255        this->value_double_ = 0.000000;
     
    193260    }
    194261
     262    /**
     263        @brief Searches the corresponding entry in the config-file and creates it, if there is no entry.
     264    */
    195265    void ConfigValueContainer::searchConfigFileLine()
    196266    {
     267        // Read the file if needed
    197268        if (!ConfigValueContainer::readConfigFile_s)
    198269            ConfigValueContainer::readConfigFile(CONFIGFILEPATH);
    199270
     271        // Just in case something goes wrong
    200272        this->configFileLine_ = 0;
    201273
     274        // The string of the section we're searching
    202275        std::string section = "";
    203276        section.append("[");
     
    205278        section.append("]");
    206279
     280        // Iterate through all config-file-lines
    207281        bool success = false;
    208282        std::list<std::string>::iterator it1;
    209283        for(it1 = ConfigValueContainer::configFileLines_s->begin(); it1 != ConfigValueContainer::configFileLines_s->end(); ++it1)
    210284        {
     285            // Don't try to parse comments
    211286            if (this->isComment(*it1))
    212287                continue;
     
    214289            if ((*it1).find(section) < (*it1).length())
    215290            {
     291                // We found the right section
    216292                bool bLineIsEmpty = false;
    217293                std::list<std::string>::iterator positionToPutNewLineAt;
    218294
     295                // Iterate through all lines in the section
    219296                std::list<std::string>::iterator it2;
    220297                for(it2 = ++it1; it2 != ConfigValueContainer::configFileLines_s->end(); ++it2)
    221298                {
     299                    // Don't try to parse comments
    222300                    if (this->isComment(*it2))
    223301                        continue;
    224302
     303                    // This if-else block is used to write a new line right after the last line of the
     304                    // section but in front of the following empty lines before the next section.
     305                    // (So this helps to keep a nice formatting with empty-lines between sections in the config-file)
    225306                    if (this->isEmpty(*it2))
    226307                    {
     
    239320                    }
    240321
     322                    // Look out for the beginning of the next section
    241323                    unsigned int open = (*it2).find("[");
    242324                    unsigned int close = (*it2).find("]");
    243325                    if ((open < (*it2).length()) && (close < (*it2).length()) && (open < close))
    244326                    {
     327                        // The next section startet, so our line isn't yet in the file - now we add it and safe the file
    245328                        this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_);
    246329                        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     
    249332                    }
    250333
     334                    // Look out for the variable-name
    251335                    if ((*it2).find(this->varname_) < (*it2).length())
    252336                    {
     337                        // We found the right line - safe it and return
    253338                        this->configFileLine_ = it2;
    254339                        success = true;
     
    256341                    }
    257342                }
     343
     344                // Check if we succeeded
    258345                if (!success)
    259346                {
     347                    // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file
    260348                    this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_);
    261349                    ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     
    265353            }
    266354        }
     355
     356        // Check if we succeeded
    267357        if (!success)
    268358        {
    269             this->configFileLines_s->push_back("[" + this->classname_ + "]");
    270             this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalue_);
    271             this->configFileLine_ = --this->configFileLines_s->end();
     359            // We obviously didn't found the right section, so we'll create it
     360            this->configFileLines_s->push_back("[" + this->classname_ + "]");           // Create the section
     361            this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalue_); // Create the line
     362            this->configFileLine_ = --this->configFileLines_s->end();                   // Set the pointer to the last element
    272363            success = true;
    273             this->configFileLines_s->push_back("");
    274             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    275         }
    276     }
    277 
     364            this->configFileLines_s->push_back("");                                     // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
     365            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);                      // Save the changed config-file
     366        }
     367    }
     368
     369    /**
     370        @brief Determines if a line in the config-file is a comment.
     371        @param line The line to check
     372        @return True = it's a comment
     373    */
    278374    bool ConfigValueContainer::isComment(const std::string& line)
    279375    {
     376        // Strip the line, whitespaces are disturbing
    280377        std::string teststring = getStrippedLine(line);
    281378
     379        // There are four possible comment-symbols:
     380        //  1) #comment in script-language style
     381        //  2) %comment in matlab style
     382        //  3) ;comment in unreal tournament config-file style
     383        //  4) //comment in code style
    282384        if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/'))
    283385            return true;
     
    286388    }
    287389
     390    /**
     391        @brief Determines if a line in the config-file is empty (contains only whitespaces).
     392        @param line The line to check
     393        @return True = it's empty
     394    */
    288395    bool ConfigValueContainer::isEmpty(const std::string& line)
    289396    {
     
    291398    }
    292399
     400    /**
     401        @brief Removes all whitespaces from a line.
     402        @param line The line to strip
     403        @return The stripped line
     404    */
    293405    std::string ConfigValueContainer::getStrippedLine(const std::string& line)
    294406    {
     
    301413    }
    302414
     415    /**
     416        @brief Returns the part in the corresponding config-file-entry of the container that defines the value.
     417        @param bStripped True = strip the value-string
     418        @return The value-string
     419    */
    303420    std::string ConfigValueContainer::getValueString(bool bStripped)
    304421    {
     
    312429    }
    313430
     431    /**
     432        @brief Reads the config-file and stores the lines in a list.
     433        @param filename The name of the config-file
     434    */
    314435    void ConfigValueContainer::readConfigFile(const std::string& filename)
    315436    {
    316437        ConfigValueContainer::readConfigFile_s = true;
    317438
     439        // Create the list if needed
    318440        if (!ConfigValueContainer::configFileLines_s)
    319441            ConfigValueContainer::configFileLines_s = new std::list<std::string>;
    320442
     443        // This creates the file if it's not existing
    321444        std::ofstream createFile;
    322445        createFile.open(filename.c_str(), std::fstream::app);
    323446        createFile.close();
    324447
     448        // Open the file
    325449        std::ifstream file;
    326450        file.open(filename.c_str(), std::fstream::in);
     
    328452        char line[1024];
    329453
     454        // Iterate through the file and add the lines into the list
    330455        while (file.good() && !file.eof())
    331456        {
     
    335460        }
    336461
     462        // The last line is useless
    337463        ConfigValueContainer::configFileLines_s->pop_back();
    338464
     465        // Add an empty line to the end of the file if needed
     466        // this is needed for the algorithm in the searchConfigFileLine-function
    339467        if ((ConfigValueContainer::configFileLines_s->size() > 0) && !isEmpty(*ConfigValueContainer::configFileLines_s->rbegin()))
    340468        {
     
    346474    }
    347475
     476    /**
     477        @param Writes the content of the list, containing all lines of the config-file, into the config-file.
     478        @param filename The name of the config-file
     479    */
    348480    void ConfigValueContainer::writeConfigFile(const std::string& filename)
    349481    {
    350         ConfigValueContainer::readConfigFile_s = true;
    351 
    352         if (!ConfigValueContainer::configFileLines_s)
    353             ConfigValueContainer::configFileLines_s = new std::list<std::string>;
    354 
    355         std::ofstream createFile;
    356         createFile.open(filename.c_str(), std::fstream::app);
    357         createFile.close();
    358 
     482        // Make sure we stored the config-file in the list
     483        if (!ConfigValueContainer::readConfigFile_s)
     484            ConfigValueContainer::readConfigFile(filename);
     485
     486        // Open the file
    359487        std::ofstream file;
    360488        file.open(filename.c_str(), std::fstream::out);
    361489
     490        // Iterate through the list an write the lines into the file
    362491        std::list<std::string>::iterator it;
    363492        for(it = ConfigValueContainer::configFileLines_s->begin(); it != ConfigValueContainer::configFileLines_s->end(); ++it)
Note: See TracChangeset for help on using the changeset viewer.