⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinifile.cpp

📁 一个ini的解析程序
💻 CPP
字号:

#include "TIniFile.h"
#include <assert.h>                      //assert()
#include <string.h>
//#define MONITOR_DEBUG      0

TIniFile::TIniFile(const char* filename) //一般构造函数
{	
	assert(filename != NULL);
	int len = strlen(filename) + 1;
	FileName = new char[len];
	if(FileName == NULL) //分配空间失败
	{
	    cout<<"can't allocate room!"<<endl;
		exit(1);
	}
	strcpy(FileName, filename);
	ifstream fin(FileName);
	if(!fin.is_open())             //文件不存在,创建新的文件
	{
	    ofstream fout(FileName);
		fout.close ();
	}
    do_open(FileName);  //将文件中的数据写入c_inimap中
}

TIniFile::~TIniFile()
{
	c_inimap.clear();
    delete FileName;
	FileName = NULL;   //防止野指针
}

void TIniFile::DeleteKey(const string Section, const string Key)
{
    string text;
    if(!LoadFromFile(text))
		return;
	Handle_DeleteKey(text,Section,Key);
	if(!WriteToFile(text))
		return;
	UpdateMap();
	return;
}

void TIniFile::EraseSection(const string Section)
{
    string text;
    if(!LoadFromFile(text))
		return;
	Handle_EraseSection(text,Section);
	if(!WriteToFile(text))
		return;
	UpdateMap();
	return;
}

int TIniFile::ReadSection(const string Section, vector<string>& key )
{
    switch(c_inimap.count(Section))
	{
	    case 0:
		{
			break;	
		}
		case 1:
		{
		    strMapIt iter;
			iter = c_inimap.find(Section);
			key.push_back((*iter).second.first);
			break;
		}
		default:
		{
		    pair<strMapIt,strMapIt> pos;
			pos = c_inimap.equal_range(Section);
			for(; pos.first != pos.second; pos.first++)
			{
			    key.push_back((*pos.first).second.first);
			}
			break;
		}
	}
   	return (key.size());
}

int TIniFile::ReadSections(vector<string>& section)
{
    //处理重复情况
	vector<string>section_temp;
	strMapIt iter = c_inimap.begin();
	for(; iter != c_inimap.end(); iter++)
	{
	   section_temp.push_back((*iter).first);
	}
#ifdef MONITOR_DEBUG
    for(int k=0; k<section_temp.size(); k++)
	{
	   cout<<section_temp[k]<<endl;
	}
#endif
	for(int i=0; i<section_temp.size(); i++)
	{
	    int count = section.size();
		if(count ==0)
		{
		   section.push_back(section_temp[i]);
		}
		else
		{
			vector<string>::iterator v_iter = find(section.begin(),section.end(),section_temp[i]);
			if(v_iter != section.end())
			{
			    continue;
			}
			else
			{
			    section.push_back(section_temp[i]);
			}
		}
	}
    return (section.size());
}

void TIniFile::ReadSectionValues(const string Section, vector<string>& Value)
{
    switch(c_inimap.count(Section))
	{
	    case 0:
		{
			break;	
		}
		case 1:
		{
		    strMapIt iter;
			iter = c_inimap.find(Section);
			Value.push_back((*iter).second.second);
			break;
		}
		default:
		{
		    pair<strMapIt,strMapIt> pos;
			pos = c_inimap.equal_range(Section);
			for(; pos.first != pos.second; pos.first++)
			{
			    Value.push_back((*pos.first).second.second);
			}
			break;
		}
	}
   	return;
}

string TIniFile::ReadString(const string Section, const string Key, const string Default)
{
    switch(c_inimap.count(Section))
	{
	    case 0:
		{
			return Default;
			break;	
		}
		case 1: 
		{
		    strMapIt iter;
			iter = c_inimap.find(Section);
			if((*iter).second.first !=Key)
			    return Default;
		    else
			{
			    return ((*iter).second.second);  
			}
			break;
		}
		default:
		{
		    pair<strMapIt,strMapIt> pos;
			pos = c_inimap.equal_range(Section);
			for(; pos.first != pos.second; pos.first++)
			{			    
				if((*pos.first).second.first != Key)
				{
				    continue;
				}
				else
				{
				    return ((*pos.first).second.second);
				}
			}
			return Default;
			break;
		}
	}   
}

void TIniFile::WriteString(const string Section, const string Key, const string Value)
{
	string text;
    if(!LoadFromFile(text))
       return;
	Handle_WriteString(text,Section,Key,Value);
	if(!WriteToFile(text))
	   return;
	UpdateMap();
	return;
}

bool TIniFile::SectionExists(const string Section)
{
    switch(c_inimap.count(Section))
	{
	    case 0:
		{
			return false;
			break;	
		}
		default:
		{
		    return true;
			break;
		}
	}
}

bool TIniFile::KeyExists(const string Section, const string Key)
{
    switch(c_inimap.count(Section))
	{
	    case 0:
		{
			return false;
			break;	
		}
		case 1: 
		{
		    strMapIt iter;
			iter = c_inimap.find(Section);
			if((*iter).second.first !=Key)
			    return false;
		    else
			{
			    return true;
			}
			break;
		}
		default:
		{
		    pair<strMapIt,strMapIt> pos;
			pos = c_inimap.equal_range(Section);
			for(; pos.first != pos.second; pos.first++)
			{			    
				if((*pos.first).second.first != Key)
				{
				    continue;
				}
				else
				{
				    return true;
				}
			}
			return false;
			break;
		}
	}
}

