因为工作的需要,写了一个 ini 文件的读取函数,下面的部分是一个简单的读取 ini 文件并分离 ini 文件的节点和对应的每个节中的 key 和 value 的方法。
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <ctype.h>
-
-
#ifndef SUCCESS
-
#define SUCCESS 1
-
#endif
-
-
/* TRIM LEFT */
-
-
int rtrim( char *pszString)
-
{
-
int nForwardCursor = 0;
-
int nTrailingCursor = 0;
-
int bTrim = 1;
-
-
for( nForwardCursor = 0 ; pszString[nForwardCursor] != '\0'; nForwardCursor++ )
-
if ( bTrim && isspace( pszString[nForwardCursor] ))
-
continue ;
-
else {
-
bTrim = 0;
-
pszString[nTrailingCursor] = pszString[nForwardCursor];
-
nTrailingCursor++;
-
}
-
pszString[nTrailingCursor] = '\0';
-
return SUCCESS;
-
}
-
-
/* TRIM RIGHT */
-
int ltrim( char *pszString )
-
{
-
int nForwardCursor = 0;
-
int nTrailingCursor = 0;
-
int bTrim = 1;
-
for ( nForwardCursor=strlen(pszString)-1;
-
nForwardCursor >= 0 && isspace( pszString[nForwardCursor] );
-
nForwardCursor-- )
-
{
-
}
-
pszString[nForwardCursor+1] = '\0';
-
return SUCCESS;
-
}
-
-
/* TRIM LEFT & RIGHT */
-
int trim(char *str)
-
{
-
ltrim(str);
-
rtrim(str);
-
return SUCCESS;
-
}
-
-
-
-
char **read_ini(char *filename,int *iniline)
-
{
-
FILE *fp;
-
char *str;
-
static char **ini_str;
-
int ini_i;
-
int i,j;
-
-
ini_str = ( char **)malloc(sizeof(char *));
-
*ini_str = ( char *)malloc(sizeof(char *)*1024);
-
str = ( char *)malloc(sizeof(char)*1024);
-
-
if ( (fp = fopen(filename,"rb")) != NULL )
-
{
-
ini_i = 0;
-
while(!feof(fp))
-
{
-
bzero(str,1024);
-
if ( fgets(str,1024,fp) != NULL )
-
{
-
for ( i = 0 ; i < strlen(str); i++ )
-
{
-
/* 切掉 ini 文件中的注释信息 */
-
if ( str[i] == ';' || str[i] == '\n' )
-
{
-
// printf(" == ; == %d \n",i);
-
str[i] = '\0';
-
}
-
}
-
trim(str);
-
if ( str[0] != '\0' && str[0] != '\n') {
-
ini_str[ini_i] = ( char *)malloc(sizeof(char)*1024);
-
snprintf(ini_str[ini_i],1024,"%s",str);
-
//printf("%s\n",str);
-
ini_i++;
-
}
-
}
-
}
-
}
-
fclose(fp);
-
*iniline = ini_i;
-
return (char **)ini_str;
-
}
-
-
-
-
void loadinifile(char *inifilename,int *sec_size)
-
{
-
char **ini_array;
-
int ini_line;
-
int i,j;
-
char *sec_str,*key_str,*val_str;
-
char *token;
-
-
j = 0 ;
-
-
sec_str = ( char *)malloc(sizeof(char)*64);
-
key_str = ( char *)malloc(sizeof(char)*64);
-
val_str = ( char *)malloc(sizeof(char)*128);
-
-
ini_array = read_ini(inifilename,&ini_line);
-
for ( i = 0; i< ini_line ; i++ )
-
printf("%s\n",ini_array[i]);
-
for ( i = 0 ; i< ini_line ; i++ )
-
{
-
//printf(" ===%d=== \n",ini_line);
-
if ( ini_array[i][0] == '[' )
-
{
-
snprintf(sec_str,64,"%s",ini_array[i]);
-
printf("\n=== SEC_NAME = %s \n",sec_str );
-
*sec_size++;
-
} else {
-
//printf("=========== SEC_ARRAY====\n");
-
printf("== KEY_VALUE = %s\n",ini_array[i]);
-
token = strtok(ini_array[i],"=");
-
while( token != NULL )
-
{
-
if ( j %2 == 0 )
-
{
-
snprintf(key_str,64,"%s",token);
-
trim(key_str);
-
printf("== KEY_STR = %s \n",key_str);
-
} else {
-
snprintf(val_str,128,"%s",token);
-
trim(val_str);
-
printf("== VAL_STR = %s \n",val_str);
-
}
-
token=strtok(NULL,"=");
-
j++;
-
}
-
}
-
}
-
}
-
-
-
-
int main ( int argc , char **argv)
-
{
-
char **ini_s;
-
int ini_line;
-
int i;
-
/*
-
ini_s = read_ini(argv[1],&ini_line);
-
-
printf(" === %d ===\n",ini_line);
-
-
for ( i = 0; i < ini_line; i++ )
-
{
-
printf(" == %s =\n",ini_s[i]);
-
}
-
-
*/
-
loadinifile(argv[1],&ini_line);
-
-
return 0;
-
}
ini 文件的内容如下:
-
[PostgreSQL]
-
Description=ODBC for PostgreSQL
-
Driver=/usr/lib/psqlodbcw.so
-
Setup=/usr/lib/libodbcpsqlS.so
-
Driver64=/usr/lib64/psqlodbcw.so
-
Setup64=/usr/lib64/libodbcpsqlS.so
-
FileUsage=1
-
-
[MySQL]
-
Description=ODBC for MySQL
-
Driver=/usr/lib/libmyodbc5.so
-
Setup=/usr/lib/libodbcmyS.so
-
Driver64=/usr/lib64/libmyodbc5.so
-
Setup64=/usr/lib64/libodbcmyS.so
-
FileUsage=1
-
-
[FreeTDS]
-
Description=Free Sybase & MS SQL Driver
-
Driver=/usr/lib/libtdsodbc.so
-
Setup=/usr/lib/libtdsS.so
-
Driver64=/usr/lib64/libtdsodbc.so
-
Setup64=/usr/lib64/libtdsS.so
-
Port=1433
-
-
[MariaDB]
-
Description=ODBC for MariaDB
-
Driver=/usr/lib/libmaodbc.so
-
Driver64=/usr/lib64/libmaodbc.so
-
FileUsage=1
-
-
[MySQL ODBC 8.0 Unicode Driver]
-
Driver=/usr/lib64/libmyodbc8w.so
-
UsageCount=1
-
-
[MySQL ODBC 8.0 ANSI Driver]
-
Driver=/usr/lib64/libmyodbc8a.so
-
UsageCount=1
本程序只是简单的演示了读取 ini 文件到内存中,并将其节点和 key value 分离出来。实际的生产环境还需要考虑注释信息的处理。
阅读(985) | 评论(0) | 转发(0) |