OptParser.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include "OptParser.h"
00018 
00019 namespace oasys {
00020 
00021 //----------------------------------------------------------------------
00022 OptParser::~OptParser()
00023 {
00024     for (u_int i = 0; i < allopts_.size(); ++i)
00025     {
00026         delete allopts_[i];
00027     }
00028     allopts_.clear();
00029 }
00030 
00031 //----------------------------------------------------------------------
00032 void
00033 OptParser::addopt(Opt* opt)
00034 {
00035     allopts_.push_back(opt);
00036 }
00037 
00038 //----------------------------------------------------------------------
00039 bool
00040 OptParser::parse_opt(const char* opt_str, size_t len, bool* invalid_value)
00041 {
00042     Opt* opt;
00043     const char* val_str;
00044     size_t opt_len, val_len;
00045 
00046     if (invalid_value) {
00047         *invalid_value = false;
00048     }
00049 
00050     opt_len = strcspn(opt_str, "= \t\r\n");
00051     if (opt_len == 0 || opt_len > len) {
00052         return false;
00053     }
00054 
00055     if (opt_str[opt_len] != '=') {
00056         val_str = NULL;
00057         val_len = 0;
00058     } else {
00059         val_str = opt_str + opt_len + 1;
00060         val_len = strcspn(val_str, " \t\r\n");
00061         if (val_len == 0 || (val_len + opt_len + 1) != len) {
00062             return false;
00063         }
00064     }
00065     
00066     int nopts = allopts_.size(); 
00067     for (int i = 0; i < nopts; ++i)
00068     {
00069         opt = allopts_[i];
00070         
00071         if (strncmp(opt_str, opt->longopt_, opt_len) == 0)
00072         {
00073             if (opt->needval_ && (val_str == NULL)) {
00074                 if (invalid_value) {
00075                     *invalid_value = true;
00076                 }
00077                 return false; // missing value
00078             }
00079 
00080             if (opt->set(val_str, val_len) != 0) {
00081                 if (invalid_value) {
00082                     *invalid_value = true;
00083                 }
00084                 return false; // error in set
00085             }
00086             
00087             return true; // all set
00088         }
00089     }
00090 
00091     return false; // no matching option
00092 }
00093 
00094 //----------------------------------------------------------------------
00095 bool
00096 OptParser::parse(const char* args, const char** invalidp)
00097 {
00098     const char* opt;
00099     size_t opt_len;
00100 
00101     opt = args;
00102     while (1) {
00103         opt_len = strcspn(opt, " \t\r\n");
00104         if (opt_len == 0) {
00105             return true; // all done
00106         }
00107 
00108         if (parse_opt(opt, opt_len) == false) {
00109             *invalidp = opt;
00110             return false;
00111         }
00112 
00113         // skip past the arg and all other whitespace
00114         opt = opt + opt_len;
00115         opt += strspn(opt, " \t\r\n");
00116     }
00117 }
00118 
00119 //----------------------------------------------------------------------
00120 bool
00121 OptParser::parse(int argc, const char* const argv[], const char** invalidp)
00122 {
00123     for (int i = 0; i < argc; ++i) {
00124         if (parse_opt(argv[i], strlen(argv[i])) == false) {
00125             *invalidp = argv[i];
00126             return false;
00127         }
00128     }
00129 
00130     return true;
00131 }
00132 
00133 //----------------------------------------------------------------------
00134 int
00135 OptParser::parse_and_shift(int argc, const char* argv[], const char** invalidp)
00136 {
00137     int last_slot = 0;
00138     int valid_count = 0;
00139     bool invalid_value;
00140 
00141     for (int i = 0; i < argc; ++i) {
00142         
00143         if (parse_opt(argv[i], strlen(argv[i]), &invalid_value) == true) {
00144             ++valid_count;
00145             
00146         } else {
00147             argv[last_slot] = argv[i];
00148             last_slot++;
00149             
00150             if (invalid_value) {
00151                 if (invalidp)
00152                     *invalidp = argv[i];
00153                 
00154                 return -1; // stop parsing
00155             }
00156         }
00157     }
00158     
00159     return valid_count;
00160 }
00161 
00162 //----------------------------------------------------------------------
00163 bool
00164 OptParser::parse(const std::vector<std::string>& args,
00165                  const char** invalidp)
00166 {
00167     std::vector<std::string>::const_iterator iter;
00168     for (iter = args.begin(); iter != args.end(); ++iter) {
00169         if (parse_opt(iter->c_str(), iter->length()) == false) {
00170             *invalidp = iter->c_str();
00171             return false;
00172         }
00173     }
00174 
00175     return true;
00176 }
00177 
00178 
00179 } // namespace oasys

Generated on Thu Jun 7 16:56:51 2007 for DTN Reference Implementation by  doxygen 1.5.1