📄 inifile.h
字号:
/*************************************************************
copywrite (c) Michael Zhao 2002
For Unix and Linux, but not limited to them.
[ History ]
2003-05-11 Zhao,Shi-liang Create the initial file
2003-05-12 Zhao,Shi-liang
(1) Support space in value, such as //[test] a=I am all right// returns
"I am all right" instead of "I"
(2) Add "GetIniInteger", "GetIniLong", "GetIniDouble", "SetIniInteger",
"SetIniLong", "SetIniDouble"
2003-05-13 Zhao,Shi-liang
(1) Add "GetSectionValues", "GetSections"
(2) Support Unix/Linux-style Ini File Only ( ending with '\n' )
(3) Add "ToUnixStyle" to support Windows Ini Files (ending with '\r\n')
*************************************************************/
#ifndef __CINIFILE_H__
#define __CINIFILE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define MAX_BUFFER_SIZE 6000
#define MAX_VALUE_BUFFER_SIZE 128
char szBuffer[MAX_BUFFER_SIZE];
int iBufferLen=0;
int bBufferChanged=0;
FILE *fpIni=NULL;
//Declarations of Interfaces
int OpenIniFile(const char *);
int CloseIniFile(void);
int ToUnixStyle(void);
int GetIniString(const char *, const char *, char *);
int SetIniString(const char *, const char *, const char *);
int GetIniInteger(const char *, const char *, const int);
long GetIniLong(const char *, const char *, const long);
double GetIniDouble(const char *, const char *, const double);
int SetIniInteger(const char *, const char *, const int);
int SetIniLong(const char *, const char *, const long);
int SetIniDouble(const char *, const char *, const double);
int GetSectionValues(const char *, const char *, char *);
int GetSections(const char *, char *);
// End of declaration of interfaces
/**********************************************
Name : OpenIniFile
Input :
char *File_name : file to be open
Output :
0 : File succefully opened
-1 : Fail to open given file
-2 : Fail to read buffer data
Process: Normal
***********************************************/
int OpenIniFile(const char *pFile)
{
struct stat statbuf;
stat(pFile,&statbuf);
iBufferLen = 0; bBufferChanged = 0;
if ((fpIni=fopen(pFile, "r+")) == NULL) return -1;
if(fread(szBuffer,statbuf.st_size,1,fpIni) != 1) {
if (fpIni != NULL) fclose(fpIni);
fpIni = NULL;
return -2;
}
iBufferLen = statbuf.st_size;
return 0;
}
/**********************************************
Name : CloseIniFile
Input : None
Output :
0 : File succefully closed
-1 : Fail to close already opened file
Process: Normal
***********************************************/
int CloseIniFile(void)
{
if (fpIni != NULL) {
if (bBufferChanged) {
rewind(fpIni);
fwrite(szBuffer, iBufferLen, 1, fpIni);
}
if (!fclose(fpIni)) return 0;
else return -1;
} else return 0;
}
/**********************************************
Name : ToUnixStyle
Input : None
Output :
0 : File succefully closed
-1 : Fail to close already opened file
Process: Normal
***********************************************/
int ToUnixStyle(void)
{
int i=0;
if (fpIni==NULL) return -1;
while (i<iBufferLen) {
if (szBuffer[i]=='\r') { szBuffer[i]=' '; bBufferChanged=1; }
i++;
}
return 0;
}
/**********************************************
Name : GetIniString
Input :
char *pSection : Session Name
char *pIdent: Identity Name
char *pResult: Returned string
Output :
0 : Identity Value successfully returned
-1 : Fail to get the designated identity value
Process: Normal
***********************************************/
int GetIniString(const char *pSection,const char *pIdent, char *pResult)
{
int i=0;
int j=0;
int min;
int iKeyFound=-1;
if (!strlen(pSection) || !strlen(pIdent) || (fpIni == NULL)) return -1;
while (i<iBufferLen) {
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen) return -1;
if (szBuffer[i]=='#') { //ignore the lines beginning with '#'
while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
if (i>=iBufferLen) return -1;
//Jump to the next line
i++;
} else {
if (szBuffer[i]=='[') {
i++;
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen) return -1;
if (strncmp(szBuffer+i, pSection, strlen(pSection))==0) {
//Section may be found, let's see
i += strlen(pSection);
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen) return -1;
if (szBuffer[i]==']') iKeyFound=0; i++;
//matched ] or not, ignore the line
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) return -1;
//Jump to the new line
i++;
} else { //ignore the line and forward
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) return -1;
//Jump to the next line
i++;
}
} else {
if (iKeyFound != 0) { //Section has not found, ignore the line
while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
if (i>=iBufferLen) return -1;
//Jump to the new line
i++;
} else { //it may be the Identity to be found, judge it
if (strncmp(szBuffer+i, pIdent, strlen(pIdent))==0) {
i += strlen(pIdent);
if (i>=iBufferLen) return -1;
while ((i<iBufferLen) &&
((szBuffer[i]=='\t') || (szBuffer[i]==' '))) i++;
if (szBuffer[i] == '=') { //Value has found
i++;
while ((i<iBufferLen) &&
((szBuffer[i]=='\t') || (szBuffer[i]==' '))) i++;
if (i>=iBufferLen) return -1;
j=i;
while ((j<iBufferLen) &&
(szBuffer[j]!='\n')) j++; j--;
while ((szBuffer[j]==' ') ||
(szBuffer[j]=='\t')) j--;
min = j-i+1;
strncpy(pResult, szBuffer+i, min);
*(pResult+min) = '\0';
return 0;
} else { //ignore the line
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) return -1;
//Jump to the next line
i++;
}
} else { //ignore the line
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) return -1;
//Jump to the next line
i++;
}
}
}
}
}
return -1;
}
/**********************************************
Name : SetIniString
Input :
char *pSection : Session Name
char *pIdent: Identity Name
char *pValue: Identity Value to write
Output :
0: value is successfully written
-1: parameter error or file not open
Process: Normal
***********************************************/
int SetIniString(const char *pSection,const char *pIdent,const char *pValue)
{
int i=0;
int j=0;
int k=0;
int iBufferMore = 0;
int bKeyFound=0,bIdentFound=0;
int iKeyPos=0,iIdentPos=0;
//Parameter is empty or file has not been openned
if (!strlen(pSection) || !strlen(pIdent) || (fpIni == NULL)) return -1;
while (i<iBufferLen) {
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen) break;
if (szBuffer[i]=='#') { //ignore the lines beginning with '#'
while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
if (i>=iBufferLen) break;
//Jump to the next line
i++;
} else {
if (szBuffer[i]=='[') {
i++;
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen) break;
if (strncmp(szBuffer+i, pSection, strlen(pSection))==0) {
//key may be found, let's see
i += strlen(pSection);
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen) break;
if (szBuffer[i]==']') bKeyFound=1; i++;
//matched ] or not, ignore the line
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) break;
//Jump to the new line
i++;
iKeyPos = i;
} else { //ignore the line and forward
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) break;
//Jump to the next line
i++;
}
} else {
if (!bKeyFound) { //key has not found, ignore the line
while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
if (i>=iBufferLen) break;
//Jump to the new line
i++;
} else { //it may be the Identity to be found, judge it
if (strncmp(szBuffer+i, pIdent, strlen(pIdent))==0) {
i += strlen(pIdent);
if (i>=iBufferLen) break;
while ((i<iBufferLen) &&
((szBuffer[i]=='\t') || (szBuffer[i]==' '))) i++;
if (szBuffer[i] == '=') { //Value has found
i++; iIdentPos=i;
bIdentFound=1;
break;
} else { //ignore the line
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) break;
//Jump to the next line
i++;
}
} else { //ignore the line
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) break;
//Jump to the next line
i++;
}
}
}
}
}
//write lines, if appropriate.
int tempLen = 0;
if (bKeyFound) {
if (bIdentFound) {
i=iIdentPos; j=0;
while ((i<iBufferLen) && (szBuffer[i] != '\n')) { i++; j++; }
tempLen = strlen(pValue);
if (tempLen<=j) {
//Space is enough, no additional space
strncpy(szBuffer+iIdentPos, pValue, strlen(pValue));
for (i=strlen(pValue); i<j; i++) szBuffer[iIdentPos+i]=' ';
iBufferMore = 0;
} else {
k = strlen(pValue);
//Space is limited, need additional space
for (i=iBufferLen; i>=iIdentPos+j; i--)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -