|
nt add(void *handle, const char *key, const char *value) { int ret = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL; if (handle == NULL || key == NULL || value == NULL) { ret = -1; printf("fun add error:%d from (handle == NULL || key == NULL || value == NULL)\n",ret); return ret; } ph = (PROPS_HANDLE *)handle; //-----------如果key存在链表中,则直接修改,否则添加到链表中-----------// pCurrent = ph->pHead; while (pCurrent->pNext != NULL) { if (strcmp(pCurrent->pNext->key,key) == 0) { break; } pCurrent = pCurrent->pNext; } if (pCurrent->pNext != NULL) { return setValue(handle, key, value); } //-----------key不存在,创建一个新的配置项,添加到链表中-----------// Properties *pMalloc; ret = createPropsNode(&pMalloc); if (ret != 0) { printf("fun add error:%d from malloc new node.",ret); return ret; } strcpy(pMalloc->key, key); if (strchr(pCurrent->value,'\n') == NULL) { strcat(pCurrent->value, "\n"); } strcpy(pMalloc->value, value); if (strchr(value, '\n') == NULL) { // 加一个换行符 strcat(pMalloc->value, "\n"); } pCurrent->pNext = pMalloc; // 新配置项入链表 // 将新配置项写入到文件 ret = saveConfig(ph->filepath, ph->pHead); return ret; } // 删除一个属性,删除成功返回0,失败返回非0值 int del(void *handle, const char *key) { int ret = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL, *pPrev = NULL; if (handle == NULL || key == NULL) { ret = -1; printf("fun del error:%d from (handle == NULL || key == NULL)\n",ret); return ret; } ph = (PROPS_HANDLE *)handle; pPrev = ph->pHead; pCurrent = ph->pHead->pNext; while (pCurrent != NULL) { if (strcmp(pCurrent->key, key) == 0) { break; } pPrev = pCurrent; // 上一个节点下移 pCurrent = pCurrent->pNext; // 当前节点下移 } if (pCurrent == NULL) { // 没有找到 ret = -2; printf("fun del warning:not found the key:%s\n",key); return ret; } pPrev->pNext = pCurrent->pNext; // 从链表中删除 free(pCurrent); // 释放内存 pCurrent = NULL; // 保存到文件 ret = saveConfig(ph->filepath, ph->pHead); return ret; } // 获取属性文件中所有的key,获取成功返回0,失败返回非0值 int getKeys(void *handle, char ***keys, int *keyscount) { int ret = 0, count = 0, index = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL; char **pKeys = NULL; if (handle == NULL || keys == NULL || keyscount == NULL) { ret = -1; printf("fun getKeys error:%d from (handle == NULL || keys == NULL || keyscount == NULL) \n",ret); return ret; } // 获取配置项数量 ret = getCount(handle, &count); if (ret != 0) { printf("fun getKeys error:%d from getCount \n",ret); return ret; } ph = (PROPS_HANDLE *)handle; pCurrent = ph->pHead->pNext; // 根据链表长度,申请内存空间 pKeys = (char **)malloc(sizeof(char *) * count); if (pKeys == NULL) { ret = -2; printf("fun getKeys error:%d from malloc keys\n",ret); return ret; } pCurrent = ph->pHead->pNext; while (pCurrent != NULL) { pKeys[index] = pCurrent->key; pCurrent = pCurrent->pNext; index++; } *keys = pKeys; *keyscount = count; return ret; } // 释放所有key的内存空间,成功返回0,失败返回非0值 int free_keys(char ***keys,int *keyscount) { int ret = 0; if (keys == NULL || keyscount == NULL) { ret = -1; printf("fun free_keys error:%d from (keys == NULL || keyscount == NULL) \n",ret); return ret; } free(*keys); *keys = NULL; *keyscount = 0; return ret; } // 获取属性文件中所有的值,成功返回0,失败返回非0值 int getValues(void *handle, char ***values, int *valuescount) { int ret = 0, count = 0, index = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL; char **pValues = NULL; if (handle == NULL || values == NULL || valuescount == NULL) { ret = -1; printf("fun getValues error:%d from (handle == NULL || values == NULL || valuescount == NULL)\n",ret); return ret; } // 获取配置项数量 ret = getCount(handle, &count); if (ret != 0) { printf("fun getValues error:%d from getC |