//----------------------protected Member--------------------------
bool TIniFile::do_open(const char* filename)
{
    ifstream fin(filename);
	if(!fin.is_open())
		return false;
	vector<string> strvect;
	while(!fin.eof())
	{
		string inbuf;
		getline(fin, inbuf,'\n');
		strvect.push_back(inbuf);
	}
	for_each(strvect.begin(), strvect.end(), analyzeini(c_inimap));
	return !c_inimap.empty();
}

void TIniFile::UpdateMap()
{
    c_inimap.clear();
    do_open(FileName);
}

bool TIniFile::LoadFromFile(string &textofIni)
{
    ifstream myIniFile; 
    myIniFile.open(FileName, ios::in); 
    if(!myIniFile) 
    { 
        cout<<"load false!"<<endl; 
        return false; 
    } 
    char ch; 
    while(myIniFile.get(ch)) 
    { 
        textofIni += ch; 
    } 
    myIniFile.close(); 
#ifdef MONITOR_DEBUG
	cout<<textofIni<<endl;
#endif
	return true;
}

bool TIniFile::WriteToFile(string &textofIni)
{
    ofstream myIniFile;
	myIniFile.open(FileName);//打开时,自动将原来的内容情空
    if(myIniFile.fail())
    { 
         cout<<"Write falese!"<<endl; 
         return false;
    } 
    myIniFile<<textofIni;
    myIniFile.close();
	return true;
}

void TIniFile::Handle_WriteString(string& textofIni,
				             const string& Section, 
				             const string& Key, 
				             const string& Value)
{
    //先处理Section
	string section_temp = "[" + Section + "]";
	string::size_type pos_sect;
	pos_sect = textofIni.find(section_temp, 0);
	if(pos_sect == string::npos)//not find
	{
	    string str;
		str = "[" + Section + "]";
		str += '\n';
		str += Key + "=" + Value;
		//检查文件的结尾是不是'\n'
		string::size_type pos1;
		pos1 = textofIni.rfind('\n');
		if(pos1 == textofIni.size() - 1)//yes
		{
			textofIni.insert(textofIni.size(), str);
		}
		else
		{
		    textofIni += '\n';
			textofIni.insert(textofIni.size(), str);
		}
	}
	else//find Section
	{
	    //定位section的范围
		string::size_type pos2,pos_next_sect;;
		pos2 = pos_sect + section_temp.size();
		pos_next_sect = textofIni.find("[", pos2);
		if(pos_next_sect == string::npos)
		{
		    pos_next_sect = textofIni.size();
		}
		//寻找Key
		string key_temp;
		key_temp = Key + "=";
		string::size_type pos_key;
		pos_key = textofIni.find(key_temp, pos2);
		if(pos_key < pos_next_sect)//在Section的范围内找到Key
		{
		    //更新value的值
			string::size_type pos_value;
			pos_value = pos_key + key_temp.size();
			int value_num;
			string::size_type pos3;
			pos3 = textofIni.find('\n', pos_value);
			if(pos3 == string::npos)
			{
			     pos3 = textofIni.size();
			}
			value_num = pos3 - pos_value;
			textofIni.replace(pos_value, value_num,Value);
		}
		else//Key不存在,创建
		{
		    string str;
			str = key_temp + Value + '\n';
			textofIni.insert(pos_next_sect, str);
		}
	}
	return;
}

void TIniFile::Handle_DeleteKey(string& textofIni,
							const string& Section, 
							const string& Key)
{
    string section_temp = "[" + Section + "]";
	string::size_type pos_sect;
	pos_sect = textofIni.find(section_temp, 0);
	if(pos_sect == string::npos)//section不存在
	{
	    return;
	}
	else//find section
	{
	    //定位section的范围
		string::size_type pos2,pos_next_sect;;
		pos2 = pos_sect + section_temp.size();
		pos_next_sect = textofIni.find("[", pos2);
		if(pos_next_sect == string::npos)
		{
		    pos_next_sect = textofIni.size();
		}
		//寻找Key
		string key_temp;
		key_temp = Key + "=";
		string::size_type pos_key;
		pos_key = textofIni.find(key_temp, pos2);
		if(pos_key < pos_next_sect)//在Section的范围内找到Key
		{
		    //删除Key
			int delete_num;
			string::size_type pos3;
			pos3 = textofIni.find('\n', pos_key);
			if(pos3 == string::npos)
			{
			     pos3 = textofIni.size();
			}
			delete_num = pos3 - pos_key + 1;//末尾的'\n'也一并删除
            textofIni.erase(pos_key, delete_num);		
		}
		else//Key不存在
		{
            return;
		}  
	}
    return;
}

void TIniFile::Handle_EraseSection(string& textofIni,const string& Section)
{
    string section_temp = "[" + Section + "]";
	string::size_type pos_sect;
	pos_sect = textofIni.find(section_temp, 0);
	if(pos_sect == string::npos)//section不存在
	{
	    return;
	}
	else//find section
	{
	    //定位section的范围
		string::size_type pos2,pos_next_sect;;
		pos2 = pos_sect + section_temp.size();
		pos_next_sect = textofIni.find("[", pos2);
		if(pos_next_sect == string::npos)
		{
		    pos_next_sect = textofIni.size();//good
		}
	    int delete_num = pos_next_sect - pos_sect;
		textofIni.erase(pos_sect, delete_num);
	}
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -