• <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;
            }

            //實現主要采用STL, 實現了ini格式文件的讀,寫,已經打印文件信息等功能,暫時覺得這些功能已經夠用了,以后有需要再進行添加.應該還少了一個宏BUFFER_LEN的定義,這個宏在一個公用的頭文件//中定義,如果你想試用這個類,可以自行進行定義,我默認定義為1024字節大小.


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

            Copyright © RichardHe

            青青久久精品国产免费看 | 久久se这里只有精品| 中文字幕成人精品久久不卡| yellow中文字幕久久网| 香蕉99久久国产综合精品宅男自| 久久久久久噜噜精品免费直播 | 久久久久波多野结衣高潮| 久久亚洲私人国产精品| 久久成人精品| 久久精品欧美日韩精品| 久久国产香蕉一区精品| 久久综合国产乱子伦精品免费| 国产日韩久久久精品影院首页| 色婷婷噜噜久久国产精品12p| 久久精品国产亚洲AV忘忧草18| 国产婷婷成人久久Av免费高清| 久久精品国产亚洲一区二区三区| 亚洲精品蜜桃久久久久久| 99热都是精品久久久久久| 久久精品人妻中文系列| 久久久久久青草大香综合精品| 97久久国产综合精品女不卡| 91精品日韩人妻无码久久不卡| 亚洲精品无码久久久| 国产精品成人久久久久三级午夜电影| 中文字幕久久久久人妻| 国产日产久久高清欧美一区| 久久精品成人欧美大片| 久久免费99精品国产自在现线 | 色综合久久久久综合体桃花网| 久久精品成人欧美大片| 激情久久久久久久久久| 久久综合欧美成人| 国产精品美女久久久久| 久久精品亚洲中文字幕无码麻豆| 偷窥少妇久久久久久久久| 伊人色综合久久天天网| 午夜福利91久久福利| 亚洲国产精品嫩草影院久久| 久久精品国产99久久丝袜| 久久精品国产第一区二区|