|
From: <hug...@li...> - 2010-10-18 16:50:51
|
branch: details: http://hugin.hg.sourceforge.net/hgweb/hugin/hugin/hgrepo/h/hu/hugin/hugin/rev/fd06e19e63a7 changeset: 4503:fd06e19e63a7 user: tmodes date: Mon Oct 18 18:49:48 2010 +0200 description: Celeste_standalone is now using getopt for command line parsing Old implementation could result in crashs if given invalid parameters diffstat: src/celeste/CMakeLists.txt | 2 +- src/celeste/Main.cpp | 108 ++++++++++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 31 deletions(-) diffs (154 lines): diff -r 0c2084090adb -r fd06e19e63a7 src/celeste/CMakeLists.txt --- a/src/celeste/CMakeLists.txt Mon Oct 18 18:36:17 2010 +0200 +++ b/src/celeste/CMakeLists.txt Mon Oct 18 18:49:48 2010 +0200 @@ -73,7 +73,7 @@ # standalone ADD_EXECUTABLE(celeste_standalone ../hugin_config.h Main.cpp ) -target_link_libraries( celeste_standalone celeste ${image_libs}) +target_link_libraries( celeste_standalone celeste ${image_libs} ${common_libs}) SET (CELESTE_MODEL data/celeste.model diff -r 0c2084090adb -r fd06e19e63a7 src/celeste/Main.cpp --- a/src/celeste/Main.cpp Mon Oct 18 18:36:17 2010 +0200 +++ b/src/celeste/Main.cpp Mon Oct 18 18:49:48 2010 +0200 @@ -30,6 +30,9 @@ #ifdef _WINDOWS #include <windows.h> +#include <getopt.h> +#else + #include <unistd.h> #endif using namespace std; @@ -178,7 +181,8 @@ }; -int main(int argc, const char* argv[]){ +int main(int argc, char* argv[]) +{ // Exit with usage unless filename given as argument if (argc < 2) @@ -187,7 +191,7 @@ } unsigned int i = 1, mask = 0; - double threshold = 0.5; + double threshold = 0.5; vector<string> images_to_mask; string pto_file = (""),output_pto = (""); string mask_format = ("PNG"); @@ -196,37 +200,81 @@ int resize_dimension=800; // Deal with arguments - while(i < argc) + // parse arguments + int c; + const char * optstring = "i:o:d:s:t:m:f:r:h"; + + while ((c = getopt (argc, argv, optstring)) != -1) { - if( argv[i][0] == '-'){ + switch(c) + { + case 'h': + usage(); + break; + case 'i': + pto_file=optarg; + break; + case 'o': + output_pto=optarg; + break; + case 't': + threshold = atof(optarg); + if(threshold<=0 || threshold>1) + { + cerr << "Invalid parameter: threshold (-t) should be between 0 and 1" << std::endl; + return 1; + }; + break; + case 'm': + mask = atoi(optarg); + if(mask<0 || mask>1) + { + cerr << "Invalid parameter: mask parameter (-m) can only be 0 or 1" << std::endl; + return 1; + }; + break; + case 'f': + mask_format = optarg; + break; + case 'd': + model_file = optarg; + break; + case 'r': + course_fine = atoi(optarg); + break; + case 's': + resize_dimension = atoi(optarg); + if(resize_dimension<100) + { + cerr << "Invalid parameter: maximum dimension (-s) should be bigger than 100" << std::endl; + return 1; + }; + break; + case ':': + cerr <<"Missing parameter for parameter " << argv[optind] << endl; + return 1; + break; + case '?': /* invalid parameter */ + return 1; + break; + default: /* unknown */ + usage(); + }; + }; + + while(optind<argc) + { + images_to_mask.push_back(argv[optind]); + optind++; + }; - if (argc == 2){ - usage(); - } + if(images_to_mask.size()==0 && pto_file.empty()) + { + cout << "No project file or image files given."<< endl; + return 1; + }; + - i++; - switch(argv[i-1][1]){ - - // Put some more argument options in here later - case 'h' : {usage();} - case 'i' : {pto_file += argv[i]; break;} - case 'o' : {output_pto += argv[i]; break;} - case 't' : {threshold = atof(argv[i]); break;} - case 'm' : {mask = atoi(argv[i]); break;} - case 'f' : {mask_format = argv[i]; break;} - case 'd' : {model_file = argv[i]; break;} - case 'r' : {course_fine = atoi(argv[i]); break;} - case 's' : {resize_dimension = atoi(argv[i]); break;} - } - - - }else{ - images_to_mask.push_back(argv[i]); - } - - i++; - } - // Check model file if (!fileexists(model_file)){ |