- 论坛徽章:
- 0
|
3个文件
configfile.h
接口定义文件
configfilev1.h
configfilev1.cpp
实现
************************************************
configfile.h
************************************************
#if !defined(AFX_MODIFYINI_H__C8CFF5D5_0F26_43EB_B95B_9F9ABAE66CF4__INCLUDED_)
#define AFX_MODIFYINI_H__C8CFF5D5_0F26_43EB_B95B_9F9ABAE66CF4__INCLUDED_
#include <list>;
using namespace std;
class ConfigFile
{
public:
virtual int DeleteData(char *index)=0;
virtual int ReadData(char *index, char *name , char *value)=0;
virtual void WriteData(char *index, char *name , char *value)=0;
virtual void ReadAllIndex(list<int>; & ListIndex)=0;
virtual int Save(char *szFileName=0)=0;
virtual ~ConfigFile(){};
};
ConfigFile *CreateConfigFile(char *szFileName);
void ReleaseConfigFile(ConfigFile *param);
#endif // !defined(AFX_MODIFYINI_H__C8CFF5D5_0F26_43EB_B95B_9F9ABAE66CF4__INCLUDED_)
************************************************
configfilev1.h
************************************************
// onfigFileV1.h: interface for the ConfigFileV1 class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_ONFIGFILEV1_H__2C3C3157_BFAC_40F3_B87A_0211AE84CF80__INCLUDED_)
#define AFX_ONFIGFILEV1_H__2C3C3157_BFAC_40F3_B87A_0211AE84CF80__INCLUDED_
#pragma warning(disable:4786)
#pragma warning(disable:4503)
#include "configfile.h"
#include <string>;
#include <map>;
#include <iostream>;
#include <stdio.h>;
#include <stdlib.h>;
typedef map<string,string>; item_map;
typedef map<string,item_map *>; ini_map;
class ConfigFileV1 : public ConfigFile
{
ini_map ini_file_map;
char m_szFileName[1024];
char* span(char *src,char span_chr,char *dst0,char *dst1);
char *GetLine(char *src,char *dst);
void ReplaceChar(char *string, char s,char d);
void DelCharList(char *string,char *dellist);
int Init();
public:
int DeleteData(char *index);
int ReadData(char *index, char *name , char *value);
void WriteData(char *index, char *name , char *value);
void ReadAllIndex(list<int>; & ListIndex);
int Save(char *szFileName);
ConfigFileV1(char *szFileName);
virtual ~ConfigFileV1();
};
#endif // !defined(AFX_ONFIGFILEV1_H__2C3C3157_BFAC_40F3_B87A_0211AE84CF80__INCLUDED_)
************************************************
configfileV1.cpp
************************************************
// onfigFileV1.cpp: implementation of the ConfigFileV1 class.
//
//////////////////////////////////////////////////////////////////////
#include "ConfigFileV1.h"
#include <stdlib.h>;
ConfigFile *CreateConfigFile(char *szFileName)
{
return new ConfigFileV1(szFileName);
}
void ReleaseConfigFile(ConfigFile *param)
{
delete param;
}
ConfigFileV1::ConfigFileV1(char *szFileName)
{
sprintf(m_szFileName,szFileName);
Init();
}
ConfigFileV1::~ConfigFileV1()
{
ini_map::iterator pb,bs,be;
item_map::iterator pi,is,ie;
bs=ini_file_map.begin();
be=ini_file_map.end();
for(pb=bs;pb!=be;pb++){
//cout<<pb->;first<<endl;
is=pb->;second->;begin();
ie=pb->;second->;end();
/*for(pi=is;pi!=ie;pi++){
cout<<pi->;first<<"=="<<pi->;second<<endl;
}*/
pb->;second->;clear();
delete pb->;second;
}
ini_file_map.clear();
}
int ConfigFileV1::Init()
{
FILE * fp=fopen(m_szFileName,"r" ;
if(!fp)
return 0;
fseek(fp,0,SEEK_END);
long len=ftell(fp);
fseek(fp,0,SEEK_SET);
char *pFileData=new char[len+1];
memset(pFileData,0,len+1);
fread(pFileData,len,1,fp);
fclose(fp);
DelCharList(pFileData,"\t " ;
//cout<<pFileData<<endl;
len=strlen(pFileData);
char *p0=pFileData;
item_map *pmap=0;
char templine[1024];
char szName[1024];
char szValue[1024];
string str0;
string str1;
while(*p0){
p0=GetLine(p0,templine);
if(strchr(templine,'[')&&strchr(templine,']')){
DelCharList(templine,"[]" ;
str0=templine;
pmap=new item_map;
ini_file_map.insert(make_pair(str0,pmap));
continue;
}
if(!strchr(templine,'=')||!pmap)
continue;
span(templine,'=',szName,szValue);
str0=szName;
str1=szValue;
pmap->;insert(make_pair(str0,str1));
}
delete []pFileData;
return 0;
}
int ConfigFileV1::Save(char *szFileName)
{
FILE * fp=fopen(szFileName?szFileName:m_szFileName,"w" ;
if(!fp)
return 0;
ini_map::iterator pb,bs,be;
item_map::iterator pi,is,ie;
bs=ini_file_map.begin();
be=ini_file_map.end();
for(pb=bs;pb!=be;pb++){
fprintf(fp,"[%s]\r\n",pb->;first.c_str());;
is=pb->;second->;begin();
ie=pb->;second->;end();
for(pi=is;pi!=ie;pi++){
fprintf(fp,"%s=%s\r\n",pi->;first.c_str(),pi->;second.c_str());
}
}
fclose(fp);
return 0;
}
void ConfigFileV1::WriteData(char *index, char *name , char *value)
{
ini_map::iterator pblock;//index map
item_map::iterator pitem;//name value
item_map *p; //map
string strIndex=index;
string strName=name;
string strValue=value;
pblock=ini_file_map.find(strIndex);
if(pblock==ini_file_map.end()){
p=new item_map;
ini_file_map.insert(make_pair(strIndex,p));
}
else{
p=pblock->;second;
}
pitem=p->;find(strName);
if(pitem==p->;end()){
p->;insert(make_pair(strName,strValue));
}
else{
pitem->;second=strValue;
}
}
int ConfigFileV1::ReadData(char *index, char *name , char *value)
{
ini_map::iterator pblock;
item_map::iterator pitem;
pblock=ini_file_map.find(index);
if(pblock==ini_file_map.end())
return 0;
pitem=pblock->;second->;find(name);
if(pitem==pblock->;second->;end())
return 0;
sprintf(value,pitem->;second.c_str());
return 1;
}
int ConfigFileV1: eleteData(char *index)
{
ini_map::iterator pblock;//index map
pblock=ini_file_map.find(index);
if(pblock==ini_file_map.end())
return 0;
item_map::iterator pi,is,ie;
is=pblock->;second->;begin();
ie=pblock->;second->;end();
for(pi=is;pi!=ie;pi++){
}
pblock->;second->;clear();
ini_file_map.erase(pblock);
return 1;
}
void ConfigFileV1: elCharList(char *string, char *dellist)
{
int len=strlen(string);
char *p0=string;
char *p1=string;
while(len--){
if(!strchr(dellist,*p1)){
*p0++=*p1;
}
*p1++;
}
*p0=0;
}
void ConfigFileV1::ReplaceChar(char *string, char s,char d)
{
while(*string){
if(*string==s)
*string=d;
string++;
}
}
char *ConfigFileV1::GetLine(char *src, char *dst)
{
char *p=dst;
while(*src){
if(*src=='\n'||*src=='\r')
break;
*dst++=*src++;
}
*dst=0;
while(*src&&(*src=='\n'||*src=='\r'))
src++;
return src;
}
char* ConfigFileV1::span(char *src,char span_chr,char *dst0, char *dst1)
{
*dst0=0;
*dst1=0;
while(*src){
*dst0++=*src++;
if(*src==span_chr)
break;
}
*dst0=0;
*src?src++:0;
while(*src){
*dst1++=*src++;
}
*dst1=0;
while(*src&&(*src=='\n'||*src=='\r'))
src++;
return src;
}
void ConfigFileV1::ReadAllIndex(list<int>; & ListIndex)
{
ListIndex.clear();
ini_map::iterator s,e,p;
s=ini_file_map.begin();
e=ini_file_map.end();
for(p=s;p!=e;p++){
ListIndex.push_back(atoi(p->;first.c_str()));
}
} |
|