Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 667


Ignore:
Timestamp:
Dec 22, 2007, 2:13:18 AM (16 years ago)
Author:
landauf
Message:
  • expanded the String2Number.h file
  • changed the SetConfigValue macro
  • changed the S2N and N2S conversion in the ConfigValueContainer
  • added unsigned int, char, unsigned char, float (additionally to double) and const char* (additionally to std::string) to the ConfigValueContainer
Location:
code/branches/FICN/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/misc/String2Number.h

    r659 r667  
    4949};
    5050
     51
     52/**
     53    @brief Converts a number of any type to a string.
     54    @param variable The variable, containing the number.
     55
     56    @example
     57    float f = 3.14;
     58    std::string output = number2String(f);
     59*/
     60template <typename T>
     61const std::string& number2String(T variable)
     62{
     63    std::ostringstream oss;
     64    if (oss << variable)
     65        return *(new std::string(oss.str()));
     66
     67    return *(new std::string(""));
     68}
     69
     70/**
     71    @brief Converts a number of any type to a string and assigns a defaultvalue if the conversion fails.
     72    @param variable The variable, containing the number.
     73    @param fallbackString The assigned string if the conversion fails.
     74
     75    @example
     76    float f = 3.14;
     77    std::string output = number2String(f, "0.000000");
     78*/
     79template <typename T>
     80const std::string& number2String(T variable, const std::string& fallbackString)
     81{
     82    std::cout << "N2Sa: " << variable << std::endl;
     83
     84    std::ostringstream oss;
     85    if (oss << variable)
     86    {
     87    std::cout << "N2Sb: " << oss.str() << std::endl;
     88        return *(new std::string(oss.str()));
     89    }
     90
     91    return fallbackString;
     92}
     93
     94/**
     95    @brief Converts a string to a number of any type.
     96    @param variable The variable wherein the value gets written
     97    @return True if the conversion was successful
     98
     99    @example
     100    float f;
     101    bool success = string2Number(f, "3.14");
     102*/
     103template <typename T>
     104bool string2Number(T& variable, const std::string& valueString)
     105{
     106    std::istringstream iss(valueString);
     107    if (iss >> variable)
     108        return true;
     109
     110    return false;
     111}
     112
     113/**
     114    @brief Converts a string to a number of any type.
     115    @param variable The variable wherein the value gets written
     116    @param fallbackValue The assigned value if the conversion fails
     117    @return True if the conversion was successful
     118
     119    @example
     120    float f;
     121    bool success = string2Number(f, "3.14", 0.000000);
     122*/
     123template <typename T>
     124bool string2Number(T& variable, const std::string& valueString, T fallbackValue)
     125{
     126    std::cout << "S2Na: " << valueString << std::endl;
     127    std::istringstream iss(valueString);
     128    if (iss >> variable)
     129    {
     130    std::cout << "S2Nb: " << variable << std::endl;
     131        return true;
     132    }
     133
     134    variable = fallbackValue;
     135    return false;
     136}
     137
    51138#endif
  • code/branches/FICN/src/orxonox/Orxonox.cc

    r665 r667  
    198198    ogre_->startRender();
    199199    //TODO: run engine
     200    Factory::createClassHierarchy();
    200201    createScene();
    201202    setupScene();
     
    206207      std::cout << "client here" << std::endl;
    207208    createFrameListener();
    208     Factory::createClassHierarchy();
    209209    switch(mode_){
    210210    case PRESENTATION:
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.cc

    r663 r667  
    2727
    2828#include <fstream>
    29 // #include <string>
    3029#include "ConfigValueContainer.h"
    31 
    32 //#define CONFIGFILEPATH "O:\\oh\\bin\\orxonox.ini"
     30#include "../../misc/Tokenizer.h"
     31#include "../../misc/String2Number.h"
     32
    3333#define CONFIGFILEPATH "orxonox.ini"
    3434
     
    3939
    4040    /**
    41         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_.value_int_.
     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.
    4243        @param classname The name of the class the variable belongs to
    4344        @param varname The name of the variable
     
    4647    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue)
    4748    {
    48         // Try to convert the default-value from int to string
    49         std::ostringstream ostream;
    50         if (ostream << defvalue)
    51             this->defvalue_ = ostream.str();
    52         else
    53             this->defvalue_ = "0";
    54 
    55         // Set the default values, then get the value-string
    56         this->setDefaultValues(classname, varname);
    57         std::string valueString = this->getValueString();
    58 
    59         // Try to convert the value-string to int
    60         std::istringstream istream(valueString);
    61         if (!(istream >> this->value_.value_int_))
    62         {
    63             // The conversion failed - use the default value and restore the entry in the config-file
    64             this->value_.value_int_ = defvalue;
    65             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    66             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    67         }
    68     }
    69 
    70     /**
    71         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_.value_double_.
     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        std::cout << "CVC: int: " << this->value_.value_int_ << std::endl;
     61    }
     62
     63    /**
     64        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     65        @param value This is only needed to determine the right type.
     66        @param classname The name of the class the variable belongs to
     67        @param varname The name of the variable
     68        @param defvalue The default-value
     69    */
     70    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned int defvalue)
     71    {
     72        this->classname_ = classname;
     73        this->varname_ = varname;
     74        this->type_ = uInt;
     75
     76        this->defvalueString_ = number2String(defvalue, "0");                   // Try to convert the default-value to a string
     77        this->searchConfigFileLine();                                           // Search the entry in the config-file
     78
     79        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
     80        if (!string2Number(this->value_.value_uint_, valueString, defvalue))    // Try to convert the string to a value
     81            this->setConfigFileEntyToDefault();                                 // The conversion failed
     82
     83        std::cout << "CVC: uint: " << this->value_.value_uint_ << std::endl;
     84    }
     85
     86    /**
     87        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     88        @param value This is only needed to determine the right type.
     89        @param classname The name of the class the variable belongs to
     90        @param varname The name of the variable
     91        @param defvalue The default-value
     92    */
     93    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, char defvalue)
     94    {
     95        this->classname_ = classname;
     96        this->varname_ = varname;
     97        this->type_ = Char;
     98
     99        this->defvalueString_ = number2String((int)defvalue, "0");              // Try to convert the default-value to a string
     100        this->searchConfigFileLine();                                           // Search the entry in the config-file
     101
     102        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
     103        // I used value_int_ instead of value_char_ to avoid number <-> char confusion in the config-file
     104        if (!string2Number(this->value_.value_int_, valueString, (int)defvalue))// Try to convert the string to a value
     105            this->setConfigFileEntyToDefault();                                 // The conversion failed
     106    }
     107
     108    /**
     109        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     110        @param value This is only needed to determine the right type.
     111        @param classname The name of the class the variable belongs to
     112        @param varname The name of the variable
     113        @param defvalue The default-value
     114    */
     115    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned char defvalue)
     116    {
     117        this->classname_ = classname;
     118        this->varname_ = varname;
     119        this->type_ = uChar;
     120
     121        this->defvalueString_ = number2String((unsigned int)defvalue, "0");     // Try to convert the default-value to a string
     122        this->searchConfigFileLine();                                           // Search the entry in the config-file
     123
     124        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
     125        // I used value_uint_ instead of value_uchar_ to avoid number <-> char confusion in the config-file
     126        if (!string2Number(this->value_.value_uint_, valueString, (unsigned int)defvalue)) // Try to convert the string to a value
     127            this->setConfigFileEntyToDefault();                                 // The conversion failed
     128    }
     129
     130    /**
     131        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     132        @param value This is only needed to determine the right type.
     133        @param classname The name of the class the variable belongs to
     134        @param varname The name of the variable
     135        @param defvalue The default-value
     136    */
     137    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, float defvalue)
     138    {
     139        this->classname_ = classname;
     140        this->varname_ = varname;
     141        this->type_ = Float;
     142
     143        this->defvalueString_ = number2String(defvalue, "0.000000");            // Try to convert the default-value to a string
     144        this->searchConfigFileLine();                                           // Search the entry in the config-file
     145
     146        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
     147        if (!string2Number(this->value_.value_float_, valueString, defvalue))   // Try to convert the string to a value
     148            this->setConfigFileEntyToDefault();                                 // The conversion failed
     149    }
     150
     151    /**
     152        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     153        @param value This is only needed to determine the right type.
    72154        @param classname The name of the class the variable belongs to
    73155        @param varname The name of the variable
     
    76158    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue)
    77159    {
    78         // Try to convert the default-value from double to string
    79         std::ostringstream ostream;
    80         if (ostream << defvalue)
    81             this->defvalue_ = ostream.str();
    82         else
    83             this->defvalue_ = "0.000000";
    84 
    85         // Set the default values, then get the value-string
    86         this->setDefaultValues(classname, varname);
    87         std::string valueString = this->getValueString();
    88 
    89         // Try to convert the value-string to double
    90         std::istringstream istream(valueString);
    91         if (!(istream >> this->value_.value_double_))
    92         {
    93             // The conversion failed - use the default value and restore the entry in the config-file
    94             this->value_.value_double_ = defvalue;
    95             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    96             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    97         }
    98     }
    99 
    100     /**
    101         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_.value_bool_.
     160        this->classname_ = classname;
     161        this->varname_ = varname;
     162        this->type_ = Double;
     163
     164        this->defvalueString_ = number2String(defvalue, "0.000000");            // Try to convert the default-value to a string
     165        this->searchConfigFileLine();                                           // Search the entry in the config-file
     166
     167        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
     168        if (!string2Number(this->value_.value_double_, valueString, defvalue))  // Try to convert the string to a value
     169            this->setConfigFileEntyToDefault();                                 // The conversion failed
     170    }
     171
     172    /**
     173        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     174        @param value This is only needed to determine the right type.
    102175        @param classname The name of the class the variable belongs to
    103176        @param varname The name of the variable
     
    106179    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue)
    107180    {
     181        this->classname_ = classname;
     182        this->varname_ = varname;
     183        this->type_ = Bool;
     184
    108185        // Convert the default-value from bool to string
    109186        if (defvalue)
    110             this->defvalue_ = "true";
    111         else
    112             this->defvalue_ = "false";
    113 
    114         // Set the default values, then get the value-string
    115         this->setDefaultValues(classname, varname);
    116         std::string valueString = this->getValueString();
     187            this->defvalueString_ = "true";
     188        else
     189            this->defvalueString_ = "false";
     190
     191        this->searchConfigFileLine();                                           // Search the entry in the config-file
     192
     193        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
    117194
    118195        // Try to parse the value-string - is it a word?
    119         if (valueString.find("true") < valueString.size() || valueString.find("yes") < valueString.size())
     196        if (valueString.find("true") < valueString.size()
     197         || valueString.find("True") < valueString.size()
     198         || valueString.find("yes") < valueString.size()
     199         || valueString.find("Yes") < valueString.size())
    120200            this->value_.value_bool_ = true;
    121         else if (valueString.find("false") < valueString.size() || valueString.find("no") < valueString.size())
     201        else if (valueString.find("false") < valueString.size()
     202              || valueString.find("False") < valueString.size()
     203              || valueString.find("no") < valueString.size()
     204              || valueString.find("No") < valueString.size())
    122205            this->value_.value_bool_ = false;
    123206        else
    124207        {
    125208            // Its not a known word - is it a number?
    126             std::istringstream istream(valueString);
    127             if (!(istream >> this->value_.value_bool_))
    128             {
    129                 // The conversion failed - use the default value and restore the entry in the config-file
    130                 this->value_.value_bool_ = defvalue;
    131                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    132                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    133             }
    134         }
    135     }
    136 
    137     /**
    138         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_string_.
    139         @param classname The name of the class the variable belongs to
    140         @param varname The name of the variable
    141         @param defvalue The default-value
    142     */
    143     ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue)
    144     {
    145         // Convert the string to a "config-file-string" with quotes
    146         this->defvalue_ = "\"" + std::string(defvalue) + "\"";
    147 
    148         // Set the default-values, then get the value-string
    149         this->setDefaultValues(classname, varname);
    150         std::string valueString = this->getValueString(false);
     209            std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
     210            if (!string2Number(this->value_.value_bool_, valueString, defvalue))    // Try to convert the string to a value
     211                this->setConfigFileEntyToDefault();                                 // The conversion failed
     212        }
     213    }
     214
     215    /**
     216        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     217        @param value This is only needed to determine the right type.
     218        @param classname The name of the class the variable belongs to
     219        @param varname The name of the variable
     220        @param defvalue The default-value
     221    */
     222    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const std::string& defvalue)
     223    {
     224        this->classname_ = classname;
     225        this->varname_ = varname;
     226        this->type_ = String;
     227
     228        this->defvalueString_ = "\"" + defvalue + "\"";                         // Convert the string to a "config-file-string" with quotes
     229
     230        this->searchConfigFileLine();                                           // Search the entry in the config-file
     231        std::string valueString = this->parseValueString(false);                // Parses the value string from the config-file-entry
    151232
    152233        // Strip the quotes
     
    165246            // It wasn't - use the default-value and restore the entry in the config-file.
    166247            this->value_string_ = defvalue;
    167             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    168             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    169         }
    170     }
    171 
    172     /**
    173         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_vector2_.
     248            this->setConfigFileEntyToDefault();
     249        }
     250    }
     251
     252    /**
     253        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     254        @param value This is only needed to determine the right type.
     255        @param classname The name of the class the variable belongs to
     256        @param varname The name of the variable
     257        @param defvalue The default-value
     258    */
     259    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue)
     260    {
     261        this->classname_ = classname;
     262        this->varname_ = varname;
     263        this->type_ = ConstChar;
     264
     265        this->defvalueString_ = "\"" + std::string(defvalue) + "\"";            // Convert the string to a "config-file-string" with quotes
     266
     267        this->searchConfigFileLine();                                           // Search the entry in the config-file
     268        std::string valueString = this->parseValueString(false);                // Parses the value string from the config-file-entry
     269
     270        // Strip the quotes
     271        unsigned int pos1 = valueString.find("\"") + 1;
     272        unsigned int pos2 = valueString.find("\"", pos1);
     273
     274        // Check if the entry was correctly quoted
     275        if (pos1 < valueString.length() && pos2 < valueString.length() && !(valueString.find("\"", pos2 + 1) < valueString.length()))
     276        {
     277            // It was - get the string between the quotes
     278            valueString = valueString.substr(pos1, pos2 - pos1);
     279            this->value_string_ = valueString;
     280        }
     281        else
     282        {
     283            // It wasn't - use the default-value and restore the entry in the config-file.
     284            this->value_string_ = defvalue;
     285            this->setConfigFileEntyToDefault();
     286        }
     287    }
     288
     289    /**
     290        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     291        @param value This is only needed to determine the right type.
    174292        @param classname The name of the class the variable belongs to
    175293        @param varname The name of the variable
     
    178296    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector2 defvalue)
    179297    {
     298        this->classname_ = classname;
     299        this->varname_ = varname;
     300        this->type_ = Vector2;
     301
    180302        // Try to convert the default-value from Vector2 to string
    181303        std::ostringstream ostream;
    182304        if (ostream << "(" << defvalue.x << "," << defvalue.y << ")")
    183             this->defvalue_ = ostream.str();
    184         else
    185             this->defvalue_ = "(0,0)";
    186 
    187         // Set the default values, then get the value-string
    188         this->setDefaultValues(classname, varname);
    189         std::string valueString = this->getValueString();
     305            this->defvalueString_ = ostream.str();
     306        else
     307            this->defvalueString_ = "(0,0)";
     308
     309        this->searchConfigFileLine();                                           // Search the entry in the config-file
     310        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
    190311
    191312        // Strip the value-string
    192         bool bEntryIsCorrupt = false;
    193         valueString = this->getStrippedLine(valueString);
    194         unsigned int pos1, pos2, pos3;
    195         pos1 = valueString.find("(");
    196         if (pos1 == 0)
    197             valueString.erase(pos1, 1);
    198         else
    199             bEntryIsCorrupt = true;
    200 
    201         pos2 = valueString.find(")");
    202         if (pos2 == valueString.length() - 1)
    203             valueString.erase(pos2, 1);
    204         else
    205             bEntryIsCorrupt = true;
    206 
    207         int count = 0;
    208         while ((pos3 = valueString.find(",")) < valueString.length())
    209         {
    210             count++;
    211             valueString.replace(pos3, 1, " ");
    212             if (pos3 < pos1)
    213                 bEntryIsCorrupt = true;
    214         }
    215 
    216         if (count != 1)
    217             bEntryIsCorrupt = true;
     313        unsigned int pos1 = valueString.find("(") + 1;
     314        unsigned int pos2 = valueString.find(")", pos1);
    218315
    219316        // Try to convert the stripped value-string to Vector2
    220         if (!bEntryIsCorrupt)
    221         {
    222             std::istringstream istream(valueString);
    223             if (!(istream >> this->value_vector2_.x))
    224             {
    225                 // The conversion failed - use the default value and restore the entry in the config-file
    226                 this->value_vector2_.x = defvalue.x;
    227                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    228                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    229             }
    230             if (!(istream >> this->value_vector2_.y))
    231             {
    232                 // The conversion failed - use the default value and restore the entry in the config-file
    233                 this->value_vector2_.y = defvalue.y;
    234                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    235                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    236             }
    237         }
    238         else
    239         {
    240             // The conversion failed - use the default value and restore the entry in the config-file
     317        if (pos1 < valueString.length() && pos2 < valueString.length() && pos1 < pos2)
     318        {
     319            valueString = valueString.substr(pos1, pos2 - pos1);
     320            std::vector<std::string> tokens = tokenize(valueString, ",");
     321            if (!string2Number(this->value_vector2_.x, tokens[0], defvalue.x))
     322                this->setConfigFileEntyToDefault();
     323            if (!string2Number(this->value_vector2_.y, tokens[1], defvalue.y))
     324                this->setConfigFileEntyToDefault();
     325        }
     326        else
     327        {
    241328            this->value_vector2_ = defvalue;
    242             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    243             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    244         }
    245     }
    246 
    247     /**
    248         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_vector3_.
     329            this->setConfigFileEntyToDefault();
     330        }
     331    }
     332
     333    /**
     334        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     335        @param value This is only needed to determine the right type.
    249336        @param classname The name of the class the variable belongs to
    250337        @param varname The name of the variable
     
    253340    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector3 defvalue)
    254341    {
     342        this->classname_ = classname;
     343        this->varname_ = varname;
     344        this->type_ = Vector3;
     345
    255346        // Try to convert the default-value from Vector3 to string
    256347        std::ostringstream ostream;
    257348        if (ostream << "(" << defvalue.x << "," << defvalue.y << "," << defvalue.z << ")")
    258             this->defvalue_ = ostream.str();
    259         else
    260             this->defvalue_ = "(0,0,0)";
    261 
    262         // Set the default values, then get the value-string
    263         this->setDefaultValues(classname, varname);
    264         std::string valueString = this->getValueString();
     349            this->defvalueString_ = ostream.str();
     350        else
     351            this->defvalueString_ = "(0,0,0)";
     352
     353        this->searchConfigFileLine();                                           // Search the entry in the config-file
     354        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
    265355
    266356        // Strip the value-string
    267         bool bEntryIsCorrupt = false;
    268         valueString = this->getStrippedLine(valueString);
    269         unsigned int pos1, pos2, pos3;
    270         pos1 = valueString.find("(");
    271         if (pos1 == 0)
    272             valueString.erase(pos1, 1);
    273         else
    274             bEntryIsCorrupt = true;
    275 
    276         pos2 = valueString.find(")");
    277         if (pos2 == valueString.length() - 1)
    278             valueString.erase(pos2, 1);
    279         else
    280             bEntryIsCorrupt = true;
    281 
    282         int count = 0;
    283         while ((pos3 = valueString.find(",")) < valueString.length())
    284         {
    285             count++;
    286             valueString.replace(pos3, 1, " ");
    287             if (pos3 < pos1)
    288                 bEntryIsCorrupt = true;
    289         }
    290 
    291         if (count != 2)
    292             bEntryIsCorrupt = true;
     357        unsigned int pos1 = valueString.find("(") + 1;
     358        unsigned int pos2 = valueString.find(")", pos1);
    293359
    294360        // Try to convert the stripped value-string to Vector3
    295         if (!bEntryIsCorrupt)
    296         {
    297             std::istringstream istream(valueString);
    298             if (!(istream >> this->value_vector3_.x))
    299             {
    300                 // The conversion failed - use the default value and restore the entry in the config-file
    301                 this->value_vector3_.x = defvalue.x;
    302                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    303                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    304             }
    305             if (!(istream >> this->value_vector3_.y))
    306             {
    307                 // The conversion failed - use the default value and restore the entry in the config-file
    308                 this->value_vector3_.y = defvalue.y;
    309                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    310                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    311             }
    312             if (!(istream >> this->value_vector3_.z))
    313             {
    314                 // The conversion failed - use the default value and restore the entry in the config-file
    315                 this->value_vector3_.z = defvalue.z;
    316                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    317                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    318             }
    319         }
    320         else
    321         {
    322             // The conversion failed - use the default value and restore the entry in the config-file
     361        if (pos1 < valueString.length() && pos2 < valueString.length() && pos1 < pos2)
     362        {
     363            valueString = valueString.substr(pos1, pos2 - pos1);
     364            std::vector<std::string> tokens = tokenize(valueString, ",");
     365            if (!string2Number(this->value_vector3_.x, tokens[0], defvalue.x))
     366                this->setConfigFileEntyToDefault();
     367            if (!string2Number(this->value_vector3_.y, tokens[1], defvalue.y))
     368                this->setConfigFileEntyToDefault();
     369            if (!string2Number(this->value_vector3_.z, tokens[2], defvalue.z))
     370                this->setConfigFileEntyToDefault();
     371        }
     372        else
     373        {
    323374            this->value_vector3_ = defvalue;
    324             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    325             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    326         }
    327     }
    328 
    329     /**
    330         @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_colourvalue_.
     375            this->setConfigFileEntyToDefault();
     376        }
     377    }
     378
     379    /**
     380        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     381        @param value This is only needed to determine the right type.
    331382        @param classname The name of the class the variable belongs to
    332383        @param varname The name of the variable
     
    335386    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue)
    336387    {
     388        this->classname_ = classname;
     389        this->varname_ = varname;
     390        this->type_ = ColourValue;
     391
    337392        // Try to convert the default-value from ColourValue to string
    338393        std::ostringstream ostream;
    339394        if (ostream << "(" << defvalue.r << "," << defvalue.g << "," << defvalue.b << "," << defvalue.a << ")")
    340             this->defvalue_ = ostream.str();
    341         else
    342             this->defvalue_ = "(0,0,0,0)";
    343 
    344         // Set the default values, then get the value-string
    345         this->setDefaultValues(classname, varname);
    346         std::string valueString = this->getValueString();
     395            this->defvalueString_ = ostream.str();
     396        else
     397            this->defvalueString_ = "(0,0,0,0)";
     398
     399        this->searchConfigFileLine();                                           // Search the entry in the config-file
     400        std::string valueString = this->parseValueString();                     // Parses the value string from the config-file-entry
    347401
    348402        // Strip the value-string
    349         bool bEntryIsCorrupt = false;
    350         valueString = this->getStrippedLine(valueString);
    351         unsigned int pos1, pos2, pos3;
    352         pos1 = valueString.find("(");
    353         if (pos1 == 0)
    354             valueString.erase(pos1, 1);
    355         else
    356             bEntryIsCorrupt = true;
    357 
    358         pos2 = valueString.find(")");
    359         if (pos2 == valueString.length() - 1)
    360             valueString.erase(pos2, 1);
    361         else
    362             bEntryIsCorrupt = true;
    363 
    364         int count = 0;
    365         while ((pos3 = valueString.find(",")) < valueString.length())
    366         {
    367             count++;
    368             valueString.replace(pos3, 1, " ");
    369             if (pos3 < pos1)
    370                 bEntryIsCorrupt = true;
    371         }
    372 
    373         if (count != 3)
    374             bEntryIsCorrupt = true;
     403        unsigned int pos1 = valueString.find("(") + 1;
     404        unsigned int pos2 = valueString.find(")", pos1);
    375405
    376406        // Try to convert the stripped value-string to Vector3
    377         if (!bEntryIsCorrupt)
    378         {
    379             std::istringstream istream(valueString);
    380             if (!(istream >> this->value_colourvalue_.r))
    381             {
    382                 // The conversion failed - use the default value and restore the entry in the config-file
    383                 this->value_colourvalue_.r = defvalue.r;
    384                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    385                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    386             }
    387             if (!(istream >> this->value_colourvalue_.g))
    388             {
    389                 // The conversion failed - use the default value and restore the entry in the config-file
    390                 this->value_colourvalue_.g = defvalue.g;
    391                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    392                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    393             }
    394             if (!(istream >> this->value_colourvalue_.b))
    395             {
    396                 // The conversion failed - use the default value and restore the entry in the config-file
    397                 this->value_colourvalue_.b = defvalue.b;
    398                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    399                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    400             }
    401             if (!(istream >> this->value_colourvalue_.a))
    402             {
    403                 // The conversion failed - use the default value and restore the entry in the config-file
    404                 this->value_colourvalue_.a = defvalue.a;
    405                 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    406                 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    407             }
    408         }
    409         else
    410         {
    411             // The conversion failed - use the default value and restore the entry in the config-file
     407        if (pos1 < valueString.length() && pos2 < valueString.length() && pos1 < pos2)
     408        {
     409            valueString = valueString.substr(pos1, pos2 - pos1);
     410            std::vector<std::string> tokens = tokenize(valueString, ",");
     411            if (!string2Number(this->value_colourvalue_.r, tokens[0], defvalue.r))
     412                this->setConfigFileEntyToDefault();
     413            if (!string2Number(this->value_colourvalue_.g, tokens[1], defvalue.g))
     414                this->setConfigFileEntyToDefault();
     415            if (!string2Number(this->value_colourvalue_.b, tokens[2], defvalue.b))
     416                this->setConfigFileEntyToDefault();
     417            if (!string2Number(this->value_colourvalue_.a, tokens[3], defvalue.a))
     418                this->setConfigFileEntyToDefault();
     419        }
     420        else
     421        {
    412422            this->value_colourvalue_ = defvalue;
    413             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    414             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    415         }
    416     }
    417 
    418     /**
    419         @brief Sets the default values of the container and searches the coresponding entry in the config-file.
    420         @param classname The name of the class the variable belongs to
    421         @param varname The name of the variable
    422     */
    423     void ConfigValueContainer::setDefaultValues(const std::string& classname, const std::string& varname)
    424     {
    425         // Set the class and variable names
    426         this->classname_ = classname;
    427         this->varname_ = varname;
    428 
    429         // Search the entry in the config-file
    430         this->searchConfigFileLine();
    431 
    432         // Set the values of all types to zero
    433         this->value_.value_int_ = 0;
    434         this->value_.value_double_ = 0.000000;
    435         this->value_.value_bool_ = false;
    436         this->value_string_ = "";
    437         this->value_vector2_ = Ogre::Vector2(0, 0);
    438         this->value_vector3_ = Ogre::Vector3(0, 0, 0);
    439         this->value_colourvalue_ = Ogre::ColourValue(0, 0, 0, 0);
    440     }
     423            this->setConfigFileEntyToDefault();
     424        }
     425    }
     426
     427    /**
     428        @brief Sets the corresponding enty in the config-file back to the default value.
     429    */
     430    void ConfigValueContainer::setConfigFileEntyToDefault()
     431    {
     432        (*this->configFileLine_) = this->varname_ + "=" + this->defvalueString_;
     433        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     434    }
     435
    441436
    442437    /**
     
    503498                    {
    504499                        // The next section startet, so our line isn't yet in the file - now we add it and safe the file
    505                         this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_);
     500                        this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
    506501                        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    507502                        success = true;
     
    523518                {
    524519                    // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file
    525                     this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_);
     520                    this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
    526521                    ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    527522                    success = true;
     
    535530        {
    536531            // We obviously didn't found the right section, so we'll create it
    537             this->configFileLines_s->push_back("[" + this->classname_ + "]");           // Create the section
    538             this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalue_); // Create the line
    539             this->configFileLine_ = --this->configFileLines_s->end();                   // Set the pointer to the last element
     532            this->configFileLines_s->push_back("[" + this->classname_ + "]");                   // Create the section
     533            this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalueString_);  // Create the line
     534            this->configFileLine_ = --this->configFileLines_s->end();                           // Set the pointer to the last element
    540535            success = true;
    541             this->configFileLines_s->push_back("");                                     // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
    542             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);                      // Save the changed config-file
     536            this->configFileLines_s->push_back("");                                             // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
     537            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);                              // Save the changed config-file
    543538        }
    544539    }
     
    597592        @return The value-string
    598593    */
    599     std::string ConfigValueContainer::getValueString(bool bStripped)
     594    std::string ConfigValueContainer::parseValueString(bool bStripped)
    600595    {
    601596        std::string output;
  • code/branches/FICN/src/orxonox/core/ConfigValueContainer.h

    r663 r667  
    4545        public:
    4646            ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue);
     47            ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned int defvalue);
     48            ConfigValueContainer(const std::string& classname, const std::string& varname, char defvalue);
     49            ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned char defvalue);
     50            ConfigValueContainer(const std::string& classname, const std::string& varname, float defvalue);
    4751            ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue);
    4852            ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue);
     53            ConfigValueContainer(const std::string& classname, const std::string& varname, const std::string& defvalue);
    4954            ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue);
    5055            ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector2 defvalue);
     
    5257            ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue);
    5358
    54             void setDefaultValues(const std::string& classname, const std::string& varname);
     59            void setConfigFileEntyToDefault();
    5560            void searchConfigFileLine();
    56             std::string getValueString(bool bStripped = true);
     61            std::string parseValueString(bool bStripped = true);
    5762
    5863            static std::string getStrippedLine(const std::string& line);
     
    6267            static void writeConfigFile(const std::string& filename);
    6368
    64             /** @returns the value of the type int. @param value This is only needed to determine the right type. */
     69            /** @returns the value. @param value This is only needed to determine the right type. */
    6570            inline int getValue(int value)                                      { return this->value_.value_int_; }
    66             /** @returns the value of the type double. @param value This is only needed to determine the right type. */
     71            /** @returns the value. @param value This is only needed to determine the right type. */
     72            inline unsigned int getValue(unsigned int value)                    { return this->value_.value_uint_; }
     73            /** @returns the value. @param value This is only needed to determine the right type. */
     74            inline char getValue(char value)                                    { return this->value_.value_char_; }
     75            /** @returns the value. @param value This is only needed to determine the right type. */
     76            inline unsigned char getValue(unsigned char value)                  { return this->value_.value_uchar_; }
     77            /** @returns the value. @param value This is only needed to determine the right type. */
     78            inline float getValue(float value)                                  { return this->value_.value_float_; }
     79            /** @returns the value. @param value This is only needed to determine the right type. */
    6780            inline double getValue(double value)                                { return this->value_.value_double_; }
    68             /** @returns the value of the type bool. @param value This is only needed to determine the right type. */
     81            /** @returns the value. @param value This is only needed to determine the right type. */
    6982            inline bool getValue(bool value)                                    { return this->value_.value_bool_; }
    70             /** @returns the value of the type std::string. @param value This is only needed to determine the right type. */
    71             inline std::string getValue(const std::string& value)               { return this->value_string_; }
    72             /** @returns the value of the type Vector2. @param value This is only needed to determine the right type. */
     83            /** @returns the value. @param value This is only needed to determine the right type. */
     84            inline const std::string& getValue(const std::string& value)        { return this->value_string_; }
     85            /** @returns the value. @param value This is only needed to determine the right type. */
     86            inline const char* getValue(const char* value)                      { return this->value_string_.c_str(); }
     87            /** @returns the value. @param value This is only needed to determine the right type. */
    7388            inline Ogre::Vector2 getValue(const Ogre::Vector2& value)           { return this->value_vector2_; }
    74             /** @returns the value of the type Vector3. @param value This is only needed to determine the right type. */
     89            /** @returns the value. @param value This is only needed to determine the right type. */
    7590            inline Ogre::Vector3 getValue(const Ogre::Vector3& value)           { return this->value_vector3_; }
    76             /** @returns the value of the type Colour�Value. @param value This is only needed to determine the right type. */
     91            /** @returns the value. @param value This is only needed to determine the right type. */
    7792            inline Ogre::ColourValue getValue(const Ogre::ColourValue& value)   { return this->value_colourvalue_; }
    7893
     
    8095            std::string         classname_;                     //!< The name of the class the variable belongs to
    8196            std::string         varname_;                       //!< The name of the variable
    82             std::string         defvalue_;                      //!< The string of the default-variable
     97            std::string         defvalueString_;                //!< The string of the default-variable
    8398
    8499            union MultiType
    85100            {
    86101                int                 value_int_;                 //!< The value, if the variable is of the type int
     102                unsigned int        value_uint_;                //!< The value, if the variable is of the type unsigned int
     103                char                value_char_;                //!< The value, if the variable is of the type char
     104                unsigned char       value_uchar_;               //!< The value, if the variable is of the type unsigned char
     105                float               value_float_;               //!< The value, if the variable is of the type float
    87106                double              value_double_;              //!< The value, if the variable is of the type double
    88107                bool                value_bool_;                //!< The value, if the variable is of the type bool
    89             } value_;
     108            } value_;                                           //!< The value of the variable
    90109
    91110            std::string         value_string_;                  //!< The value, if the variable is of the type string
     
    97116            static std::list<std::string>* configFileLines_s;   //!< A list, containing all entrys in the config-file
    98117            static bool readConfigFile_s;                       //!< True if the config-file is read and stored in the list
     118
     119            enum VariableType
     120            {
     121                Int,
     122                uInt,
     123                Char,
     124                uChar,
     125                Float,
     126                Double,
     127                Bool,
     128                ConstChar,
     129                String,
     130                Vector2,
     131                Vector3,
     132                ColourValue
     133            } type_;                                            //!< The type of the variable
    99134    };
    100135}
  • code/branches/FICN/src/orxonox/core/CoreIncludes.h

    r531 r667  
    106106    if (!container##varname) \
    107107    { \
    108         container##varname = new orxonox::ConfigValueContainer(this->getIdentifier()->getName(), #varname, defvalue); \
     108        container##varname = new orxonox::ConfigValueContainer(this->getIdentifier()->getName(), #varname, varname = defvalue); \
    109109        this->getIdentifier()->setConfigValueContainer(#varname, container##varname); \
    110110    } \
  • code/branches/FICN/src/orxonox/core/Factory.cc

    r564 r667  
    8484    void Factory::createClassHierarchy()
    8585    {
    86         COUT(4) << "*** Factory -> Create class-hierarchy\n";
     86        COUT(3) << "*** Factory -> Create class-hierarchy\n";
    8787        std::map<std::string, Identifier*>::iterator it;
    8888        it = getFactoryPointer()->identifierStringMap_.begin();
     
    9595        }
    9696        (*getFactoryPointer()->identifierStringMap_.begin()).second->stopCreatingHierarchy();
    97         COUT(4) << "*** Factory -> Finished class-hierarchy creation\n";
     97        COUT(3) << "*** Factory -> Finished class-hierarchy creation\n";
    9898    }
    9999
  • code/branches/FICN/src/orxonox/objects/test3.cc

    r496 r667  
    1616    void Test3::setConfigValues()
    1717    {
    18         SetConfigValue(value_int_, -100);
    19         SetConfigValue(value_double_, 10.555678);
    20         SetConfigValue(value_bool_, true);
    21         SetConfigValue(value_string_, "Dies ist ein Test");
     18        SetConfigValue(value_int_, 1);
     19        SetConfigValue(value_uint_, 1);
     20        SetConfigValue(value_char_, 1);
     21        SetConfigValue(value_uchar_, 1);
     22        SetConfigValue(value_float_, 1);
     23        SetConfigValue(value_double_, 1);
     24        SetConfigValue(value_bool_, 1);
     25        SetConfigValue(value_string_, "This is a test");
     26        SetConfigValue(value_constchar_, "This is another test");
    2227        SetConfigValue(value_vector2_, Vector2(101, 202));
    2328        SetConfigValue(value_vector3_, Vector3(13, 26, 39));
     
    2934    }
    3035
    31         #include <fstream>
    3236    void Test3::configOutput()
    3337    {
    3438        std::cout << this->value_int_ << std::endl;
     39        std::cout << this->value_uint_ << std::endl;
     40        std::cout << (int)this->value_char_ << std::endl;
     41        std::cout << (int)this->value_uchar_ << std::endl;
     42        std::cout << this->value_float_ << std::endl;
    3543        std::cout << this->value_double_ << std::endl;
    3644        std::cout << this->value_bool_ << std::endl;
    3745        std::cout << this->value_string_ << std::endl;
     46        std::cout << this->value_constchar_ << std::endl;
    3847        std::cout << this->value_vector2_ << std::endl;
    3948        std::cout << this->value_vector3_ << std::endl;
     
    4150    }
    4251
    43         #define testandcout(code) \
    44           std::cout << #code << " " << code << "\n"
     52    #define testandcout(code) \
     53      std::cout << #code << " " << code << "\n"
    4554
    4655    void Test3::usefullClassesIsATest(Test1* test1)
  • code/branches/FICN/src/orxonox/objects/test3.h

    r496 r667  
    2424        private:
    2525            int                 value_int_;
     26            unsigned int        value_uint_;
     27            char                value_char_;
     28            unsigned char       value_uchar_;
     29            float               value_float_;
    2630            double              value_double_;
    2731            bool                value_bool_;
    2832            std::string         value_string_;
     33            const char*         value_constchar_;
    2934            Vector2             value_vector2_;
    3035            Vector3             value_vector3_;
Note: See TracChangeset for help on using the changeset viewer.