int main(int argc, const char *argv[]) { TCLAP::CmdLine cmd("Command description message", ' ', "0.9"); TCLAP::ValueArg<std::string> inputPath("i", "input", "Input file", false, "", "string", cmd); TCLAP::ValueArg<std::string> outputpath("o", "output", "Output file", false, "", "string", cmd); cmd.parse(argc, argv); return EXIT_SUCCESS; }
int _tmain(int argc, _TCHAR* argv[]) { try { po::options_description desc( "Options" ); desc.add_options() ( "help,h", "Show detailed help." ) ( "options,p", "Show parameter information." ) ( "version,p", "Show version information." ) ( "input-file,i", po::value<std::vector<std::string>>(), "Define file(s) for the convertion input." ) ( "input-type,I", po::value<std::vector<std::string>>(), "Define file type(s) for automatic conversion of files in the working directory." ) ( "output-file,o", po::value<std::string>(), "Define file for the convertion output." ) ( "noheader,h", "Disable adding of header support defines." ) ( "const,C", "Define array as const." ) ( "respectcase,r", "Disable converting file types into lower case." ) ( "wxnone,w", "Disable adding of wxWidgets support macro's." ) ( "wxheader,W", po::value<std::string>()->default_value( "wx/wx.h" ), "Select the header that includes wxWidgets (precompiled header?)." ) ( "appendtype,t", "Add the file type at the end of the identifier (myimage_png)." ) ( "text,T", "Disable binary output and use text output, converts feed codes to systems defaults." ) ; po::positional_options_description posdesc; posdesc.add( "input-file", -1 ); po::variables_map opt; po::store( po::command_line_parser( argc, argv ).options( desc ).positional( posdesc ).run(), opt ); po::store( po::parse_config_file( fs::ifstream( fs::path( "default.cfg" ) ), desc ), opt ); po::notify( opt ); std::cout << WXINCLUDE_INFO << std::endl; /* Show options when requested */ if ( opt.count( "options" ) ) { std::cout << desc << std::endl << std::endl; exit( 0 ); } /* Show help when requested */ if ( opt.count( "help" ) ) { std::cout << WXINCLUDE_HELP; std::cout << std::endl << desc << std::endl << std::endl; exit( 0 ); } /* Show version when requested */ if ( opt.count( "version" ) ) { std::cout << WXINCLUDE_VERSION << std::endl; exit( 0 ); } /* Process */ if ( opt.count( "input-file" ) || opt.count( "input-type" ) ) { if ( opt.count( "output-file" ) ) { /* Create timer */ boost::timer timer; /* Create output file */ std::string headername( opt[ "output-file" ].as<std::string>() ); fs::path outputpath( headername ); fs::ofstream output( outputpath, std::ios::out | std::ios::trunc ); /* Use buffer */ char outbuffer[BUFFER_SIZE]; output.rdbuf()->pubsetbuf( outbuffer, BUFFER_SIZE ); if ( !opt.count( "text" ) ) output.setf( std::ios::binary ); if ( !output ) throw std::runtime_error( "Failed to create output file!" ); /* Show status */ std::cout << "Build : file '" << outputpath.leaf() << "'..." << std::endl; /* Get base name of file */ headername = fs::basename( outputpath ); /* Data string stream */ std::ostringstream data; /* Write header start when wanted */ if ( !opt.count( "noheader" ) ) defineheader_start( data, headername, opt.count( "wxnone" ) ? false : true, opt.count( "const" ) ? true : false ); /* Get defined or else default wx header */ std::string includename( opt[ "wxheader" ].as<std::string>() ); /* Write macros when wanted */ if ( !opt.count( "wxnone" ) ) definemacros( data, includename, opt[ "wxheader" ].defaulted() ); /* Common input buffer */ char inbuffer[BUFFER_SIZE]; /* Process input files based on provided list */ if ( opt.count( "input-file" ) ) { std::vector<std::string> files( opt[ "input-file" ].as<std::vector<std::string>>() ); BOOST_FOREACH( std::string& file, files ) { fs::path inputpath( file ); std::string fileext( fs::extension( inputpath ) ); fs::ifstream input( inputpath, std::ios::in | std::ios::binary | std::ios::ate ); input.rdbuf()->pubsetbuf( inbuffer, BUFFER_SIZE ); if ( input.is_open() ) { /* Show status */ std::cout << "Process: file '" << file << "'..." << std::endl; /* Remove extension */ boost::erase_last( file, fileext ); if ( !opt.count( "respectcase" ) ) boost::to_lower( fileext ); /* Append type */ if ( opt.count( "appendtype" ) ) { boost::erase_first( fileext, "." ); /* Static and NO copy constructor for speed */ static boost::format fmt( "%1%_%2%" ); file = boost::str( fmt % file % fileext ); } /* Lower case names when wanted */ if ( !opt.count( "respectcase" ) ) boost::to_lower( file ); /* Process file */ definefile( data, input, file, opt.count( "const" ) ? true : false ); } else { /* Only show warning, other files need to be processed */ std::cout << "Warning: input file '" << file << "' failed to open." << std::endl; } } } /* Process input files based on provided type */ if ( opt.count( "input-type" ) ) { std::vector<std::string> types( opt[ "input-type" ].as<std::vector<std::string>>() ); for ( fs::directory_iterator dir_itr( fs::initial_path() ); dir_itr != fs::directory_iterator(); ++dir_itr ) { BOOST_FOREACH( std::string& type, types ) { /* Normal file? */ if ( fs::is_regular( dir_itr->status() ) ) { /* Wanted type? */ std::string fileext( fs::extension( dir_itr->path() )); bool equals = false; if ( opt.count( "respectcase" ) ) equals = boost::equals( fileext, type ); else equals = boost::iequals( fileext, type ); if ( equals ) { fs::ifstream input( dir_itr->path(), std::ios::in | std::ios::binary | std::ios::ate ); input.rdbuf()->pubsetbuf( inbuffer, BUFFER_SIZE ); std::string file( dir_itr->path().leaf() ); if ( input.is_open() ) { /* Show status */ std::cout << "Process: file '" << file << "'..." << std::endl; /* Remove extension */ boost::erase_last( file, fileext ); /* Append type */ if ( opt.count( "appendtype" ) ) { boost::erase_first( fileext, "." ); /* Static and NO copy constructor for speed */ static boost::format fmt( "%1%_%2%" ); file = boost::str( fmt % file % fileext ); } /* Lower case names when wanted */ if ( !opt.count( "respectcase" ) ) boost::to_lower( file ); /* Process file */ definefile( data, input, file, opt.count( "const" ) ? true : false ); } else { /* Only show warning, other files need to be processed */ std::cout << "Warning: input file '" << file << "' failed to open." << std::endl; } } } } } }
int main(int argc, char* argv[]) #ifndef _DEBUG try #endif { int bound_feature_freq = 5; LdigFloat eta = 0.1, reg = 0; size_t cvn = 5, cvt = 2; MODE mode = detection; LdigFloat margin = -1; std::string modelpath("ldig.model"), outputpath(""); std::vector<std::string> files; for(int i=1; i<argc; ++i) { std::string st(argv[i]); if (st == "--ff") { if (++i>=argc) goto ERROR_OPT_FF; bound_feature_freq = atoi(argv[i]); } else if (st == "-e") { if (++i>=argc) goto ERROR_OPT_E; eta = atof(argv[i]); } else if (st == "-r") { if (++i>=argc) goto ERROR_OPT_R; reg = atof(argv[i]); } else if (st == "-m") { modelpath = argv[++i]; } else if (st == "-o") { outputpath = argv[++i]; } else if (st == "--init") { mode = initialization; } else if (st == "--learning") { mode = learning; } else if (st == "--detection") { mode = detection; } else if (st == "--shrink") { mode = shrink; } else if (st == "--maxsubst") { mode = maxsubst; } else if (st == "--dump") { mode = dump; } else if (st == "--cv") { mode = varidation; } else if (st == "--cvn") { if (++i>=argc) goto ERROR_OPT_CV; cvn = atoi(argv[i]); } else if (st == "--cvt") { if (++i>=argc) goto ERROR_OPT_CV; cvt = atoi(argv[i]); } else if (st == "--margin") { if (++i>=argc) goto ERROR_OPT_MARGIN; margin = atof(argv[i]); } else { files.push_back(st); } } switch (mode) { case initialization: ldig_init(modelpath, files, bound_feature_freq, eta, reg); break; case learning: std::cerr << "learning is not implemented yet" << std::endl; break; case detection: ldig_detect(modelpath, outputpath, files, margin); break; case shrink: ldig_shrink(modelpath); break; case varidation: ldig_varidation(files, outputpath, cvn, cvt, bound_feature_freq); break; case maxsubst: // for debug maxsubstring(files[0], files[1]); break; case dump: ldig_dump(modelpath, outputpath); break; } return 0; /* error */ char *p; ERROR_OPT_E: p = (char *)"[ERROR] -e option needs positive real number"; goto ERROR_EXIT; ERROR_OPT_R: p = (char *)"[ERROR] -r option needs positive real number"; goto ERROR_EXIT; ERROR_OPT_FF: p = (char *)"[ERROR] --ff option needs non-negative integer"; goto ERROR_EXIT; ERROR_OPT_CV: p = (char *)"[ERROR] --cvn/cvt option needs positive integer"; goto ERROR_EXIT; ERROR_OPT_MARGIN: p = (char *)"[ERROR] --margin option needs positive integer"; goto ERROR_EXIT; ERROR_EXIT: std::cerr << p << std::endl; return 1; #ifndef _DEBUG } catch (std::exception ex) { printf("err = %s\n", ex.what()); #endif }