class FileBot { public: FileBot(boost::filesystem::path filePath); int constructParentPath(); void constructChildPath(); ... ... private: boost::filesystem::path inputPath; };The purpose of this variable is to keep track of the starting point where the files should begin to search with. It is so important that nothing is missed during the initialization, otherwise nothing will get from the search. Thus I'm doing the initialization through the constructor.
FileBot::FileBot(boost::filesystem::path filePath) { // remove trailing file separator if found any char lastChar = filePath.generic_string().at(filePath.generic_string().length() - 1); if( lastChar == '/' ) inputPath = filePath.generic_string().substr(0, filePath.generic_string().length() - 1); else inputPath = filePath; }In the client code, the programmer must instantiate the class through the following way. Indirectly the variable will get initialized as well.
FileBot fb("/home"); // or FileBot *fb = new FileBot("/home");And then this variable was also incorporated in other methods such as loading path into memory. There are 2 scenarios on loading path into memory; the first scenario is to load the parent path into memory. Say for example when the given input value is /home/user, then only the particular home and user were constructed into memory. Any other files located under the /home directory will be ignored. This is the piece of this construct:
int FileBot::constructParentPath() { // bail out if the path doesn't exists if( !exists(inputPath.generic_string()) ) return 0; // construct the parent path first string path = inputPath.generic_string(); FileNode *parent = NULL; typedef split_iterator<string::iterator> string_split_iterator; for( string_split_iterator it = make_split_iterator(path, first_finder("/", is_equal())); it != string_split_iterator(); ++it) { FileNode *node = new FileNode(); node->setType('d'); node->setName(copy_range<string>(*it)); cout << "value inserted: [" << copy_range<string>(*it) << "]" << endl; if( parent == NULL ) { fileList.push_back(*node); } else parent->sibling.push_back(*node); parent = node; cout << "parent: " << parent; cout << " parent value: " << parent->getName(); cout << " sibling size: " << parent->sibling.size() << endl; } // keep the current path in memory root = parent; cout << "***** debug : root node " << root << " : " << root->getName() << endl; return 1; }
The second scenario would be loading the child path into memory. This time it will load all the files under the directory and expand to load the files in any other subdirectories (if any). Continuing from the previous example, /home/user, this section will load the files under the user directory, including the underlying sub directory. The construct for this is as follows:
void FileBot::constructChildPath() { vector<path> pathList; // where am I now? BOOST_LOG_TRIVIAL(info) << "current path: " << inputPath << inputPath.filename() << endl; // capture the paths/files list under the source copy(filesystem::directory_iterator(inputPath), filesystem::directory_iterator(), back_inserter(pathList)); std::sort(pathList.begin(), pathList.end()); // scan through path and those files sit in that path // scan for files in sub directory if there is any sub directory in that path for(vector<path>::const_iterator it(pathList.begin()); it != pathList.end(); ++it) { string file = FileHelper::convertPathToString((*it)); // extract the file name from the path file = file.substr(file.find_last_of(boost::filesystem::path::preferred_separator) + 1, file.length()); if( is_directory(file) ) { cout << file << " is a directory." << endl; FileNode *node = new FileNode(); node->setName(inputPath.filename()); node->setType('d'); root->sibling.push_back(*node); } else { FileNode *node = new FileNode(); node->setName(file); node->setType('f'); root->sibling.push_back(*node); } } }To ensure my code is always implemented correctly, the following test case must return a successful result:
- load 1 level parent path.
- load 2 level parent path.
- load 3 level parent path.
- file separator appear at the end of a path.
- test on one file is captured without sub folder.
- test on two files are captured without sub folder.
- test on one files are captured when there is a sub folder.
No comments:
Post a Comment