RSS Feed for This PostCurrent Article

Unix: A C++ Configuration Reader

Download Code

This a C++ configuration reader I wrote long time back to read a properties file. Some one asked me for some code recently. So I posted the source code here.

You can see the functions from the header file.

class ConfigReader : 
        public std::map<std::string, std::string> {
        public:
                // Constructor
                ConfigReader(const std::string& initFileName);

                // Destructor
                ~ConfigReader();

                // static methods
                static ConfigReader* getInstance
                 (const std::string& initFileName = 
                  CONFIG_FILE_NAME) ;
                
                void refresh() ;
                void load(const std::string& initFileName) 
                      throw (ConfigureFailure);
                void save(const std::string& outputFileName) 
                      throw (ConfigureFailure);

                int getInt(const std::string& property, 
                        int defaultValue = 0);
                bool getBool(const std::string& property,
                       bool defaultValue = false);
                std::string getString(const std::string& property,
                           const char* defaultValue = "");

        protected:
                // Singleton object
                static ConfigReader* _instance ;

                #if defined(__MULTITHREAD__)
                        static pthread_mutex_t _lock ;
                #endif
};

It is a Singleton class and make use of the Standard Template Library.


Trackback URL


RSS Feed for This Post3 Comment(s)

  1. Alastair Taylor | Apr 15, 2008 | Reply

    Works nicely, thanks.

    Added a spaces trim to allow ‘property = value’, ‘property=value’ etc..

    if(length != std::string::npos)
    {
    leftSide = command.substr(0, length);
    rightSide = command.substr(length + 1, command.size() – length);

    /* remove trailing spaces */
    /* remove leading spaces */
    /* allows for property = stuff, property=stuff etc…. */
    TrimSpaces(leftSide);
    TrimSpaces(rightSide);
    }

  2. Alastair Taylor | Apr 15, 2008 | Reply

    void TrimSpaces(string &str)
    {
    size_t startpos = str.find_first_not_of(” \t”); // Find the first character position after excluding leading blank spaces
    size_t endpos = str.find_last_not_of(” \t”); // Find the first character position from reverse af

    // if all spaces or empty return an empty string
    if((string::npos == startpos) || (string::npos == endpos))
    {
    str = “”;
    }
    else
    {
    str = str.substr(startpos, endpos – startpos + 1);
    }
    }

  3. Benjamin Jackson | Nov 11, 2008 | Reply

    That you much for posting this. I found it a great starting point and it reduced the work of getting a property reader up and running by a lot.

    Also thanks for the previous comment.

    I changed it up just a bit to allow multiple files to
    reside in the program at once:


    static std::map instances ;

    ConfigReader* ConfigReader::getInstance(const std::string& initFileName) {

    if (instances.find(initFileName) == instances.end())

    instances[initFileName] = new ConfigReader(initFileName);

    return instances[initFileName] ;
    }

RSS Feed for This PostPost a Comment

CAPTCHA Image
Refresh Image
*