读取配置
2016-05-09 16:33
232 查看
//#include"init.hpp"
#include<string>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef struct global_configure
{
string Ip;
bool Module;
unsigned int Port;
unsigned int Buffer;
unsigned int timer;
}Configure;
Configure __config;
char * GetString(char *title,char * key,char *filename)
{
FILE *fp;
char szLine[1024];
static char tmpstr[1024];
int rtnval;
int i = 0;
int flag = 0;
char *tmp;
if((fp = fopen(filename, "r")) == NULL)
{
printf("have no such file \n");
return "";
}
while(!feof(fp))
{
rtnval = fgetc(fp);
if(rtnval == EOF)
{
break;
}
else
{
szLine[i++] = rtnval;
}
if(rtnval == '\n')
{
i--;
szLine[--i] = '\0';
i = 0;
tmp = strchr(szLine, '=');
if(( tmp != NULL )&&(flag == 1))
{
if(strstr(szLine,key)!=NULL)
{
//注释行
if ('#' == szLine[0])
{
}
else
{
//找打key对应变量
strcpy(tmpstr,tmp+1);
fclose(fp);
return tmpstr;
}
}
}
else
{
strcpy(tmpstr,"[");
strcat(tmpstr,title);
strcat(tmpstr,"]");
if( strncmp(tmpstr,szLine,strlen(tmpstr)) == 0 )
{
//找到title
flag = 1;
}
}
}
}
fclose(fp);
return "";
}
unsigned int GetInt(char *title,char *key,char * filename)
{
return atoi(GetString(title,key,filename));
}
void init_(void)
{
try
{
//读取配置文件,加载全局配置
printf("enter into init\n");
FILE *fp=NULL;
fp=fopen("../config.ini","r");
if(!fp)
{
//此处后续添加日志记录错误
printf("error for open \n");
return ;
}
char buff[16]={0};
strcpy(buff,GetString("global","Ip","../config.ini"));
printf("buff is %s\n",buff);
strncpy(const_cast<char *>(__config.Ip.c_str()),GetString("global","Ip","../config.ini"),16);
}
catch(...)
{
}
}
#include<string>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef struct global_configure
{
string Ip;
bool Module;
unsigned int Port;
unsigned int Buffer;
unsigned int timer;
}Configure;
Configure __config;
char * GetString(char *title,char * key,char *filename)
{
FILE *fp;
char szLine[1024];
static char tmpstr[1024];
int rtnval;
int i = 0;
int flag = 0;
char *tmp;
if((fp = fopen(filename, "r")) == NULL)
{
printf("have no such file \n");
return "";
}
while(!feof(fp))
{
rtnval = fgetc(fp);
if(rtnval == EOF)
{
break;
}
else
{
szLine[i++] = rtnval;
}
if(rtnval == '\n')
{
i--;
szLine[--i] = '\0';
i = 0;
tmp = strchr(szLine, '=');
if(( tmp != NULL )&&(flag == 1))
{
if(strstr(szLine,key)!=NULL)
{
//注释行
if ('#' == szLine[0])
{
}
else
{
//找打key对应变量
strcpy(tmpstr,tmp+1);
fclose(fp);
return tmpstr;
}
}
}
else
{
strcpy(tmpstr,"[");
strcat(tmpstr,title);
strcat(tmpstr,"]");
if( strncmp(tmpstr,szLine,strlen(tmpstr)) == 0 )
{
//找到title
flag = 1;
}
}
}
}
fclose(fp);
return "";
}
unsigned int GetInt(char *title,char *key,char * filename)
{
return atoi(GetString(title,key,filename));
}
void init_(void)
{
try
{
//读取配置文件,加载全局配置
printf("enter into init\n");
FILE *fp=NULL;
fp=fopen("../config.ini","r");
if(!fp)
{
//此处后续添加日志记录错误
printf("error for open \n");
return ;
}
char buff[16]={0};
strcpy(buff,GetString("global","Ip","../config.ini"));
printf("buff is %s\n",buff);
strncpy(const_cast<char *>(__config.Ip.c_str()),GetString("global","Ip","../config.ini"),16);
}
catch(...)
{
}
}
相关文章推荐
- How to Create Custom Filters in AngularJs
- 加载离线的谷歌地图
- 精华阅读第 12 期 | 最新 App Store 审核指南与10大被拒理由?
- 属性和点语法(16.5.9)
- 雅虎优化
- 数据库重构工具 LiquiBase(5 Liquibase被锁)
- 淘宝、京东跨域共享会话的分析与总结
- 图像处理和计算机视觉中的经典论文
- Source Tree for MAC1.6
- Liquibase被锁
- <html>全局属性
- 批处理(.bat)文件中使用相对路径
- 三极管静态稳定电路判断依据
- jQuery 三级菜单
- location对象的属性和方法应用(解析URL)
- 阶段冲刺4
- 312-金胜维 P系列2.5寸 480G SATA3 SSD固态硬盘
- java获取当前操作系统
- (4.1.40)android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
- POJ 3026 Borg Maze(BFS+最小生成树【有坑】)