📄 inifile.c
字号:
// --iniFile.c------
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include "inifile.h"
#define MAXROWS 100
char ApplicationDirectory[1024];
struct ITEMS Items[MAXROWS];
void GetApplicationPath(char *strPath)
{
int i=0,j=0;
while((strPath[i]) != '\0')
{
if(strPath[i] == '/' )
{
j = i;
}
i++;
}
j++;
strncpy(ApplicationDirectory,strPath,j);
}
int ReadINI(char *FileName)
{
FILE *fp;
int i =0;
int itemnum,ifitemvalue;
int ifitemhead=0,itemheadrow = 0;
char path[1024];
char itemhead[50];
strcpy(path,ApplicationDirectory);
strcat(path,FileName);
itemnum = 0;
fp=fopen(path,"r");
if(fp==NULL)return i;
char ch;
int j = 0;
int k = 0;
int n = 0;
while(!feof(fp))
{
ch = fgetc(fp);
if (!isalnum(ch)) //非字母字符处理
{
if(ch == '\n')
{
Items[itemnum].Item[k] = '\0';
Items[itemnum].ItemValue[j] = '\0';
k = 0;
j = 0;
if(ifitemvalue == 1)
{
itemnum++;
}else if(itemheadrow == 1)
{
itemnum++;
itemheadrow = 0;
}
ifitemvalue = 0;
if(itemnum > 100)
{
return -1;
}
strcpy(Items[itemnum].Itemhead,itemhead); //
n = 0;
ifitemhead = 0;
}
if(ch == '=')ifitemvalue = 1;
if(ch == '.' && ifitemvalue == 1)
{
Items[itemnum].ItemValue[j] = ch;
j++;
}
if(ch == '[')ifitemhead = 1;
if((ch == ']') && (ifitemhead == 1))
{
Items[itemnum].Itemhead[n] = '\0';
strcpy(itemhead,Items[itemnum].Itemhead); //
ifitemhead = 0;
itemheadrow = 1;
}
}else
{//字母字符处理
if (ifitemvalue == 1)
{
Items[itemnum].ItemValue[j] = ch;
j++;
}else{
Items[itemnum].Item[k] = ch;
k++;
}
if(ifitemhead == 1)
{
Items[itemnum].Itemhead[n] = ch;
n++;
}
}
}
Items[itemnum].Item[k] = '\0';
Items[itemnum].ItemValue[j] = '\0';
fclose(fp);
i = itemnum++;
return i;
}
char *GetItem(char * itemhead,char *Item)
{
int i=0;
int p;
int h;
while(i < MAXROWS)
{
h = strcmp(Items[i].Itemhead,itemhead);
p = strcmp(Items[i].Item,Item);
if((p == 0) && (h == 0))
{
return Items[i].ItemValue;
}
i++;
}
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -