码迷,mamicode.com
首页 > 其他好文 > 详细

一份采用单例模式编写,可读取配置文件的代码

时间:2015-04-14 16:42:03      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:linux   c++   配置文件   单例模式   

Confaccess.h

#ifndef __CONFACCESS_H__ 
#define __CONFACCESS_H__ 

#include <pthread.h>
#include <stdlib.h>
#include <string>
#include <map>

class CConfAccess
{
    public:
        static CConfAccess* getInstance()
        {
            pthread_once(&once_, &initInstance);
            return pInstance_;
        }

        static void initInstance()
        {
            ::atexit(&destoryInstance);
            pInstance_ = new CConfAccess;
        }

        static void destoryInstance()
        {
            delete pInstance_;
        }

        bool Load(const std::string& filename);

        std::string GetValue(const std::string& strKey);

    private:
        CConfAccess() {};
        CConfAccess(const CConfAccess&);
        void operator=(const CConfAccess&);

        static CConfAccess* pInstance_;
        static pthread_once_t once_;

        std::map<std::string, std::string> mapKeyValue_; 
};


#endif //__CONFACCESS_H__ 



Confaccess.cpp

#include "Confaccess.h"
#include <fstream>

#define MAX_LINE 1024
using namespace std;

pthread_once_t CConfAccess::once_ = PTHREAD_ONCE_INIT;
CConfAccess* CConfAccess::pInstance_ = NULL;

//Trim space in the beginning and ending of the string.
string StrTrim(const string& strOriginal)
{
     static const char* whiteSpace = " \t\r\n";

     if (strOriginal.empty())
     {
        return strOriginal;
     }

     string::size_type uFrontPos = strOriginal.find_first_not_of(whiteSpace);

     if (string::npos == uFrontPos)
     {
        return "";
     }

     string::size_type uRearPos = strOriginal.find_last_not_of(whiteSpace);

     return string(strOriginal, uFrontPos, uRearPos - uFrontPos + 1);
}

bool CConfAccess::Load(const string& filename)
{
    ifstream initFile(filename.c_str());
    if (!initFile)
    {
        return false;
    }

    string strLine;
    while (getline(initFile, strLine))
    {   
        if ("" == StrTrim(strLine))
        {
            continue;
        }

        string::size_type uPos = strLine.find("#");
        if (uPos > 0 || (string::npos == uPos))
        {
            strLine = strLine.substr(0, uPos);
        }
        else
        {
            continue;
        }

        uPos = strLine.find("=");
        if (string::npos == uPos)
        {
            continue;
        }

        string strKey = StrTrim(strLine.substr(0, uPos));
        string strValue = StrTrim(strLine.substr(uPos + 1));
        
        mapKeyValue_[strKey] = strValue;
    }

    return true;
}

string CConfAccess::GetValue(const string& strKey)
{
    if (strKey.empty() || (mapKeyValue_.end() == mapKeyValue_.find(strKey)))
    {
        return "";
    }

    return mapKeyValue_[strKey];
}



一份采用单例模式编写,可读取配置文件的代码

标签:linux   c++   配置文件   单例模式   

原文地址:http://blog.csdn.net/nyist327/article/details/45043215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!