linux如何读配置文件,linux读配置文件

#include "ReadConfig.h" CReadConfig::CReadConfig() { } CReadConfig::~CReadConfig() { } int CReadConfig::readline(char* line,FILE* stream) { int flag = 1; char buf[CFG_LINE]; unsigned int i, k = 0; if( fgets(buf,CFG_LINE, stream) != NULL ) { /*Delete the last '/r' or '/n' or ''or '/t' character*/ for(i = strlen(buf) - 1; i >= 0; i--) { if( buf[i] == '/r' || buf[i] == '/n' || buf[i] == ' ' || buf[i] == '/t') buf[i] = '/0'; else break;/*dap loop*/ } /*Delete the front '/r' or '/n' or '' or '/t' character*/ for(i = 0; i <= strlen(buf); i++) { if( flag && (buf[i] == '/r' || buf[i] == '/n' || buf[i] == ' ' || buf[i] == '/t')) continue; else { flag = 0; line[k++] = buf[i]; } } return 0; } return -1; } int CReadConfig::isremark(const char* line) { unsigned int i; for(i = 0; i < strlen(line); i++) { if(isgraph(line[i])) { if(line[i] == '#') return 1; else return 0; } } return -1; } int CReadConfig::getsection(const char* line, char* section) { unsigned int start, end; for(start = 0; start < strlen(line); start++) { if(line[start] != '[') return -1; /* fail */ else break; } for(end = strlen(line) - 1; end > 1; end--) { if(line[end] != ']' ) return -1; /* fail */ else break; } if( end - start < 2 ) return -1; memcpy(section, line + start + 1, end - start - 1); section[end-start-1] = '/0'; return 0; } int CReadConfig::getkey(const char* line, char* keyname, char* keyvalue) { unsigned int i, start; /* Find key name */ for(start = 0; start < strlen(line); start++) { /* Find '=' character */ if( line[start] == '=') break; } if(start >= strlen(line) ) return -1; /* not find '=', return */ memcpy(keyname, line, start); keyname[start] = '/0'; /* Delete the last '/t' or ' ' character */ for(i = strlen(keyname) - 1; i >= 0; i--) { if( keyname[i] == ' ' || keyname[i] == '/t' ) keyname[i] = '/0'; else break; } /* Find key value */ for(start = start + 1; start < strlen(line); start++) { if(line[start] != ' ' && line[start] != '/t') break; } strcpy(keyvalue, line + start); /* Delete the last '/t' or ' ' character */ for(i = strlen(keyvalue) - 1; i >= 0; i--) { if(keyvalue[i] == ' ' || keyvalue[i] == '/t') keyname[i] = '/0'; else break; } return 0; } int CReadConfig::getconfigstr(const char* section, const char* keyname, char* keyvalue, unsigned int len, const char* filename) { int step = 0; FILE* stream; char sec[CFG_LINE]; char ken[CFG_LINE]; char kev[CFG_LINE]; char line[CFG_LINE]; if((stream = fopen(filename, "r")) == NULL) return CFG_NOFILE; while(!feof(stream)) { if(readline(line, stream) == -1 ) { fclose(stream); return CFG_NOFIND; } if(!isremark(line)) { if( step == 0 ) /*first, find section*/ { if( getsection(line,sec) == 0 ) { if(strcmp(sec, section) == 0 ) step = 1; } } else { /* second, find keyname, keyvalue */ if(getkey(line, ken, kev) == 0 ) { if( strcmp(ken,keyname) == 0 ) { strncpy(keyvalue, kev, len); fclose(stream); return CFG_NOERROR; } } } /* end isremark() */ } }/* end while */ fclose(stream); return CFG_NOFIND; } int CReadConfig::getconfigint(const char* section, const char* keyname, int* keyvalue, const char* filename) { int rs; char kev[20]; memset(kev, 0, sizeof(kev)); rs = getconfigstr(section, keyname, kev, sizeof(kev), filename); if(rs == CFG_NOERROR) *keyvalue = atoi(kev); return rs; }

#ifndef _READCONFIG_H_ #define _READCONFIG_H_ #include #include #include #include #define CFG_LINE 1024 #define CFG_NOERROR 0 /*read configuration file successfully */ #define CFG_NOFILE -1 /*not find or open configuration file */ #define CFG_NOFIND -2 /*not find section or key name in configuration file*/ class CReadConfig { public: CReadConfig(); virtual ~CReadConfig(); int getconfigstr(const char* section, const char* keyname, char* keyvalue, unsigned int len, const char* filename); int getconfigint(const char* section, const char* keyname, int* keyvalue, const char* filename); public: // int CFG_LINE; // int CFG_NOERROR; /* read configuration file successfully */ // int CFG_NOFILE; /* not find or open configuration file */ // int CFG_NOFIND ; /* not find section or key name in configuration file */ private: int readline(char* line, FILE* stream); int isremark(const char* line); int getsection(const char* line, char* section); int getkey(const char* line, char* keyname, char* keyvalue); }; #endif //_READCONFIG_H_

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值