/*
通用C语言函数:文本文件有关操作,.CFG/.INI类配置文件读写。我们知道WINDOWS下的软件可以使用.INI文件
处理程序配置,那么在WIN32、*NIX、Solaris上又如何才能读写.CFG/.INI类配置文件? 你可以使用本程序的有关函
数很方便地实现。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef unsigned char USGC;
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifdef WIN32
#define snprintf _snprintf
#endif
struct txt_line_flds
{
int fldsp; /* fld start pointer */
int fldlen; /* fld len */
};
#define MAX_TXT_LINE_FLD 128
#define MAX_CFG_BUF 512
#define INI_OK 0
#define INI_SECTION_NOT_FOUND -1
#define INI_KEY_NOT_FOUND -2
#define INI_ERR -10
#define INI_ERR_FILE -10
#define INI_ERR_OPEN_FILE -10
#define INI_ERR_CREATE_FILE -11
#define INI_ERR_READ_FILE -12
#define INI_ERR_WRITE_FILE -13
#define INI_ERR_FILE_FORMAT -14
#define INI_ERR_SYSTEM -20
#define INI_ERR_SYSTEM_CALL -20
#define INI_ERR_INTERNAL -21
#define INI_ERR_EXCEED_BUF_SIZE -22
#define INI_ERR_NULLSECTION -23
#define INI_ERR_NULLKEY -24
#define INI_ERR_NULLBUF -25
#define INI_ERR_NULLINIFILE -26
#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
int fgetline(FILE* fp, USGC* buffer, int maxlen);
int copy_txt_file(FILE* source_file_fp, void* dest_file);
int split_txt_line_fld(char* txt_line, char* sep_chars);
char* get_txt_line_fld(char* txt_line, int fldno, char* buf);
int split_key_val(USGC* buf, USGC** key, USGC** val);
int read_txt_line(void* txt_file, int line_no, void* buf);
int seek_txt_line(void* txt_file, void* line_tag, void* buf);
int cfg_get_sections(void* cfg_file, USGC* sections[]);
int cfg_get_keys(void* cfg_file, void* section, USGC* keys[]);
struct txt_line_flds txt_line_fld[MAX_TXT_LINE_FLD];
char cfg_ssl = '[', cfg_ssr = ']'; /* .CFG/.INI file section symbol */
int cfg_section_line_no, cfg_key_line_no, cfg_key_lines;
char* myini_trim(char* pStr)
{
char* pBegin;
char* pEnd;
long len;
if (pStr == NULL)
{
fprintf(stderr, "myini_trim(): Null para pStr\n");
return NULL;
}
len=strlen(pStr);
if(len<=0)
{
return pStr;
}
if(len>1000)
{
fprintf(stderr, "myini_trim(): pStr's len %d is wrong.",(int)len);
return NULL;
}
pBegin = pStr;
pEnd = pStr + strlen(pStr) - 1;
while (isspace(*pBegin) && *pBegin != '\0')
pBegin ++;
while (isspace(*pEnd) && pEnd > pBegin)
pEnd --;
len = pEnd - pBegin + 1;
memmove(pStr, pBegin, len);
pStr[len] = '\0';
return pStr;
}
char* myini_getword(char* pStr, char* pWord, int Num)
{
char* pBegin;
char* pEnd;
int i;
if (pStr == NULL)
{
fprintf(stderr, "GetWord(): Null para pStr\n");
return NULL;
}
if (pWord == NULL)
{
fprintf(stderr, "GetWord(): Null para pWord\n");
return NULL;
}
if (Num < 1)
{
fprintf(stderr, "GetWord(): Invalid para Num -- %d\n", Num);
return NULL;
}
pEnd = pStr - 1;
for (i = 0; i < Num; i ++)
{
pBegin = pEnd + 1;
while (*pBegin != '\0' && isspace(*pBegin))
pBegin ++;
if (*pBegin == '\0') return NULL;
pEnd = pBegin + 1;
while (*pEnd != '\0' && !isspace(*pEnd))
pEnd ++;
if (*pEnd == '\0') break;
}
if (i+1 < Num) return NULL;
strncpy(pWord, pBegin, pEnd-pBegin);
pWord[pEnd-pBegin] = '\0';
return pWord;
}
/**************************************************************************
* The Alternate of Unix fnmatch(), support '?', '*'
*************************************************************************/
int myini_fnmatch(const char *pattern, const char *string, int flags)
{
int c1=0, c2=0, len1=0, len2=0, match=0;
char *pPos=NULL;
char Pattern[200], String[200];
memset(Pattern, 0x00, sizeof(Pattern));
memset(String, 0x00, sizeof(String));
sprintf(Pattern, "%s", pattern);
sprintf(String, "%s", string);
len1 = strlen(Pattern);
len2 = strlen(String);
while (c1 < len1 && c2 < len2)
{
if (Pattern[c1] == '?')
{
c1++;
c2++;
}
else if (Pattern[c1] == '*')
{
c1++;
while (c1 <= len1 && Pattern[c1] != String[c2] && c2 < len2)
{
c2++;
}
}
else
{
if (Pattern[c1] == String[c2])
{
c1++;
c2++;
}
else
{
match = -1;
break;
}
}
}
if ((c1 == len1) && (c2 == len2) && (match == 0))
{
return 0;
}
else
{
return -1;
}
}
/* ------------------------------------------------------------------------- */
/*
Read 1 line text into *buffer from file *fp, return length actually read.
maxlen --- max. length of *buffer
CR or LF is not put into *buffer
FF(0x0C) is put into *buffer and then return
Ctrl-Z(0x1A) is put into *buffer and then return
If EOF was detected then return -1
If an error occurred then return -2
*/
int fgetline(FILE* fp, USGC* buffer, int maxlen)
{
int i, j;
USGC ch1;
for (i = 0, j = 0; i < maxlen; j++)
{
if (fread(&ch1, sizeof(USGC), 1, fp) != 1)
{
if (feof(fp) != 0)
{
if (j == 0)
return -1; /* EOF */
else
break;
}
if (ferror(fp) != 0)
return -2; /* error */
return -2;
}
else
{
if (ch1 == '\n' || ch1 == 0x00)
break; /* end of line */
if (ch1 == '\f' || ch1 == 0x1A) /* '\f': Form Feed */
{
buffer[i++] = ch1;
break;
}
if (ch1 != '\r')
buffer[i++] = ch1; /* ignore CR */
}
}
buffer[i] = '\0';
return i;
}
/* ------------------------------------------------------------------------- */
/*
Copy source_file to dest_file
Return code:
0 -- Ok
<0 -- Error
*/
//int copy_txt_file(void* source_file, void* dest_file)
int copy_txt_file(FILE* source_file_fp, void* dest_file)
{
FILE* fp1, * fp2;
USGC buf[1024 + 1];
int rc;
fp1 = source_file_fp;
// if ((fp1 = fopen((char *) source_file, "r")) == NULL)
if(fp1 == NULL)
return COPYF_ERR_OPEN_FILE;
rc = COPYF_ERR_CREATE_FILE;
if ((fp2 = fopen((char *) dest_file, "w")) == NULL)
goto copy_end;
while (1)
{
rc = 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; /* EOF */
}
}
rc = COPYF_ERR_WRITE_FILE;
if (fputs((char *) buf, fp2) == EOF)
goto copy_end;
纯C语言读写ini配置文件
4星 · 超过85%的资源 需积分: 49 170 浏览量
2013-10-17
19:02:32
上传
评论 4
收藏 231KB RAR 举报
kvw3000
- 粉丝: 10
- 资源: 5
最新资源
- STM32单片机FPGA毕设电路原理论文报告一种具有传统中医针刺补泻手法的新型智能电针仪设计
- 2023-04-06-项目笔记 - 第七十七阶段 - 4.4.2.75全局变量的作用域-75 -2024.03.19
- VuforiaObjectScanner-8-3-8.apk.1.1.1
- 上下班打卡_日报_20240201-20240319.xlsx
- 创业天下3.5.500.apk
- POD-data.mat
- ZF逆变器课程电子档及源码
- FileZilla-3.66.5-win64-sponsored2-setup
- SourceTreeSetup-3.4.17
- Docker Desktop Installer
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