Options.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <config.h>
00040 #ifdef OASYS_BLUETOOTH_ENABLED
00041 #include <bluetooth/bluetooth.h>
00042 #include "bluez/Bluetooth.h"
00043 #endif // OASYS_BLUETOOTH_ENABLED
00044 
00045 #include <stdio.h>
00046 #include <unistd.h>
00047 
00048 #include "Options.h"
00049 #include "io/NetUtils.h"
00050 
00051 namespace oasys {
00052 
00053 //----------------------------------------------------------------------
00054 Opt::Opt(char shortopt, const char* longopt,
00055          void* valp, bool* setp, bool needval,
00056          const char* valdesc, const char* desc)
00057     
00058     : shortopt_(shortopt),
00059       longopt_(longopt),
00060       valp_(valp),
00061       setp_(setp),
00062       needval_(needval),
00063       valdesc_(valdesc),
00064       desc_(desc),
00065       next_(0)
00066 {
00067     if (setp) *setp = false;
00068 }
00069 
00070 //----------------------------------------------------------------------
00071 Opt::~Opt()
00072 {
00073 }
00074 
00075 //----------------------------------------------------------------------
00076 BoolOpt::BoolOpt(const char* opt, bool* valp,
00077                  const char* desc, bool* setp)
00078     : Opt(0, opt, valp, setp, false, "", desc)
00079 {
00080 }
00081 
00082 //----------------------------------------------------------------------
00083 BoolOpt::BoolOpt(char shortopt, const char* longopt, bool* valp,
00084                  const char* desc, bool* setp)
00085     : Opt(shortopt, longopt, valp, setp, false, "", desc)
00086 {
00087 }
00088 
00089 //----------------------------------------------------------------------
00090 int
00091 BoolOpt::set(const char* val, size_t len)
00092 {
00093     if ((val == 0) ||
00094         (strncasecmp(val, "t", len) == 0)     ||
00095         (strncasecmp(val, "true", len) == 0) ||
00096         (strncasecmp(val, "1", len) == 0))
00097     {
00098         *((bool*)valp_) = true;
00099     }
00100     else if ((strncasecmp(val, "f", len) == 0)     ||
00101              (strncasecmp(val, "false", len) == 0) ||
00102              (strncasecmp(val, "0", len) == 0))
00103     {
00104         *((bool*)valp_) = false;
00105     }
00106     else
00107     {
00108         return -1;
00109     }
00110 
00111     if (setp_)
00112         *setp_ = true;
00113 
00114     return 0;
00115 }
00116 
00117 //----------------------------------------------------------------------
00118 IntOpt::IntOpt(const char* opt, int* valp,
00119                const char* valdesc, const char* desc, bool* setp)
00120     : Opt(0, opt, valp, setp, true, valdesc, desc)
00121 {
00122 }
00123 
00124 //----------------------------------------------------------------------
00125 IntOpt::IntOpt(char shortopt, const char* longopt, int* valp,
00126                const char* valdesc, const char* desc, bool* setp)
00127     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00128 {
00129 }
00130 
00131 //----------------------------------------------------------------------
00132 int
00133 IntOpt::set(const char* val, size_t len)
00134 {
00135     int newval;
00136     char* endptr = 0;
00137 
00138     newval = strtol(val, &endptr, 0);
00139     if (endptr != (val + len))
00140         return -1;
00141             
00142     *((int*)valp_) = newval;
00143     
00144     if (setp_)
00145         *setp_ = true;
00146     
00147     return 0;
00148 }
00149 
00150 //----------------------------------------------------------------------
00151 UIntOpt::UIntOpt(const char* opt, u_int* valp,
00152                  const char* valdesc, const char* desc, bool* setp)
00153     : Opt(0, opt, valp, setp, true, valdesc, desc)
00154 {
00155 }
00156 
00157 //----------------------------------------------------------------------
00158 UIntOpt::UIntOpt(char shortopt, const char* longopt, u_int* valp,
00159                  const char* valdesc, const char* desc, bool* setp)
00160     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00161 {
00162 }
00163 
00164 //----------------------------------------------------------------------
00165 int
00166 UIntOpt::set(const char* val, size_t len)
00167 {
00168     u_int newval;
00169     char* endptr = 0;
00170 
00171     newval = strtoul(val, &endptr, 0);
00172     if (endptr != (val + len))
00173         return -1;
00174             
00175     *((u_int*)valp_) = newval;
00176     
00177     if (setp_)
00178         *setp_ = true;
00179     
00180     return 0;
00181 }
00182 
00183 //----------------------------------------------------------------------
00184 UInt16Opt::UInt16Opt(const char* opt, u_int16_t* valp,
00185                      const char* valdesc, const char* desc, bool* setp)
00186     : Opt(0, opt, valp, setp, true, valdesc, desc)
00187 {
00188 }
00189 
00190 //----------------------------------------------------------------------
00191 UInt16Opt::UInt16Opt(char shortopt, const char* longopt, u_int16_t* valp,
00192                      const char* valdesc, const char* desc, bool* setp)
00193     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00194 {
00195 }
00196 
00197 //----------------------------------------------------------------------
00198 int
00199 UInt16Opt::set(const char* val, size_t len)
00200 {
00201     u_int newval;
00202     char* endptr = 0;
00203 
00204     newval = strtoul(val, &endptr, 0);
00205     if (endptr != (val + len))
00206         return -1;
00207 
00208     if (newval > 65535)
00209         return -1;
00210             
00211     *((u_int16_t*)valp_) = (u_int16_t)newval;
00212     
00213     if (setp_)
00214         *setp_ = true;
00215     
00216     return 0;
00217 }
00218 
00219 //----------------------------------------------------------------------
00220 UInt8Opt::UInt8Opt(const char* opt, u_int8_t* valp,
00221                    const char* valdesc, const char* desc, bool* setp)
00222     : Opt(0, opt, valp, setp, true, valdesc, desc)
00223 {
00224 }
00225 
00226 //----------------------------------------------------------------------
00227 UInt8Opt::UInt8Opt(char shortopt, const char* longopt, u_int8_t* valp,
00228                    const char* valdesc, const char* desc, bool* setp)
00229     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00230 {
00231 }
00232 
00233 //----------------------------------------------------------------------
00234 int
00235 UInt8Opt::set(const char* val, size_t len)
00236 {
00237     u_int newval;
00238     char* endptr = 0;
00239 
00240     newval = strtoul(val, &endptr, 0);
00241     if (endptr != (val + len))
00242         return -1;
00243 
00244     if (newval > 65535)
00245         return -1;
00246 
00247     *((u_int8_t*)valp_) = (u_int8_t)newval;
00248 
00249     if (setp_)
00250         *setp_ = true;
00251 
00252     return 0;
00253 }
00254 
00255 //----------------------------------------------------------------------
00256 DoubleOpt::DoubleOpt(const char* opt, double* valp,
00257                      const char* valdesc, const char* desc, bool* setp)
00258     : Opt(0, opt, valp, setp, true, valdesc, desc)
00259 {
00260 }
00261 
00262 //----------------------------------------------------------------------
00263 DoubleOpt::DoubleOpt(char shortopt, const char* longopt, double* valp,
00264                      const char* valdesc, const char* desc, bool* setp)
00265     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00266 {
00267 }
00268 
00269 //----------------------------------------------------------------------
00270 int
00271 DoubleOpt::set(const char* val, size_t len)
00272 {
00273     double newval;
00274     char* endptr = 0;
00275 
00276     newval = strtod(val, &endptr);
00277     if (endptr != (val + len))
00278         return -1;
00279             
00280     *((double*)valp_) = newval;
00281     
00282     if (setp_)
00283         *setp_ = true;
00284     
00285     return 0;
00286 }
00287 
00288 //----------------------------------------------------------------------
00289 StringOpt::StringOpt(const char* opt, std::string* valp,
00290                      const char* valdesc, const char* desc, bool* setp)
00291     : Opt(0, opt, valp, setp, true, valdesc, desc)
00292 {
00293 }
00294 
00295 //----------------------------------------------------------------------
00296 StringOpt::StringOpt(char shortopt, const char* longopt, std::string* valp,
00297                      const char* valdesc, const char* desc, bool* setp)
00298     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00299 {
00300 }
00301 
00302 //----------------------------------------------------------------------
00303 int
00304 StringOpt::set(const char* val, size_t len)
00305 {
00306     ((std::string*)valp_)->assign(val, len);
00307 
00308     if (setp_)
00309         *setp_ = true;
00310     
00311     return 0;
00312 }
00313 
00314 
00315 //----------------------------------------------------------------------
00316 CharBufOpt::CharBufOpt(const char* opt, char* valp, size_t* lenp, size_t buflen,
00317                        const char* valdesc, const char* desc, bool* setp)
00318     : Opt(0, opt, valp, setp, true, valdesc, desc), buflen_(buflen), lenp_(lenp)
00319 {
00320 }
00321 
00322 //----------------------------------------------------------------------
00323 CharBufOpt::CharBufOpt(char shortopt, const char* longopt,
00324                        char* valp, size_t* lenp, size_t buflen,
00325                        const char* valdesc, const char* desc, bool* setp)
00326     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc),
00327       buflen_(buflen), lenp_(lenp)
00328 {
00329 }
00330 
00331 //----------------------------------------------------------------------
00332 int
00333 CharBufOpt::set(const char* val, size_t len)
00334 {
00335     if (len > buflen_) {
00336         return -1;
00337     }
00338 
00339     memcpy(valp_, val, len);
00340 
00341     *lenp_ = len;
00342     
00343     if (setp_)
00344         *setp_ = true;
00345     
00346     return 0;
00347 }
00348 
00349 //----------------------------------------------------------------------
00350 InAddrOpt::InAddrOpt(const char* opt, in_addr_t* valp,
00351                      const char* valdesc, const char* desc, bool* setp)
00352     : Opt(0, opt, valp, setp, true, valdesc, desc)
00353 {
00354 }
00355 
00356 //----------------------------------------------------------------------
00357 InAddrOpt::InAddrOpt(char shortopt, const char* longopt, in_addr_t* valp,
00358                      const char* valdesc, const char* desc, bool* setp)
00359     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00360 {
00361 }
00362 
00363 //----------------------------------------------------------------------
00364 int
00365 InAddrOpt::set(const char* val, size_t len)
00366 {
00367     (void)len;
00368     
00369     in_addr_t newval;
00370 
00371     if (oasys::gethostbyname(val, &newval) != 0) {
00372         return -1;
00373     }
00374         
00375     *((in_addr_t*)valp_) = newval;
00376     
00377     if (setp_)
00378         *setp_ = true;
00379     
00380     return 0;
00381 }
00382 
00383 //----------------------------------------------------------------------
00384 EnumOpt::EnumOpt(const char* opt, Case* cases, int* valp,
00385                  const char* valdesc, const char* desc, bool* setp)
00386     : Opt(0, opt, valp, setp, true, valdesc, desc), cases_(cases)
00387 {
00388 }
00389 
00390 //----------------------------------------------------------------------
00391 EnumOpt::EnumOpt(char shortopt, const char* longopt,
00392                  Case* cases, int* valp,
00393                  const char* valdesc, const char* desc, bool* setp)
00394     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc), 
00395       cases_(cases)
00396 {
00397 }
00398 
00399 //----------------------------------------------------------------------
00400 int
00401 EnumOpt::set(const char* val, size_t len)
00402 {
00403     (void)len;
00404     
00405     size_t i = 0;
00406 
00407     while (cases_[i].key != 0)
00408     {
00409         if (!strcasecmp(cases_[i].key, val)) {
00410 
00411             (*(int*)valp_) = cases_[i].val;
00412             
00413             if (setp_)
00414                 *setp_ = true;
00415             
00416             return 0;
00417         }
00418         ++i;
00419     }
00420     
00421     return -1;
00422 }
00423 
00424 #ifdef OASYS_BLUETOOTH_ENABLED
00425 //----------------------------------------------------------------------
00426 BdAddrOpt::BdAddrOpt(const char* opt, bdaddr_t* valp,
00427                      const char* valdesc, const char* desc, bool* setp)
00428     : Opt(0, opt, valp, setp, true, valdesc, desc)
00429 {
00430 }
00431 
00432 //----------------------------------------------------------------------
00433 BdAddrOpt::BdAddrOpt(char shortopt, const char* longopt, bdaddr_t* valp,
00434                      const char* valdesc, const char* desc, bool* setp)
00435     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00436 {
00437 }
00438 
00439 //----------------------------------------------------------------------
00440 int
00441 BdAddrOpt::set(const char* val, size_t len)
00442 {
00443     bdaddr_t newval;
00444     (void)len;
00445 
00446     /* returns NULL on failure */
00447     if (Bluetooth::strtoba(val, &newval) == 0) {
00448         return -1;
00449     }
00450 
00451     *((bdaddr_t*)valp_) = newval;
00452 
00453     if (setp_)
00454         *setp_ = true;
00455 
00456     return 0;
00457 }
00458 #endif // OASYS_BLUETOOTH_ENABLED
00459 
00460 } // namespace oasys

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1