Skip to content

Commit

Permalink
Merge pull request #29 from krshrimali/dir-exists-util
Browse files Browse the repository at this point in the history
FEAT: Add `exists` utility to check if directory exists
  • Loading branch information
krshrimali authored Dec 5, 2022
2 parents 4366bc9 + 96688de commit 6ff529c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
Binary file modified fmanager
Binary file not shown.
2 changes: 2 additions & 0 deletions include/FileManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class FileManager {

void set_separator(const std::string &);
std::string get_separator();
bool exists(const std::string &path);

private:
// These are the private methods, documentation will be added later. (TODO)
Expand All @@ -91,4 +92,5 @@ class FileManager {
bool itemInList(std::string, std::vector<std::string>);
file_info make_file_info(std::string, std::string, bool);
void __print_all();
bool __check_path_if_exists(const std::string &);
};
2 changes: 2 additions & 0 deletions samples/FileManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class FileManager {

void set_separator(const std::string &);
std::string get_separator();
bool exists(const std::string &path);

private:
// These are the private methods, documentation will be added later. (TODO)
Expand All @@ -91,4 +92,5 @@ class FileManager {
bool itemInList(std::string, std::vector<std::string>);
file_info make_file_info(std::string, std::string, bool);
void __print_all();
bool __check_path_if_exists(const std::string &);
};
Binary file modified samples/libcpp-file-manager.a
Binary file not shown.
19 changes: 11 additions & 8 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ void FileManager::info() {
__print_all();
}

FileManager::file_info
FileManager::make_file_info(std::string filename, std::string relative_filename,
bool FileManager::exists(const std::string &path) { return __check_path_if_exists(path); }

bool FileManager::__check_path_if_exists(const std::string &path) {
return opendir(path.c_str()) != nullptr;
}

FileManager::file_info FileManager::make_file_info(std::string filename, std::string relative_filename,
bool is_dir) {
// f.name is the absolute name
// f.rname is name of the folder/file
Expand All @@ -47,7 +52,6 @@ FileManager::list_files(std::vector<std::string> extensions,
// This returns the list of files present in the folder: corePath
// TODO: Add tests, check if corePath is not empty
// Converting #ifdef DEBUG and #endif to a macro

std::vector<file_info> list_files;
std::string base_name;

Expand All @@ -61,7 +65,8 @@ FileManager::list_files(std::vector<std::string> extensions,
DIR *dir;
struct dirent *ent;
bool is_dir;
if ((dir = opendir(base_name.c_str())) != NULL) {
if (exists(base_name)) {
dir = opendir(base_name.c_str());
while ((ent = readdir(dir)) != NULL) {
bool include = false;
std::string relative_filename = ent->d_name;
Expand Down Expand Up @@ -211,10 +216,8 @@ void FileManager::writeToFile(std::vector<std::string> ignore_dirs,
file.close();
}

void FileManager::set_separator(const std::string& new_separator) {
void FileManager::set_separator(const std::string &new_separator) {
this->separator = new_separator;
}

std::string FileManager::get_separator() {
return this->separator;
}
std::string FileManager::get_separator() { return this->separator; }

0 comments on commit 6ff529c

Please sign in to comment.