Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1006


Ignore:
Timestamp:
Apr 10, 2008, 1:24:29 AM (16 years ago)
Author:
landauf
Message:

started implementing a config-file manager, but this is still unfinished.

Location:
code/branches/core2/src
Files:
1 added
4 edited

Legend:

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

    r966 r1006  
    66  IdentifierDistributor.cc
    77  MetaObjectList.cc
     8  ConfigFileManager.cc
    89  ConfigValueContainer.cc
    910  Error.cc
  • code/branches/core2/src/orxonox/core/ConfigFileManager.h

    r989 r1006  
    2929#define _ConfigFileManager_H__
    3030
     31#include <iostream>
     32#include <string>
    3133#include <list>
     34#include <map>
     35
     36#include "util/Math.h"
    3237
    3338#include "CorePrereqs.h"
     39
     40#define DEFAULT_CONFIG_FILE "default.ini"
    3441
    3542namespace orxonox
    3643{
    37     class ConfigFileEntry
    38     {
    39         public:
    40             ConfigFileEntry(const std::string& name, const std::string& value = "");
    41 
    42             void setValue(const std::string& value);
    43             const std::string& getValue() const;
    44             const std::string& getName() const;
    45 
    46         private:
     44    enum _CoreExport ConfigFileType
     45    {
     46        CFT_Settings,
     47        CFT_Keybindings
     48    };
     49
     50
     51    void reloadConfig();
     52    void saveConfig();
     53    void cleanConfig();
     54    void loadSettings(const std::string& filename);
     55    void loadKeybindings(const std::string& filename);
     56
     57
     58    /////////////////////
     59    // ConfigFileEntry //
     60    /////////////////////
     61    class _CoreExport ConfigFileEntry
     62    {
     63        public:
     64            virtual void setValue(const std::string& value) = 0;
     65            virtual const std::string& getValue() const = 0;
     66            virtual const std::string& getName() const = 0;
     67            virtual unsigned int getIndex() const { return 0; }
     68            virtual std::string getFileEntry() const = 0;
     69    };
     70
     71
     72    //////////////////////////
     73    // ConfigFileEntryValue //
     74    //////////////////////////
     75    class _CoreExport ConfigFileEntryValue : public ConfigFileEntry
     76    {
     77        public:
     78            inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", const std::string& additionalComment = "") : name_(name), value_(value), additionalComment_(additionalComment), bString_(false) {}
     79            inline virtual ~ConfigFileEntryValue() {}
     80
     81            inline virtual const std::string& getName() const
     82                { return this->name_; }
     83
     84            inline virtual void setValue(const std::string& value)
     85                { this->value_ = value; }
     86            inline virtual const std::string& getValue() const
     87                { return this->value_; }
     88
     89            inline bool isString() const
     90                { return this->bString_; }
     91            inline void setString(bool bString)
     92                { this->bString_ = bString; }
     93
     94            virtual std::string getFileEntry() const;
     95
     96        protected:
    4797            std::string name_;
    4898            std::string value_;
    49     };
    50 
    51     class ConfigFileSection
    52     {
    53         public:
    54             void addEntry(const std::string& name);
    55             ConfigFileEntry* getEntry(const std::string& name) const;
    56 
    57             void setValue(const std::string& name, const std::string& value);
    58             const std::string& getValue(const std::string& name) const;
    59 
    60         private:
     99            std::string additionalComment_;
     100            bool bString_;
     101    };
     102
     103
     104    ///////////////////////////////
     105    // ConfigFileEntryArrayValue //
     106    ///////////////////////////////
     107    class _CoreExport ConfigFileEntryArrayValue : public ConfigFileEntryValue
     108    {
     109        public:
     110            inline ConfigFileEntryArrayValue(const std::string& name, unsigned int index, const std::string& value = "", const std::string& additionalComment = "") : ConfigFileEntryValue(name, value, additionalComment), index_(index) {}
     111            inline virtual ~ConfigFileEntryArrayValue() {}
     112
     113            inline virtual unsigned int getIndex() const
     114                { return this->index_; }
     115
     116            virtual std::string getFileEntry() const;
     117
     118        private:
     119            unsigned int index_;
     120    };
     121
     122
     123    ////////////////////////////
     124    // ConfigFileEntryComment //
     125    ////////////////////////////
     126    class _CoreExport ConfigFileEntryComment : public ConfigFileEntry
     127    {
     128        public:
     129            inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {}
     130            inline virtual ~ConfigFileEntryComment() {}
     131
     132            inline virtual const std::string& getName() const
     133                { return this->comment_; }
     134
     135            inline virtual void setValue(const std::string& value)
     136                {}
     137            inline virtual const std::string& getValue() const
     138                { return this->comment_; }
     139
     140            inline virtual std::string getFileEntry() const
     141                { return this->comment_; }
     142
     143        private:
     144            std::string comment_;
     145    };
     146
     147
     148    ///////////////////////
     149    // ConfigFileSection //
     150    ///////////////////////
     151    class _CoreExport ConfigFileSection
     152    {
     153        friend class ConfigFile;
     154
     155        public:
     156            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {}
     157            ~ConfigFileSection();
     158
     159            inline const std::string& getName() const
     160                { return this->name_; }
     161
     162            inline void setValue(const std::string& name, const std::string& value)
     163                { this->getEntry(name)->setValue(value); }
     164            inline const std::string& getValue(const std::string& name)
     165                { return this->getEntry(name)->getValue(); }
     166
     167            inline void setValue(const std::string& name, unsigned int index, const std::string& value)
     168                { this->getEntry(name, index)->setValue(value); }
     169            inline const std::string& getValue(const std::string& name, unsigned int index)
     170                { return this->getEntry(name, index)->getValue(); }
     171
     172            std::string getFileEntry() const;
     173
     174        private:
     175            std::list<ConfigFileEntry*>& getEntries()
     176                { return this->entries_; }
     177            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
     178                { return this->entries_.begin(); }
     179            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
     180                { return this->entries_.end(); }
     181
     182            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index);
     183
     184            inline ConfigFileEntry* getEntry(const std::string& name)
     185                { return (*this->getEntryIterator(name, 0)); }
     186            inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index)
     187                { return (*this->getEntryIterator(name, index)); }
     188
    61189            std::string name_;
    62             std::list<ConfigFileEntry> entries_;
    63     };
    64 
    65     class ConfigFile
    66     {
    67         public:
    68             enum Type
    69             {
    70                 Settings,
    71                 Keybindings
    72             };
    73 
    74         public:
     190            std::string additionalComment_;
     191            std::list<ConfigFileEntry*> entries_;
     192            bool bUpdated_;
     193    };
     194
     195
     196    ////////////////
     197    // ConfigFile //
     198    ////////////////
     199    class _CoreExport ConfigFile
     200    {
     201        public:
     202            inline ConfigFile(const std::string& filename) : filename_(filename), bUpdated_(false) {}
     203            ~ConfigFile();
     204
     205            void load();
     206            void save() const;
     207            void clean();
     208
     209            inline void setValue(const std::string& section, const std::string& name, const std::string& value)
     210                { this->getSection(section)->setValue(name, value); this->save(); }
     211            inline const std::string& getValue(const std::string& section, const std::string& name)
     212                { return this->getSection(section)->getValue(name); this->saveIfUpdated(); }
     213
     214            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value)
     215                { this->getSection(section)->setValue(name, index, value); this->save(); }
     216            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index)
     217                { return this->getSection(section)->getValue(name, index); this->saveIfUpdated(); }
     218
     219        private:
     220            ConfigFileSection* getSection(const std::string& section);
     221            void saveIfUpdated();
     222
     223            std::string filename_;
     224            std::list<ConfigFileSection*> sections_;
     225            bool bUpdated_;
     226    };
     227
     228
     229    ///////////////////////
     230    // ConfigFileManager //
     231    ///////////////////////
     232    class _CoreExport ConfigFileManager
     233    {
     234        public:
     235            static ConfigFileManager* getInstance();
     236
     237            void setFile(ConfigFileType type, const std::string& filename);
     238
    75239            void load();
    76240            void save();
    77 
    78             void addSection(const std::string& name);
    79             ConfigFileSection* getSection(const std::string& name) const;
    80 
    81             void setValue(const std::string& section, const std::string& name, const std::string& value);
    82             const std::string& getValue(const std::string& section, const std::string& name) const;
    83 
    84         private:
    85             std::string name_;
    86             Type type_;
    87             std::list<ConfigFileSection> sections_;
    88     };
    89 
    90     class ConfigFileManager
    91     {
    92         public:
    93             static ConfigFileManater* getInstance();
    94 
    95             void load(const std::string& filename);
    96             void save(const std::string& filename) const;
    97 
    98             void setFile(const std::string& filename);
    99             ConfigFile& getFile(const std::string& filename) const;
    100 
    101             void setValue(const std::string& filename, const std::string& section, const std::string& name, const std::string& value);
    102             const std::string& getValue(const std::string& filename, const std::string& section, const std::string& name) const;
    103 
    104         private:
    105             std::list<ConfigFile> configFiles_;
     241            void clean();
     242
     243            void load(ConfigFileType type);
     244            void save(ConfigFileType type);
     245            void clean(ConfigFileType type);
     246
     247            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value)
     248                { this->getFile(type)->setValue(section, name, value); }
     249            inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name)
     250                { return this->getFile(type)->getValue(section, name); }
     251
     252            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value)
     253                { this->getFile(type)->setValue(section, name, index, value); }
     254            inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index)
     255                { return this->getFile(type)->getValue(section, name, index); }
     256
     257            void updateConfigValues() const;
     258            void updateConfigValues(ConfigFileType type) const;
     259
     260        private:
     261            ConfigFileManager();
     262            ConfigFileManager(const ConfigFileManager& other) {}
     263            ~ConfigFileManager();
     264
     265            ConfigFile* getFile(ConfigFileType type);
     266
     267            std::string getFilePath(const std::string& name) const;
     268
     269            std::map<ConfigFileType, ConfigFile*> configFiles_;
    106270    };
    107271}
  • code/branches/core2/src/util/String.cc

    r994 r1006  
    5353    strip(&output);
    5454    return output;
     55}
     56
     57/**
     58    @brief Returns a copy of a string without trailing whitespaces.
     59    @param str The string
     60    @return The modified copy
     61*/
     62std::string removeTrailingWhitespaces(const std::string& str)
     63{
     64}
     65
     66/**
     67    @brief Returns true if the string contains something like '..."between quotes"...'
     68    @param The string
     69    @return True if there is something between quotes
     70*/
     71bool hasStringBetweenQuotes(const std::string& str)
     72{
     73}
     74
     75/**
     76    @brief If the string contains something like '..."between quotes"...' then 'between quotes' gets returned (without quotes).
     77    @param The string
     78    @param The string between the quotes
     79*/
     80std::string getStringBetweenQuotes(const std::string& str)
     81{
    5582}
    5683
     
    218245
    219246/**
    220  * @brief compares two strings without ignoring the case
    221  * @param s1 first string
    222  * @param s2 second string
    223  */
     247   @brief compares two strings without ignoring the case
     248   @param s1 first string
     249   @param s2 second string
     250*/
    224251int nocaseCmp(const std::string& s1, const std::string& s2)
    225252{
     
    246273
    247274/**
    248  * @brief compares two strings without ignoring the case
    249  * @param s1 first string
    250  * @param s2 second string
    251  * @param len how far from the beginning to start.
    252  */
     275   @brief compares two strings without ignoring the case
     276   @param s1 first string
     277   @param s2 second string
     278   @param len how far from the beginning to start.
     279*/
    253280int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len)
    254281{
     
    270297    return 0;
    271298}
     299
     300/**
     301    @brief Returns true if the string contains a comment, introduced by #, %, ; or //.
     302    @param str The string
     303    @return True if the string contains a comment
     304*/
     305bool hasComment(const std::string& str)
     306{
     307    return (getCommentPosition(str) != std::string::npos);
     308}
     309
     310/**
     311    @brief If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise.
     312    @param str The string
     313    @return The comment
     314*/
     315std::string getComment(const std::string& str)
     316{
     317    return str.substr(getCommentPosition(str));
     318}
     319
     320/**
     321    @brief If the string contains a comment, the position of the comment-symbol gets returned, std::string::npos otherwise.
     322    @param str The string
     323    @return The position
     324*/
     325unsigned int getCommentPosition(const std::string& str)
     326{
     327    for (unsigned int i = 0; i < str.size(); i++)
     328        if (isComment(str.substr(i)))
     329            return i;
     330
     331    return std::string::npos;
     332}
  • code/branches/core2/src/util/String.h

    r994 r1006  
    3434#include "UtilPrereqs.h"
    3535
    36 _UtilExport void        strip(std::string* str);
    37 _UtilExport std::string getStripped(const std::string& str);
     36_UtilExport void         strip(std::string* str);
     37_UtilExport std::string  getStripped(const std::string& str);
    3838
    39 _UtilExport void        stripEnclosingQuotes(std::string* str);
    40 _UtilExport std::string getStrippedEnclosingQuotes(const std::string& str);
     39_UtilExport std::string  removeTrailingWhitespaces(const std::string& str);
    4140
    42 _UtilExport bool        isEmpty(const std::string& str);
    43 _UtilExport bool        isComment(const std::string& str);
    44 _UtilExport bool        isNumeric(const std::string& str);
     41_UtilExport bool         hasStringBetweenQuotes(const std::string& str);
     42_UtilExport std::string  getStringBetweenQuotes(const std::string& str);
    4543
    46 _UtilExport void        lowercase(std::string* str);
    47 _UtilExport std::string getLowercase(const std::string& str);
     44_UtilExport void         stripEnclosingQuotes(std::string* str);
     45_UtilExport std::string  getStrippedEnclosingQuotes(const std::string& str);
    4846
    49 _UtilExport void        uppercase(std::string* str);
    50 _UtilExport std::string getUppercase(const std::string& str);
     47_UtilExport bool         isEmpty(const std::string& str);
     48_UtilExport bool         isComment(const std::string& str);
     49_UtilExport bool         isNumeric(const std::string& str);
    5150
    52 _UtilExport int         nocaseCmp(const std::string& s1, const std::string& s2);
    53 _UtilExport int         nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len);
     51_UtilExport void         lowercase(std::string* str);
     52_UtilExport std::string  getLowercase(const std::string& str);
     53
     54_UtilExport void         uppercase(std::string* str);
     55_UtilExport std::string  getUppercase(const std::string& str);
     56
     57_UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2);
     58_UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len);
     59
     60_UtilExport bool         hasComment(const std::string& str);
     61_UtilExport std::string  getComment(const std::string& str);
     62_UtilExport unsigned int getCommentPosition(const std::string& str);
    5463
    5564//! The Convert class has some static member functions to convert strings to values and values to strings.
Note: See TracChangeset for help on using the changeset viewer.