c语言实现读取ini文件操作

包括三部分:ini.h,ini.c,main.c,代码实现如下:

一、ini.h

#ifndef __INI_H__
#define __INI_H__

#include <windows.h>
#include <tchar.h>

#define DEF_PROFILE_NUM_LEN  64
#define ERROR_OK    0
#define ERROR_NO_SECTION  1
#define ERROR_NO_KEY   2 

typedef enum{
    false,
    true
}alexbool;

typedef struct _record
{
 char comments[DEF_PROFILE_NUM_LEN];
 char key[DEF_PROFILE_NUM_LEN];
 char value[DEF_PROFILE_NUM_LEN];
 struct _record* next;
}record;

typedef struct _section
{
 record* first;
 record* last;
 unsigned int sizeRecord;
 char comments[DEF_PROFILE_NUM_LEN];
 char name[DEF_PROFILE_NUM_LEN];
 struct _section* next;
}section;

typedef struct _content
{
 unsigned int sizeSection;
 section* first;
 section* last;
}content;

#define bool unsigned char

extern bool iniStart(const char* filename);
extern bool iniLoad(const char* filename);
bool iniLoad(const char* lpFile);
extern void iniAddSection(const char* sec, const char* comment);
extern struct _section* iniGetSection(const char* sec);
extern struct _record* iniGetRecord(struct _section* sec, const char* key);
extern char* iniGetValue(const char* sec, const char* key);
extern bool iniSave(const char* filename);
extern int iniRemoveAllSection(const char* section);
extern int iniRemoveSelSection(const char* sec, const char* key);
extern BOOL iniWriteInt(const char* lpSection, const char* lpKey, unsigned int nValue,const char* lpComment);
extern void iniGetInt(const char* lpSection, const char* lpKey, unsigned int* nDefault);
extern DWORD iniGetString(const char* lpSection, const char* lpKey, char* lpBuffer);
extern BOOL iniWriteString(const char* lpSection, const char* lpKey, const char* lpValue, const char* lpComment);
extern void iniGetBool(const char* lpSection, const char* lpKey, BOOL* bDefault);
extern BOOL iniWriteBool(const char* lpSection, const char* lpKey, BOOL bDefault, const char* lpComment);
extern DWORD iniInvertBool(const char* lpSection, const char* lpKey);

#endif


二、实现文件

//ini.c

#include "ini.h"
#include <string.h>
#include <stdio.h>

content* pContent;
char gfilename[255];

void iniGetInt(const char* lpSection, const char* lpKey, unsigned int* nDefault)
{
 char sz[DEF_PROFILE_NUM_LEN + 1] = "";

 iniGetString(lpSection, lpKey, sz);
 sscanf(sz, "%d", nDefault);
}

BOOL iniWriteInt(const char* lpSection, const char* lpKey, unsigned int nValue,
 const char* lpComment)
{
 char szValue[DEF_PROFILE_NUM_LEN + 1] = "";

 sprintf(szValue, "%d", nValue);
 return iniWriteString(lpSection, lpKey, szValue, lpComment);
}

void iniGetBool(const char* lpSection, const char* lpKey, BOOL* bDefault)
{
 char sz[DEF_PROFILE_NUM_LEN + 1] = "";

 iniGetString(lpSection, lpKey, sz);
 sscanf(sz, "%d", bDefault);
 *bDefault > 0 ? 1 : 0;
}

BOOL iniWriteBool(const char* lpSection, const char* lpKey, BOOL bDefault, const char* lpComment)
{
 char szValue[DEF_PROFILE_NUM_LEN + 1] = "";

 sprintf(szValue, "%d", bDefault > 0 ? 1 : 0);
 return iniWriteString(lpSection, lpKey, szValue, lpComment);
}

DWORD iniInvertBool(const char* lpSection, const char* lpKey)
{
 BOOL bDefault;
 char szValue[DEF_PROFILE_NUM_LEN + 1] = "";
 struct _section* psection = iniGetSection(lpSection);
 struct _record* precord = iniGetRecord(psection, lpKey);

 iniGetBool(lpSection,lpKey,&bDefault);
 bDefault > 0 ? 1 : 0;
 sprintf(szValue, "%d", !bDefault);   
 
 if (psection == NULL)
 {
  return ERROR_NO_SECTION;
 } 
  
 if (precord == NULL)
 {
  return ERROR_NO_KEY;
 }

 strcpy(precord->key, lpKey);
 {
  strcpy(precord->value, szValue);
 }
 return ERROR_OK;
}

DWORD iniGetString(const char* lpSection, const char* lpKey, char* lpBuffer)
{
 struct _section* psection = iniGetSection(lpSection);
    struct _record* precord = iniGetRecord(psection, lpKey); 
 if (psection == NULL)
 {
  return ERROR_NO_SECTION;
 }
 
 if (precord == NULL)
 {
  return ERROR_NO_KEY;
 }
 else
 {
  strcpy(lpBuffer, precord->value);
  return ERROR_OK;
 }
}

