Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 449


Ignore:
Timestamp:
Dec 10, 2007, 12:30:50 AM (16 years ago)
Author:
landauf
Message:

improved config-file parsing - more error detection (and correction of course)

File:
1 edited

Legend:

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

    r447 r449  
    116116    ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue)
    117117    {
    118         // Not much to do here - just set all member-variables and check the config-file
    119         this->defvalue_ = defvalue;
     118        // Convert the string to a "config-file-string" with quotes
     119        this->defvalue_ = "\"" + std::string(defvalue) + "\"";
     120
     121        // Set the default-values, then get the value-string
    120122        this->setDefaultValues(classname, varname);
    121         this->value_string_ = this->getValueString(false);
     123        std::string valueString = this->getValueString(false);
     124
     125        // Strip the quotes
     126        unsigned int pos1 = valueString.find("\"") + 1;
     127        unsigned int pos2 = valueString.find("\"", pos1);
     128
     129        // Check if the entry was correctly quoted
     130        if (pos1 < valueString.length() && pos2 < valueString.length() && !(valueString.find("\"", pos2 + 1) < valueString.length()))
     131        {
     132            // It was - get the string between the quotes
     133            valueString = valueString.substr(pos1, pos2 - pos1);
     134            this->value_string_ = valueString;
     135        }
     136        else
     137        {
     138            // It wasn't - use the default-value and restore the entry in the config-file.
     139            this->value_string_ = defvalue;
     140            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     141            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     142        }
    122143    }
    123144
     
    142163
    143164        // Strip the value-string
     165        bool bEntryIsCorrupt = false;
    144166        valueString = this->getStrippedLine(valueString);
    145         unsigned int pos;
    146         while ((pos = valueString.find("(")) < valueString.length())
    147             valueString.erase(pos, 1);
    148         while ((pos = valueString.find(")")) < valueString.length())
    149             valueString.erase(pos, 1);
    150         while ((pos = valueString.find(",")) < valueString.length())
    151             valueString.replace(pos, 1, " ");
     167        unsigned int pos1, pos2, pos3;
     168        pos1 = valueString.find("(");
     169        if (pos1 == 0)
     170            valueString.erase(pos1, 1);
     171        else
     172            bEntryIsCorrupt = true;
     173
     174        pos2 = valueString.find(")");
     175        if (pos2 == valueString.length() - 1)
     176            valueString.erase(pos2, 1);
     177        else
     178            bEntryIsCorrupt = true;
     179
     180        int count = 0;
     181        while ((pos3 = valueString.find(",")) < valueString.length())
     182        {
     183            count++;
     184            valueString.replace(pos3, 1, " ");
     185            if (pos3 < pos1)
     186                bEntryIsCorrupt = true;
     187        }
     188
     189        if (count != 2)
     190            bEntryIsCorrupt = true;
    152191
    153192        // Try to convert the stripped value-string to Vector3
    154         std::istringstream istream(valueString);
    155         if (!(istream >> this->value_vector3_.x))
     193        if (!bEntryIsCorrupt)
     194        {
     195            std::istringstream istream(valueString);
     196            if (!(istream >> this->value_vector3_.x))
     197            {
     198                // The conversion failed - use the default value and restore the entry in the config-file
     199                this->value_vector3_.x = defvalue.x;
     200                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     201                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     202            }
     203            if (!(istream >> this->value_vector3_.y))
     204            {
     205                // The conversion failed - use the default value and restore the entry in the config-file
     206                this->value_vector3_.y = defvalue.y;
     207                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     208                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     209            }
     210            if (!(istream >> this->value_vector3_.z))
     211            {
     212                // The conversion failed - use the default value and restore the entry in the config-file
     213                this->value_vector3_.z = defvalue.z;
     214                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     215                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     216            }
     217        }
     218        else
    156219        {
    157220            // The conversion failed - use the default value and restore the entry in the config-file
    158             this->value_vector3_.x = defvalue.x;
    159             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    160             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    161         }
    162         if (!(istream >> this->value_vector3_.y))
    163         {
    164             // The conversion failed - use the default value and restore the entry in the config-file
    165             this->value_vector3_.y = defvalue.y;
    166             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    167             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    168         }
    169         if (!(istream >> this->value_vector3_.z))
    170         {
    171             // The conversion failed - use the default value and restore the entry in the config-file
    172             this->value_vector3_.z = defvalue.z;
     221            this->value_vector3_ = defvalue;
    173222            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    174223            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     
    196245
    197246        // Strip the value-string
     247        bool bEntryIsCorrupt = false;
    198248        valueString = this->getStrippedLine(valueString);
    199         unsigned int pos;
    200         while ((pos = valueString.find("(")) < valueString.length())
    201             valueString.erase(pos, 1);
    202         while ((pos = valueString.find(")")) < valueString.length())
    203             valueString.erase(pos, 1);
    204         while ((pos = valueString.find(",")) < valueString.length())
    205             valueString.replace(pos, 1, " ");
     249        unsigned int pos1, pos2, pos3;
     250        pos1 = valueString.find("(");
     251        if (pos1 == 0)
     252            valueString.erase(pos1, 1);
     253        else
     254            bEntryIsCorrupt = true;
     255
     256        pos2 = valueString.find(")");
     257        if (pos2 == valueString.length() - 1)
     258            valueString.erase(pos2, 1);
     259        else
     260            bEntryIsCorrupt = true;
     261
     262        int count = 0;
     263        while ((pos3 = valueString.find(",")) < valueString.length())
     264        {
     265            count++;
     266            valueString.replace(pos3, 1, " ");
     267            if (pos3 < pos1)
     268                bEntryIsCorrupt = true;
     269        }
     270
     271        if (count != 3)
     272            bEntryIsCorrupt = true;
    206273
    207274        // Try to convert the stripped value-string to Vector3
    208         std::istringstream istream(valueString);
    209         if (!(istream >> this->value_colourvalue_.r))
     275        if (!bEntryIsCorrupt)
     276        {
     277            std::istringstream istream(valueString);
     278            if (!(istream >> this->value_colourvalue_.r))
     279            {
     280                // The conversion failed - use the default value and restore the entry in the config-file
     281                this->value_colourvalue_.r = defvalue.r;
     282                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     283                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     284            }
     285            if (!(istream >> this->value_colourvalue_.g))
     286            {
     287                // The conversion failed - use the default value and restore the entry in the config-file
     288                this->value_colourvalue_.g = defvalue.g;
     289                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     290                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     291            }
     292            if (!(istream >> this->value_colourvalue_.b))
     293            {
     294                // The conversion failed - use the default value and restore the entry in the config-file
     295                this->value_colourvalue_.b = defvalue.b;
     296                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     297                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     298            }
     299            if (!(istream >> this->value_colourvalue_.a))
     300            {
     301                // The conversion failed - use the default value and restore the entry in the config-file
     302                this->value_colourvalue_.a = defvalue.a;
     303                (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
     304                ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     305            }
     306        }
     307        else
    210308        {
    211309            // The conversion failed - use the default value and restore the entry in the config-file
    212             this->value_colourvalue_.r = defvalue.r;
    213             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    214             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    215         }
    216         if (!(istream >> this->value_colourvalue_.g))
    217         {
    218             // The conversion failed - use the default value and restore the entry in the config-file
    219             this->value_colourvalue_.g = defvalue.g;
    220             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    221             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    222         }
    223         if (!(istream >> this->value_colourvalue_.b))
    224         {
    225             // The conversion failed - use the default value and restore the entry in the config-file
    226             this->value_colourvalue_.b = defvalue.b;
    227             (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    228             ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    229         }
    230         if (!(istream >> this->value_colourvalue_.a))
    231         {
    232             // The conversion failed - use the default value and restore the entry in the config-file
    233             this->value_colourvalue_.a = defvalue.a;
     310            this->value_colourvalue_ = defvalue;
    234311            (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_;
    235312            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
     
    409486        while ((pos = output.find(" ")) < output.length())
    410487            output.erase(pos, 1);
     488        while ((pos = output.find("\t")) < output.length())
     489            output.erase(pos, 1);
    411490
    412491        return output;
Note: See TracChangeset for help on using the changeset viewer.