Linux下C读取ini文件

zthhyt 2003-10-20 12:39:09
请问一下各位,C如何读取ini文件,谢谢
...全文
910 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xharry 2003-11-08
  • 打赏
  • 举报
回复
经典,收藏:)
dchg2000 2003-11-08
  • 打赏
  • 举报
回复
结贴
dchg2000 2003-10-22
  • 打赏
  • 举报
回复
给分呀?!
没人管呀!
dchg2000 2003-10-22
  • 打赏
  • 举报
回复
/* inet_api.c */

#include "inet_api.h"
#include "debug.h"
#include "config.h"


/*
* Define inet_api global variables
*/
char cfg_ipsmg[16];
unsigned int cfg_portsmg;
unsigned int cfg_portsp;


int inet_initialize(const char* configfile)
{

if ( getconfigstr("INET", "ipsmg", cfg_ipsmg, sizeof(cfg_ipsmg), configfile) )
return -1;

if ( getconfigint("INET", "portsmg", &cfg_portsmg, configfile) )
return -1;

if ( getconfigint("INET", "portsp", &cfg_portsp, configfile) )
return -1;

// deg_printf("Load configuration ... ipsmg = %s \n", cfg_ipsmg);
// deg_printf("Load configuration ... portsmg = %u \n", cfg_portsmg);
// deg_printf("Load configuration ... portsp = %u \n", cfg_portsp);

return 0;
}
liuty2006 2003-10-21
  • 打赏
  • 举报
回复
can you give a demo to use it?

thanks
shally5 2003-10-20
  • 打赏
  • 举报
回复
多谢啦!明白啦!
楼主应结帖啦!
dchg2000 2003-10-20
  • 打赏
  • 举报
回复
头文件不是有吗?!
shally5 2003-10-20
  • 打赏
  • 举报
回复
太好啦!
如何使用啊!?
dchg2000 2003-10-20
  • 打赏
  • 举报
回复
如果有用的话,记得多给我分,谢谢!
dchg2000 2003-10-20
  • 打赏
  • 举报
回复
/* config.c */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "config.h"

static int isremark(const char* line);
static int isjoint(const char* line);
static char* readline(char* line, int n, FILE *stream);
static int getsection(const char* line, char* section, unsigned int seclen);
static int getkeyname(const char* line, char* keyname, unsigned int keylen);
static int getkeyvalue(const char* line, char* keyvalue, unsigned int valen);
static int getkeyadd(const char* line, char* keyvalue, unsigned int valen);




static char* readline(char* line, int n, FILE *stream)
{
static char* rs;

rs = fgets(line, n, stream);
if ( strlen(line) >= 1 ) {
line[strlen(line) - 1] = line[strlen(line)];
}

return rs;
}


static int isjoint(const char* line)
{
unsigned int i;
unsigned int sign = 0;

for (i = 0; i < strlen(line); i++) {
if ( line[i] == ' ' || line[i] == '\t' || line[i] == '\\' ) {
if ( line[i] == ' ' || line[i] == '\t' ) {
if ( sign < 1 ) sign++;
} else {
if ( sign > 0 ) sign++;
else return 0;
}
} else {
return 0;
}
}

if ( sign == 2 )
return 1;
else
return 0;

}


static int 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;
}


static int getsection(const char* line, char* section, unsigned int seclen)
{
unsigned int start, mid, end;

for (start = 0; start < strlen(line); start++) {
if ( isgraph(line[start]) ) {
if ( line[start] != '[' )
return 0;
else
break;
}
}

if ( start >= strlen(line) )
return 0;


for (end = strlen(line); end > 1; end --) {
if ( isgraph(line[end]) ) {
if (line[end] != ']' )
return 0;
else
break;
}
}

if ( end <= 1 )
return 0;

if ( end - start < 2 )
return 0;


for (mid = start + 1; mid < end; mid++) {
if ( !isalnum(line[mid]) )
return 0;
}

if ( seclen > end - start - 1 ) {
memcpy(section, &line[start + 1], end - start - 1);
section[end - start - 1] = '\0';
return (end - start - 1);
}
else {
memcpy(section, &line[start + 1], seclen - 1);
section[seclen - 1] = '\0';
return (seclen -1);
}

}


static int getkeyname(const char* line, char* keyname, unsigned int keylen)
{
unsigned int start, mid, end;

for (start = 0; start < strlen(line); start++) {
if ( isgraph(line[start]) ) {
if ( !isalnum(line[start]) )
return 0;
else
break;
}
}

if ( start >= strlen(line) )
return 0;

for (end = start; line[end] != '=' && end < strlen(line); end++) {
if ( !isalnum(line[end]) && line[end] != ' ' && line[end] != '\t' )
return 0;
}

if ( end >= strlen(line) )
return 0;

for (end = end - 1; !isalnum(line[end]); end--) {
}

for (mid = start; mid <= end; mid++) {
if ( line[mid] == ' ' || line[mid] == '\t' )
return 0;
}


if ( keylen > end - start + 1 ) {
memcpy(keyname, &line[start], end - start + 1);
keyname[end - start + 1] = '\0';
return (end - start + 1);
}
else {
memcpy(keyname, &line[start], keylen - 1);
keyname[keylen - 1] = '\0';
return (keylen -1);
}

}