BOOL iniWriteString(const char* lpSection, const char* lpKey, const char* lpValue,
 const char* lpComment)
{
 struct _section* psection = iniGetSection(lpSection);
    struct _record* precord = iniGetRecord(psection, lpKey);
 if (psection == NULL)
 {
  iniAddSection(lpSection, "");
  psection = iniGetSection(lpSection);
 }
  
 if (precord == NULL)
 {
  precord = (struct _record *) malloc(sizeof(struct _record));
  if (precord == NULL)
  {
   printf("cannot malloc memory !\n");
   return ERROR;
  }
  precord->next = NULL;  

  psection->sizeRecord++;

  if (psection->first == NULL)
  {
   psection->first = precord;
   psection->last = precord;
  }
  else
  {
   psection->last->next = precord;
   psection->last = precord;
  }
 }
 if ((lpComment[0] != '#' || lpComment[0] != ';') && (strlen(lpComment) > 0))
 {
  sprintf(precord->comments, "#%s", lpComment);
 }
 else
 {
  strcpy(precord->comments, lpComment);
 }
 strcpy(precord->key, lpKey);
 {
  strcpy(precord->value, lpValue);
 }
 return ERROR_OK;
}

bool iniStart(const char* lpFile)
{
 pContent = (content *) malloc(sizeof(content));
 if (pContent == NULL)
 {
  printf("cannot malloc memory !\n");
  return false;
 }

 pContent->sizeSection = 0; 
 pContent->first = NULL;
 pContent->last = NULL;

 memcpy(gfilename, lpFile, sizeof(gfilename));
 if (iniLoad(gfilename) == false)
 {
  printf("initial parse file error !\n");
  return false;
 };
 return true;
}

bool iniLoad(const char* lpFile)
{
 FILE* fp;
 char buffer[255];
 char comments[1024];
 char NowSection[255];  
 char key[255];
 char value[255];
 char* pdest;
 int index;

 strcpy(comments, "");
 strcpy(NowSection, "");

 if ((fp = fopen(lpFile, "r")) != NULL)
 {
  while (fgets(buffer, sizeof(buffer), fp) != NULL)
  {
   if (buffer[strlen(buffer) - 1] == '\n')
   {
    buffer[strlen(buffer) - 1] = '\0';
   }
   switch (buffer[0])
   {
   case '[':
    pdest = strrchr(buffer, ']');
    if (pdest == NULL)
    {
     fclose(fp);
     printf("parse ini error!\n");
     return false;
    }
    index = pdest - buffer;
    memcpy(NowSection, buffer + 1, index - 1);
    NowSection[index - 1] = '\0';
    iniAddSection(NowSection, comments); 
    strcpy(comments, "");
    break;
   case '#' :
   case ';' :
    if (strlen(comments) > 0)
    {
     strcat(comments, "\n");
    }
    strcat(comments, buffer);
    break;
   default :
    pdest = strrchr(buffer, '=');
    if (pdest == NULL)
    {
     fclose(fp);
     printf("parse ini error\n");
     return false;
    }
    index = pdest - buffer;
    memcpy(key, buffer, index);
    key[index] = '\0';
    memcpy(value, buffer + index + 1, strlen(buffer) - index);

    if (strcmp(NowSection, "") == 0)
    {
     fclose(fp);
     printf("parse ini error\n");
     return false;
    }
    else
    {
     iniWriteString(NowSection, key, value, comments);
     strcpy(comments, "");
    }
    break;
   }
  }
  fclose(fp);
 }
 else
 {
  printf("open file error !\n");
  return false;
 }
 return true;
}

void iniAddSection(const char* lpSection, const char* lpComment)
{
 struct _section* psection;
 psection = iniGetSection(lpSection);

 if (psection == NULL)
 {
  psection = (struct _section *) malloc(sizeof(struct _section));

  if (psection == NULL)
  {
   printf("cannot malloc memory !\n");
   return;
  }

  strcpy(psection->name, lpSection);

  if ((lpComment[0] != '#' || lpComment[0] != ';') && (strlen(lpComment) > 0))
  {
   sprintf(psection->comments, "#%s", lpComment);
  }
  else
  {
   strcpy(psection->comments, lpComment);
  }

  psection->first = NULL;
  psection->last = NULL;
  psection->next = NULL;
  psection->sizeRecord = 0;

  pContent->sizeSection++;

  if (pContent->first == NULL)
  {
   pContent->first = psection;
   pContent->last = psection;
  }
  else
  {
   pContent->last->next = psection;
   pContent->last = psection;
  }
 }
 else
 {
  strcpy(psection->name, lpSection);
  if ((lpComment[0] != '#' || lpComment[0] != ';') && (strlen(lpComment) > 0))
  {
   sprintf(psection->comments, "#%s", lpComment);
  }
  else
  {
   strcpy(psection->comments, lpComment);
  }
 }
}

