首页 > 学院 > 开发设计 > 正文

文章标题

2019-11-08 20:04:12
字体:
来源:转载
供稿:网友

IniFile.h /*****************************************************************//** /file inifile_ansi.h /brief declaration of the CIniFile class. To manipulate ini file by the means of reading all contents to memory first. This ensures fast search for key-value pair. It uses limited STL feature(stream Operations absent yet). It supports ANSI only. *********************************************************************/

ifndef INIFILE_H

define INIFILE_H

include

// #include seems not support under V30’s ARM compiler (under WinCE)

include

include

include

include

include

include

include // you can’t pass DisplayPC’s compiling until you add this!

include

using namespace std;

include

include

undef MX_PTH

define MX_PTH 512 // 1024

define MX_RES 2048 // 4096

define SZ_EMPTY “”

typedef struct tagStSect { string sSect; map

endif // #define INIFILE_H

iniFile.cpp /*****************************************************************//** /file inifile_ansi.cpp /brief implementation of the CIniFile class iniFile.cpp *********************************************************************/

include “stdafx.h”

include “IniFile.h”

///////////////////////////////////////////////////////////////////// // Construction/Destruction /////////////////////////////////////////////////////////////////////

CIniFile::CIniFile() { }

CIniFile::~CIniFile() { Release(); }

void CIniFile::Release() { m_setScts.clear(); }

///////////////////////////////////////////////////////////////////// // Public Functions /////////////////////////////////////////////////////////////////////

BOOL CIniFile::ReadFile(LPCSTR lpszPath,BOOL bKeyCaseSnstv/=TRUE/ ,BOOL bAppendData/=FALSE/) { // open file for read FILE* pf = NULL; pf = fopen(lpszPath,”r”); if(pf==NULL) { AfxMessageBox(_T(“File %s couldn’t be opened for read”)); return FALSE; }

// do nothing for empty fileINT n_pos = 0;fseek(pf,0,SEEK_END);n_pos = ftell(pf);if(n_pos==0){ fclose(pf); return FALSE;}fseek(pf,0,SEEK_SET);char p_line[MX_PTH] = {0};string s_line;string s_key;string s_value;// read all file contents by lines, load in our dataif(!bAppendData){ Release(); //lint !e1551}while(fgets(p_line,MX_PTH-1,pf)){ s_line = p_line; // trim '/n' at tail if(s_line.length()>1&&s_line[s_line.length()-1]=='/n') { s_line.erase(s_line.length()-1,1); } // trim '/r' at tail if(s_line.length()>1&&s_line[s_line.length()-1]=='/r') { s_line.erase(s_line.length()-1,1); } // trim white space at head n_pos = s_line.find_first_not_of(' '); if(n_pos!=string::npos&&n_pos!=0) { s_line.erase(0,n_pos); } // trim comments at tail n_pos = s_line.find("//"); if(n_pos!=string::npos) { s_line.erase(n_pos); } // trim white space at tail n_pos = s_line.find_last_not_of(' '); if(n_pos!=string::npos&&n_pos!=s_line.length()-1) { s_line.erase(n_pos+1); } // ignore this line if it can't be a valid section or key-value line if(s_line.length()<2) // e.g. "[X]", "Y=" { continue; } if(s_line[0]=='['&&s_line[s_line.length()-1]==']') { // this could be a section line, trim the '[' and ']' s_line.erase(0,1); s_line.erase(s_line.length()-1,1); // trim white space at head n_pos = s_line.find_first_not_of(' '); if(n_pos!=string::npos&&n_pos!=0) { s_line.erase(0,n_pos); } // trim white space at tail n_pos = s_line.find_last_not_of(' '); if(n_pos!=string::npos&&n_pos!=s_line.length()-1) { s_line.erase(n_pos+1); } if(s_line.empty()) { continue; } // find section_KVs_map if exist, or create it otherwise StSect st_sect(s_line); it = m_setScts.find(st_sect); if(it==m_setScts.end()) { m_setScts.insert(st_sect); it = m_setScts.find(st_sect); } }

// else if(isalpha(s_line[0])&&(n_pos=s_line.find(_T(‘=’)))!=-1) else if((n_pos=s_line.find(_T(‘=’)))!=-1) { // this could be a key-value pair line

// if no section line above this line, just ignore it if(it==m_setScts.end()) { continue; } // retrieve key & value s_key = s_line; s_key.erase(n_pos); s_value = s_line; s_value.erase(0,n_pos+1); // trim white space at tail for key n_pos = s_key.find_last_not_of(' '); if(n_pos!=string::npos&&n_pos!=s_key.length()-1) { s_key.erase(n_pos+1); } if(!bKeyCaseSnstv) { transform(s_key.begin(),s_key.end(),s_key.begin(),tolower); } // trim white space at head for value n_pos = s_value.find_first_not_of(' '); if(n_pos!=string::npos&&n_pos!=0) { s_value.erase(0,n_pos); } // add key-value pair, or cover the value if key already existed it_kv = (it->mapKv).find(s_key); if(it_kv!=(it->mapKv).end()) { (it->mapKv).erase(it_kv); } (it->mapKv).insert(pair<string,string>(s_key,s_value)); } // else ignore other lines, maybe they are comments or something weird }fclose(pf);return TRUE;

}

BOOL CIniFile::WriteFile(LPCSTR lpszPath) { // create brand-new file for write FILE* pf = NULL; pf = fopen(lpszPath,”w”); if(pf==NULL) { AfxMessageBox(_T(“File %s couldn’t be created for write”)); return FALSE; }

// dump all the ini data to a string -- s_allchar c_tmp = 0;string s_all;for(it=m_setScts.begin();it!=m_setScts.end();it++){ // section line s_all = s_all + '[' + it->sSect + "]/r/n"; for(it_kv=(it->mapKv).begin();it_kv!=(it->mapKv).end();it_kv++) { // key-value pair line s_all = s_all + it_kv->first + '=' + it_kv->second + "/r/n"; } s_all += "/r/n";}// write filefwrite(s_all.c_str(),1,s_all.length(),pf);fclose(pf);return TRUE;

}

LPCSTR CIniFile::GetValue(LPCSTR lpszSect,LPCSTR lpszKey , LPCSTR lpszDefault/=SZ_EMPTY/ , BOOL bKeyCaseSnstv/=TRUE/) { it = m_setScts.find(StSect(lpszSect)); if(it!=m_setScts.end()) { string s_key(lpszKey); if(!bKeyCaseSnstv) { transform(s_key.begin(),s_key.end(),s_key.begin(),tolower); }

it_kv = (it->mapKv).find(s_key); if(it_kv==(it->mapKv).end()) { return lpszDefault; } else { return (it_kv->second).c_str(); }}return lpszDefault;

}

INT CIniFile::GetValueI(LPCSTR lpszSect,LPCSTR lpszKey ,INT const nDefault/=0/,BOOL bKeyCaseSnstv/=TRUE/) { LPCSTR lpsz = GetValue(lpszSect,lpszKey,SZ_EMPTY,bKeyCaseSnstv); if(strlen(lpsz)!=0) { return atoi(lpsz); }

return nDefault;

}

FLOAT CIniFile::GetValueF(LPCSTR lpszSect,LPCSTR lpszKey ,FLOAT const fDefault/=.0f/ ,BOOL bKeyCaseSnstv/=TRUE/) { LPCSTR lpsz = GetValue(lpszSect,lpszKey,SZ_EMPTY,bKeyCaseSnstv); if(strcmp(lpsz,SZ_EMPTY)!=0) { FLOAT f_ret = .0f; sscanf(lpsz,”%f”,&f_ret); return f_ret; }

return fDefault;

}

BOOL CIniFile::SetValue(LPCSTR lpszSect, LPCSTR lpszKey, LPCSTR lpszValue , BOOL bCreate/=TRUE/, BOOL bKeyCaseSnstv/=TRUE/) { // locate to specified section, create new section if it isn’t existed StSect st_sect(lpszSect); it = m_setScts.find(st_sect); if(it==m_setScts.end()) { if(bCreate) { m_setScts.insert(st_sect); it = m_setScts.find(st_sect); } else { // user does not want to create new key-value pair return FALSE; } }

// set key-value pairstring s_key(lpszKey);if(!bKeyCaseSnstv){ transform(s_key.begin(),s_key.end(),s_key.begin(),tolower);}it_kv = (it->mapKv).find(s_key);if(it_kv==(it->mapKv).end()){ if(bCreate) { (it->mapKv).insert(pair<string,string>(s_key,lpszValue)); } else { return FALSE; }}else{ (it->mapKv).erase(it_kv); (it->mapKv).insert(pair<string,string>(s_key,lpszValue));}return TRUE;

}

BOOL CIniFile::SetValueI(LPCSTR lpszSect, LPCSTR lpszKey, INT nValue , BOOL bCreate/=TRUE/, BOOL bKeyCaseSnstv/=TRUE/) { char ch_buf[MAX_PATH] = {0}; sPRintf(ch_buf,”%d”,nValue); return SetValue(lpszSect,lpszKey,ch_buf,bCreate,bKeyCaseSnstv); }

BOOL CIniFile::SetValueF(LPCSTR lpszSect, LPCSTR lpszKey, float fValue , BOOL bCreate/=TRUE/, BOOL bKeyCaseSnstv/=TRUE/) { char ch_buf[MAX_PATH] = {0}; sprintf(ch_buf,”%f”,fValue); return SetValue(lpszSect,lpszKey,ch_buf,bCreate,bKeyCaseSnstv); }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表