ount \n",ret); return ret; } // 申请内存空间,存放所有的value pValues = (char **)malloc(sizeof(char *) * count); if (pValues == NULL) { ret = -2; printf("fun getValues error:%d from malloc values\n",ret); return ret; } ph = (PROPS_HANDLE *)handle; pCurrent = ph->pHead->pNext; while (pCurrent != NULL) { pValues[index] = pCurrent->value; pCurrent = pCurrent->pNext; index++; } *values = pValues; *valuescount = count; return ret; } // 释放所有value的内存空间,成功返回0,失败返回非0值 int free_values(char ***values, int *valuescount) { int ret = 0; if (values == NULL || valuescount == NULL) { ret = -1; printf("fun free_values error:%d from (values == NULL || valuescount == NULL) \n",ret); return ret; } free(*values); *values = NULL; *valuescount = 0; return ret; } // 释放环境资源,成功返回0,失败返回非0值 int release(void **handle) { int ret = 0; PROPS_HANDLE *ph = NULL; if(handle == NULL) { ret = -1; printf("release error:%d from (handler == NULL)\n",ret); return ret; } ph = (PROPS_HANDLE *)*handle; // 释放链表内存资源 Properties *pCurr = ph->pHead; Properties *pTemp = NULL; while (pCurr != NULL) { if (pCurr->key != NULL) { free(pCurr->key); pCurr->key = NULL; } if (pCurr->value != NULL) { free(pCurr->value); pCurr->value = NULL; } pTemp = pCurr->pNext; free(pCurr); pCurr = pTemp; } // 释放存放配置文件路径分配的内存空间 if(ph->filepath != NULL) { free(ph->filepath); ph->filepath = NULL; } // 释放环境句柄本身 free(ph); *handle = NULL; // 避免野指针 return ret; } // 去空格 static int trimeSpace(const char *src,char *dest) { int ret = 0; if (src == NULL || dest == NULL) { ret = -1; printf("trimeSpace error:%d from (src == NULL || dest == NULL)\n",ret); return ret; } const char *psrc = src; unsigned long i = 0,j = strlen(psrc) - 1,len; while (psrc[i] == ' ') { i++; } while (psrc[j] == ' ') { j--; } len = j - i + 1; memcpy(dest,psrc+i,len); *(dest+len) = '\0'; return ret; } // 创建一个节点 static int createPropsNode(Properties **props) { int ret = 0; Properties *p = NULL; if (props == NULL) { ret = -100; printf("createProps error:%d from (props == NULL)\n",ret); return ret; } p = (Properties *)malloc(sizeof(Properties)); if (p == NULL) { ret = -200; printf("createProps malloc %ld bytes error:%d\n",sizeof(Properties),ret); return ret; } p->key = (char *)malloc(KEY_SIZE); p->value = (char *)malloc(VALUE_SIZE); p->pNext = NULL; *props = p; return ret; } // 保存到文件 static int saveConfig(const char *filepath,Properties *head) { int ret = 0,writeLen = 0; FILE *fp = NULL; Properties *pCurrent = NULL; if (filepath == NULL || head == NULL) { ret = -100; printf("fun saveConfig error:%d from (filepath == NULL || head == NULL)\n",ret); return ret; } fp = fopen(filepath,"w"); if (fp == NULL) { ret = -200; printf("fun saveConfig:open file error:%d from %s\n",ret,filepath); return ret; } pCurrent = head->pNext; while (pCurrent != NULL) { writeLen = fprintf(fp, "%s=%s",pCurrent->key,pCurrent->value); // 返回写入的字节数,出现错误返回一个负值 if (writeLen < 0) { //TODO 如果写入失败,如何将写入的数据回退??? ret = -300; printf("fun saveConfig err:%d from (%s=%s)\n",ret,pCurrent->key,pCurrent->value); break; } pCurrent = pCurrent->pNext; } fclose(fp); // 关闭文件 return ret; }
3、测试代码(需要在项目根目录创建props.txt)
//
// main.c
// 读写配置文件
//
// Created by 杨信 on 14-4-24.
// Copyright (c) 2014年 yangxin. All rights reserved.
//
#include
#include
#include
#include "Properties.h" int main(int argc, const char * argv[]) { int ret; void *handle; const char *filepath = "/Users/yangxin/Desktop/props.txt"; // 初始化 ret = init(filepath, &handle); if (ret != 0) { printf("env init error:%d\n",ret); return 0; } char valuebuf[128]; // 测试获取配置项 ret = getValue(handle, "host", valuebuf); if (ret == 0) { printf("value=%s\n",valuebuf); } else { printf("获取值 |