Click here to Skip to main content
15,891,372 members
Articles / Programming Languages / C++11

Extending boost::filesystem for Windows and Linux: Part 1

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
19 Mar 2013CPOL16 min read 31.2K   977   33  
Extending boost::filesystem for Windows and Linux.
/********************************************************************************
 *                                                                              *
 *                      Written by Stanic Igor 2012/2013                        *
 *                                                                              *
 *  This is an entry point for File System Helper application                   *
 *  It handles accepting command line args and printing most of final results   *
 ********************************************************************************/

#include "COMMAND_PROCESSOR.hpp"

using namespace std;
namespace bpo = boost::program_options;
namespace
{
#define appName CHRARR_CON("File System Helper")
#define appVer CHRARR_CON("1.0")
#define _sp CHRARR_CON(" ")
#define copyright CHRARR_CON("Written by Igor Stanic,2013.( site: http://presentation.itstudionet.com )")
#define helpOpt CHRARR_CON("Type -h or --help for option list.")
inline void PrintHeader()
{
    TCOUT << appName << _sp << CHRARR_CON("version: ") << appVer << endl;
    TCOUT << endl;
    TCOUT << copyright << endl;
    TCOUT <<  CHRARR_CON(R"(This software is free. I do NOT provide ANY kind of warranty for eventual loss or damage of any type or nature arising out of use of this software.)") << endl;
    TCOUT << helpOpt << endl;
    TCOUT << endl;
    TCOUT << endl;
}


} //END OF ANONYMOUS NS


int main(int argc, char** argw)
{
//print header BEGIN
    PrintHeader();
//print header END

//Parsing command line args BEGIN
    string wildcardArg;
    string watchLocArg;
    string makePathArg;
    string permDelArg;
    string trashArg;
#ifdef LINUX
    int restorationPathArg;
    string customPathFromTrash;
#endif
    string searchPathArg;
    string searchLocArg;
    string listLocArg;
    string fromArg;
    string toArg;
    UINT numOfResults;

    bpo::options_description desc("Allowed options");
    desc.add_options()
        ("help,h", "produce help message")
            ("test_wildcard",bpo::value<string>(&wildcardArg), "test effectiveness of | *.* | x*.y | *.y | *x.y | *x*.y | x*.* | wildcard combinations")
            ("watch_full", "start file system watcher by watching full set of changes")
            ("watch_here", "start file system watcher by watching current location")
            ("watch_location", bpo::value<string>(&watchLocArg), "define filesystem watcher location(s) with \';\' as separator")
            ("copy",  "copy path(s) operation") //options nooverwrite, overwrite
            ("move",  "move path(s) operation") //options nooverwrite, overwrite
            ("overwrite", "allow overwrite destination items when copy/move")
            ("merge", "allow merge during copy operation")
            ("from",bpo::value<string>(&fromArg), "relevant to copy, move and rename ops and can accept multiple paths with \';\' separator")
            ("to", bpo::value<string>(&toArg), "relevant for copy, move and rename ops")
            ("make_path", bpo::value<string>(&makePathArg), "create folders which are missing in path")
            ("pdel", bpo::value<string>(&permDelArg), "permanent delete operation with path(s) to delete")
            ("rn", "rename operation")
            ("to_trash", bpo::value<string>(&trashArg), "delete to trash/recycle bin operation")
#ifdef LINUX
            ("from_trash", bpo::value<int>(&restorationPathArg), "number of listed entry to recover")
            ("restore_path", bpo::value<string>(&customPathFromTrash), "optional custom path for restoring trashed file")
            ("size_trash",  "get current size of the trash")
#endif
            ("list_trash", "enumerated restoration paths of items in trash/recycle bin")
            ("search_loc", bpo::value<string>(&searchLocArg), "relevant for search, set location from where the search should begin")
            ("search_string", bpo::value<string>(&searchPathArg), "relevant for search, part of the name to search for")
            ("search_path", "relevant for search, defines searching for path fragment")
            ("case_sensitive", "relevant for search, defines if search should be case-sensitive(Windows)")
            ("exact_match", "relevant for search, if search should find only exact name matches")
            ("result_count", bpo::value<UINT>(&numOfResults)->default_value(0), "relevant for search, limit number of search findings for faster results")
            ("list", bpo::value<string>(&listLocArg)->implicit_value(""), "list files/folders on specified location")
            ("all","relevant to list option, shows all items present like hidden and system files")
            ("desc", "relevant to list option, lists items in descending order")
            ("v","verbose list output")
    ;

    OpResults results;
    bpo::variables_map vm;
    try
    {
        auto parsed = bpo::parse_command_line(argc, argw, desc);
        bpo::store(parsed, vm);
        bpo::notify(vm);
    }
    catch(const bpo::error_with_option_name& exc)
    {
        results.AddResult(OperationResult(false, TO_STRING_TYPE("command line option: ") + TO_STRING_TYPE(exc.get_option_name()),
                                          TO_STRING_TYPE(exc.what())));
    }
    catch(const bpo::error& exc)
    {
        results.AddResult(OperationResult(false, TO_STRING_TYPE("command line option"), TO_STRING_TYPE(exc.what())));
    }

    //Parsing command line args END

    //Handling parsing results BEGIN
    if(!results.hasErrors())
    {
        auto iter = vm.begin();
        if((*iter).first == "result_count")
        {
            ++iter;
            if(iter == vm.end())
                Print(TO_STRING_TYPE("No options detected...nothing to do."));
        }

        if(vm.count("help"))
        {
           cout<< desc << "\n";
           return 1;
        }

        results += exec_commands(vm);
    }
    //Handling parsing results END

    if(results.hasErrors())
    {
        Print(TO_STRING_TYPE("Errors ocurred during operation execution:"));
        for(const OperationResult& opres : results.errors())
        {
            TCOUT << CHRARR_CON("Error: ") << opres.errorMsg << CHRARR_CON(" source: ") << opres.path << endl;
        }
    }
    else
        Print(TO_STRING_TYPE("Operation executed without errors."));
    TCOUT << endl;
    return 0;

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Serbia Serbia
Software developer with couple of years of experience mostly with .NET programming and MS SQL databases currently interested in expanding knowledge to C++ and other operating systems.

Comments and Discussions