1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Hechong.xyf |
17 | * \date Dec 2020 |
18 | * \brief Interface of AiLego Utility File Helper |
19 | */ |
20 | |
21 | #ifndef __AILEGO_UTILITY_FILE_HELPER_H__ |
22 | #define __AILEGO_UTILITY_FILE_HELPER_H__ |
23 | |
24 | #include <cstring> |
25 | #include <string> |
26 | #include <ailego/internal/platform.h> |
27 | |
28 | namespace ailego { |
29 | |
30 | /*! File Helper Module |
31 | */ |
32 | struct FileHelper { |
33 | #if defined(_WIN32) || defined(_WIN64) |
34 | //! Native Handle in Windows |
35 | typedef void *NativeHandle; |
36 | #else |
37 | //! Native Handle in POSIX |
38 | typedef int NativeHandle; |
39 | #endif |
40 | |
41 | //! Invalid Handle |
42 | static constexpr NativeHandle InvalidHandle = (NativeHandle)(-1); |
43 | |
44 | //! Retrieve the path of self process |
45 | static bool GetSelfPath(std::string *path); |
46 | |
47 | //! Retrieve the final path for the specified file |
48 | static bool GetFilePath(NativeHandle handle, std::string *path); |
49 | |
50 | //! Retrieve current working directory |
51 | static bool GetWorkingDirectory(std::string *path); |
52 | |
53 | //! Get the size of a file |
54 | static bool GetFileSize(const char *path, size_t *psz); |
55 | |
56 | //! Delete a name and possibly the file it refers to |
57 | static bool DeleteFile(const char *path); |
58 | |
59 | //! Change the name or location of a file |
60 | static bool RenameFile(const char *oldpath, const char *newpath); |
61 | |
62 | //! Make directories' path |
63 | static bool MakePath(const char *path); |
64 | |
65 | //! Remove a file or a directory (includes files & subdirectories) |
66 | static bool RemovePath(const char *path); |
67 | |
68 | //! Remove a directory (includes files & subdirectories) |
69 | static bool RemoveDirectory(const char *path); |
70 | |
71 | //! Retrieve non-zero if the path exists |
72 | static bool IsExist(const char *path); |
73 | |
74 | //! Retrieve non-zero if the path is a regular file |
75 | static bool IsRegular(const char *path); |
76 | |
77 | //! Retrieve non-zero if the path is a directory |
78 | static bool IsDirectory(const char *path); |
79 | |
80 | //! Retrieve non-zero if the path is a symbolic link |
81 | static bool IsSymbolicLink(const char *path); |
82 | |
83 | //! Retrieve non-zero if two paths are pointing to the same file |
84 | static bool IsSame(const char *path1, const char *path2); |
85 | |
86 | //! Retrieve the size of a file |
87 | static size_t FileSize(const char *path) { |
88 | size_t file_size = 0; |
89 | GetFileSize(path, &file_size); |
90 | return file_size; |
91 | } |
92 | |
93 | //! Retrieve the base name from a path |
94 | static const char *BaseName(const char *path) { |
95 | const char *output = std::strrchr(path, '/'); |
96 | if (!output) { |
97 | output = std::strrchr(path, '\\'); |
98 | } |
99 | return (output ? output + 1 : path); |
100 | } |
101 | }; |
102 | |
103 | } // namespace ailego |
104 | |
105 | #endif // __AILEGO_UTILITY_FILE_HELPER_H__ |
106 | |