CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
filesystem++.cpp
Go to the documentation of this file.
1
21// C++ libraries
22#include "filesystem"
23#include "fstream"
24#include "regex"
25#include "stdlib.h"
26#include "string"
27#include "vector"
28// Project files
32/* CBuild::fs namespace */
33namespace CBuild {
34namespace fs {
35std::regex c_cpp_file(".*\\.(cpp|cxx|cc|c)");
36} // namespace fs
37} // namespace CBuild
38/* filesystem++.hpp */
39std::vector<std::string> CBuild::fs::dir(std::string path, std::string search) {
40 // List of all found files
41 std::vector<std::string> files;
42 // List of files that need to be returned
43 std::vector<std::string> ret;
44 // Regex for search, use ECMAScript syntax
45 std::regex reg(search, std::regex::ECMAScript);
46 // Found all files in directory and store in array
47 try {
48 for (const auto& entry : std::filesystem::directory_iterator(path)) {
49 files.push_back(entry.path().generic_string());
50 }
51 } catch (std::exception& e) {
52 CBuild::printf_full(CBuild::RED, "Error iterating over dorectory '%s'\n", path.c_str());
53 }
54 // Check all elements with regex and add it to return array
55 for (auto elem : files) {
56 if (std::regex_match(elem, reg)) {
57 ret.push_back(elem);
58 }
59 }
60 // Return list of found files that match given regex
61 return ret;
62}
63std::vector<std::string> CBuild::fs::dir(std::string path) {
64 // List of found files
65 std::vector<std::string> files;
66 // List of returned files
67 std::vector<std::string> ret;
68 // Get all files and store it's paths in array
69 try {
70 for (const auto& entry : std::filesystem::directory_iterator(path)) {
71 files.push_back(entry.path().generic_string());
72 }
73 } catch (std::exception& e) {
74 CBuild::printf_full(CBuild::RED, "Error iterating over dorectory '%s'\n", path.c_str());
75 }
76 // Check every file with internal regex and add to return array
77 for (auto elem : files) {
78 if (std::regex_match(elem, c_cpp_file)) {
79 ret.push_back(elem);
80 }
81 }
82 // Return list of found files
83 return ret;
84}
85std::vector<std::string> CBuild::fs::dir_rec(std::string path, std::string search) {
86 // List of all found files
87 std::vector<std::string> files;
88 // List of files for return
89 std::vector<std::string> ret;
90 // Create regex
91 std::regex reg(search);
92 // Check all directories recursively and add to array
93 try {
94 for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
95 files.push_back(entry.path().generic_string());
96 }
97 } catch (std::exception& e) {
98 CBuild::printf_full(CBuild::RED, "Error iterating over dorectory '%s'\n", path.c_str());
99 }
100 // Check all files with given regex and add to return array
101 for (auto elem : files) {
102 if (std::regex_match(elem, reg)) {
103 ret.push_back(elem);
104 }
105 }
106 // Return list of found files
107 return ret;
108}
109std::vector<std::string> CBuild::fs::dir_rec(std::string path) {
110 // List of all found files
111 std::vector<std::string> files;
112 // List of files for return
113 std::vector<std::string> ret;
114 // Check all directories recursively and add to array
115 try {
116 for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
117 files.push_back(entry.path().generic_string());
118 }
119 } catch (std::exception& e) {
120 CBuild::printf_full(CBuild::RED, "Error iterating over dorectory '%s'\n", path.c_str());
121 }
122 // Check all files with internal regex and add to return array
123 for (auto elem : files) {
124 if (std::regex_match(elem, c_cpp_file)) {
125 ret.push_back(elem);
126 }
127 }
128 // Return list of found filews
129 return ret;
130}
131bool CBuild::fs::rename(std::string start, std::string end) {
132 // Two paths
133 std::filesystem::path from, to;
134 // Create paths from strings
135 from.assign(start);
136 to.assign(end);
137 // Try to rename file
138 try {
139 std::filesystem::rename(from, to);
140 } catch (std::exception& e) {
141 // Something goes wrong
142 return false;
143 }
144 // All good
145 return true;
146}
147bool CBuild::fs::remove(std::string path, bool force) {
148 // Create path from string
149 std::filesystem::path p;
150 p.assign(path);
151 // If we need force remove
152 if (force) {
153 // Try force remove
154 try {
155 if (std::filesystem::remove_all(p) == 0) {
156 // Something goes wrong
157 return false;
158 }
159 } catch (std::exception& e) {
160 // Something goes wrong
161 return false;
162 }
163 }
164 // We can use "lite" remove
165 else {
166 // Try force remove
167 try {
168 if (std::filesystem::remove(p) == false)
169 return false;
170 } catch (std::exception& e) {
171 // Something goes wrong
172 return false;
173 }
174 }
175 // All good
176 return true;
177}
178bool CBuild::fs::copy(std::string start, std::string end) {
179 // Create paths from strings
180 std::filesystem::path from, to;
181 from.assign(start);
182 to.assign(end);
183 // Try to copy elements
184 try {
185 std::filesystem::copy(from, to, std::filesystem::copy_options::recursive);
186 } catch (std::exception& e) {
187 // Something goes wrong
188 return false;
189 }
190 // All good
191 return true;
192}
193bool CBuild::fs::move(std::string start, std::string end) {
194 // Create paths from strings
195 std::filesystem::path from, to;
196 from.assign(start);
197 to.assign(end);
198 // Try to copy files
199 try {
200 std::filesystem::copy(from, to);
201 if (CBuild::fs::remove(start, true) == false)
202 return false; // Something wrong with removal
203 } catch (std::exception& e) {
204 // Something goes wrong
205 return false;
206 }
207 // All good
208 return true;
209}
210bool CBuild::fs::create(std::vector<std::string> paths, CBuild::fs::type what) {
211 // Check type of new element
212 switch (what) {
213 // We creating simple file
215 // Argument number check
216 std::string path = "";
217 try {
218 path = paths.at(0);
219 } catch (std::exception& e) {
220 // Invalid argument count
221 throw std::runtime_error("Invalid count of elements");
222 return false;
223 }
224 // Try to create file using std::ofstream
225 try {
226 std::ofstream file(path);
227 if (file.bad())
228 return false;
229 file.close();
230 } catch (std::exception& e) {
231 // Something goes wrong
232 return false;
233 }
234 } break;
235 // We creating directory
236 case CBuild::fs::DIR:
238 // Check argument count
239 std::filesystem::path p;
240 try {
241 p.assign(paths.at(0));
242 } catch (std::exception& e) {
243 // Invalid arguments count
244 throw std::runtime_error("Invalid count of elements");
245 return false;
246 }
247 // Try to create new directory
248 try {
249 std::filesystem::create_directory(p);
250 } catch (std::exception& e) {
251 // Something goes wrong
252 return false;
253 }
254 } break;
255 // We creating symlink to file
257 // Check argument count
258 std::filesystem::path file, link;
259 try {
260 file.assign(paths.at(0));
261 } catch (std::exception& e) {
262 // Invalid argument count
263 throw std::runtime_error("Invalid count of elements");
264 return false;
265 }
266 try {
267 link.assign(paths.at(1));
268 } catch (std::exception& e) {
269 // Invalid argument count
270 throw std::runtime_error("Invalid count of elements");
271 return false;
272 }
273 // Try to create new symlink, can be unsupported
274 try {
275 std::filesystem::create_symlink(file, link);
276 } catch (std::exception& e) {
277 // Something goes wrong
278 return false;
279 }
280 } break;
281 // We creating symlink to directory
284 // Check argument count
285 std::filesystem::path directory, link;
286 try {
287 directory.assign(paths.at(0));
288 } catch (std::exception& e) {
289 // Invalid argument count
290 throw std::runtime_error("Invalid count of elements");
291 return false;
292 }
293 try {
294 link.assign(paths.at(1));
295 } catch (std::exception& e) {
296 // Invalid argument count
297 throw std::runtime_error("Invalid count of elements");
298 return false;
299 }
300 // try to create symlink, can be unsupported
301 try {
302 std::filesystem::create_directory_symlink(directory, link);
303 } catch (std::exception& e) {
304 // Something goes wrong
305 return false;
306 }
307 } break;
308 // We creating hardlink, can be unsupported
310 // Checking argument count
311 std::filesystem::path element, link;
312 try {
313 element.assign(paths.at(0));
314 } catch (std::exception& e) {
315 // Invalid argument count
316 throw std::runtime_error("Invalid count of elements");
317 return false;
318 }
319 try {
320 link.assign(paths.at(1));
321 } catch (std::exception& e) {
322 // Invalid argument count
323 throw std::runtime_error("Invalid count of elements");
324 return false;
325 }
326 // Try to create hardlink, can be unsupported
327 try {
328 std::filesystem::create_hard_link(element, link);
329 } catch (std::exception& e) {
330 // Something goes wrong
331 return false;
332 }
333 } break;
334 default:
335 // Invalid type of new element
336 throw std::runtime_error("Invalid type of new element");
337 return false;
338 break;
339 }
340 // All good
341 return true;
342}
343bool CBuild::fs::exists(std::string path) {
344 std::filesystem::path p;
345 p.assign(path);
346 return std::filesystem::exists(p);
347}
348std::string CBuild::fs::normalize_path(std::string path, std::string base_path) {
349 if (base_path == std::string("")) {
350 return std::filesystem::canonical(std ::filesystem::path(path));
351 } else {
352 return std::filesystem::canonical(std::filesystem::path(base_path) /
353 std ::filesystem::path(path));
354 }
355}
356std::string CBuild::fs::normalize_relative_path(std::string path, std::string base_path) {
357 if (base_path == std::string("")) {
358 return std::filesystem::relative(std::filesystem::path(path), CBuild::fs::curr_path());
359 } else {
360 return std::filesystem::relative(std::filesystem::path(base_path) /
361 std ::filesystem::path(path),
363 }
364}
365std::string CBuild::fs::base(std::string file) {
366 return std::filesystem::path(file).parent_path();
367}
369 return std::filesystem::current_path();
370}
filesystem++ api
std::string normalize_path(std::string path, std::string base_path="")
Get absolute path to file using relative path and base path.
bool exists(std::string path)
Check if file exists.
bool create(std::vector< std::string > paths, CBuild::fs::type what)
Create element.
std::string base(std::string file)
Get base file path (path to dir in what file is)
bool remove(std::string path, bool force=false)
Delete files or directories.
std::string curr_path()
Get current working dir of CBuild.run process.
std::string normalize_relative_path(std::string path, std::string base_path="")
Get absolute path to file using relative path and base path.
bool copy(std::string start, std::string end)
Copy files or directories.
type
Type for element creating.
@ FILE
Standard file.
@ DIRECTORY
Standard directory.
@ HARDLINK
Standard hardlink (can be unsupported or supported partially) (though std::filesystem::create_hard_li...
@ SYMLINK_DIR
Alias for SYMLINK_DIRECTORY.
@ SYMLINK_FILE
Standard symlink (can be unsupported or supported partially) (thought std::filesystem::create_symlink...
@ SYMLINK_DIRECTORY
Standard symlink (can be unsupported or supported partially) (std::filesystem::create_directory_symli...
@ DIR
Alias for DIRECTORY.
bool move(std::string start, std::string end)
Move files or directories, remove starting files or directories.
std::vector< std::string > dir_rec(std::string path, std::string search)
Recursively search files using provided regex.
std::regex c_cpp_file(".*\\.(cpp|cxx|cc|c)")
bool rename(std::string start, std::string end)
Rename files or directories,.
std::vector< std::string > dir(std::string path, std::string search)
Search files using provided regex.
Filebuffer for CBuild ecosystem.
Definition Build.hpp:34
void void printf_full(color fg, const char *fmt,...) __attribute__((format(printf
Some poor printf implementation for verbouse-only prints.
Definition print.cpp:89
@ RED
Definition print.hpp:34
Custom print that support color codes.