Skip to content

Conversation

@ayushmehar7
Copy link

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

@krshrimali krshrimali self-requested a review January 21, 2021 17:07
@krshrimali krshrimali self-assigned this Jan 21, 2021
@krshrimali krshrimali added enhancement New feature or request high-priority Issues/PRs with this label are the topmost priority. May Day? open-source Anyone can contribute. labels Jan 21, 2021
@krshrimali
Copy link
Owner

Hi @ayushmehar7 - Thank you for the PR!

I'll take a look at this PR and get back to you tomorrow.

Copy link
Owner

@krshrimali krshrimali left a 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.

Comment on lines +6 to +7
#include<iterator>
#include<boost/filesystem.hpp>
Copy link
Owner

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)){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

Comment on lines +68 to +69


Copy link
Owner

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? :)

Comment on lines +6 to +7
#include<iterator>
#include<boost/filesystem.hpp>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before.

Comment on lines +68 to +69


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before.

Comment on lines -6 to -95
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);
}
}
Copy link
Owner

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+"/";
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request high-priority Issues/PRs with this label are the topmost priority. May Day? open-source Anyone can contribute.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants