|
|
@@ -18,18 +18,18 @@
|
|
|
#include "swfile.h"
|
|
|
|
|
|
/* 装载文件 */
|
|
|
-int sw_file_load(const char *filename, const char *mode, char *buf, int bufSize)
|
|
|
+int sw_file_load(const char *path, const char *mode, char *buf, int bufSize)
|
|
|
{
|
|
|
FILE *fp = NULL;
|
|
|
int filesize = 0, readsize = 0;
|
|
|
|
|
|
- if(!filename || !mode || !buf || bufSize < 0) return -1;
|
|
|
+ if(!path || !mode || !buf || bufSize < 0) return -1;
|
|
|
|
|
|
- filesize = sw_file_getSize(filename);
|
|
|
+ filesize = sw_file_getSize(path);
|
|
|
if(filesize < 0) return -1;
|
|
|
else if(filesize == 0) return 0;
|
|
|
|
|
|
- fp = fopen(filename, mode);
|
|
|
+ fp = fopen(path, mode);
|
|
|
if(!fp) return -1;
|
|
|
if(filesize > bufSize) readsize = -1;
|
|
|
else readsize = fread(buf, sizeof(char), filesize, fp);
|
|
|
@@ -39,14 +39,14 @@ int sw_file_load(const char *filename, const char *mode, char *buf, int bufSize)
|
|
|
}
|
|
|
|
|
|
/* 更新文件 */
|
|
|
-int sw_file_update(const char *filename, const char *mode, const char *buf, int bufSize)
|
|
|
+int sw_file_update(const char *path, const char *mode, const char *buf, int bufSize)
|
|
|
{
|
|
|
FILE *fp = NULL;
|
|
|
int writesize = 0;
|
|
|
|
|
|
- if(!filename || !mode || !buf || bufSize < 0) return -1;
|
|
|
+ if(!path || !mode || !buf || bufSize < 0) return -1;
|
|
|
|
|
|
- fp = fopen(filename, mode);
|
|
|
+ fp = fopen(path, mode);
|
|
|
if(!fp) return -1;
|
|
|
if(bufSize > 0) writesize = fwrite(buf, sizeof(char), bufSize, fp);
|
|
|
fclose(fp);
|
|
|
@@ -55,13 +55,13 @@ int sw_file_update(const char *filename, const char *mode, const char *buf, int
|
|
|
}
|
|
|
|
|
|
/* 得到文件大小 */
|
|
|
-int sw_file_getSize(const char *filename)
|
|
|
+int sw_file_getSize(const char *path)
|
|
|
{
|
|
|
struct stat statBuf = { 0 };
|
|
|
|
|
|
- if(!filename) return -1;
|
|
|
+ if(!path) return -1;
|
|
|
|
|
|
- if(stat(filename, &statBuf) == 0) return statBuf.st_size;
|
|
|
+ if(stat(path, &statBuf) == 0) return statBuf.st_size;
|
|
|
else return -1;
|
|
|
}
|
|
|
|
|
|
@@ -92,32 +92,32 @@ int sw_file_copy(const char *existingFilename, const char *newFilename, bool fai
|
|
|
}
|
|
|
|
|
|
/* 删除文件 */
|
|
|
-int sw_file_delete(const char *filename)
|
|
|
+int sw_file_delete(const char *path)
|
|
|
{
|
|
|
- return remove(filename);
|
|
|
+ return remove(path);
|
|
|
}
|
|
|
|
|
|
/* 判断文件是否存在 */
|
|
|
-bool sw_file_exists(const char *filename)
|
|
|
+bool sw_file_exists(const char *path)
|
|
|
{
|
|
|
- if(access(filename, 0) == 0) return true;
|
|
|
+ if(access(path, 0) == 0) return true;
|
|
|
else return false;
|
|
|
}
|
|
|
|
|
|
/* 读配置或字典文件(mode : "GetFieldName", 根据"字段值"获取"字段名"; "GetFieldValue", 根据"字段名"获取"字段值") */
|
|
|
-char *sw_file_getCfgString(const char *filename, const char *section, const char *mode, char *field, char *value, int resultBufSize)
|
|
|
+char *sw_file_getCfgString(const char *path, const char *section, const char *mode, char *field, char *value, int resultBufSize)
|
|
|
{
|
|
|
char *pBuf = NULL, *pSection = NULL, *pStr = NULL;
|
|
|
int filesize = 0;
|
|
|
|
|
|
- if(!filename || !mode || !field || !value || resultBufSize <= 0) return NULL;
|
|
|
+ if(!path || !mode || !field || !value || resultBufSize <= 0) return NULL;
|
|
|
|
|
|
- filesize = sw_file_getSize(filename);
|
|
|
+ filesize = sw_file_getSize(path);
|
|
|
if(filesize <= 0) return NULL;
|
|
|
|
|
|
pBuf = (char *)malloc(filesize+1);
|
|
|
if(!pBuf) return NULL;
|
|
|
- if(sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
|
|
|
+ if(sw_file_load(path, "rb", pBuf, filesize) != filesize) goto EndP;
|
|
|
else pBuf[filesize] = '\0';
|
|
|
|
|
|
if(section)
|
|
|
@@ -138,20 +138,20 @@ EndP:
|
|
|
}
|
|
|
|
|
|
/* 写配置或字典文件(mode : "SetFieldName", 根据"字段值"设置"字段名"; "SetFieldValue", 根据"字段名"设置"字段值") */
|
|
|
-bool sw_file_setCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value, bool autoAdd)
|
|
|
+bool sw_file_setCfgString(const char *path, const char *section, const char *mode, const char *field, const char *value, bool autoAdd)
|
|
|
{
|
|
|
char *pBuf = NULL, *pSection = NULL;
|
|
|
int filesize = 0, varysize1 = 0, varysize2 = 0;
|
|
|
bool bRet = false;
|
|
|
|
|
|
- if(!filename || !mode || !field || !value) return false;
|
|
|
+ if(!path || !mode || !field || !value) return false;
|
|
|
|
|
|
- filesize = sw_file_getSize(filename);
|
|
|
+ filesize = sw_file_getSize(path);
|
|
|
if(filesize < 0) filesize = 0;
|
|
|
|
|
|
pBuf = (char *)malloc(filesize+MAX_LINE_CHARS*2);
|
|
|
if(!pBuf) return false;
|
|
|
- if(filesize > 0 && sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
|
|
|
+ if(filesize > 0 && sw_file_load(path, "rb", pBuf, filesize) != filesize) goto EndP;
|
|
|
memset(pBuf+filesize, 0, MAX_LINE_CHARS*2);
|
|
|
|
|
|
if(section)
|
|
|
@@ -183,7 +183,7 @@ bool sw_file_setCfgString(const char *filename, const char *section, const char
|
|
|
if(!bRet) goto EndP;
|
|
|
}
|
|
|
|
|
|
- if(bRet) bRet = (sw_file_update(filename, "wb", pBuf, filesize+varysize1+varysize2) == filesize+varysize1+varysize2);
|
|
|
+ if(bRet) bRet = (sw_file_update(path, "wb", pBuf, filesize+varysize1+varysize2) == filesize+varysize1+varysize2);
|
|
|
|
|
|
EndP:
|
|
|
free(pBuf);
|
|
|
@@ -191,21 +191,21 @@ EndP:
|
|
|
}
|
|
|
|
|
|
/* 从配置或字典文件中删除(mode : "DelFieldByName", 根据"字段名"删除该"字段"; "DelFieldByValue", 根据"字段值"删除该"字段") */
|
|
|
-bool sw_file_delCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value)
|
|
|
+bool sw_file_delCfgString(const char *path, const char *section, const char *mode, const char *field, const char *value)
|
|
|
{
|
|
|
char *pBuf = NULL, *pSection = NULL;
|
|
|
int filesize = 0, varysize = 0;
|
|
|
bool bRet = false;
|
|
|
|
|
|
- if(!filename || !mode || (!field && !value)) return false;
|
|
|
+ if(!path || !mode || (!field && !value)) return false;
|
|
|
|
|
|
- filesize = sw_file_getSize(filename);
|
|
|
+ filesize = sw_file_getSize(path);
|
|
|
if(filesize < 0) return false;
|
|
|
else if(filesize == 0) return true;
|
|
|
|
|
|
pBuf = (char *)malloc(filesize+1);
|
|
|
if(!pBuf) return false;
|
|
|
- if(sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
|
|
|
+ if(sw_file_load(path, "rb", pBuf, filesize) != filesize) goto EndP;
|
|
|
else pBuf[filesize] = '\0';
|
|
|
|
|
|
if(section)
|
|
|
@@ -226,7 +226,7 @@ bool sw_file_delCfgString(const char *filename, const char *section, const char
|
|
|
if(!bRet) goto EndP;
|
|
|
}
|
|
|
|
|
|
- if(bRet) bRet = (sw_file_update(filename, "wb", pBuf, filesize+varysize) == filesize+varysize);
|
|
|
+ if(bRet) bRet = (sw_file_update(path, "wb", pBuf, filesize+varysize) == filesize+varysize);
|
|
|
|
|
|
EndP:
|
|
|
free(pBuf);
|