#ifndef _H_INI_INCLUDE
#define _H_INI_INCLUDE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <dirent.h>
#define STATIC static
#define char_tolower(c) (unsigned char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
#define char_toupper(c) (unsigned char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
STATIC char *ltrim(char *s)
{
if (s)
{
while(*s && *s==' ')
{
s++;
}
return s;
}
return 0;
}
STATIC char *rtrim(char *s)
{
if (s)
{
char *p=s+strlen(s);
while(p!=s && (*p==' ' || *p==0) )
{
*p=0; p--;
}
return s;
}
return 0;
}
STATIC char *trim(char *s)
{
return rtrim(ltrim(s));
}
STATIC char *ltrim_section(char *s)
{
if (s)
{
while(*s && *s=='[')
{
s++;
}
return s;
}
return 0;
}
STATIC char *rtrim_section(char *s)
{
if (s)
{
char *p=s+strlen(s);
while(p!=s && (*p==']' || *p==0) )
{
*p=0; p--;
}
return s;
}
return 0;
}
STATIC char *trim_section(char *s)
{
return rtrim_section(ltrim_section(s));
}
/*
STATIC char *str_tolower(char *s)
{
if (s)
{
char *p=s;
while(*p)
{
*p=char_tolower(*p);
p++;
}
}
return s;
}
STATIC char *str_toupper(char *s)
{
if (s)
{
char *p=s;
while(*p)
{
*p=char_toupper(*p);
p++;
}
}
return s;
}
*/
#ifdef _NGX_STRING_H_INCLUDED_
#define _strcasecmp ngx_strcasecmp
#define _strncasecmp ngx_strncasecmp
#else
STATIC unsigned int _strcasecmp(unsigned char *s1, unsigned char *s2)
{
unsigned int c1, c2;
for ( ;; ) {
c1 = (unsigned int) *s1++;
c2 = (unsigned int) *s2++;
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
if (c1 == c2) {
if (c1) {
continue;
}
return 0;
}
return c1 - c2;
}
}
STATIC unsigned int strncasecmp(unsigned char *s1, unsigned char *s2, size_t n)
{
unsigned int c1, c2;
while (n) {
c1 = (unsigned int) *s1++;
c2 = (unsigned int) *s2++;
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
if (c1 == c2) {
if (c1) {
n--;
continue;
}
return 0;
}
return c1 - c2;
}
return 0;
}
#endif
typedef struct
{
unsigned int len;
char *buf;
unsigned int nline;
char **plines;
unsigned int ngroup;
unsigned int *pgroup;
}INIFILE;
STATIC void scan_ini(INIFILE *ini)
{
char *pstr=ini->buf;
char *p;
unsigned int i,n;
for (i=0;i<ini->nline;i++)
{
p=pstr;
pstr=strchr(pstr,'\n');
pstr[0]=0;
p=(char *)trim(p);
if(*p=='[' && !strchr(p,'=')) ini->ngroup++;
pstr++;
ini->plines[i]=p;
}
ini->pgroup=(unsigned int *)malloc(ini->ngroup*sizeof(unsigned int));
if (ini->pgroup)
{
n=0;
for (i=0;i<ini->nline;i++)
{
if (*ini->plines[i]=='[' && !strchr(ini->plines[i],'='))
{
ini->pgroup[n]=i;
n++;
}
}
}
}
STATIC void ini_free(INIFILE *ini, unsigned int freethis)
{
if (ini)
{
if (ini->pgroup) {free(ini->pgroup);ini->pgroup=0;}
if (ini->plines) {free(ini->plines);ini->plines=0;}
if (ini->buf) {free(ini->buf);ini->buf=0;}
if (freethis) free(ini);
}
}
STATIC INIFILE *ini_new()
{
return (INIFILE *)malloc(sizeof(INIFILE));
}
STATIC unsigned int read_ini(INIFILE *ini, const char *file)
{
unsigned int ret;
unsigned int i;
ret=0;
if (ini)
{
memset(ini,0,sizeof(INIFILE));
FILE *pf=fopen(file,"rb");
if (pf)
{
fseek(pf,0,SEEK_END);
ini->len=ftell(pf);
if (ini->len)
{
ini->buf=(char *)malloc(ini->len+2);
if (ini->buf)
{
i=0;
fseek(pf,0,SEEK_SET);
while(!feof(pf))
{
ini->buf[i]=fgetc(pf);
if (ini->buf[i]=='\n') ini->nline++;
if (ini->buf[i]!='\r') i++;
}
if (i && ini->buf[i-1]!='\n')
{
ini->buf[i]='\n';
i++;
}
ini->buf[i]=0;
if (ini->nline)
{
ini->plines=(char **)malloc(ini->nline*sizeof(char *));
scan_ini(ini);
ret=1;
}
}
}
fclose(pf);
}
}
return ret;
}
STATIC const char *get_group(INIFILE * ini, unsigned int iGruop)
{
if (ini->plines && ini->pgroup)
{
return trim_section(ini->plines[ini->pgroup[iGruop]]);
}
return 0;
}
STATIC const char *get_key_str(INIFILE * ini, unsigned int iGruop, const char *szkey)
{
unsigned int in_keylen;
if (ini->plines && ini->pgroup && szkey && (in_keylen=strlen(szkey)) )
{
unsigned int i;
for (i=ini->pgroup[iGruop]+1;i<ini->nline;i++)
{
if (*ini->plines[i])
{
char keyname1[100];
char keyname2[100];
char *pval=strchr(ini->plines[i],'=');
if (pval)
{
unsigned int th_keylen=pval-ini->plines[i];
if (th_keylen)
{
if (th_keylen>=100) th_keylen=99;
if (in_keylen>=100) in_keylen=99;
memcpy(keyname1,ini->plines[i],th_keylen);
keyname1[th_keylen]=0;
memcpy(keyname2,szkey,in_keylen);
keyname2[in_keylen]=0;
if ( _strcasecmp((unsigned char *)trim(keyname1),(unsigned char *)trim(keyname2))==0 )
{
pval++;
return trim(pval);
}
}
}else if (*trim(ini->plines[i])=='['){
break;
}
}
}
}
return 0;
}
STATIC unsigned int get_key_uint(INIFILE * ini, unsigned int iGruop, const char *szkey)
{
unsigned int ret=0;
const char *p=get_key_str(ini,iGruop,szkey);
if (p && *p>='0' && *p<='9')
{
ret=(unsigned int)strtoul(p,0,10);
}
return ret;
}
#endif
ini reading class
Guess you like
Origin blog.csdn.net/TDGX2004/article/details/7086448
Recommended
Ranking