文本装入Config的一个快速方法。模版,稍加更改后可装入使用。
#include<public.hpp>
void Fill_config(char *option,char *property,StateBuffer *statebuffer)
{
string option_buffer(option);
string property_buffer(property);
if(option_buffer=="ipaddr")
{
statebuffer->data_origin = property_buffer ;
statebuffer->data_origin.erase(statebuffer->data_origin.find_first_of("'"),1);
statebuffer->data_origin.erase(statebuffer->data_origin.find_last_of("'"),1);
}
else if(option_buffer=="Error_Retrans_Enable")
{
statebuffer->Error_Retrans_Enable=0 ;
if(property_buffer=="'1'"){
statebuffer->Error_Retrans_Enable =1 ;
}
}
else{
cout<<"message bad format"<<endl;
}
}
int LoadConfigtxt()
{
char line[60];
char option[30], property[30],buffer[15];
FILE *fp = fopen("/etc/config/Config","a+");
if(fp==NULL)
{
printf("Config text black !!! ");
return 0 ;
}
while(!feof(fp))//end return ture
{
memset(line,0,60); // clean memory line
memset(option,0,30); // clean memory name
memset(property,0,30); // clean ..
fgets(line,60,fp); // read 50 chars frome fp to line untill \t or end of file
sscanf(line, "%s%s%s", buffer,option, property); // format input keyword space
//printf("%s\n",line);
printf("option:%s, property:%s\n",option,property);
Fill_config(option,property,&statebuffer);
}
return 1 ;
}