#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
int mkstemp(char *tmp)
{
strcpy(tmp,"tmp123.txt");
return 0;
}
#endif
#define MAX_CFG_BUF 512
#define CFG_OK 0
#define CFG_SECTION_NOT_FOUND -1
#define CFG_KEY_NOT_FOUND -2
#define CFG_ERR -10
#define CFG_ERR_FILE -10
#define CFG_ERR_OPEN_FILE -10
#define CFG_ERR_CREATE_FILE -11
#define CFG_ERR_READ_FILE -12
#define CFG_ERR_WRITE_FILE -13
#define CFG_ERR_FILE_FORMAT -14
#define CFG_ERR_SYSTEM -20
#define CFG_ERR_SYSTEM_CALL -20
#define CFG_ERR_INTERNAL -21
#define CFG_ERR_EXCEED_BUF_SIZE -22
#define COPYF_OK 0
#define COPYF_ERR_OPEN_FILE -10
#define COPYF_ERR_CREATE_FILE -11
#define COPYF_ERR_READ_FILE -12
#define COPYF_ERR_WRITE_FILE -13
#define TXTF_OK 0
#define TXTF_ERR_OPEN_FILE -1
#define TXTF_ERR_READ_FILE -2
#define TXTF_ERR_WRITE_FILE -3
#define TXTF_ERR_DELETE_FILE -4
#define TXTF_ERR_NOT_FOUND -5
char CFG_ssl = '[', CFG_ssr = ']';
int CFG_section_line_no, CFG_key_line_no, CFG_key_lines;
char * strtrimr(char * buf)
{
int len,i;
char * tmp = NULL;
len = strlen(buf);
tmp = (char*)malloc(len);
memset(tmp,0x00,len);
for(i = 0;i < len;i++)
{
if (buf[i] !=' ')
break;
}
if (i < len)
{
strncpy(tmp,(buf+i),(len-i));
}
strncpy(buf,tmp,len);
free(tmp);
return buf;
}
char * strtriml(char * buf)
{
int len,i;
char * tmp=NULL;
len = strlen(buf);
tmp = (char*)malloc(len);
memset(tmp,0x00,len);
for(i = 0;i < len;i++)
{
if (buf[len-i-1] !=' ')
break;
}
if (i < len)
{
strncpy(tmp,buf,len-i);
}
strncpy(buf,tmp,len);
free(tmp);
return buf;
}
int FileGetLine(FILE *fp, char *buffer, int maxlen)
{
int i, j;
char ch1;
for(i = 0, j = 0; i < maxlen; j++)
{
if(fread(&ch1, sizeof(char), 1, fp) != 1)
{
if(feof(fp) != 0)
{
if(j == 0)
return -1; /* 文件结束 */
else
break;
}
if(ferror(fp) != 0)
return -2; /* 读文件出错 */
return -2;
}
else
{
if(ch1 == '\n' || ch1 == 0x00)
break; /* 换行 */
if(ch1 == '\f' || ch1 == 0x1A) /* '\f':换页符也算有效字符 */
{
buffer[i++] = ch1;
break;
}
if(ch1 != '\r')
buffer[i++] = ch1; /* 忽略回车符 */
}
}
buffer[i] = '\0';
return i;
}
int FileCopy(char *source_file, char *dest_file)
{
FILE *fp1, *fp2;
char buf[1024+1];
int ret;
if((fp1 = fopen((char *)source_file, "r")) == NULL)
return COPYF_ERR_OPEN_FILE;
ret = COPYF_ERR_CREATE_FILE;
if((fp2 = fopen((char *)dest_file, "w")) == NULL)
goto copy_end;
while(1)
{
ret = COPYF_ERR_READ_FILE;
memset(buf, 0x00, 1024+1);
if(fgets((char *)buf, 1024, fp1) == NULL)
{
if(strlen(buf) == 0)
{
if(ferror(fp1) != 0)
goto copy_end;
break; /* 文件尾 */
}
}
ret = COPYF_ERR_WRITE_FILE;
if(fputs((char *)buf, fp2) == EOF)
goto copy_end;
}
ret = COPYF_OK;
copy_end:
if(fp2 != NULL)
fclose(fp2);
if(fp1 != NULL)
fclose(fp1);
return ret;
}
int SplitKeyValue(char *buf, char **key, char **val)
{
int i, k1, k2, n;
if((n = strlen((char *)buf)) < 1)
return 0;
for(i = 0; i < n; i++)
if(buf[i] != ' ' && buf[i] != '\t')
break;
if(i >= n)
return 0;
if(buf[i] == '=')
return -1;
k1 = i;
for(i++; i < n; i++)
if(buf[i] == '=')
break;
if(i >= n)
return -2;
k2 = i;
for(i++; i < n; i++)
if(buf[i] != ' ' && buf[i] != '\t')
break;
buf[k2] = '\0';
*key = buf + k1;
*val = buf + i;
return 1;
}
int ConfigGetKey(char *CFG_file, char *section, char *key, char *buf)
{
FILE *fp;
char buf1[MAX_CFG_BUF + 1], buf2[MAX_CFG_BUF + 1];
char *key_ptr, *val_ptr;
int line_no, n, ret;
line_no = 0;
CFG_section_line_no = 0;
CFG_key_line_no = 0;
CFG_key_lines = 0;
if((fp = fopen((char *)CFG_file, "rb")) == NULL)
return CFG_ERR_OPEN_FILE;
while(1) /* 搜找项section */
{
ret = CFG_ERR_READ_FILE;
n = FileGetLine(fp, buf1, MAX_CFG_BUF);
if(n < -1)
goto r_cfg_end;
ret = CFG_SECTION_NOT_FOUND;
if(n < 0)
goto r_cfg_end; /* 文件尾,未发现 */
line_no++;
n = strlen(strtriml(strtrimr(buf1)));
if(n == 0 || buf1[0] == '#' || buf1[0] == ';')
continue; /* 空行 或 注释行 */
ret = CFG_ERR_FILE_FORMAT;
if(n > 2 && ((buf1[0] == CFG_ssl && buf1[n-1] != CFG_ssr)))
goto r_cfg_end;
if(buf1[0] == CFG_ssl)
{
buf1[n-1] = 0x00;
if(strcmp(buf1+1, section) == 0)
break; /* 找到项section */
}
}
CFG_section_line_no = line_no;
while(1) /* 搜找key */
{
ret = CFG_ERR_READ_FILE;
n = FileGetLine(fp, buf1, MAX_CFG_BUF);
if(n < -1)
goto r_cfg_end;
ret = CFG_KEY_NOT_FOUND;
if(n < 0)
goto r_cfg_end; /* 文件尾,未发现key */
line_no++;
CFG_key_line_no = line_no;
CFG_key_lines = 1;
n = strlen(strtriml(strtrimr(buf1)));
if(n == 0 || buf1[0] == '#' || buf1[0] == ';')
continue; /* 空行 或 注释行 */
ret = CFG_KEY_NOT_FOUND;
if(buf1[0] == CFG_ssl)
goto r_cfg_end;
if(buf1[n-1] == '+') /* 遇+号表示下一行继续 */
{
buf1[n-1] = 0x00;
while(1)
{
ret = CFG_ERR_READ_FILE;
n = FileGetLine(fp, buf2, MAX_CFG_BUF);
if(n < -1)
goto r_cfg_end;
if(n < 0)
break; /* 文件结束 */
line_no++;
CFG_key_lines++;
n = strlen(strtrimr(buf2));
ret = CFG_ERR_EXCEED_BUF_SIZE;
if(n > 0 && buf2[n-1] == '+') /* 遇+号表示下一行继续 */
{
buf2[n-1] = 0x00;
if(strlen(buf1) + strlen(buf2) > MAX_CFG_BUF)
goto r_cfg_end;
strcat(buf1, buf2);
continue;
}
if(strlen(buf1) + strlen(buf2) > MAX_CFG_BUF)
goto r_cfg_end;
strcat(buf1, buf2);
break;
}
}
ret = CFG_ERR_FILE_FORMAT;
if(SplitKeyValue(buf1, &key_ptr, &val_ptr) != 1)
goto r_cfg_end;
strtriml(strtrimr(key_ptr));
if(strcmp(key_ptr, key) != 0)
continue; /* 和key值不匹配 */
strcpy(buf, val_ptr);
break;
}
ret = CFG_OK;
r_cfg_end:
if(fp != NULL)
fclose(fp);
return ret;
}
int ConfigSetKey(char *CFG_file, char *section, char *key, char *buf)
{
FILE *fp1, *fp2;
char buf1[MAX_CFG_BUF + 1];
int line_no, line_no1, n, ret, ret2;
char tmpfname[128] = "/tmp/tempfileXXXXXX";
int fd;
ret = ConfigGetKey(CFG_file, section, key, buf1);
if(ret <= CFG_ERR && ret != CFG_ERR_OPEN_FILE)
return ret;
if(ret == CFG_ERR_OPEN_FILE || ret == CFG_SECTION_NOT_FOUND)
{
if((fp1 = fopen((char *)CFG_file, "a")) == NULL)
return CFG_ERR_CREATE_FILE;
if(fprintf(fp1, "%c%s%c\n", CFG_ssl, section, CFG_ssr) == EOF)
{
fclose(fp1);
return CFG_ERR_WRITE_FILE;
}
if(fprintf(fp1, "%s=%s\n", key, buf) == EOF)
{
fclose(fp1);
return CFG_ERR_WRITE_FILE;
}
fclose(fp1);
return CFG_OK;
}
if ((fd = mkstemp(tmpfname)) == -1)
{
return CFG_ERR_CREATE_FILE;
}
if((fp2 = fopen(tmpfname, "w")) == NULL)
return CFG_ERR_CREATE_FILE;
ret2 = CFG_ERR_OPEN_FILE;
if((fp1 = fopen((char *)CFG_file, "rb")) == NULL)
goto w_cfg_end;
if(ret == CFG_KEY_NOT_FOUND)
line_no1 = CFG_section_line_no;
else /* ret = CFG_OK */
line_no1 = CFG_key_line_no - 1;
for(line_no = 0; line_no < line_no1; line_no++)
{
ret2 = CFG_ERR_READ_FILE;
n = FileGetLine(fp1, buf1, MAX_CFG_BUF);
if(n < 0)
goto w_cfg_end;
ret2 = CFG_ERR_WRITE_FILE;
if(fprintf(fp2, "%s\n", buf1) == EOF)
goto w_cfg_end;
}
if(ret != CFG_KEY_NOT_FOUND)
for( ; line_no < line_no1+CFG_key_lines; line_no++)
{
ret2 = CFG_ERR_READ_FILE;
n = FileGetLine(fp1, buf1, MAX_CFG_BUF);
if(n < 0)
goto w_cfg_end;
}
ret2 = CFG_ERR_WRITE_FILE;
if(fprintf(fp2, "%s=%s\n", key, buf) == EOF)
goto w_cfg_end;
while(1)
{
ret2 = CFG_ERR_READ_FILE;
n = FileGetLine(fp1, buf1, MAX_CFG_BUF);
if(n < -1)
goto w_cfg_end;
if(n < 0)
break;
ret2 = CFG_ERR_WRITE_FILE;
if(fprintf(fp2, "%s\n", buf1) == EOF)
goto w_cfg_end;
}
ret2 = CFG_OK;
w_cfg_end:
if(fp1 != NULL)
fclose(fp1);
if(fp2 != NULL)
fclose(fp2);
if(ret2 == CFG_OK)
{
ret = FileCopy(tmpfname, CFG_file);
if(ret != 0)
return CFG_ERR_CREATE_FILE;
}
remove(tmpfname);
return ret2;
}
void main(void)
{
char buf[128]="";
int ret;
int age=0;
ret = ConfigSetKey("a.ini", "system", "age", "123");
ret = ConfigGetKey("a.ini", "system", "age", buf);
if (strcmp(buf,"")!=0)
{
age = atol(buf);
}
printf("\n buf=%s\n",buf);
printf("\n age=%ld\n",age);
ConfigSetKey("a.ini","param", "name", "yue");
}
//win下vc6亦可用