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

            STL容器實現IniFileParser

            IniFileParser.h

            /*************************************************************************/
            /* FileName:IniFileParser.cpp                                                                                                   */
            /* Describe:IniFile@read、write                                                                                               */
            /* Author  :Kagayaku                                                                                                               */
            /* Date    :3.22.2010                                                                                                                */
            /************************************************************************/

            #ifndef _INIFILEPARSER_H_
            #define _INIFILEPARSER_H_

            #include <string>
            #include <vector>
            using namespace std;

             

            struct CIniEntry
            {
             CIniEntry(){}
             CIniEntry(char *szName,char *szValue):m_strIEName(szName),m_strIEValue(szValue){}
             string m_strIEName;
             string m_strIEValue;
            };

            struct CIniComment
            {
             CIniComment(){}
             CIniComment(char *szIC):m_strIC(szIC){}
             string m_strIC;
            };

            struct CIniSection
            {
             vector<CIniEntry>     m_vecIE;
             vector<CIniComment>   m_vecIC;
             string                m_strISName;
            };


            class CIniFile
            {
            public:
             CIniFile(const char *szIniFileFullPath);
             ~CIniFile();
             bool ReadIniFile(const char *szinifile);
             bool WriteIniFile(const char *szinifile);
             void TrimIniFile(char* &szinifile) const;
             void RemoveComment(char *szinifile) const;
             bool SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname);
             bool SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue);
               
            private:
             vector<CIniSection> m_vecIS;
             string              m_strBufIEValue;
             string              m_strIniFileName;


            };
            #endif

            IniFileParser.cpp

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

            CIniFile::CIniFile(const char *szIniFileFullPath):m_strIniFileName(szIniFileFullPath)
            {
             ReadIniFile(szIniFileFullPath);
            }

            CIniFile::~CIniFile()
            {

            }

            bool CIniFile::ReadIniFile(const char *szinifile)
            {
             if (NULL==szinifile)
             {
              return false;
             }
             ifstream inifile(szinifile);
             if (NULL==inifile)
             {
              return false;
             }

             const int MAX_ROW_LENGTH=200;
             char chLineBufArray[MAX_ROW_LENGTH]={0};
             while(inifile.getline(chLineBufArray,MAX_ROW_LENGTH))
             {
              char *p=chLineBufArray;
              TrimIniFile(p);

              if (*p=='[')
              {
               RemoveComment(p);
               char *pEnd=strchr(p,']');
               if (NULL==pEnd || pEnd==p+1)
               {
                continue;
               }
               *pEnd = 0;
               CIniSection is;
               is.m_strISName=string(p+1);
               m_vecIS.push_back(is);
               continue;
              }

              if (m_vecIS.size()<1)
              {
               continue;
              }
              

              if (*p==';')
              {
               if (NULL==*(p+1))
               {
                continue;
               }
               else
               {
                CIniComment ic(p+1);
                m_vecIS[m_vecIS.size()-1].m_vecIC.push_back(ic);
                continue;
               }
               
              }
              
              char *pFlag=strchr(p,'=');
              if (NULL==pFlag || pFlag==p || NULL==*(pFlag+1))
              {
               continue;
              }
              else
              {
               *pFlag=0;
               CIniEntry ie;
               ie.m_strIEName=string(p);
               ie.m_strIEValue=string(pFlag+1);
               m_vecIS[m_vecIS.size()-1].m_vecIE.push_back(ie);
               continue;

              }

             }
             inifile.close();
             return true;

            }

            bool CIniFile::WriteIniFile(const char *szinifile)
            {
             if (NULL==szinifile || m_strIniFileName!=szinifile)
             {
              return false;
             }

             ofstream inifile(szinifile);
             if (NULL==inifile)
             {
              return false;
             }

             for (int i=0;i!=m_vecIS.size();++i)
             {
              inifile.write("[",1);
              inifile.write(m_vecIS[i].m_strISName.c_str(),m_vecIS[i].m_strISName.size());
              inifile.write("]",1);
              inifile << endl;
              for (int j=0;j!=m_vecIS[i].m_vecIE.size();++j)
              {
               inifile.write(m_vecIS[i].m_vecIE[j].m_strIEName.c_str(),m_vecIS[i].m_vecIE[j].m_strIEName.size());
               inifile.write("=",1);
               inifile.write(m_vecIS[i].m_vecIE[j].m_strIEValue.c_str(),m_vecIS[i].m_vecIE[j].m_strIEValue.size());
               inifile << endl;
              }
             }
             inifile.close();
             return true;
            }

            void CIniFile::TrimIniFile(char* &szinifile) const
            {
             if (NULL==szinifile)
             {
              return;
             }

             char *p=szinifile;

             while(*p==' ' || *p=='\t')
             {
              ++p;
             }

             szinifile=p;
             p=szinifile+strlen(szinifile)-1;

             while(*p==' ' || *p=='\t'|| *p=='\r'|| *p=='\n')
             {
              *p=0;
              --p;
             }

            }

            void CIniFile::RemoveComment(char *szinifile) const
            {
             if (NULL==szinifile)
             {
              return;
             }
             char *p=strchr(szinifile,';');
             *p = 0;

            }

            bool CIniFile::SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname)
            {
             if (NULL==szinifile || NULL==szsectionname || NULL==szentryname)
             {
              return false;
             }

             if (m_strIniFileName!=szinifile)
             {
              return false;
             }

                bool temp=false;

             for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
             {
              if ((*iterIS).m_strISName==szsectionname)
              {
               for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
               {
                if ((*iterIE).m_strIEName==szentryname)
                {
                 m_strBufIEValue=(*iterIE).m_strIEValue;
                 temp=true;
                }
               }
              }
             }
             return temp;

            }

            bool CIniFile::SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue)
            {
             if (NULL==szinifile || NULL==szsectionname || NULL==szentryname|| NULL==szentryvalue)
             {
              return false;
             }

             if (m_strIniFileName!=szinifile)
             {
              return false;
             }

             bool temp=false;

             for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
             {
              if ((*iterIS).m_strISName==szsectionname)
              {
               for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
               {
                if ((*iterIE).m_strIEName==szentryname)
                {
                 (*iterIE).m_strIEValue=szentryvalue;
                 temp=true;
                }
               }
              }
             }
               
             
             return temp?WriteIniFile(szinifile):false;

            }

            posted on 2010-03-22 23:01 avatar 閱讀(1799) 評論(5)  編輯 收藏 引用

            評論

            # re: STL容器實現IniFileParser 2010-03-23 11:23 陳梓瀚(vczh)

            http://www.shnenglu.com/vczh/archive/2010/03/07/109103.html  回復  更多評論   

            # re: STL容器實現IniFileParser 2010-03-23 20:03 萌萌

            你不寫提要 怎么看啊  回復  更多評論   

            # re: STL容器實現IniFileParser 2010-03-29 16:29 淡月清風

            哇,完全沒有注釋  回復  更多評論   

            # re: STL容器實現IniFileParser 2010-04-08 12:12 jmchxy

            #ifndef __JFILECONFIG_H__
            #define __JFILECONFIG_H__
            /////////////////////////////////////////////
            /// JFileConfig ini文件操作類
            /////////////////////////////////////////////
            #include "jconfig.h"
            #include <map>
            #include <vector>

            namespace jlib
            {

            // ini 文件中名字不分大小寫
            struct SectionLess
            {
            bool operator() (const JString& Key1, const JString& Key2)const
            {
            return Key1.compareNoCase(Key2) < 0;
            }
            };
            //-------------------------------------------
            // 定義foreach 函數需要使用的函數類型
            // pArg 為傳遞給 函數的數據
            //-------------------------------------------
            typedef bool (*FOREACHFUNC)(const JString& section, const JString& name, JConfigVal& value, LPVOID pArg);
            class JLIBAPI JFileConfig: JConfigBase
            {
            public:
            // 構造析構函數
            JFileConfig();
            virtual ~JFileConfig();
            public:
            // 裝載配置信息, 從文件或其他媒介
            virtual bool load(LPCTSTR pszFilename);
            // 保存配置到文件
            virtual bool save(LPCTSTR pszFilename)const;
            // 獲取配置信息, key, name, 獲得value
            // 讀取失敗返回 false
            // 獲取通用類型的值
            virtual bool getValue(LPCTSTR sectionName, LPCTSTR name, JConfigVal& jRval)const;
            // 獲取整數值
            virtual bool getInt(LPCTSTR sectionName, LPCTSTR name, int& iRval)const;
            // 獲取字符串
            virtual bool getString(LPCTSTR sectionName, LPCTSTR name, JString& szRval)const;
            // 設置配置, 如果沒有則創建指定名/值
            // 設置通用類型的值
            virtual bool setValue(LPCTSTR sectionName, LPCTSTR name, const JConfigVal& jVal);
            // 設置整型的值
            virtual bool setInt(LPCTSTR sectionName, LPCTSTR name, int iVal);
            // 設置字符串值, 如果沒有則創建指定名/值
            virtual bool setString(LPCTSTR sectionName, LPCTSTR name, LPCTSTR szVal);
            //--------------------------------------------------
            // 遍歷函數
            // 如果用戶定義的遍歷函數返回了 false, 結束遍歷
            bool foreach(FOREACHFUNC fpForeach, LPVOID pArgs);
            //========================================
            #ifdef _UNIT_TEST_ //單元測試, 遍歷一個ini文件
            static void unitTest(LPCTSTR filename);
            #endif //_UNIT_TEST_
            //========================================
            public:
            /////////////////////////////////////////////
            /// 配置節信息
            /////////////////////////////////////////////
            typedef std::map<JString, JConfigVal, SectionLess> Dict;
            typedef std::map<JString, JConfigVal, SectionLess>::iterator DictIterator;
            typedef std::map<JString, JConfigVal, SectionLess>::const_iterator ConstDictIterator;
            // 結點信息
            struct JConfigSection
            {
            JString m_strSectionName; //節名
            Dict m_Items; //名/值對
            public:
            JConfigSection(): m_strSectionName(), m_Items() { }
            ~JConfigSection(){ m_Items.clear(); }
            // 加入一個 名/值 對
            bool set(const JString& name, const JConfigVal& value)
            {
            // 查找內容
            DictIterator iter = m_Items.find( name );
            if(iter == m_Items.end())
            {
            // 沒找到, 插入
            m_Items.insert( Dict::value_type( name, value));
            }
            // 如果已存在, 修改
            m_Items[name] = value;
            return true;
            }
            bool get(JString& name, JConfigVal& value)const
            {
            // 查找內容
            ConstDictIterator iter = m_Items.find( name );
            if(iter == m_Items.end())
            {
            // 沒找到,
            return false;
            }
            // 如果已存在, 返回
            value = iter->second;
            return true;
            }
            };
            private:
            //
            typedef std::vector<JConfigSection>::iterator SectionIter;
            typedef std::vector<JConfigSection>::const_iterator ConstSectionIter;
            // 創建一個節
            bool createSection(const JString& sectionName);
            // 查找節, 返回對應的索引
            // 如果不存在, 返回 end(),
            ConstSectionIter findSection(const JString& sectionName)const;
            SectionIter findSection(const JString& sectionName);
            // const static int MAX_SECTION = 10; //最多的節數
            private:
            std::vector<JConfigSection> m_Sections;
            // 不允許拷貝對象
            DECLARE_NO_COPY_CLASS(JFileConfig);
            };

            } //end namespace

            #endif //__JFILECONFIG_H__  回復  更多評論   

            # re: STL容器實現IniFileParser 2010-04-08 12:17 jmchxy

            //===============================================
            bool JFileConfig::getValue(LPCTSTR sectionName, LPCTSTR name, JConfigVal& rval)const
            {
            // 查找節
            JString jstrSection(sectionName);
            JString jstrName(name);
            bool bRet = false;
            // 查看是否已經存在
            ConstSectionIter iter = findSection( jstrSection );
            // 存在節
            if(iter == m_Sections.end())
            {
            return false;
            }
            return iter->get(jstrName, rval);
            }

            /////////////////////////////////////////////
            // 裝載配置信息, 從文件或其他媒介
            /////////////////////////////////////////////
            #ifdef _DEBUG
            #define _OUTPUT_STATE
            #endif
            bool JFileConfig::load(LPCTSTR pszFilename)
            {
            JStdioFile inifile(pszFilename, JFileBase::modeRead);
            JString CurSection;

            while(!inifile.eof())
            {
            //逐行分析數據
            JString line = inifile.getline();
            // 去掉末尾的行計數符
            line.chomp();
            if(line.length() == 0)
            {
            continue; //跳過空行
            }
            // 如果是注釋行, 跳過
            // ; 和 # 開始的行是注釋
            if( (line[0] == _T(';'))||(line[0] == _T('#')))
            {
            continue;
            }
            if(line[0] == _T('['))
            {
            //是節名?
            int i = line.find( _T(']') );
            if(i > 0)
            {// 節名, 取出節的名字, 創建節
            CurSection = line.substr(1, i-1);
            createSection(CurSection);
            #ifdef _OUTPUT_STATE //調試用
            _tprintf( _T("create seciton: %s\n"), CurSection.c_str());
            #endif
            }
            continue; //錯誤的行直接跳過
            }
            else
            { // 名/值 對定義的行, 查找 '=' 字符
            int i = line.find( _T('='));
            if(i <= 0)
            {
            continue; //沒有 = 字符, 跳過
            }
            JString strName = line.substr(0, i);
            strName.TrimRight();
            JString strValue = line.substr(i + 1, -1);
            strValue.TrimLeft();
            // 插入值
            setString(CurSection, strName, strValue);
            }
            }
            return true;
            }

            //-------------------------------------
            // 保存配置到文件
            //-------------------------------------
            bool JFileConfig::save(LPCTSTR pszFilename)const
            {
            JFile inifile(pszFilename, JFileBase::modeWrite|JFileBase::modeCreate);
            JString strLine(128);

            for(ConstSectionIter sectIter = m_Sections.begin();
            sectIter != m_Sections.end();
            ++sectIter)
            {
            // 當前節的名字
            const JString& sectionNmae = sectIter->m_strSectionName;
            // 寫入節名
            strLine.clear();
            strLine += _T("[");
            strLine += sectionNmae;
            strLine += _T("]\r\n");
            inifile.write(strLine.c_str(), strLine.length());
            // 當前節對應的map
            const Dict& CurSection = sectIter->m_Items;
            // 遍歷名字/值
            ConstDictIterator dictIter = CurSection.begin();
            while(dictIter != CurSection.end())
            {
            const JString& name = dictIter->first;
            const JConfigVal& value = dictIter->second;
            strLine.clear();
            strLine += name;
            strLine += _T("=");
            strLine += value.toString();
            strLine += _T("\r\n");
            inifile.write(strLine.c_str(), strLine.length());
            ++dictIter;
            }
            }
            return true;
            }

            回復不能太長, 這是我的庫中定義的 inifile 處理類  回復  更多評論   

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日本免费一区二区久久人人澡| 亚洲AV无码久久精品蜜桃| 91久久香蕉国产熟女线看| 精品久久久久久国产三级| 欧美亚洲国产精品久久| 97久久香蕉国产线看观看| 深夜久久AAAAA级毛片免费看 | 欧美日韩中文字幕久久久不卡| 精品久久久无码人妻中文字幕| 久久r热这里有精品视频| 婷婷国产天堂久久综合五月| 久久精品国产99国产精品澳门 | 激情五月综合综合久久69| 波多野结衣久久| 久久国产视频网| 国产精品久久毛片完整版| 97精品依人久久久大香线蕉97| 久久www免费人成看国产片| 久久99精品久久久久久动态图| yy6080久久| 中文字幕精品久久久久人妻| 国产精品成人99久久久久| 国产精品99精品久久免费| 国产69精品久久久久APP下载| 99久久亚洲综合精品成人| 国产精品99久久免费观看| 欧美午夜精品久久久久免费视| 亚洲精品午夜国产va久久| 久久有码中文字幕| 精品久久久久久无码中文字幕| 精品国产91久久久久久久| 国产一久久香蕉国产线看观看| 亚洲国产精品18久久久久久| 精品久久久无码21p发布| 香蕉久久久久久狠狠色| 国产亚洲精品久久久久秋霞| 亚洲午夜精品久久久久久app| 一本大道久久东京热无码AV | 久久久久亚洲av毛片大| 久久国产福利免费| 久久久久一本毛久久久|