Skip to content

Commit 6ff529c

Browse files
authored
Merge pull request #29 from krshrimali/dir-exists-util
FEAT: Add `exists` utility to check if directory exists
2 parents 4366bc9 + 96688de commit 6ff529c

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

fmanager

224 Bytes
Binary file not shown.

include/FileManager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class FileManager {
7878

7979
void set_separator(const std::string &);
8080
std::string get_separator();
81+
bool exists(const std::string &path);
8182

8283
private:
8384
// These are the private methods, documentation will be added later. (TODO)
@@ -91,4 +92,5 @@ class FileManager {
9192
bool itemInList(std::string, std::vector<std::string>);
9293
file_info make_file_info(std::string, std::string, bool);
9394
void __print_all();
95+
bool __check_path_if_exists(const std::string &);
9496
};

samples/FileManager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class FileManager {
7878

7979
void set_separator(const std::string &);
8080
std::string get_separator();
81+
bool exists(const std::string &path);
8182

8283
private:
8384
// These are the private methods, documentation will be added later. (TODO)
@@ -91,4 +92,5 @@ class FileManager {
9192
bool itemInList(std::string, std::vector<std::string>);
9293
file_info make_file_info(std::string, std::string, bool);
9394
void __print_all();
95+
bool __check_path_if_exists(const std::string &);
9496
};

samples/libcpp-file-manager.a

720 Bytes
Binary file not shown.

src/FileManager.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ void FileManager::info() {
2828
__print_all();
2929
}
3030

31-
FileManager::file_info
32-
FileManager::make_file_info(std::string filename, std::string relative_filename,
31+
bool FileManager::exists(const std::string &path) { return __check_path_if_exists(path); }
32+
33+
bool FileManager::__check_path_if_exists(const std::string &path) {
34+
return opendir(path.c_str()) != nullptr;
35+
}
36+
37+
FileManager::file_info FileManager::make_file_info(std::string filename, std::string relative_filename,
3338
bool is_dir) {
3439
// f.name is the absolute name
3540
// f.rname is name of the folder/file
@@ -47,7 +52,6 @@ FileManager::list_files(std::vector<std::string> extensions,
4752
// This returns the list of files present in the folder: corePath
4853
// TODO: Add tests, check if corePath is not empty
4954
// Converting #ifdef DEBUG and #endif to a macro
50-
5155
std::vector<file_info> list_files;
5256
std::string base_name;
5357

@@ -61,7 +65,8 @@ FileManager::list_files(std::vector<std::string> extensions,
6165
DIR *dir;
6266
struct dirent *ent;
6367
bool is_dir;
64-
if ((dir = opendir(base_name.c_str())) != NULL) {
68+
if (exists(base_name)) {
69+
dir = opendir(base_name.c_str());
6570
while ((ent = readdir(dir)) != NULL) {
6671
bool include = false;
6772
std::string relative_filename = ent->d_name;
@@ -211,10 +216,8 @@ void FileManager::writeToFile(std::vector<std::string> ignore_dirs,
211216
file.close();
212217
}
213218

214-
void FileManager::set_separator(const std::string& new_separator) {
219+
void FileManager::set_separator(const std::string &new_separator) {
215220
this->separator = new_separator;
216221
}
217222

218-
std::string FileManager::get_separator() {
219-
return this->separator;
220-
}
223+
std::string FileManager::get_separator() { return this->separator; }

0 commit comments

Comments
 (0)