NERsuite  1.1.1
src/nersuite_common/option_parser.h
00001 /*
00002 *      NERSuite
00003 *      Option Parser utility
00004 *
00005 * Copyright (c) 
00006 * All rights reserved.
00007 *
00008 * Redistribution and use in source and binary forms, with or without
00009 * modification, are permitted provided that the following conditions are met:
00010 *     * Redistributions of source code must retain the above copyright
00011 *       notice, this list of conditions and the following disclaimer.
00012 *     * Redistributions in binary form must reproduce the above copyright
00013 *       notice, this list of conditions and the following disclaimer in the
00014 *       documentation and/or other materials provided with the distribution.
00015 *     * Neither the names of the authors nor the names of its contributors
00016 *       may be used to endorse or promote products derived from this
00017 *       software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00023 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 */
00031 
00032 #ifndef         _OPTION_PARSER_H
00033 #define         _OPTION_PARSER_H
00034 
00035 #include <iostream>
00036 #include <string>
00037 #include <vector>
00038 
00039 namespace NER
00040 {
00041 
00042         // Define the structure of a parameter
00043         typedef struct {
00044                 std::string     name;
00045                 std::string     value;
00046         } PARAM;
00047         typedef         std::vector<PARAM>                      V1_PARAM;
00048 
00057         class OPTION_PARSER {
00058         private:
00059                 V1_PARAM        params;                 // All parameters will be stored here.
00060                 std::vector<const char*>        args;
00061 
00062         public:
00069                 int parse(int n, char* items[]) {
00070                         int             consumed = 0;
00071                         PARAM   param;
00072 
00073                         for (int i = 0; i < n; ++i) {
00074                                 if (items[i][0] == '-') {
00075                                         // 1. Get parameter name.
00076                                         param.name = items[i];
00077                                         ++consumed;
00078 
00079                                         // 2. Get parameter value
00080                                         if ((i+1) < n) {
00081                                                 if(items[i+1][0] == '-') {
00082                                                         param.value = "";
00083                                                 }else {
00084                                                         param.value = items[i+1];
00085                                                         ++i;
00086                                                         ++consumed;
00087                                                 }
00088                                         }else {
00089                                                 param.value = "";
00090                                         }
00091 
00092                                         // 3. Put a parameter in the parameter container
00093                                         params.push_back(param);
00094                                 } else {
00095                                         args.push_back(items[i]);
00096                                 }
00097                         }
00098                         return consumed;
00099                 }
00100 
00107                 bool get_value(const std::string &name, std::string &value) {
00108                         bool found = false;
00109                         for (V1_PARAM::const_iterator citr = params.begin(); citr != params.end(); ++citr) {
00110                                 if (citr->name == name) {
00111                                         value = citr->value;
00112                                         found = true;
00113                                 }
00114                         }
00115                         return found;
00116                 }
00117 
00121                 void output_params() {
00122                         for (V1_PARAM::const_iterator citr = params.begin(); citr != params.end(); ++citr) {
00123                                 if (citr->value == "")
00124                                         std::cout << citr->name << std::endl;
00125                                 else
00126                                         std::cout << citr->name << " : " << citr->value << std::endl;   
00127                         }
00128                 }
00129 
00134                 const std::vector<const char*>& get_args() const
00135                 {
00136                         return args;
00137                 }
00138         };
00139 }
00140 #endif
00141 
 All Classes Functions Variables