struct _section* iniGetSection(const char* lpSection)
{
 bool found = false;

 struct _section* psection = pContent->first;
 while (psection != NULL)
 {
  if (strcmp(psection->name, lpSection) == 0)
  {
   found = true;
   break;
  }    
  psection = psection->next;
 }

 if (found == true)
 {
  return psection;
 }
 else
 {
  return NULL;
 }
}

struct _record* iniGetRecord(struct _section* psection, const char* lpKey)
{
 bool found = false;
 struct _record* pRecord;

 if (psection != NULL)
 {
  pRecord = psection->first;

  while (pRecord != NULL)
  {
   if (strcmp(lpKey, pRecord->key) == 0)
   {
    found = true;
    break;
   }
   pRecord = pRecord->next;
  }
  if (found == true)
  {
   return pRecord;
  }
  else
  {
   return NULL;
  }
 }
 else
 {
  return NULL;
 }
}

char* iniGetValue(const char* lpSeciton, const char* lpKey)
{
 struct _section* psection = iniGetSection(lpSeciton);
    struct _record* precord = iniGetRecord(psection, lpKey);
 if (psection == NULL)
 {
  return "NO Section!";
 }
  
 if (precord == NULL)
 {
  return "No Key!";
 }
 else
 {
  return precord->value;
 }
}

bool iniSave(const char* lpFile)
{
 FILE* fp;
 struct _section* psection = pContent->first;
 struct _record* precord; 


 if ((fp = fopen(lpFile, "w")) == NULL)
 {
  printf("open file error\n");
  return false;
 }

 while (psection != NULL)
 {
  if (strlen(psection->comments) != 0)
  {
   fprintf(fp, "%s\n", psection->comments);
  }
  fprintf(fp, "[%s]\n", psection->name);
  precord = psection->first;
  while (precord != NULL)
  {
   if (strlen(precord->comments) != 0)
   {
    fprintf(fp, "%s\n", precord->comments);
   }
   fprintf(fp, "%s=%s\n", precord->key, precord->value);

   precord = precord->next;
  }  
  psection = psection->next;
 } 
 fclose(fp);
 return true;
}

int iniRemoveAllSection(const char* lpSection)
{
 struct _section* psection = iniGetSection(lpSection);
 struct _record* precord;
 int remove = 0;

 if (psection == NULL)
  return 0;

 precord = psection->first;
 while (precord != NULL)
 {
  psection->first = precord->next;
  psection->sizeRecord--;
  free(precord);
  remove++;
  precord = psection->first;
 }
 return remove;
}

int iniRemoveSelSection(const char* lpSection, const char* lpKey)
{
 struct _section* psection = iniGetSection(lpSection);
 struct _record* precord1, * precord2;
 int remove = 0;

 if (psection == NULL)
  return 0;

 precord1 = psection->first;

 if (precord1 == NULL)
  return 0;

 if (strcmp(lpKey, precord1->key) == 0)
 {
  psection->first = precord1->next;
  psection->sizeRecord--;
  free(precord1);
  return 1;
 }

 while (precord1 != NULL)
 {
  if (precord1->next != NULL)
  {
   if (strcmp(lpKey, precord1->next->key) == 0)
   {
    precord2 = precord1->next;    
    precord1->next = precord1->next->next;
    psection->sizeRecord--;
    free(precord2);    
    remove = 1;
    break;
   }
  }  
  precord1 = precord1->next;
 }  
 return remove;
}


三、实现实例//main.c

#include "ini.h"
#include "stdio.h"

void main(void)
{
  char buf[20];
 unsigned int data;
 BOOL bTest;

 iniStart("e:/PG/VC/ini/sample.ini");

 iniWriteString("section1","key1","1","");
 iniWriteString("section1","key2","2","comment2");
 iniWriteInt("section2","key1",1234,"");
 iniWriteInt("section2","key2",5678,"comment2");
 iniWriteBool("section3","key1",100,"bool comment1 true");
 iniWriteBool("section3","key2",0,"bool comment2 false");
 iniInvertBool("section3","key1");
 iniInvertBool("section3","key2");

 iniGetString("section1","key1",buf);
 printf("key1=%s\n",buf);
 iniGetString("section1","key2",buf);
 printf("key2=%s\n",buf);
 iniGetInt("section2","key1",&data);
 printf("key1=%d\n",data);
 iniGetInt("section2","key2",&data);
 printf("key2=%d\n",data);
 iniGetBool("section3","key1",&bTest);
 printf("key1=%d\n",bTest);
 iniGetBool("section3","key2",&bTest);
 printf("key2=%d\n",bTest);

 

 iniSave("e:/PG/VC/ini/sample.ini");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值