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;
}
/*
* 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 */