Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 28, 2009, 7:46:37 PM (15 years ago)
Author:
rgrieder
Message:

Merged buildsystem3 containing buildsystem2 containing Adi's buildsystem branch back to the trunk.
Please update the media directory if you were not using buildsystem3 before.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/ConfigFileManager.cc

    r2662 r2710  
    3030
    3131#include <cassert>
     32#include <boost/filesystem.hpp>
     33
    3234#include "util/Convert.h"
    3335#include "util/String.h"
    3436#include "ConsoleCommand.h"
    3537#include "ConfigValueContainer.h"
     38#include "Core.h"
    3639
    3740namespace orxonox
     
    220223    void ConfigFile::load(bool bCreateIfNotExisting)
    221224    {
    222         // Be sure we start from new
     225        // Be sure we start from new in the memory
    223226        this->clear();
    224227
    225         // This creates the config file if it's not existing
    226         std::ofstream createFile;
    227         createFile.open(this->filename_.c_str(), std::fstream::app);
    228         createFile.close();
     228        // Get default file if necessary and available
     229        boost::filesystem::path filepath(Core::getConfigPath() / this->filename_);
     230        if (!boost::filesystem::exists(filepath))
     231        {
     232            // Try to get default one from the media folder
     233            boost::filesystem::path defaultFilepath(Core::getMediaPath() / "defaultConfig" / this->filename_);
     234            if (boost::filesystem::exists(defaultFilepath))
     235            {
     236                boost::filesystem::copy_file(defaultFilepath, filepath);
     237            }
     238        }
    229239
    230240        // Open the file
    231241        std::ifstream file;
    232         file.open(this->filename_.c_str(), std::fstream::in);
    233 
    234         if (!file.is_open())
    235         {
    236             COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl;
    237             COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl;
    238             return;
    239         }
    240 
    241         char linearray[CONFIG_FILE_MAX_LINELENGHT];
    242 
    243         ConfigFileSection* newsection = 0;
    244 
    245         while (file.good() && !file.eof())
    246         {
    247             file.getline(linearray, CONFIG_FILE_MAX_LINELENGHT);
    248 
    249             std::string line = std::string(linearray);
    250 
    251             std::string temp = getStripped(line);
    252             if (!isEmpty(temp) && !isComment(temp))
    253             {
    254                 size_t   pos1 = temp.find('[');
    255                 if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos;
    256                 size_t   pos2 = line.find(']');
    257 
    258                 if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1)
     242        file.open(filepath.file_string().c_str(), std::fstream::in);
     243        if (file.is_open())
     244        {
     245
     246            char linearray[CONFIG_FILE_MAX_LINELENGHT];
     247
     248            ConfigFileSection* newsection = 0;
     249
     250            while (file.good() && !file.eof())
     251            {
     252                file.getline(linearray, CONFIG_FILE_MAX_LINELENGHT);
     253
     254                std::string line = std::string(linearray);
     255
     256                std::string temp = getStripped(line);
     257                if (!isEmpty(temp) && !isComment(temp))
    259258                {
    260                     // New section
    261                     std::string comment = line.substr(pos2 + 1);
    262                     if (isComment(comment))
    263                         newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment);
    264                     else
    265                         newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1));
    266                     this->sections_.insert(this->sections_.end(), newsection);
    267                     continue;
    268                 }
    269             }
    270 
    271             if (newsection != 0)
    272             {
    273                 if (isComment(line))
    274                 {
    275                     // New comment
    276                     newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line)));
    277                     continue;
    278                 }
    279                 else
    280                 {
    281                     size_t pos1 = line.find('=');
    282 
    283                     if (pos1 != std::string::npos && pos1 > 0)
     259                    size_t   pos1 = temp.find('[');
     260                    if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos;
     261                    size_t   pos2 = line.find(']');
     262
     263                    if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1)
    284264                    {
    285                         // New entry
    286                         size_t pos2 = line.find('[');
    287                         size_t pos3 = line.find(']');
    288                         size_t commentposition = getNextCommentPosition(line, pos1 + 1);
    289                         while (isBetweenQuotes(line, commentposition))
    290                         {
    291                             commentposition = getNextCommentPosition(line, commentposition + 1);
    292                         }
    293                         std::string value = "", comment = "";
    294                         if (commentposition == std::string::npos)
    295                         {
    296                             value = removeTrailingWhitespaces(line.substr(pos1 + 1));
    297                         }
     265                        // New section
     266                        std::string comment = line.substr(pos2 + 1);
     267                        if (isComment(comment))
     268                            newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment);
    298269                        else
    299                         {
    300                             value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1));
    301                             comment = removeTrailingWhitespaces(line.substr(commentposition));
    302                         }
    303 
    304                         if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1)
    305                         {
    306                             // There might be an array index
    307                             unsigned int index = 0;
    308                             if (ConvertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1)))
    309                             {
    310                                 // New array
    311                                 std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
    312                                 (*it)->setValue(value);
    313                                 (*it)->setComment(comment);
    314                                 continue;
    315                             }
    316                         }
    317 
    318                         // New value
    319                         newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment));
     270                            newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1));
     271                        this->sections_.insert(this->sections_.end(), newsection);
    320272                        continue;
    321273                    }
    322274                }
    323             }
    324         }
    325 
    326         file.close();
    327 
    328         COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
    329 
    330         // Save the file in case something changed (like stripped whitespaces)
    331         this->save();
    332 
    333         // Update all ConfigValueContainers
    334         this->updateConfigValues();
     275
     276                if (newsection != 0)
     277                {
     278                    if (isComment(line))
     279                    {
     280                        // New comment
     281                        newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line)));
     282                        continue;
     283                    }
     284                    else
     285                    {
     286                        size_t pos1 = line.find('=');
     287
     288                        if (pos1 != std::string::npos && pos1 > 0)
     289                        {
     290                            // New entry
     291                            size_t pos2 = line.find('[');
     292                            size_t pos3 = line.find(']');
     293                            size_t commentposition = getNextCommentPosition(line, pos1 + 1);
     294                            while (isBetweenQuotes(line, commentposition))
     295                            {
     296                                commentposition = getNextCommentPosition(line, commentposition + 1);
     297                            }
     298                            std::string value = "", comment = "";
     299                            if (commentposition == std::string::npos)
     300                            {
     301                                value = removeTrailingWhitespaces(line.substr(pos1 + 1));
     302                            }
     303                            else
     304                            {
     305                                value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1));
     306                                comment = removeTrailingWhitespaces(line.substr(commentposition));
     307                            }
     308
     309                            if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1)
     310                            {
     311                                // There might be an array index
     312                                unsigned int index = 0;
     313                                if (ConvertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1)))
     314                                {
     315                                    // New array
     316                                    std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
     317                                    (*it)->setValue(value);
     318                                    (*it)->setComment(comment);
     319                                    continue;
     320                                }
     321                            }
     322
     323                            // New value
     324                            newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment));
     325                            continue;
     326                        }
     327                    }
     328                }
     329            }
     330
     331            file.close();
     332
     333            COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
     334
     335            // Save the file in case something changed (like stripped whitespaces)
     336            this->save();
     337
     338            // Update all ConfigValueContainers
     339            this->updateConfigValues();
     340        } // end file.is_open()
    335341    }
    336342
    337343    void ConfigFile::save() const
    338344    {
     345        boost::filesystem::path filepath(Core::getConfigPath() / this->filename_);
     346
    339347        std::ofstream file;
    340         file.open(this->filename_.c_str(), std::fstream::out);
     348        file.open(filepath.file_string().c_str(), std::fstream::out);
    341349        file.setf(std::ios::fixed, std::ios::floatfield);
    342350        file.precision(6);
Note: See TracChangeset for help on using the changeset viewer.