Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 23, 2009, 8:57:42 PM (15 years ago)
Author:
rgrieder
Message:
  • Moved def_keybindings to media repository in folder defaultConfig
  • If you have a better name for that folder, you're welcome
  • the "def_" prefix has been removed
  • ConfigFileManager now looks for a file in media/defaultConfig with the same name if the config file does not exist yet
  • No file gets written while only loading
  • Removed hacky GCC 3 warning code for each library and instead just put "Wno-sign-compare" to the GCC 3 flags (that will remove all boost::filesystem warnings)
  • ogre.cfg still remains on tardis for the development build (not install though)
Location:
code/branches/buildsystem3/src/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/buildsystem3/src/core/CMakeLists.txt

    r2685 r2690  
    6868GENERATE_TOLUA_BINDINGS(Core CORE_FILES INPUTFILES LuaBind.h CommandExecutor.h)
    6969
    70 IF(GCC_NO_SYSTEM_HEADER_SUPPORT)
    71   # Get around displaying a few hundred lines of warning code
    72   SET_SOURCE_FILES_PROPERTIES(
    73     ArgumentCompletionFunctions.cc
    74     CommandLine.cc
    75     ConfigFileManager.cc
    76     Language.cc
    77     LuaBind.cc
    78     input/KeyBinder.cc
    79     PROPERTIES COMPILE_FLAGS "-Wno-sign-compare")
    80 ENDIF()
    81 
    8270ADD_LIBRARY(core SHARED ${CORE_FILES})
    8371
  • code/branches/buildsystem3/src/core/ConfigFileManager.cc

    r2687 r2690  
    223223    void ConfigFile::load(bool bCreateIfNotExisting)
    224224    {
    225         // Be sure we start from new
     225        // Be sure we start from new in the memory
    226226        this->clear();
    227227
    228         boost::filesystem::path filepath(Core::getConfigPath() + "/" + this->filename_);
    229 
    230         // This creates the config file if it's not existing
    231         std::ofstream createFile;
    232         createFile.open(filepath.file_string().c_str(), std::fstream::app);
    233         createFile.close();
     228        // Get default file if necessary and available
     229        boost::filesystem::path filepath(Core::getConfigPath());
     230        filepath /= this->filename_;
     231        if (!boost::filesystem::exists(filepath))
     232        {
     233            // Try to get default one from the media folder
     234            boost::filesystem::path defaultFilepath(Core::getMediaPath());
     235            defaultFilepath = defaultFilepath / "defaultConfig" / this->filename_;
     236            if (boost::filesystem::exists(defaultFilepath))
     237            {
     238                boost::filesystem::copy_file(defaultFilepath, filepath);
     239            }
     240        }
    234241
    235242        // Open the file
    236243        std::ifstream file;
    237244        file.open(filepath.file_string().c_str(), std::fstream::in);
    238 
    239         if (!file.is_open())
    240         {
    241             COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl;
    242             COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl;
    243             return;
    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))
    258             {
    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)
     245        if (file.is_open())
     246        {
     247
     248            char linearray[CONFIG_FILE_MAX_LINELENGHT];
     249
     250            ConfigFileSection* newsection = 0;
     251
     252            while (file.good() && !file.eof())
     253            {
     254                file.getline(linearray, CONFIG_FILE_MAX_LINELENGHT);
     255
     256                std::string line = std::string(linearray);
     257
     258                std::string temp = getStripped(line);
     259                if (!isEmpty(temp) && !isComment(temp))
    264260                {
    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);
    269                     else
    270                         newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1));
    271                     this->sections_.insert(this->sections_.end(), newsection);
    272                     continue;
    273                 }
    274             }
    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)
     261                    size_t   pos1 = temp.find('[');
     262                    if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos;
     263                    size_t   pos2 = line.find(']');
     264
     265                    if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1)
    289266                    {
    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                         }
     267                        // New section
     268                        std::string comment = line.substr(pos2 + 1);
     269                        if (isComment(comment))
     270                            newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment);
    303271                        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));
     272                            newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1));
     273                        this->sections_.insert(this->sections_.end(), newsection);
    325274                        continue;
    326275                    }
    327276                }
    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();
     277
     278                if (newsection != 0)
     279                {
     280                    if (isComment(line))
     281                    {
     282                        // New comment
     283                        newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line)));
     284                        continue;
     285                    }
     286                    else
     287                    {
     288                        size_t pos1 = line.find('=');
     289
     290                        if (pos1 != std::string::npos && pos1 > 0)
     291                        {
     292                            // New entry
     293                            size_t pos2 = line.find('[');
     294                            size_t pos3 = line.find(']');
     295                            size_t commentposition = getNextCommentPosition(line, pos1 + 1);
     296                            while (isBetweenQuotes(line, commentposition))
     297                            {
     298                                commentposition = getNextCommentPosition(line, commentposition + 1);
     299                            }
     300                            std::string value = "", comment = "";
     301                            if (commentposition == std::string::npos)
     302                            {
     303                                value = removeTrailingWhitespaces(line.substr(pos1 + 1));
     304                            }
     305                            else
     306                            {
     307                                value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1));
     308                                comment = removeTrailingWhitespaces(line.substr(commentposition));
     309                            }
     310
     311                            if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1)
     312                            {
     313                                // There might be an array index
     314                                unsigned int index = 0;
     315                                if (ConvertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1)))
     316                                {
     317                                    // New array
     318                                    std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
     319                                    (*it)->setValue(value);
     320                                    (*it)->setComment(comment);
     321                                    continue;
     322                                }
     323                            }
     324
     325                            // New value
     326                            newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment));
     327                            continue;
     328                        }
     329                    }
     330                }
     331            }
     332
     333            file.close();
     334
     335            COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
     336
     337            // Save the file in case something changed (like stripped whitespaces)
     338            this->save();
     339
     340            // Update all ConfigValueContainers
     341            this->updateConfigValues();
     342        } // end file.is_open()
    340343    }
    341344
    342345    void ConfigFile::save() const
    343346    {
    344         boost::filesystem::path filepath(Core::getConfigPath() + "/" + this->filename_);
     347        boost::filesystem::path filepath(Core::getConfigPath());
     348        filepath /= this->filename_;
    345349
    346350        std::ofstream file;
  • code/branches/buildsystem3/src/core/Core.cc

    r2685 r2690  
    3434#include "Core.h"
    3535#include <cassert>
     36#include <fstream>
     37#include <boost/filesystem.hpp>
     38
     39#include "util/Exception.h"
    3640#include "Language.h"
    3741#include "CoreIncludes.h"
     
    5155    std::string Core::configPath_s(ORXONOX_CONFIG_INSTALL_PATH); // from OrxonoxConfig.h
    5256    std::string Core::logPath_s   (ORXONOX_LOG_INSTALL_PATH);    // from OrxonoxConfig.h
     57    std::string Core::mediaPath_s (ORXONOX_MEDIA_INSTALL_PATH);  // from OrxonoxConfig.h
    5358
    5459    Core* Core::singletonRef_s = 0;
     
    119124            defaultMediaPath = ORXONOX_MEDIA_DEV_PATH;
    120125
    121         SetConfigValue(mediaPath_, defaultMediaPath)
     126        SetConfigValue(mediaPath_s, defaultMediaPath)
    122127            .description("Relative path to the game data.").callback(this, &Core::mediaPathChanged);
    123128
     
    157162    void Core::mediaPathChanged()
    158163    {
    159         if (mediaPath_ != "" && mediaPath_[mediaPath_.size() - 1] != '/')
    160         {
    161             ModifyConfigValue(mediaPath_, set, mediaPath_ + "/");
    162         }
    163 
    164         if (mediaPath_ == "")
    165         {
    166             ModifyConfigValue(mediaPath_, set, "/");
     164        if (mediaPath_s != "" && mediaPath_s[mediaPath_s.size() - 1] != '/')
     165        {
     166            ModifyConfigValue(mediaPath_s, set, mediaPath_s + "/");
     167        }
     168
     169        if (mediaPath_s == "")
     170        {
     171            ModifyConfigValue(mediaPath_s, set, "/");
    167172            COUT(2) << "Warning: Data path set to \"/\", is that really correct?" << std::endl;
    168173        }
     
    245250        if (*path.end() != '/' && *path.end() != '\\')
    246251        {
    247             ModifyConfigValue(mediaPath_, tset, path + "/");
     252            ModifyConfigValue(mediaPath_s, tset, path + "/");
    248253        }
    249254        else
    250255        {
    251             ModifyConfigValue(mediaPath_, tset, path);
     256            ModifyConfigValue(mediaPath_s, tset, path);
    252257        }
    253258    }
     
    264269    }
    265270
    266     /*static*/ void Core::setDevBuild()
    267     {
    268         // Be careful never to call this function before main()!
    269 
    270         Core::isDevBuild_s = true;
    271         // Constants taken from OrxonoxConfig.h
    272         Core::configPath_s = ORXONOX_CONFIG_DEV_PATH;
    273         Core::logPath_s    = ORXONOX_LOG_DEV_PATH;
     271    /**
     272    @brief
     273        Checks for "orxonox_dev_build.keep_me" in the working diretory.
     274        If found it means that this is not an installed run, hence we
     275        don't write the logs and config files to ~/.orxonox
     276    */
     277    /*static*/ void Core::checkDevBuild()
     278    {
     279        std::ifstream probe;
     280        probe.open("orxonox_dev_build.keep_me");
     281        if (probe)
     282        {
     283            Core::isDevBuild_s = true;
     284            // Constants are taken from OrxonoxConfig.h
     285            Core::configPath_s = ORXONOX_CONFIG_DEV_PATH;
     286            Core::logPath_s    = ORXONOX_LOG_DEV_PATH;
     287            Core::mediaPath_s  = ORXONOX_MEDIA_DEV_PATH;
     288            probe.close();
     289        }
     290    }
     291
     292    /*
     293    @brief
     294        Checks for the log and the config directory and creates them
     295        if necessary. Otherwise me might have problems opening those files.
     296    */
     297    /*static*/ void Core::createDirectories()
     298    {
     299        std::vector<std::pair<boost::filesystem::path, std::string> > directories;
     300        directories.push_back(std::pair<boost::filesystem::path, std::string>
     301            (boost::filesystem::path(Core::configPath_s), "config"));
     302        directories.push_back(std::pair<boost::filesystem::path, std::string>
     303            (boost::filesystem::path(Core::logPath_s),    "log"));
     304
     305        for (std::vector<std::pair<boost::filesystem::path, std::string> >::iterator it = directories.begin();
     306            it != directories.end(); ++it)
     307        {
     308            if (boost::filesystem::exists(it->first) && !boost::filesystem::is_directory(it->first))
     309            {
     310                ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \
     311                                         Please remove " + it->first.file_string());
     312            }
     313            if (boost::filesystem::create_directory(it->first)) // function may not return true at all (bug?)
     314            {
     315                COUT(4) << "Created " << it->second << " directory" << std::endl;
     316            }
     317        }
    274318    }
    275319}
  • code/branches/buildsystem3/src/core/Core.h

    r2685 r2690  
    6868            static bool isDevBuild() { return Core::isDevBuild_s; }
    6969
    70             static const std::string& getMediaPath()
    71             { assert(singletonRef_s); return singletonRef_s->mediaPath_; }
    7270            static void tsetMediaPath(const std::string& path)
    7371            { assert(singletonRef_s); singletonRef_s->_tsetMediaPath(path); }
     72            static const std::string& getMediaPath()  { return mediaPath_s; }
    7473            static const std::string& getConfigPath() { return configPath_s; }
    7574            static const std::string& getLogPath()    { return logPath_s; }
     
    9695            void _tsetMediaPath(const std::string& path);
    9796
    98             static void setDevBuild();
     97            static void createDirectories();
     98            static void checkDevBuild();
    9999
    100100            int softDebugLevel_;                            //!< The debug level
     
    104104            std::string language_;                          //!< The language
    105105            bool bInitializeRandomNumberGenerator_;         //!< If true, srand(time(0)) is called
    106             std::string mediaPath_;                         //!< Path to the data/media file folder
    107106
    108107            static bool bShowsGraphics_s;                   //!< global variable that tells whether to show graphics
     
    115114            static std::string configPath_s;                //!< Path to the config file folder
    116115            static std::string logPath_s;                   //!< Path to the log file folder
     116            static std::string mediaPath_s;                 //!< Path to the data/media file folder
    117117
    118118            static Core* singletonRef_s;
  • code/branches/buildsystem3/src/core/input/KeyBinder.cc

    r2687 r2690  
    248248        True if loading succeeded.
    249249    */
    250     void KeyBinder::loadBindings(const std::string& filename, const std::string& defaultFilename)
     250    void KeyBinder::loadBindings(const std::string& filename)
    251251    {
    252252        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
     
    255255            return;
    256256
    257         boost::filesystem::path folder(Core::getConfigPath());
    258         boost::filesystem::path filepath(folder/filename);
    259 
    260         // get bindings from default file if filename doesn't exist.
    261         std::ifstream infile;
    262         infile.open(filepath.file_string().c_str());
    263         if (!infile)
    264         {
    265             ConfigFileManager::getInstance().setFilename(this->configFile_, defaultFilename);
    266             ConfigFileManager::getInstance().saveAs(this->configFile_, filename);
    267         }
    268         else
    269             infile.close();
    270257        ConfigFileManager::getInstance().setFilename(this->configFile_, filename);
    271258
  • code/branches/buildsystem3/src/core/input/KeyBinder.h

    r2662 r2690  
    6161        virtual ~KeyBinder();
    6262
    63         void loadBindings(const std::string& filename, const std::string& defaultFilename);
     63        void loadBindings(const std::string& filename);
    6464        void clearBindings();
    6565        bool setBinding(const std::string& binding, const std::string& name, bool bTemporary = false);
Note: See TracChangeset for help on using the changeset viewer.