NERsuite  1.1.1
src/gtagger/option_parser/option_parser.h
00001 
00008 #ifndef         __OPTION_PARSER__
00009 #define         __OPTION_PARSER__
00010 
00011 #include <iostream>
00012 #include <string>
00013 #include <vector>
00014 
00015 using namespace std;
00016 
00017 // Define the structure of a parameter
00018 typedef struct {
00019         string  name;
00020         string  value;
00021 } PARAM;
00022 typedef         vector<PARAM>                   V1_PARAM;
00023 
00024 
00025 class OPTION_PARSER {
00026 private:
00027         V1_PARAM        params;                 // All parameters will be stored here.
00028 
00029 public:
00030         // Parse input parameters.
00031         int parse(int n, char* items[]) {
00032                 int             consumed = 0;
00033                 PARAM   param;
00034 
00035                 for (int i = 0; i < n; ++i) {
00036                         if (items[i][0] == '-') {
00037                                 // 1. Get parameter name.
00038                                 param.name = items[i];
00039                                 ++consumed;
00040 
00041                                 // 2. Get parameter value
00042                                 if ((i+1) < n) {
00043                                         if(items[i+1][0] == '-') {
00044                                                 param.value = "";
00045                                         }else {
00046                                                 param.value = items[i+1];
00047                                                 ++i;
00048                                                 ++consumed;
00049                                         }
00050                                 }else {
00051                                         param.value = "";
00052                                 }
00053 
00054                                 // 3. Put a parameter in the parameter container
00055                                 params.push_back(param);
00056                         }else {                                                                         // 2. No more parameters.
00057                                 break;
00058                         }
00059                 }
00060                 return consumed;
00061         }
00062 
00063         // Get the value of a given parameter name.
00064         bool get_value(const string &name, string &value) {
00065                 bool found = false;
00066                 for (V1_PARAM::const_iterator citr = params.begin(); citr != params.end(); ++citr) {
00067                         if (citr->name == name) {
00068                                 value = citr->value;
00069                                 found = true;
00070                         }
00071                 }
00072                 return found;
00073         }
00074 
00075         // Output parameters.
00076         void output_params(void) {
00077                 for (V1_PARAM::const_iterator citr = params.begin(); citr != params.end(); ++citr) {
00078                         if (citr->value == "")
00079                                 cout << citr->name << endl;
00080                         else
00081                                 cout << citr->name << " : " << citr->value << endl;     
00082                 }
00083         }
00084 
00085 };
00086 
00087 
00088 #endif
00089 
 All Classes Functions Variables