static int getkeyvalue(const char* line, char* keyvalue, unsigned int valen)
{
unsigned int start, end;
int len;

for (start = 0; line[start] != '=' && start < strlen(line); start++) {
}

for (start = start + 1; !isgraph(line[start]) && start < strlen(line); start++) {
}

if ( start >= strlen(line) )
return 0;

for (end = start; line[end] != ' ' && line[end] != '\t' && end < strlen(line); end++) {
}

if ( valen > end - start ) {
memcpy(keyvalue, &line[start], end - start);
keyvalue[end - start] = '\0';
len = end - start;
}
else {
memcpy(keyvalue, &line[start], valen - 1);
keyvalue[valen - 1] = '\0';
len = valen -1;
}

if ( end >= strlen(line) )
return len;
else
{
if ( isjoint(&line[end]) )
return -len;
return len;
}

}


static int getkeyadd(const char* line, char* keyvalue, unsigned int valen)
{
unsigned int start, end;
int len;

for (start = 0; !isgraph(line[start]) && start < strlen(line); start++) {
}

if ( start >= strlen(line) )
return 0;

for (end = start; line[end] != ' ' && line[end] != '\t' && end < strlen(line); end++) {
}

if ( valen > end - start ) {
memcpy(keyvalue, &line[start], end - start);
keyvalue[end - start] = '\0';
len = end - start;
}
else {
memcpy(keyvalue, &line[start], valen - 1);
keyvalue[valen - 1] = '\0';
len = valen -1;
}

if ( end >= strlen(line) )
return len;
else
{
if ( isjoint(&line[end]) )
return -len;
return len;
}

}



int getconfigstr(const char* section,
const char* keyname,
char* keyvalue,
unsigned int len,
const char* filename)
{
int step = 0;
int rs = 0;
int ra = 0;
FILE* stream;
char line[LINE_LEN];
char sec[LINE_LEN];
char ken[LINE_LEN];
char kev[LINE_LEN];

if( (stream = fopen(filename, "r") ) == NULL )
return 0x2;

while ( !feof(stream) ) {
if ( readline(line, 100, stream) == NULL ) {
fclose(stream);
return 0x3;
}

if ( !isremark(line) ) {
if ( step == 0 ) {
if ( getsection(line, sec, LINE_LEN) ) {
if ( strcmp(sec, section) == 0 )
step = 1;
}
} else if (step == 1) {
if ( getkeyname(line, ken, LINE_LEN) ) {
if ( strcmp(ken, keyname) == 0 ) {
rs = getkeyvalue(line, kev, LINE_LEN);
if ( rs > 0 ) {
strncpy(keyvalue, kev, len);
fclose(stream);
return 0x0;
} else if ( rs < 0 ) {
step = 2;
rs = abs(rs);
} else {
fclose(stream);
return 0x4;
}

}
}

} else {
ra = getkeyadd(line, kev + rs, LINE_LEN - rs);
if ( ra > 0 ) {
strncpy(keyvalue, kev, len);
fclose(stream);
return 0x0;
} else if ( ra < 0 ) {
rs += abs(ra);
} else {
fclose(stream);
return 0x4;
}


}


}

}

fclose(stream);
return 0x5;

}


int getconfigint(const char* section,
const char* keyname,
int* keyvalue,
const char* filename)
{
int rs;
char kev[12];

memset(kev, 0, 12);
rs = getconfigstr(section, keyname, kev, 12, filename);
if ( rs == 0 )
*keyvalue = atoi(kev);
return rs;
}

dchg2000 2003-10-20
  • 打赏
  • 举报
回复
/*
* config.h - declarations/definitions for reading configuration files
*
* Gang He - dchg2000@mail.com .
*/

#ifndef _HEGANG_CONFIG_H_
#define _HEGANG_CONFIG_H_


/*
* Set line length in configuration files
*/
#define LINE_LEN 128


/*
* Define return error code value
*/
#define ERR_NONE 0 /* read configuration file successfully */
#define ERR_NOFILE 2 /* not find or open configuration file */
#define ERR_READFILE 3 /* error occur in reading configuration file */
#define ERR_FORMAT 4 /* invalid format in configuration file */
#define ERR_NOTHING 5 /* not find section or key name in configuration file */


/*
* Read the value of key name in string form
*/
int getconfigstr(const char* section, /* points to section name */
const char* keyname, /* points to key name */
char* keyvalue, /* points to destination buffer */
unsigned int len, /* size of destination buffer */
const char* filename); /* points to configuration filename */


/*
* Read the value of key name in integer form
*/
int getconfigint(const char* section, /* points to section name */
const char* keyname, /* points to key name */
int* keyvalue, /* points to destination address */
const char* filename); /* points to configuration filename */



#endif

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