Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 27, 2007, 5:49:03 AM (16 years ago)
Author:
landauf
Message:

hopefully replaced all static pre-main variables with more secure wrapper functions. and now i'll sleep.

File:
1 edited

Legend:

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

    r682 r698  
    3636namespace orxonox
    3737{
    38     std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; // Set the static member variable configFileLines_s to zero
    39     bool ConfigValueContainer::readConfigFile_s = false;                 // Set the static member variable readConfigFile_s to false
    40 
    4138    /**
    4239        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
     
    438435    {
    439436        // Read the file if needed
    440         if (!ConfigValueContainer::readConfigFile_s)
     437        if (!ConfigValueContainer::finishedReadingConfigFile())
    441438            ConfigValueContainer::readConfigFile(CONFIGFILEPATH);
    442439
     
    450447        bool success = false;
    451448        std::list<std::string>::iterator it1;
    452         for(it1 = ConfigValueContainer::configFileLines_s->begin(); it1 != ConfigValueContainer::configFileLines_s->end(); ++it1)
     449        for(it1 = ConfigValueContainer::getConfigFileLines().begin(); it1 != ConfigValueContainer::getConfigFileLines().end(); ++it1)
    453450        {
    454451            // Don't try to parse comments
     
    464461                // Iterate through all lines in the section
    465462                std::list<std::string>::iterator it2;
    466                 for(it2 = ++it1; it2 != ConfigValueContainer::configFileLines_s->end(); ++it2)
     463                for(it2 = ++it1; it2 != ConfigValueContainer::getConfigFileLines().end(); ++it2)
    467464                {
    468465                    // Don't try to parse comments
     
    495492                    {
    496493                        // The next section startet, so our line isn't yet in the file - now we add it and safe the file
    497                         this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
     494                        this->configFileLine_ = this->getConfigFileLines().insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
    498495                        ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    499496                        success = true;
     
    515512                {
    516513                    // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file
    517                     this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
     514                    this->configFileLine_ = this->getConfigFileLines().insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_);
    518515                    ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);
    519516                    success = true;
     
    527524        {
    528525            // We obviously didn't found the right section, so we'll create it
    529             this->configFileLines_s->push_back("[" + this->classname_ + "]");                   // Create the section
    530             this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalueString_);   // Create the line
    531             this->configFileLine_ = --this->configFileLines_s->end();                           // Set the pointer to the last element
     526            this->getConfigFileLines().push_back("[" + this->classname_ + "]");                   // Create the section
     527            this->getConfigFileLines().push_back(this->varname_ + "=" + this->defvalueString_);   // Create the line
     528            this->configFileLine_ = --this->getConfigFileLines().end();                           // Set the pointer to the last element
    532529            success = true;
    533             this->configFileLines_s->push_back("");                                             // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
     530            this->getConfigFileLines().push_back("");                                             // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function
    534531            ConfigValueContainer::writeConfigFile(CONFIGFILEPATH);                              // Save the changed config-file
    535532        }
     
    601598
    602599    /**
     600        @returns a list, containing all entrys in the config-file.
     601    */
     602    std::list<std::string>& ConfigValueContainer::getConfigFileLines()
     603    {
     604        // This is done to avoid problems while executing this code before main()
     605        static std::list<std::string> configFileLinesStaticReference = std::list<std::string>();
     606        return configFileLinesStaticReference;
     607    }
     608
     609    /**
     610        @brief Returns true if the ConfigFile is read and stored into the ConfigFile-lines-list.
     611        @param finished This is used to change the state
     612        @return True if the ConfigFile is read and stored into the ConfigFile-lines-list
     613    */
     614    bool ConfigValueContainer::finishedReadingConfigFile(bool finished)
     615    {
     616        // This is done to avoid problems while executing this code before main()
     617        static bool finishedReadingConfigFileStaticVariable = false;
     618
     619        if (finished)
     620            finishedReadingConfigFileStaticVariable = true;
     621
     622        return finishedReadingConfigFileStaticVariable;
     623    }
     624
     625    /**
    603626        @brief Reads the config-file and stores the lines in a list.
    604627        @param filename The name of the config-file
     
    606629    void ConfigValueContainer::readConfigFile(const std::string& filename)
    607630    {
    608         ConfigValueContainer::readConfigFile_s = true;
    609 
    610         // Create the list if needed
    611         if (!ConfigValueContainer::configFileLines_s)
    612             ConfigValueContainer::configFileLines_s = new std::list<std::string>;
     631        ConfigValueContainer::finishedReadingConfigFile(true);
    613632
    614633        // This creates the file if it's not existing
     
    627646        {
    628647            file.getline(line, 1024);
    629             ConfigValueContainer::configFileLines_s->push_back(line);
     648            ConfigValueContainer::getConfigFileLines().push_back(line);
    630649//            std::cout << "### ->" << line << "<- : empty: " << isEmpty(line) << " comment: " << isComment(line) << std::endl;
    631650        }
    632651
    633652        // The last line is useless
    634         ConfigValueContainer::configFileLines_s->pop_back();
     653        ConfigValueContainer::getConfigFileLines().pop_back();
    635654
    636655        // Add an empty line to the end of the file if needed
    637656        // this is needed for the algorithm in the searchConfigFileLine-function
    638         if ((ConfigValueContainer::configFileLines_s->size() > 0) && !isEmpty(*ConfigValueContainer::configFileLines_s->rbegin()))
     657        if ((ConfigValueContainer::getConfigFileLines().size() > 0) && !isEmpty(*ConfigValueContainer::getConfigFileLines().rbegin()))
    639658        {
    640659//            std::cout << "### newline added" << std::endl;
    641             ConfigValueContainer::configFileLines_s->push_back("");
     660            ConfigValueContainer::getConfigFileLines().push_back("");
    642661        }
    643662
     
    652671    {
    653672        // Make sure we stored the config-file in the list
    654         if (!ConfigValueContainer::readConfigFile_s)
     673        if (!ConfigValueContainer::finishedReadingConfigFile())
    655674            ConfigValueContainer::readConfigFile(filename);
    656675
     
    661680        // Iterate through the list an write the lines into the file
    662681        std::list<std::string>::iterator it;
    663         for(it = ConfigValueContainer::configFileLines_s->begin(); it != ConfigValueContainer::configFileLines_s->end(); ++it)
     682        for(it = ConfigValueContainer::getConfigFileLines().begin(); it != ConfigValueContainer::getConfigFileLines().end(); ++it)
    664683        {
    665684            file << (*it) << std::endl;
Note: See TracChangeset for help on using the changeset viewer.