Sunday, December 18, 2016

The searching mechanism is ready

The fun part of this program, file searching, is created. This module was dominated by boost::filesystem. I start off with some experiment on this module. Then slowly evolve piece by piece from my test lab, and now it has graduate become a man that took up the responsible on file searching. This is the attribute of the object:
class FilePath
{
public:
    FilePath();
    FilePath(path thePath);

    void captureFiles(path searchPath);
    set<wstring> getFileSet() { return fileSet; }


private:
    bool checkPathExists(path workingPath);


private:

    set<wstring> fileSet;
    path rootPath;
    bool searchDone = false;
    void clearFileSet() { fileSet.clear(); }
};
Each of this object will be responsible on each path. Only one and no more! The strongest power, captureFiles() is the most important function this object could ever had. It searches for files, and capture them into memory (cool huh?):
void FilePath::captureFiles(path searchPath)
{
    vector<path> pathList;

    if( rootPath.wstring().length() == 0 )
        rootPath = searchPath;

    if( exists(searchPath) ) {
        // capture the paths/files list under the source
        copy(directory_iterator(searchPath), directory_iterator(), back_inserter(pathList));

        // scan through each path/file to categorize them either path or file
        for(vector<path>::const_iterator it(pathList.begin()); it != pathList.end(); ++it) {
            wstring file = (*it).wstring();
            wstring thePath = rootPath.wstring();
            file = file.substr(thePath.length() + 1, file.length());

            // it is a directory
            if( is_directory(status(*it)) ) {
                captureFiles(*it);
            }
            // it is a file
            else {
                fileSet.insert(file);
            }
        }
    }

    searchDone = true;
}
The key of this object is the rootPath. When this object is summoned, one must not forget is to tell which path to go, just as shown below:
FilePath::FilePath(path thePath)
    :rootPath(thePath), searchDone(false)
{
}
If the key was missing during initialization, this object might behave abnormally.

No comments: