swfile.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: swfile.c
  4. * CONTENT: 常用的文件操作接口函数
  5. * NOTE:
  6. * HISTORY:
  7. * 1, [2010-08-19] created by NiuJiuRu
  8. * 2, [2014-01-10] 整体优化修改
  9. * 3, [2014-01-22] 整体优化修改
  10. * 4, [2014-06-24] a) 修正"sw_file_setCfgString()"函数设置已存在的节时, 无
  11. * 法设置成功的问题
  12. * b) 修正"sw_file_setCfgString()"函数自动新增的节前没有回
  13. * 车换行的问题
  14. * c) 加强"sw_file_setCfgString()"函数的缓存溢出判断
  15. ************************************************************************/
  16. #include "swapi.h"
  17. #include "swstring.h"
  18. #include "swfile.h"
  19. /* 装载文件 */
  20. int sw_file_load(const char *filename, const char *mode, char *buf, int bufSize)
  21. {
  22. FILE *fp = NULL;
  23. int filesize = 0, readsize = 0;
  24. if(!filename || !mode || !buf || bufSize < 0) return -1;
  25. filesize = sw_file_getSize(filename);
  26. if(filesize < 0) return -1;
  27. else if(filesize == 0) return 0;
  28. fp = fopen(filename, mode);
  29. if(!fp) return -1;
  30. if(filesize > bufSize) readsize = -1;
  31. else readsize = fread(buf, sizeof(char), filesize, fp);
  32. fclose(fp);
  33. return readsize;
  34. }
  35. /* 更新文件 */
  36. int sw_file_update(const char *filename, const char *mode, const char *buf, int bufSize)
  37. {
  38. FILE *fp = NULL;
  39. int writesize = 0;
  40. if(!filename || !mode || !buf || bufSize < 0) return -1;
  41. fp = fopen(filename, mode);
  42. if(!fp) return -1;
  43. if(bufSize > 0) writesize = fwrite(buf, sizeof(char), bufSize, fp);
  44. fclose(fp);
  45. return writesize;
  46. }
  47. /* 得到文件大小 */
  48. int sw_file_getSize(const char *filename)
  49. {
  50. struct stat statBuf = { 0 };
  51. if(!filename) return -1;
  52. if(stat(filename, &statBuf) == 0) return statBuf.st_size;
  53. else return -1;
  54. }
  55. /* 重命名文件 */
  56. int sw_file_rename(const char *oldName, const char *newName)
  57. {
  58. return rename(oldName, newName);
  59. }
  60. /* 拷贝文件 */
  61. int sw_file_copy(const char *existingFilename, const char *newFilename, bool failIfExists)
  62. {
  63. char *pBuf = NULL;
  64. int filesize = 0;
  65. if(!existingFilename || !newFilename || (failIfExists && sw_file_exists(newFilename))) return -1;
  66. filesize = sw_file_getSize(existingFilename);
  67. if(filesize < 0) return -1;
  68. pBuf = (char *)malloc(filesize);
  69. if(!pBuf) return -1;
  70. if(sw_file_load(existingFilename, "rb", pBuf, filesize) != filesize || \
  71. sw_file_update(newFilename, "wb", pBuf, filesize) != filesize) filesize = -1;
  72. free(pBuf);
  73. return filesize;
  74. }
  75. /* 删除文件 */
  76. int sw_file_delete(const char *filename)
  77. {
  78. return remove(filename);
  79. }
  80. /* 判断文件是否存在 */
  81. bool sw_file_exists(const char *filename)
  82. {
  83. if(access(filename, 0) == 0) return true;
  84. else return false;
  85. }
  86. /* 读配置或字典文件(mode : "GetFieldName", 根据"字段值"获取"字段名"; "GetFieldValue", 根据"字段名"获取"字段值") */
  87. char *sw_file_getCfgString(const char *filename, const char *section, const char *mode, char *field, char *value, int resultBufSize)
  88. {
  89. char *pBuf = NULL, *pSection = NULL, *pStr = NULL;
  90. int filesize = 0;
  91. if(!filename || !mode || !field || !value || resultBufSize <= 0) return NULL;
  92. filesize = sw_file_getSize(filename);
  93. if(filesize <= 0) return NULL;
  94. pBuf = (char *)malloc(filesize+1);
  95. if(!pBuf) return NULL;
  96. if(sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
  97. else pBuf[filesize] = '\0';
  98. if(section)
  99. {
  100. pSection = xstrcasestr(pBuf, "left", section);
  101. if(!pSection) goto EndP;
  102. }
  103. else pSection = pBuf;
  104. if(xstrcasecmp(mode, "GetFieldName") == 0)
  105. pStr = sw_str_getFieldName(pSection, filesize-(pSection-pBuf), 0, field, resultBufSize, value);
  106. else if(xstrcasecmp(mode, "GetFieldValue") == 0)
  107. pStr = sw_str_getFieldValue(pSection, filesize-(pSection-pBuf), 0, field, value, resultBufSize);
  108. EndP:
  109. free(pBuf);
  110. return pStr;
  111. }
  112. /* 写配置或字典文件(mode : "SetFieldName", 根据"字段值"设置"字段名"; "SetFieldValue", 根据"字段名"设置"字段值") */
  113. bool sw_file_setCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value, bool autoAdd)
  114. {
  115. char *pBuf = NULL, *pSection = NULL;
  116. int filesize = 0, varysize1 = 0, varysize2 = 0;
  117. bool bRet = false;
  118. if(!filename || !mode || !field || !value) return false;
  119. filesize = sw_file_getSize(filename);
  120. if(filesize < 0) filesize = 0;
  121. pBuf = (char *)malloc(filesize+MAX_LINE_CHARS*2);
  122. if(!pBuf) return false;
  123. if(filesize > 0 && sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
  124. memset(pBuf+filesize, 0, MAX_LINE_CHARS*2);
  125. if(section)
  126. {
  127. pSection = xstrcasestr(pBuf, "left", section);
  128. if(!pSection)
  129. {
  130. if(autoAdd && (strlen(section)+4) <= MAX_LINE_CHARS)
  131. {
  132. pSection = pBuf+filesize;
  133. sprintf(pSection, "\r\n%s\r\n", section);
  134. varysize1 = strlen(section)+4;
  135. }
  136. else goto EndP;
  137. }
  138. }
  139. else pSection = pBuf;
  140. if((strlen(field)+strlen(value)) > MAX_LINE_CHARS) goto EndP;
  141. if(xstrcasecmp(mode, "SetFieldName") == 0)
  142. {
  143. bRet = sw_str_setFieldName(pSection, filesize+MAX_LINE_CHARS*2-(pSection-pBuf), 0, field, value, autoAdd, &varysize2);
  144. if(!bRet) goto EndP;
  145. }
  146. else if(xstrcasecmp(mode, "SetFieldValue") == 0)
  147. {
  148. bRet = sw_str_setFieldValue(pSection, filesize+MAX_LINE_CHARS*2-(pSection-pBuf), 0, field, value, autoAdd, &varysize2);
  149. if(!bRet) goto EndP;
  150. }
  151. if(bRet) bRet = (sw_file_update(filename, "wb", pBuf, filesize+varysize1+varysize2) == filesize+varysize1+varysize2);
  152. EndP:
  153. free(pBuf);
  154. return bRet;
  155. }
  156. /* 从配置或字典文件中删除(mode : "DelFieldByName", 根据"字段名"删除该"字段"; "DelFieldByValue", 根据"字段值"删除该"字段") */
  157. bool sw_file_delCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value)
  158. {
  159. char *pBuf = NULL, *pSection = NULL;
  160. int filesize = 0, varysize = 0;
  161. bool bRet = false;
  162. if(!filename || !mode || (!field && !value)) return false;
  163. filesize = sw_file_getSize(filename);
  164. if(filesize < 0) return false;
  165. else if(filesize == 0) return true;
  166. pBuf = (char *)malloc(filesize+1);
  167. if(!pBuf) return false;
  168. if(sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
  169. else pBuf[filesize] = '\0';
  170. if(section)
  171. {
  172. pSection = xstrcasestr(pBuf, "left", section);
  173. if(!pSection) { bRet = true; goto EndP; }
  174. }
  175. else pSection = pBuf;
  176. if(xstrcasecmp(mode, "DelFieldByName") == 0)
  177. {
  178. bRet = sw_str_delFieldByName(pSection, filesize-(pSection-pBuf), 0, field, &varysize);
  179. if(!bRet) goto EndP;
  180. }
  181. else if(xstrcasecmp(mode, "DelFieldByValue") == 0)
  182. {
  183. bRet = sw_str_delFieldByValue(pSection, filesize-(pSection-pBuf), 0, value, &varysize);
  184. if(!bRet) goto EndP;
  185. }
  186. if(bRet) bRet = (sw_file_update(filename, "wb", pBuf, filesize+varysize) == filesize+varysize);
  187. EndP:
  188. free(pBuf);
  189. return bRet;
  190. }