-
Notifications
You must be signed in to change notification settings - Fork 5
Rewritten code using C++ Boost Library #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Hi @ayushmehar7 - Thank you for the PR! I'll take a look at this PR and get back to you tomorrow. |
krshrimali
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ayushmehar7
Great work! I took a look at it, and have left a few comments. Please remove the CMakefiles from the commits. We don't need them except CMakeLists.txt changes if any.
I'll continue taking a look at src/FileManager.cpp later in the day.
Let's chat on our dc for more changes.
Thanks for your contribution.
| #include<iterator> | ||
| #include<boost/filesystem.hpp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually follow the format: #include <iterator> instead of #include<iterator> (notice the spaces) for readability.
| // You can change the path using: <FileManagerObject>.clear(string newPath) | ||
| FileManager(std::string path) { | ||
| this->corePath = path; | ||
| if(!boost::filesystem::exists(path)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove these new lines? :)
| #include<iterator> | ||
| #include<boost/filesystem.hpp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before.
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before.
| const char* program_name = "fmanager"; // Use for printing usage | ||
|
|
||
| void print_usage() { | ||
| printf("Usage: %s options\n", program_name); | ||
| printf(" -h --help Print usage.\n" | ||
| " -p --path filePath Input path (to be iterated).\n" | ||
| " -l --list_files Call the list files function.\n" | ||
| " -t --tree Call the tree function.\n" | ||
| " -d --ignore_dirs dir1,dir2 Ignore dirs while creating tree\n" | ||
| " -e --ignore_extensions ext1,ext2 Ignore extensions while creating tree\n"); | ||
| exit(-1); | ||
| } | ||
|
|
||
| std::vector<std::string> split(std::string s, std::string delimiter) { | ||
| size_t pos = 0; | ||
| std::vector<std::string> output; | ||
| while ( (pos = s.find(delimiter)) != std::string::npos ) { | ||
| std::string token = s.substr(0, pos); | ||
| output.push_back(token); | ||
| s.erase(0, pos + delimiter.length()); | ||
| } | ||
| output.push_back(s); | ||
| return output; | ||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| const char* short_options = "hp:ltd:e:"; | ||
| const struct option long_options[] = { | ||
| { "help", 0, NULL, 'h'}, | ||
| { "path", 1, NULL, 'p'}, | ||
| { "list_files", 0, NULL, 'l'}, | ||
| { "tree", 0, NULL, 't'}, | ||
| { "ignore_dirs", 1, NULL, 'd'}, | ||
| { "ignore_extensions", 1, NULL, 'e'}, | ||
| { NULL, 0, NULL, 0 } | ||
| }; | ||
|
|
||
| std::string path = ""; | ||
| bool list_files = true; | ||
| bool draw_tree = false; | ||
| int opt; | ||
| std::vector<std::string> ignore_dirs = {}; | ||
| std::vector<std::string> ignore_extensions = {}; | ||
|
|
||
| do { | ||
| opt = getopt_long (argc, argv, short_options, long_options, NULL); | ||
| switch (opt) { | ||
| case 'h': /* -h or --help */ | ||
| // Print usage information | ||
| print_usage(); | ||
| break; | ||
| case 'p': /* -p or --path */ | ||
| std::cout << "Got path: " << optarg << std::endl; | ||
| path = optarg; | ||
| break; | ||
| case 'l': /* -l or --list_files */ | ||
| list_files = true; | ||
| break; | ||
| case 't': /* -t or --tree */ | ||
| draw_tree = true; | ||
| break; | ||
| case 'd': /* -d or --ignore_dirs */ | ||
| ignore_dirs = split(optarg, ","); | ||
| break; | ||
| case 'e': /* -e or --ignore_extensions */ | ||
| ignore_extensions = split(optarg, ","); | ||
| break; | ||
| case -1: /* Done with options */ | ||
| break; | ||
| default: /* Unexpected */ | ||
| abort (); | ||
| } | ||
| } while(opt != -1); | ||
|
|
||
| if (path == "") { | ||
| // By default use the current folder as the path | ||
| path = "."; | ||
| } | ||
|
|
||
| FileManager file(path); | ||
| // file.info(); // Prints the path you entered to the console | ||
| if (list_files) { | ||
| for (auto const& item: file.list_files()) { | ||
| std::cout << item.rname << std::endl; | ||
| } | ||
| } | ||
| if (draw_tree) { | ||
| file.writeToFile(/*ignore_folders=*/ ignore_dirs, /*ignore_extensions=*/ ignore_extensions); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why these were removed? let's bring them back?
| std::string base_name; | ||
|
|
||
| if(*corePath.rbegin() != '/') base_name = corePath + "/"; | ||
| if(*corePath.rbegin() != '/') base_name = corePath+"/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do this while using boost library?
Hi sir !
I have modified FileManager.hpp to check valid path in the constructor itself. dirent.h, cassert and cstring has been removed and boost has been used for directory operations. A working demo has been provided in samples folder to run the files.
You can install boost filesystem on linux using:
sudo apt-get install libboost-all-dev
To run the file use the following commad :
g++ sample.cpp -lboost_filesystem -L . -lcpp-file-manager -o out