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

📄 conffile.c

📁 linux下的一个mount工具
💻 C
字号:
// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU Library General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA./* * $Id: CONFFile.C,v 1.1.1.1 2000/09/13 19:36:41 lyonel Exp $ * * */ #include "CONFFile.h"#include <fstream>#include <vector>using namespace std;bool CONFFile::same(string s1, string s2){	while(s1[0]==' ') s1 = s1.substr(1);	while(s1[s1.length()-1]==' ') s1 = s1.substr(0,s1.length()-1);	while(s2[0]==' ') s2 = s2.substr(1);	while(s2[s2.length()-1]==' ') s2 = s2.substr(0,s2.length()-1);	return strcasecmp(s1.c_str(), s2.c_str())==0;}string stripblanks(string line){	while(line[0]==' ') line = line.substr(1);	// remove leading blanks	while(line[line.length()-1]==' ') line = line.substr(0,line.length()-1);	// remove ending blanks	if(line[0]=='"')	{		size_t pos = line.rfind('"');		if(pos!=string::npos) line = line.substr(1,pos-1);	}	return line;}vector<string> CONFFile::split(string s, string separator){	vector<string> result;	size_t pos = string::npos, quote = string::npos;	while(s.length()>0)	{		string item;		pos = s.find(separator);		if(separator.find("\"")==string::npos) quote = s.find("\"");		if(pos==string::npos)		{			item = s;			s = "";		}		else		if((quote==string::npos)||(quote>pos))		{			item = s.substr(0, pos);			s = s.substr(pos+separator.length());		}		else		{			size_t quote2 = s.find('"', quote+1);			if(quote2==string::npos)	// warning: the quote is unbalanced !			{				item = s;				s = "";			}			else			{				pos = s.find(separator, quote2);				if(pos==string::npos)				{					item = s;					s = "";				}				else				{					item = s.substr(0, pos);					s = s.substr(pos+separator.length());				}			}		}		if(separator.find(" ")==string::npos)	// we remove the blanks only if separator doesn't contain any			item = stripblanks(item);		result.push_back(item);	}	return result;}class CONFFile_i{  public:	CONFFile_i(): refcount(1)	{	}	void addref()	{		refcount++;	}	void unref()	{		refcount--;	}	bool canfree()	{		return refcount<=0;	}	bool finalize()	{		unref();		return canfree();	}	vector<string> content;  private:	long refcount;	};CONFFile::CONFFile(string filename):	This(NULL){	if(filename.length()>0)	{		This = new CONFFile_i;	}	if(This)	{		ifstream in;		char buffer[1024];		string line;		size_t pos;		in.open(filename.c_str());		while(in)		{			strcpy(buffer, "");			in.getline(buffer, sizeof(buffer));			if(buffer[strlen(buffer)-1]=='\n')	// chop the ending \n				buffer[strlen(buffer)-1]='\0';			if(buffer[strlen(buffer)-1]=='\r')	// MS-DOS style text file				buffer[strlen(buffer)-1]='\0';			line = string(buffer);			pos = line.find(";");			if(pos!=string::npos)				line = line.substr(0, pos);			while(line[0]==' ') line = line.substr(1);			if(line.length())				This->content.push_back(line);		}	}}CONFFile::CONFFile(const CONFFile & ini):	This(NULL){	This = ini.This;	if(This) This->addref();}CONFFile::~CONFFile(){	if(This && This->finalize()) delete This;	This = NULL;}CONFFile & CONFFile::operator =(const CONFFile & ini){	if(&ini == this) return *this;	// protect against auto-affectation	if(This && This->finalize()) delete This;	This = ini.This;	This->addref();}string CONFFile::getValue(string section, string key, string defaultval=""){	string currentsection = "";	string currentline = "";	long currentindex = 0;	if(!This) return defaultval;	section = "[" + section + "]";	while(currentindex<This->content.size())	{		currentline = This->content[currentindex];		if(currentline[0]=='[')			currentsection=currentline;		if(same(currentsection,section))		{			size_t pos = currentline.find("=");			if((pos!=string::npos)&&				same(expandVars(currentline.substr(0,pos)),key))					return expandVars(stripblanks(currentline.substr(pos+1)));		}		currentindex++;	}	return defaultval;}string CONFFile::expandVars(string value){	size_t pos1 = value.find("%");	if(pos1==string::npos) return value;	size_t pos2 = value.find("%", pos1+1);	if(pos2==string::npos) return value;	return value.substr(0,pos1)+		getValue("Strings", value.substr(pos1+1,pos2-pos1-1),value.substr(pos1,pos2-pos1))+		expandVars(value.substr(pos2+1));}vector<string> CONFFile::listSections(){	vector<string> result;	string currentsection = "";	string currentline = "";	long currentindex = 0;	if(!This) return result;	while(currentindex<This->content.size())	{		currentline = This->content[currentindex];		if((currentline[0]=='[') &&			(currentline[currentline.length()-1]==']'))		{			result.push_back(currentline.substr(1,currentline.length()-2));		}		currentindex++;	}	return result;}vector<string> CONFFile::listValues(string section){	vector<string> result;	string currentsection = "";	string currentline = "";	long currentindex = 0;	if(!This) return result;	section = "[" + section + "]";	while(currentindex<This->content.size())	{		currentline = This->content[currentindex];		if(currentline[0]=='[')			currentsection=currentline;		else		if(same(currentsection,section))		{			vector<string> entry = split(currentline,"=");			result.push_back(expandVars(entry[0]));		}		currentindex++;	}	return result;}

⌨️ 快捷键说明

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