表格线识别通用库文档
载入中...
搜索中...
未找到
file.hpp
浏览该文件的文档.
1/*
2 * @Description: 文件工具函数 头文件及其实现
3 * @Version:
4 * @Autor: dreamy-xay
5 * @date: 2023-12-14
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-05-18
8 */
9
10#ifndef COMMON_UTILS_FILE_HPP
11#define COMMON_UTILS_FILE_HPP
12
13#include <string>
14
15#include "common/base/list.hpp"
16
17#if defined(__linux__)
18#include <dirent.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22#elif defined(_WIN32) || defined(_WIN64)
23#include <Windows.h>
24#include <direct.h>
25#endif
26
27#include "common/enum.h"
28#include "common/macro.h"
29#include "common/type.h"
30
31/* declare */
32namespace cm {
33
34bool IsExist(const char* path);
35bool IsFolder(const char* path);
36bool IsFile(const char* path);
37bool CreateFolder(const char* path);
38bool DeleteFolder(const char* path);
39
47
49
50} // namespace cm
51
52/* implement */
53namespace cm {
54
73inline bool IsExist(const char* path) {
74 // 判断操作系统,只有 Linux 和 Windows 支持标准库中的文件/文件夹存在检查
75#if defined(__linux__)
76 // Linux 下使用 stat 函数获取文件/文件夹信息
77 struct stat buffer;
78 return (stat(path, &buffer) == 0); // 若调用成功则返回 true,否则返回 false
79
80#elif defined(_WIN32) || defined(_WIN64)
81 // Windows 下使用 GetFileAttributesA 函数获取文件/文件夹属性
83 return (attributes != INVALID_FILE_ATTRIBUTES); // 若调用成功则返回 true,否则返回 false
84
85#else
86 // 不支持的系统,抛出异常
87 throw Exception("This function does not support the operating system");
88
89#endif
90}
91
110inline bool IsFolder(const char* path) {
111 // 判断操作系统,只有 Linux 和 Windows 支持标准库中的文件/文件夹存在检查
112#if defined(__linux__)
113 struct stat info;
114 if (stat(path, &info) != 0)
115 throw std::runtime_error("Path does not exist!");
116 return S_ISDIR(info.st_mode);
117
118#elif defined(_WIN32) || defined(_WIN64)
121 throw std::runtime_error("Path does not exist!");
122 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
123
124#else
125 // 不支持的系统,抛出异常
126 throw Exception("This function does not support the operating system");
127
128#endif
129}
130
149inline bool IsFile(const char* path) {
150 // 判断操作系统,只有 Linux 和 Windows 支持标准库中的文件/文件夹存在检查
151#if defined(__linux__)
152 struct stat info;
153 if (stat(path, &info) != 0)
154 throw std::runtime_error("Path does not exist!");
155 return S_ISREG(info.st_mode);
156
157#elif defined(_WIN32) || defined(_WIN64)
160 throw std::runtime_error("Path does not exist!");
161 return (attr & FILE_ATTRIBUTE_DIRECTORY) == 0;
162
163#else
164 // 不支持的系统,抛出异常
165 throw Exception("This function does not support the operating system");
166
167#endif
168}
169
188inline bool CreateFolder(const char* path) {
189 // 判断路径是否存在
190 if (!IsExist(path)) {
191 // 判断操作系统,只有 Linux 和 Windows 支持标准库中的创建文件夹操作
192#if defined(__linux__)
193 // 在 Linux 下使用 mkdir 函数创建文件夹
194 // 使用 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH 参数设置权限为 rwxr-xr-x
195 return mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0; // 若调用成功则返回 true,否则返回 false
196
197#elif defined(_WIN32) || defined(_WIN64)
198 // 在 Windows 下使用 mkdir 函数创建文件夹
199 return mkdir(path) == 0; // 若调用成功则返回 true,否则返回 false
200
201#else
202 // 不支持的系统,抛出异常
203 throw Exception("This function does not support the operating system");
204#endif
205 }
206
207 // 路径已存在,直接返回 true
208 return true;
209}
210
229inline bool DeleteFolder(const char* path) {
230 // 判断操作系统,只有 Linux 和 Windows 支持标准库中的文件夹操作
231#if defined(__linux__)
232 // 打开要删除的文件夹
233 DIR* dir = opendir(path);
234 if (dir == nullptr)
235 return false; // 打开文件夹失败,返回 false
236
237 // 遍历文件夹中的所有文件和子文件夹
238 struct dirent* entry;
239 while ((entry = readdir(dir)) != nullptr) {
240 std::string name = entry->d_name;
241
242 // 忽略当前文件夹和上一级文件夹
243 if (name == "." || name == "..")
244 continue;
245
246 // 拼接文件/文件夹全路径
247 std::string fullPath = std::string(path) + '/' + name;
248 struct stat st;
249 // 获取文件/文件夹信息
250 if (lstat(fullPath.c_str(), &st) == -1)
251 continue;
252
253 // 如果是文件夹,递归调用 DeleteFolder 函数删除子文件夹及其内容
254 if (S_ISDIR(st.st_mode)) {
255 if (!DeleteFolder(fullPath.c_str())) { // 子文件夹删除失败
256 closedir(dir);
257 return false;
258 }
259
260 } else {
261 // 如果是文件,使用 unlink 函数删除
262 if (unlink(fullPath.c_str()) == -1) {
263 closedir(dir);
264 return false;
265 }
266 }
267 }
268
269 // 关闭文件夹
270 closedir(dir);
271
272 // 删除空文件夹
273 return rmdir(path) != -1;
274
275#elif defined(_WIN32) || defined(_WIN64)
276 // Windows 下使用 RemoveDirectoryA 函数删除文件夹
277 return RemoveDirectoryA(path) != 0;
278
279#else
280 // 不支持的系统,抛出异常
281 throw Exception("This function does not support the operating system");
282
283#endif
284}
285
302inline DirectoryInfo ListDirectory(const char* path) {
304
305 // 判断路径是否存在
306 if (!IsExist(path))
307 return dir_info;
308
309 // 判断操作系统,只有 Linux 和 Windows 支持标准库中的文件夹操作
310#if defined(__linux__)
311 DIR* dir = opendir(path);
312 if (dir) {
313 struct dirent* entry;
314 while ((entry = readdir(dir)) != nullptr) {
315 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
316 continue;
317
318 if (entry->d_type == DT_DIR)
319 dir_info.directories.emplace_back(entry->d_name);
320 else if (entry->d_type == DT_REG)
321 dir_info.files.emplace_back(entry->d_name);
322 }
323
324 closedir(dir);
325 }
326#elif defined(_WIN32) || defined(_WIN64)
327 std::string search_path = path;
328 search_path += +"\\*.*";
331
333 do {
334 if (strcmp(file_data.cFileName, ".") == 0 || strcmp(file_data.cFileName, "..") == 0)
335 continue;
336
337 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
338 result.directories.emplace_back(file_data.cFileName);
339 else
340 result.files.emplace_back(file_data.cFileName);
341 } while (FindNextFile(hFind, &file_data));
342
344 }
345#else
346 // 不支持的系统,抛出异常
347 throw Exception("This function does not support the operating system");
348
349#endif
350
351 // 排序
352 dir_info.files.Sort();
353 dir_info.directories.Sort();
354
355 return std::move(dir_info);
356}
357
358} // namespace cm
359
360#endif
通用异常类
Definition type.h:39
点类
Definition point.hpp:52
bool IsFolder(const char *path)
检查是否文件夹
Definition file.hpp:110
bool DeleteFolder(const char *path)
删除文件夹函数
Definition file.hpp:229
bool IsFile(const char *path)
检查是否文件
Definition file.hpp:149
bool CreateFolder(const char *path)
创建文件夹函数
Definition file.hpp:188
DirectoryInfo ListDirectory(const char *path)
列出目录信息函数
Definition file.hpp:302
bool IsExist(const char *path)
检查文件或文件夹是否存在
Definition file.hpp:73
文件目录信息
Definition file.hpp:41
List< std::string > directories
文件夹列表
Definition file.hpp:45
List< std::string > files
文件列表
Definition file.hpp:43