• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            #ifndef __CONFIG_H__
            #define __CONFIG_H__

            #include 
            <string>
            #include 
            <map>

            using namespace std;

            class CConfig
            {
            public:
                CConfig();
                CConfig(
            const char* pFileName);
                CConfig(
            const string& strFileName);
                
            ~CConfig();

                
            int Init();
                
            int Init(const char* pFileName);
                
            int Init(const string& strFileName);
                
            int Dump();

                
            int ReadItem(const string& strSection, const string& strKey, string& strValue);
                
            int WriteItem(const string& strSection, const string& strKey, const string& strValue);

            private:
                
            int LoadFile();
                
            int WriteFile();

                
            int ReadLine(FILE* pFile, string& strLine);
                
            int TrimString(string& strToken);
            private:
                
            string m_strFileName;

                typedef map
            <stringstring> ConfigType;
                map
            <string, ConfigType> m_mSec2Config;
            };

            #endif /* __CONFIG_H__ */
            /********************************************************************
                created:    2008/07/28
                filename:     config.h
                author:        Lichuang
                            
                purpose:    封裝讀取ini格式的配置文件操作
            ********************************************************************
            */

            #include 
            "config.h"
            #include 
            "comdef.h"
            #include 
            <stdio.h>
            #include 
            <iostream>

            CConfig::CConfig()
            {
            }

            CConfig::CConfig(
            const char* pFile)
                : m_strFileName(pFile)
            {
            }

            CConfig::CConfig(
            const string& strFile)
                : m_strFileName(strFile)
            {
            }

            CConfig::
            ~CConfig()
            {
            }

            int CConfig::Init(const char* pFileName)
            {
                m_strFileName 
            = pFileName;
                
            return LoadFile();
            }

            int CConfig::Init(const string& strFileName)
            {
                m_strFileName 
            = strFileName;
                
            return LoadFile();
            }
            int CConfig::Init()
            {
                
            return LoadFile();
            }

            int CConfig::Dump()
            {
                map
            <string, ConfigType>::iterator tSecIter1 = m_mSec2Config.begin(), tSecIter2 = m_mSec2Config.end();
                ConfigType::iterator tConfigTypeIter1, tConfigTypeIter2;

                
            while (tSecIter1 != tSecIter2)
                {
                    cout 
            << "[" << tSecIter1->first << "]" << endl;
                    tConfigTypeIter1 
            = tSecIter1->second.begin(), tConfigTypeIter2 = tSecIter1->second.end();
                    
            while (tConfigTypeIter1 != tConfigTypeIter2)
                    {
                        cout 
            << tConfigTypeIter1->first << "=" << tConfigTypeIter1->second << endl;

                        
            ++tConfigTypeIter1;
                    }
                    cout 
            << endl;
                    
            ++tSecIter1;
                }

                
            return 0;
            }

            int CConfig::ReadItem(const string& strSection, const string& strKey, string& strValue)
            {
                
            if (!m_mSec2Config.count(strSection))
                {
                    
            return -1;
                }

                ConfigType
            & tConfigType = m_mSec2Config[strSection];
                strValue 
            = tConfigType[strKey];

                
            return (strValue.empty()) ? -1 : 0;
            }

            int CConfig::WriteItem(const string& strSection, const string& strKey, const string& strValue)
            {
                ConfigType
            & tConfigType = m_mSec2Config[strSection];
                
            if (tConfigType.count(strKey))
                {
                    
            return -1;
                }
                tConfigType[strKey] 
            = strValue;

                
            return WriteFile();
            }

            int CConfig::LoadFile()
            {
                FILE
            * pFile;

                
            if (NULL == (pFile = ::fopen(m_strFileName.c_str(), "r")))
                {
                    
            return -1;
                }

                
            string strLine, strSection;
                
            string strKey, strValue;
                size_t nPos, nEndPos;
                ConfigType tConfigType;
                
            while (0 == ReadLine(pFile, strLine))
                {
                    
            if (string::npos != (nPos = strLine.find_first_of("[")))
                    {
                        
            if (string::npos == (nEndPos = strLine.find_first_of("]")))
                        {
                            ::fclose(pFile);
                            
            return -1;
                        }

                        strSection 
            = strLine.substr(nPos + 1, nEndPos - nPos - 1);
                        
            if (0 > TrimString(strSection))
                        {
                            ::fclose(pFile);
                            
            return -1;
                        }
                    }
                    
            else if (string::npos != (nPos = strLine.find_first_of("=")))
                    {
                        strKey 
            = strLine.substr(0, nPos);
                        strValue 
            = strLine.substr(nPos + 1);

                        
            if (0 > TrimString(strKey) || 0 > TrimString(strValue) || strSection.empty())
                        {
                            ::fclose(pFile);
                            
            return -1;
                        }

                        m_mSec2Config[strSection][strKey] 
            = strValue;
                    }
                } 

                
            return ::fclose(pFile);
            }

            int CConfig::WriteFile()
            {
                FILE
            * pFile;

                
            if (NULL == (pFile = ::fopen(m_strFileName.c_str(), "w")))
                {
                    
            return -1;
                }

                map
            <string, ConfigType>::iterator tSecIter1 = m_mSec2Config.begin(), tSecIter2 = m_mSec2Config.end();
                ConfigType::iterator tConfigTypeIter1, tConfigTypeIter2;

                
            string strSection, strConfig;
                
            while (tSecIter1 != tSecIter2)
                {
                    strSection 
            = string("["+ tSecIter1->first + string("]\n");
                    ::fwrite(strSection.c_str(), 
            sizeof(char), strSection.length(), pFile);
                    tConfigTypeIter1 
            = tSecIter1->second.begin(), tConfigTypeIter2 = tSecIter1->second.end();
                    
            while (tConfigTypeIter1 != tConfigTypeIter2)
                    {
                        strConfig 
            = tConfigTypeIter1->first + string("="+ tConfigTypeIter1->second + string("\n");
                        ::fwrite(strConfig.c_str(), 
            sizeof(char), strConfig.length(), pFile);

                        
            ++tConfigTypeIter1;
                    }
                    ::fwrite(
            "\n"sizeof(char), 1, pFile);
                    
            ++tSecIter1;
                }

                
            return ::fclose(pFile);
            }

            int CConfig::ReadLine(FILE* pFile, string& strLine)
            {
                
            char szBuff[BUFFER_LEN];
                
            int nLen;

                
            do 
                {
                    
            if (NULL == ::fgets(szBuff, BUFFER_LEN, pFile))
                    {
                        
            return -1;
                    }
                    
            if (0 < (nLen = ::strlen(szBuff)))
                    {
                        
            break;
                    }
                } 
            while (true);

                szBuff[nLen 
            - 1= '\0';

                strLine 
            = szBuff;

                
            return 0;
            }

            int CConfig::TrimString(string& strToken)
            {
                
            if (strToken.empty())
                {
                    
            return -1;
                }

                size_t nPos    
            = strToken.find_first_not_of(" \t");
                size_t nEndPos 
            = strToken.find_last_not_of(" \t");

                strToken 
            = strToken.substr(nPos, nEndPos - nPos + 1);

                
            return (strToken.empty()) ? -1 : 0;
            }

            //實(shí)現(xiàn)主要采用STL, 實(shí)現(xiàn)了ini格式文件的讀,寫(xiě),已經(jīng)打印文件信息等功能,暫時(shí)覺(jué)得這些功能已經(jīng)夠用了,以后有需要再進(jìn)行添加.應(yīng)該還少了一個(gè)宏BUFFER_LEN的定義,這個(gè)宏在一個(gè)公用的頭文件//中定義,如果你想試用這個(gè)類(lèi),可以自行進(jìn)行定義,我默認(rèn)定義為1024字節(jié)大小.


            posts - 94, comments - 138, trackbacks - 0, articles - 94

            Copyright © RichardHe

            亚洲人成精品久久久久| 久久精品国产2020| 久久婷婷五月综合国产尤物app| 久久综合给合久久狠狠狠97色| 国产一区二区精品久久凹凸| 久久久噜噜噜www成人网| 四虎影视久久久免费| 中文字幕亚洲综合久久2| 久久Av无码精品人妻系列| 九九精品久久久久久噜噜| 欧美粉嫩小泬久久久久久久 | 色综合久久中文综合网| 人妻丰满AV无码久久不卡| 久久国产劲爆AV内射—百度| 久久久高清免费视频| 日本加勒比久久精品| 久久久久无码专区亚洲av| 久久久久久A亚洲欧洲AV冫| 国产毛片久久久久久国产毛片 | 精品午夜久久福利大片| 国产V亚洲V天堂无码久久久| 久久久精品国产sm调教网站| 国产精品久久久久天天影视| 久久最近最新中文字幕大全| 九九久久精品国产| 久久免费视频6| 狠狠色狠狠色综合久久| 久久夜色精品国产欧美乱| 精品久久无码中文字幕| 成人精品一区二区久久久| 欧美久久综合九色综合| 亚洲欧美成人综合久久久 | 国产一久久香蕉国产线看观看| 久久精品视频免费| 欧美亚洲日本久久精品| 亚洲成色www久久网站夜月| 久久精品成人免费网站| 性做久久久久久久久| 久久精品夜夜夜夜夜久久| 国产福利电影一区二区三区久久老子无码午夜伦不| 久久99国产精品成人欧美|