📄 tinifile.h
字号:
/*-----------------------------ini文件基本知识-----------------------------
为了方便用户使用和使系统具有灵活性,大多数Win-dows应用程序将用户所
做的选择以及各种变化的系统信息记录在初始化(INI)文件中。因此,当系统的环
境发生变化时,可以直接修改INI文件,而无需修改程序。由此可见,INI文件对系
统功能是至关重要的。
INI文件是文本文件,由若干部分(section)组成,在每个带括号的标题下面,
是若干个以单个单词开头的关键词(keyword)和一个等号,每个关键词会控制应用
程序某个功能的工作 方式,等号右边的值(value)指定关键词的操作方式。其一般
形式如下:
[Section1]
key1=valuel
key2=value2
……
[section2]
key1=value1
key2=value2
--------------------------------------------------------------------------*/
/*
* Copyright (c) 2006,重庆爱思网安有限公司
* All rights reserved.
* 文件名称:TIniFile.h
* 摘 要:对ini文件的基本操作
* 当前版本:2.0
* 作 者:唐翼
* 完成日期:2006年3月14日
* 说 明:在VC++ 6.0上编译通过
*/
#ifndef _INIFILE_H__
#define _INIFILE_H__
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <iostream>
#include <fstream>
using namespace std;
typedef pair<string,string> Key_Value;
typedef multimap<string, Key_Value> strMap;
typedef strMap::value_type valType;
typedef strMap::iterator strMapIt;
struct analyzeini
{
string strsect;
strMap *pmap;
analyzeini(strMap & strmap):pmap(&strmap){}
void operator()( const string& strini)
{
int first = strini.find('[');
int last = strini.rfind(']');
if( first != string::npos && last != string::npos && first != last+1)
{
strsect = strini.substr(first+1,last-first-1);
return ;
}
if(strsect.empty())
return ;
if((first=strini.find('=')) == string::npos)
return ;
string strtmp1 = strini.substr(0,first);
string strtmp2 = strini.substr(first+1, string::npos);
first = strtmp1.find_first_not_of(" \t");
last = strtmp1.find_last_not_of(" \t");
if(first == string::npos || last == string::npos)
return ;
string strkey = strtmp1.substr(first, last-first+1);
first = strtmp2.find_first_not_of(" \t");
if(((last = strtmp2.find("\t#", first )) != -1) ||
((last = strtmp2.find(" #", first )) != -1) ||
((last = strtmp2.find("\t//", first )) != -1)||
((last = strtmp2.find(" //", first )) != -1))
{
strtmp2 = strtmp2.substr(0, last-first);
}
last = strtmp2.find_last_not_of(" \t");
if(first == string::npos || last == string::npos)
return ;
string value = strtmp2.substr(first, last-first+1);
string mapkey = strsect;
Key_Value mapvalue(strkey,value);
//(*pmap)[mapkey] = mapvalue; //multimap不支持下标操作
(*pmap).insert(valType(mapkey,mapvalue));
return ;
}
};
class TIniFile
{
public:
TIniFile(void){};
TIniFile(const char* filename);
/*Frees the memory associated with the TIniFile object*/
~TIniFile(void);
/*Removes an INI file entry for a specified key value
*attempting to delete a key in a nonexistent section or
*attempting to delete a nonexistent key are not errors.
*In these cases, DeleteKey does nothing.*/
void DeleteKey(const string Section, const string Key);
/*Call EraseSection to remove a section, all its keys,
*and their data values from an INI file. Section identifies
*the INI file section to remove.*/
void EraseSection(const string Section);
/*Reads all the key names from a specified section of an INI file into vector<string>
success return the number of key,false return 0*/
int ReadSection(const string Section, vector<string>& key );
/*Reads the names of all sections in an INI file into into vector<string>
success return the number of key,false return 0*/
int ReadSections(vector<string>& section);
/*Reads the values from all keys within a section of an INI file into a vetcor<string>*/
void ReadSectionValues(const string Section, vector<string>& Value);
/*Retrieves a string value from an INI file,
Default is the string value to return if the:
Section does not exist.
Key does not exist.
Data value for the key is not assigned*/
string ReadString(const string Section, const string Key, const string Default);
/*Writes a string value to an INI file*/
void WriteString(const string Section, const string Key, const string Value);
/*Indicates whether a section exists in the INI file*/
bool SectionExists(const string Section);
/*Indicates whether a key exists in the INI file*/
bool KeyExists(const string Section, const string Key);
protected:
void UpdateMap();
bool LoadFromFile(string &textofIni);
bool WriteToFile(string &textofIni);
void Handle_WriteString(string& textofIni,
const string& Section,
const string& Key,
const string& Value);
void Handle_DeleteKey(string& textofIni,
const string& Section,
const string& Key);
void Handle_EraseSection(string& textofIni,
const string& Section);
bool do_open(const char* filename);
private:
strMap c_inimap;
char* FileName;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -