utils_filesystem.h
Go to the documentation of this file.
1/*!
2 * \file utils_filesystem.h
3 * \brief File system related functions in CCGL.
4 *
5 * \remarks
6 * - 1. 2018-05-02 - lj - Make part of CCGL.
7 *
8 * \author Liangjun Zhu, zlj(at)lreis.ac.cn)
9 * \version 1.0
10 */
11#ifndef CCGL_UTILS_FILESYSTEM_H
12#define CCGL_UTILS_FILESYSTEM_H
13
14#include "basic.h"
15
16#include <vector>
17
18using std::vector;
19
20namespace ccgl {
21/*!
22 * \namespace ccgl::utils_filesystem
23 * \brief File Input and output related functions
24 */
25namespace utils_filesystem {
26/*!
27 * \brief Check the given directory path (not regular file!) is exists or not.
28 */
29bool DirectoryExists(const string& dirpath);
30
31/*!
32 * \brief Make directory if not exists
33 */
34bool MakeDirectory(const string& dirpath);
35
36/*!
37 * \brief Clean a directory if exists, otherwise create it.
38 */
39bool CleanDirectory(const string& dirpath);
40
41/*!
42 * \brief Delete a directory if exists.
43 *
44 * Reference:
45 * - 1. Windows: https://stackoverflow.com/questions/734717/how-to-delete-a-folder-in-c
46 * - 2. Linux: https://www.linuxquestions.org/questions/programming-9/deleting-a-directory-using-c-in-linux-248696/
47 */
48bool DeleteDirectory(const string& dirpath, bool del_subdirs = true);
49
50/*!
51 * \brief Get the root path of the current executable file
52 * \return \a string root path
53 */
54string GetAppPath();
55
56/*!
57 * \brief Return the absolute file path from a given file path
58 * \param[in] full_filename Full file path
59 * \sa GetPathFromFullName
60 */
61string GetAbsolutePath(string const& full_filename);
62
63/*!
64 * \brief Return the file name from a given file's path
65 * \param[in] full_filename Full file path
66 * \sa GetPathFromFullName
67 */
68string GetCoreFileName(string const& full_filename);
69
70/*!
71 * \brief Return the suffix of a given file's path without dot, e.g., "tif", "asc"
72 * \param[in] full_filename Full file path
73 * \sa GetPathFromFullName
74 */
75string GetSuffix(string const& full_filename);
76
77/*!
78 * \brief Replace the suffix by a given suffix
79 * \param[in] full_filename Full file path
80 * \param[in] new_suffix New suffix without dot, e.g., "tif", "asc"
81 */
82string ReplaceSuffix(string const& full_filename, string const& new_suffix);
83
84/*!
85 * \brief Append a given string to the core filename
86 * \param[in] full_filename Full file path
87 * \param[in] endstr End string
88 * \param[in] deli (Optional) Delimiter
89 * \return new full_filename
90 */
91string AppendCoreFileName(string const& full_filename, string const& endstr, char deli = '_');
92
93/*!
94 * \brief Append a given integer to the core filename
95 */
96string AppendCoreFileName(string const& full_filename, vint endint, char deli = '_');
97
98/*!
99 * \brief Add a prefix to the core filename
100 * \param[in] full_filename Full file path
101 * \param[in] prestr Start string
102 * \param[in] deli (Optional) Delimiter
103 * \return new full_filename
104 */
105string PrefixCoreFileName(string const& full_filename, string const& prestr, char deli = '_');
106
107/*!
108 * \brief Add a prefix to the core filename
109 */
110string PrefixCoreFileName(string const& full_filename, vint preint, char deli = '_');
111
112/*!
113 * \brief Get Path From full file path string
114 * \param[in] full_filename Full file path
115 * \sa GetCoreFileName
116 */
117string GetPathFromFullName(string const& full_filename);
118
119/*!
120 * \brief Concatenate directory, core file name, and suffix
121 */
122string ConcatFullName(string const& fdir, string const& corename, string const& suffix = std::string());
123
124/*!
125 * \brief Return a flag indicating if the given file exists
126 * \param[in] filename String path of file
127 * \return True if Exists, and false if not.
128 */
129bool FileExists(string const& filename);
130
131/*!
132 * \brief Return a flag indicating if given files exist
133 * \param[in] filenames Vector of full file paths
134 * \return True if all existed, else false
135 */
136bool FilesExist(vector<string>& filenames);
137
138/*!
139 * \brief Return a flag indicating if the given path (directory or file) exists
140 * \param[in] path String path
141 * \return True if Exists, and false if not.
142 */
143bool PathExists(string const& path);
144
145/*!
146 * \brief Delete the given file if existed.
147 * \param[in] filepath \a string File path, full path or relative path
148 * \return 0 if deleted successful, else return nonzero value, e.g. -1.
149 */
150int DeleteExistedFile(const string& filepath);
151
152/*!
153 * \brief Find files in given paths
154 * \param[in] lp_path Directory path
155 * \param[in] expression Wildcard characters, e.g., "*.*" means any filename with any suffix
156 * \param[out] vec_files Vector of full file paths
157 * \return 0 means success
158 */
159int FindFiles(const char* lp_path, const char* expression, vector<string>& vec_files);
160
161/*!
162 * \brief Load short plain text file as string vector, ignore comments begin with '#' and empty lines
163 * \param[in] filepath Plain text file path
164 * \param[out] content_strs Each line without CRLF or LF stored in vector
165 * \return True when read successfully, and false with empty content_strs when failed
166 */
167bool LoadPlainTextFile(const string& filepath, vector<string>& content_strs);
168} /* namespace: utils_filesystem */
169
170} /* namespace: ccgl */
171
172#endif /* CCGL_UTILS_FILESYSTEM_H */
Basic definitions.
int DeleteExistedFile(const string &filepath)
Delete the given file if existed.
string GetPathFromFullName(string const &full_filename)
Get Path From full file path string.
string AppendCoreFileName(string const &full_filename, string const &endstr, char deli='_')
Append a given string to the core filename.
bool DeleteDirectory(const string &dirpath, bool del_subdirs=true)
Delete a directory if exists.
bool FileExists(string const &filename)
Return a flag indicating if the given file exists.
string PrefixCoreFileName(string const &full_filename, string const &prestr, char deli='_')
Add a prefix to the core filename.
string GetCoreFileName(string const &full_filename)
Return the file name from a given file's path.
bool PathExists(string const &path)
Return a flag indicating if the given path (directory or file) exists.
string ConcatFullName(string const &fdir, string const &corename, string const &suffix=std::string())
Concatenate directory, core file name, and suffix.
string GetAppPath()
Get the root path of the current executable file.
bool CleanDirectory(const string &dirpath)
Clean a directory if exists, otherwise create it.
string GetSuffix(string const &full_filename)
Return the suffix of a given file's path without dot, e.g., "tif", "asc".
string GetAbsolutePath(string const &full_filename)
Return the absolute file path from a given file path.
int FindFiles(const char *lp_path, const char *expression, vector< string > &vec_files)
Find files in given paths.
string ReplaceSuffix(string const &full_filename, string const &new_suffix)
Replace the suffix by a given suffix.
bool FilesExist(vector< string > &filenames)
Return a flag indicating if given files exist.
bool MakeDirectory(const string &dirpath)
Make directory if not exists.
bool LoadPlainTextFile(const string &filepath, vector< string > &content_strs)
Load short plain text file as string vector, ignore comments begin with '#' and empty lines.
bool DirectoryExists(const string &dirpath)
Check the given directory path (not regular file!) is exists or not.
Common Cross-platform Geographic Library (CCGL)