tc_file.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_FILE_H_
  17. #define __TC_FILE_H_
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include "util/tc_platform.h"
  21. #include "util/tc_port.h"
  22. #include <iostream>
  23. #include <fstream>
  24. #include "util/tc_ex.h"
  25. #include "util/tc_common.h"
  26. #if TARGET_PLATFORM_LINUX ||TARGET_PLATFORM_IOS
  27. #define FILE_SEP "/"
  28. #else
  29. #define FILE_SEP "\\"
  30. #endif
  31. namespace tars
  32. {
  33. /////////////////////////////////////////////////
  34. /**
  35. * @file tc_file.h
  36. * @brief 文件处理类.
  37. *
  38. */
  39. /////////////////////////////////////////////////
  40. /**
  41. * @brief 文件异常类.
  42. *
  43. */
  44. struct TC_File_Exception : public TC_Exception
  45. {
  46. TC_File_Exception(const string &buffer) : TC_Exception(buffer){};
  47. TC_File_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
  48. ~TC_File_Exception() throw(){};
  49. };
  50. /**
  51. * @brief 常用文件名处理函数.
  52. *
  53. */
  54. class TC_File
  55. {
  56. public:
  57. /**
  58. * @brief 获取文件大小, 如果文件不存在, 则返回-1.
  59. *
  60. * @param sFullFileName 文件全路径(所在目录和文件名)
  61. * @return ofstream::pos_type类型文件大小
  62. */
  63. static ifstream::pos_type getFileSize(const string &sFullFileName);
  64. /**
  65. * @brief 判断是否为绝对路径, 忽略空格以'/'开头.
  66. *
  67. * @param sFullFileName 文件全路径(所在目录和文件名)
  68. * @return ture是绝对路径,false代表非绝对路径
  69. */
  70. static bool isAbsolute(const string &sFullFileName);
  71. /**
  72. * @brief 判断给定路径的文件是否存在.
  73. * 如果文件是符号连接,则以符号连接判断而不是以符号连接指向的文件判断
  74. * @param sFullFileName 文件全路径
  75. * @param iFileType 文件类型, 缺省S_IFREG
  76. * @return true代表存在,fals代表不存在
  77. */
  78. static bool isFileExist(const string &sFullFileName, mode_t iFileType = S_IFREG);
  79. /**
  80. * @brief 判断给定路径的文件是否存在.
  81. * 注意: 如果文件是符号连接,则以符号连接指向的文件判断
  82. * @param sFullFileName 文件全路径
  83. * @param iFileType 文件类型, 缺省S_IFREG
  84. * @return true-存在,fals-不存在
  85. */
  86. static bool isFileExistEx(const string &sFullFileName, mode_t iFileType = S_IFREG);
  87. /**
  88. * @brief 规则化目录名称, 把一些不用的去掉, 例如./等.
  89. *
  90. * @param path 目录名称
  91. * @return 规范后的目录名称
  92. */
  93. static string simplifyDirectory(const string& path);
  94. /**
  95. * @brief 创建目录, 如果目录已经存在, 则也返回成功.
  96. *
  97. * @param sFullPath 要创建的目录名称
  98. * @return bool true-创建成功 ,false-创建失败
  99. */
  100. static bool makeDir(const string &sDirectoryPath);
  101. /**
  102. *@brief 循环创建目录, 如果目录已经存在, 则也返回成功.
  103. *
  104. * @param sFullPath 要创建的目录名称
  105. * @return true-创建成功,false-创建失败
  106. */
  107. static bool makeDirRecursive(const string &sDirectoryPath);
  108. /**
  109. * @brief 删除一个文件或目录.
  110. *
  111. * @param sFullFileName 文件或者目录的全路径
  112. * @param bRecursive 如果是目录是否递归删除
  113. * @return 0-成功,失败可以通过errno查看失败的原因
  114. */
  115. static int removeFile(const string &sFullFileName, bool bRecursive);
  116. /**
  117. * @brief 重命名一个文件或目录.
  118. *
  119. * @param sSrcFullFileName 源文件名
  120. * @param sDstFullFileName 目的文件名
  121. * @return 0-成功,失败可以通过errno查看失败的原因
  122. */
  123. static int renameFile(const string &sSrcFullFileName, const string &sDstFullFileName);
  124. /**
  125. * @brief 读取文件到string
  126. * 文件存在则返回文件数据,不存在或者读取文件错误的时候, 返回为空
  127. * @param sFullFileName 文件名称
  128. * @return 文件数据
  129. */
  130. static string load2str(const string &sFullFileName);
  131. static bool load2str(const string &sFullFileName, vector<char> &data);
  132. /**
  133. * @brief 写文件.
  134. *
  135. * @param sFullFileName 文件名称
  136. * @param sFileData 文件内容
  137. * @return
  138. */
  139. static void save2file(const string &sFullFileName, const string &sFileData);
  140. /**
  141. * @brief 写文件.
  142. *
  143. * @param sFullFileName 文件名
  144. * @param sFileData 数据指针
  145. * @param length 写入长度
  146. * @return 0-成功,-1-失败
  147. */
  148. static int save2file(const string &sFullFileName, const char *sFileData, size_t length);
  149. /**
  150. * @brief 设置文件是否可执行.
  151. *
  152. * @param sFullFileName 文件全路径
  153. * @param canExecutable true表示可执行, false代表不可之行
  154. * @return 成功返回0, 其他失败
  155. */
  156. static int setExecutable(const string &sFullFileName, bool canExecutable);
  157. /**
  158. * @brief 判断文件是否可执行.
  159. *
  160. * @param sFullFileName 文件全路径
  161. * @return true-可执行, false-不可执行
  162. */
  163. static bool canExecutable(const string &sFullFileName);
  164. /**
  165. * @brief 获取前当可执行文件路径.
  166. *
  167. * @return string 可执行文件的路径全名称
  168. */
  169. static string getExePath();
  170. /**
  171. * @brief 提取文件名称
  172. *从一个完全文件名中去掉路径,例如:/usr/local/temp.gif获取temp.gif
  173. *@param sFullFileName 文件的完全名称
  174. *@return string 提取后的文件名称
  175. */
  176. static string extractFileName(const string &sFullFileName);
  177. /**
  178. * @brief 从一个完全文件名中提取文件的路径.
  179. *
  180. * 例如1: "/usr/local/temp.gif" 获取"/usr/local/"
  181. * 例如2: "temp.gif" 获取 "./"
  182. * @param sFullFileName 文件的完全名称
  183. * @return 提取后的文件路径
  184. */
  185. static string extractFilePath(const string &sFullFileName);
  186. /**
  187. * @brief 提取文件扩展名.
  188. *
  189. * 例如1: "/usr/local/temp.gif" 获取"gif"
  190. * 例如2: "temp.gif" 获取"gif"
  191. *@param sFullFileName 文件名称
  192. *@return 文件扩展名
  193. */
  194. static string extractFileExt(const string &sFullFileName);
  195. /**
  196. * @brief 提取文件名称,去掉扩展名.
  197. * 例如1: "/usr/local/temp.gif" 获取"/usr/local/temp"
  198. * 例如2: "temp.gif" 获取"temp"
  199. * @param sFullFileName 文件名称
  200. * @return 去掉扩展名的文件名称
  201. */
  202. static string excludeFileExt(const string &sFullFileName);
  203. /**
  204. * @brief 替换文件扩展名
  205. *
  206. * 改变文件类型,如果无扩展名,则加上扩展名 =?1:
  207. * 例如1:"/usr/temp.gif" 替 换 "jpg" 得到"/usr/temp.jpg"
  208. * 例如2: "/usr/local/temp" 替 换 "jpg" 得到"/usr/local/temp.jpg"
  209. * @param sFullFileName 文件名称
  210. * @param sExt 扩展名
  211. * @return 替换扩展名后的文件名
  212. */
  213. static string replaceFileExt(const string &sFullFileName, const string &sExt);
  214. /**
  215. * @brief 从一个url中获取完全文件名.
  216. *
  217. * 获取除http://外,第一个'/'后面的所有字符
  218. * 例如1:http://www.qq.com/tmp/temp.gif 获取tmp/temp.gif
  219. * 例如2:www.qq.com/tmp/temp.gif 获取tmp/temp.gif
  220. * 例如3:/tmp/temp.gif 获取tmp/temp.gif
  221. * @param sUrl url字符串
  222. * @return 文件名称
  223. */
  224. static string extractUrlFilePath(const string &sUrl);
  225. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  226. /**
  227. * @brief 遍历文件时确定是否选择.
  228. *
  229. * @return 1-选择, 0-不选择
  230. */
  231. typedef int(*FILE_SELECT)(const dirent *);
  232. /**
  233. * @brief 扫描一个目录.
  234. *
  235. * @param sFilePath 需要扫描的路径
  236. * @param vtMatchFiles 返回的文件名矢量表
  237. * @param f 匹配函数,为NULL表示所有文件都获取
  238. * @param iMaxSize 最大文件个数,iMaxSize <=0时,返回所有匹配文件
  239. * @return 文件个数
  240. */
  241. static size_t scanDir(const string &sFilePath, vector<string> &vtMatchFiles, FILE_SELECT f = NULL, int iMaxSize = 0);
  242. #endif
  243. /**
  244. * @brief 遍历目录, 获取目录下面的所有文件和子目录.
  245. *
  246. * @param path 需要遍历的路径
  247. * @param files 目标路径下面所有文件
  248. * @param bRecursive 是否递归子目录
  249. *
  250. **/
  251. static void listDirectory(const string &path, vector<string> &files, bool bRecursive);
  252. /**
  253. * @brief 复制文件或目录.
  254. * 将文件或者目录从sExistFile复制到sNewFile
  255. * @param sExistFile 复制的文件或者目录源路径
  256. * @param sNewFile 复制的文件或者目录目标路径
  257. * @param bRemove 是否先删除sNewFile再copy ,防止Textfile busy导致复制失败
  258. * @return
  259. */
  260. static void copyFile(const string &sExistFile, const string &sNewFile, bool bRemove = false);
  261. /**
  262. * @brief 是否以windows盘符开头.
  263. * @return
  264. */
  265. static bool startWindowsPanfu(const string & sPath);
  266. private:
  267. static bool isPanfu(const string & sPath);
  268. };
  269. }
  270. #endif // TC_FILE_H