/*********************************************************************
**
** This program is offer two function GetIniValue() and SetIniValue()
** to access ini file, to get a value from ini file just call GetIni-
** Value(IniFilename, GroupName, ValueName, Value). to set a value in
** ini file, just call SetIniValue(IniFileName, GroupName, ValueName,
** Value), when ini file not found, SetIniValue() function will create
** it. if group name or value name not found, SetIniFile() will create
** it and then set the value.
**
** Note: 1. in ini file, '#' is a comment charater, the content follow-
** ed it will be ignored;
** 2. group name, value name and value is a string that must not
** include space or tab character;
** 3. the length of group name, value name and value must less
** than 100;
** 4. in ini file group name is like [abc]. it must within '[' and
** ']';
** 5. when get value from ini file, all space and tab are ignored;
** 6. max length of a line is less than 1000.
** Return value:
** GetIniValue:
** 0: suceess
** -11: can not open ini file
** -22: pointer to the value is null
** -21: value not found
**
** SetIniValue:
** 0: suceess
** -11: can not open ini file
** -12: write value error
** -20: alloc memory error
** -100: unknow error due to fseek
** Ini file example:
** [groupname]
** valuename = value #this a example
**
***********************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include "readini.h"
#include "myerror.h"
/*去除字符串中的空白字符*/
void TrimSpace(char *sSource);
void SetValue(char *sValue, char *sBuff);
int WriteRemain(FILE *fpIniFile,
long lReadPosition,
long lWritePosition,
char *sWriteBuff
);
/*
** this function is to get value from ini file.
**
**
** Note: 1. '#' is the rem char, if '#' at the head of line then the
** line is a comment line or if '#' at the middle means the
** content following by '#' is ignored.
** 2. the length of group name, value name must less than 100.
** group name not include '[' and ']'.
** 3. in ini file group name is like [XXX].
**
** return value:
** 0: suceess
** 11: can not open ini file
** 22: point to the value is null
** -21: value not found
**
*/
int GetIniValue(char *sIniFile,
char *sGroup,
char *sValueName,
char *sValue
)
{
FILE *fpIniFile;
char ReadBuff[RBUFFSIZE];
char group[101];
char tmpGroup[103];
char valuename[101];
unchar bContinue=1;
unchar bGetValue=0;
int num;
int len;
len = strlen(sGroup);
if(len > 100) len = 100;
strcpy(tmpGroup, "[");
strncat(tmpGroup, sGroup, len);
strcat(tmpGroup, "]");
if(!sValue) return PARAMERROR;
if((fpIniFile = fopen(sIniFile,"r")) == NULL)
return OPEN_FILE_ERR;
while(!feof(fpIniFile) && bContinue) {
memset(ReadBuff, 0, RBUFFSIZE);
memset(group, 0, sizeof(group));
num = 0;
fgets(ReadBuff,RBUFFSIZE-1, fpIniFile);
TrimSpace(ReadBuff);
if(ReadBuff[0] == REMCHAR) continue;
if(ReadBuff[0] != GROUPCHAR) continue;
num = strlen(ReadBuff) - 1;
if( num > 100) num = 100;
strncpy(group, ReadBuff, num);
/* get group name ? */
if(strncmp(group, tmpGroup, num)) continue;
while(!feof(fpIniFile)) {
memset(ReadBuff, 0, RBUFFSIZE);
memset(valuename, 0, sizeof(valuename));
num = 0;
fgets(ReadBuff,RBUFFSIZE-1, fpIniFile);
/*printf("pp%spp\n",ReadBuff);*/
TrimSpace(ReadBuff);
/*printf("oo%soo\n",ReadBuff);*/
if(ReadBuff[0] == REMCHAR) continue;
if(ReadBuff[0] == GROUPCHAR) {
bContinue = 0;
break;
}
while(ReadBuff[num] != '=' && num <= 100) num++;
strncpy(valuename, ReadBuff, num);
/* get value name ? */
if(strncmp(valuename, sValueName, num)) continue;
num++;
strcpy(sValue, &ReadBuff[num]);
bContinue = 0;
bGetValue = 1;
break;
}
}
if(fpIniFile) fclose(fpIniFile);
if(bGetValue) return 0;
else return NOTFOUND;
}
/*
**
** this function is to set value in ini file.
**
**
** Note: 1. '#' is the rem char, if '#' at the head of line then the
** line is a comment line or if '#' at the middle means the
** content following by '#' is ignored.
** 2. the length of group name, value name must less than 100.
** group name not include '[' and ']'.
** 3. in inifilename group name is like [XXX].
**
** return value:
** 0 : suceess
** -11 : can not open ini file
** -12 : write value error
** -20 : alloc memory error
** -100 : unknow error due to fseek
**
*/
int SetIniValue(char *sIniFile,
char *sGroup,
char *sValueName,
char *sValue
)
{
FILE *fpIniFile;
char ReadBuff[RBUFFSIZE];
char tmpBuff[RBUFFSIZE];
char group[101];
char tmpGroup[103];
char valuename[101];
unchar bContinue=1;
unchar bGroupFound=0;
unchar bValueNameFound=0;
int num;
int len;
int ret = 0;
long lGroupPosition;
long lValuePosition;
long lCurrentPosition;
len = strlen(sGroup);
if(len > 100) len = 100;
strcpy(tmpGroup, "[");
strncat(tmpGroup, sGroup, len);
strcat(tmpGroup, "]");
if(!sValue) return PARAMERROR;
if(access(sIniFile, F_OK)) {
if((fpIniFile = fopen(sIniFile, "w")) == NULL)
return OPEN_FILE_ERR;
fprintf(fpIniFile,"%s\n", tmpGroup);
fprintf(fpIniFile,"%s = %s\n", sValueName, sValue);
return 0;
}
if(access(sIniFile, W_OK | R_OK))
return ACCESSERROR;
if((fpIniFile = fopen(sIniFile,"r+w")) == NULL)
return OPEN_FILE_ERR;
while(!feof(fpIniFile) && bContinue) {
memset(ReadBuff, 0, RBUFFSIZE);
memset(group, 0, sizeof(group));
num = 0;
fgets(ReadBuff,RBUFFSIZE-1, fpIniFile);
TrimSpace(ReadBuff);
if(ReadBuff[0] == REMCHAR) continue;
if(ReadBuff[0] != GROUPCHAR) continue;
num = strlen(ReadBuff) - 1;
if( num > 100) num = 100;
strncpy(group, ReadBuff, num);
/* get group name ? */
if(strncmp(group, tmpGroup, num)) continue;
bGroupFound = 1;
lGroupPosition = ftell(fpIniFile);
while(!feof(fpIniFile)) {
memset(ReadBuff, 0, RBUFFSIZE);
memset(valuename, 0, sizeof(valuename));
num = 0;
lValuePosition = ftell(fpIniFile);
fgets(ReadBuff,RBUFFSIZE-1, fpIniFile);
memcpy(tmpBuff, ReadBuff, RBUFFSIZE);
TrimSpace(tmpBuff);
if(tmpBuff[0] == REMCHAR) continue;
if(tmpBuff[0] == GROUPCHAR) {
bContinue = 0;
break;
}
while(tmpBuff[num] != '=' && num <= 100) num++;
strncpy(valuename, tmpBuff, num);
/* get value name ? */
if(strncmp(valuename, sValueName, num)) continue;
rohnaw
- 粉丝: 3
- 资源: 6
最新资源
- (源码)基于Arduino和Firebase的智能家庭管理系统NodeSmartHome.zip
- (源码)基于C++的East Zone DSTADSO Robotics Challenge 2019机器人控制系统.zip
- (源码)基于Arduino平台的焊接站控制系统.zip
- (源码)基于ESPboy系统的TZXDuino WiFi项目.zip
- (源码)基于Java的剧场账单管理系统.zip
- (源码)基于Java Swing的船只资料管理系统.zip
- (源码)基于Python框架的模拟购物系统.zip
- (源码)基于C++的图书管理系统.zip
- (源码)基于Arduino的简易温度显示系统.zip
- (源码)基于Arduino的智能电动轮椅系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页