Options.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-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 
00018 #include <config.h>
00019 #ifdef OASYS_BLUETOOTH_ENABLED
00020 #include "bluez/Bluetooth.h"
00021 #endif // OASYS_BLUETOOTH_ENABLED
00022 
00023 #include <stdio.h>
00024 #include <unistd.h>
00025 
00026 #include "Options.h"
00027 #include "io/NetUtils.h"
00028 #include "util/StringBuffer.h"
00029 
00030 namespace oasys {
00031 
00032 //----------------------------------------------------------------------
00033 Opt::Opt(char shortopt, const char* longopt,
00034          void* valp, bool* setp, bool needval,
00035          const char* valdesc, const char* desc)
00036     
00037     : shortopt_(shortopt),
00038       longopt_(longopt),
00039       valp_(valp),
00040       setp_(setp),
00041       needval_(needval),
00042       valdesc_(valdesc),
00043       desc_(desc)
00044 {
00045     if (setp) *setp = false;
00046 }
00047 
00048 //----------------------------------------------------------------------
00049 Opt::~Opt()
00050 {
00051 }
00052 
00053 //----------------------------------------------------------------------
00054 BoolOpt::BoolOpt(const char* opt, bool* valp,
00055                  const char* desc, bool* setp)
00056     : Opt(0, opt, valp, setp, false, "", desc)
00057 {
00058 }
00059 
00060 //----------------------------------------------------------------------
00061 BoolOpt::BoolOpt(char shortopt, const char* longopt, bool* valp,
00062                  const char* desc, bool* setp)
00063     : Opt(shortopt, longopt, valp, setp, false, "", desc)
00064 {
00065 }
00066 
00067 //----------------------------------------------------------------------
00068 int
00069 BoolOpt::set(const char* val, size_t len)
00070 {
00071     if ((val == 0) ||
00072         (strncasecmp(val, "t", len) == 0)     ||
00073         (strncasecmp(val, "true", len) == 0) ||
00074         (strncasecmp(val, "1", len) == 0))
00075     {
00076         *((bool*)valp_) = true;
00077     }
00078     else if ((strncasecmp(val, "f", len) == 0)     ||
00079              (strncasecmp(val, "false", len) == 0) ||
00080              (strncasecmp(val, "0", len) == 0))
00081     {
00082         *((bool*)valp_) = false;
00083     }
00084     else
00085     {
00086         return -1;
00087     }
00088 
00089     if (setp_)
00090         *setp_ = true;
00091 
00092     return 0;
00093 }
00094 
00095 //----------------------------------------------------------------------
00096 void
00097 BoolOpt::get(StringBuffer* buf)
00098 {
00099     if (*((bool*)valp_)) {
00100         buf->appendf("true");
00101     } else {
00102         buf->appendf("false");
00103     }
00104 }
00105 
00106 //----------------------------------------------------------------------
00107 IntOpt::IntOpt(const char* opt, int* valp,
00108                const char* valdesc, const char* desc, bool* setp)
00109     : Opt(0, opt, valp, setp, true, valdesc, desc)
00110 {
00111 }
00112 
00113 //----------------------------------------------------------------------
00114 IntOpt::IntOpt(char shortopt, const char* longopt, int* valp,
00115                const char* valdesc, const char* desc, bool* setp)
00116     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00117 {
00118 }
00119 
00120 //----------------------------------------------------------------------
00121 int
00122 IntOpt::set(const char* val, size_t len)
00123 {
00124     int newval;
00125     char* endptr = 0;
00126 
00127     newval = strtol(val, &endptr, 0);
00128     if (endptr != (val + len))
00129         return -1;
00130             
00131     *((int*)valp_) = newval;
00132     
00133     if (setp_)
00134         *setp_ = true;
00135     
00136     return 0;
00137 }
00138 
00139 //----------------------------------------------------------------------
00140 void
00141 IntOpt::get(StringBuffer* buf)
00142 {
00143     buf->appendf("%d", *(int*)valp_);
00144 }
00145 
00146 //----------------------------------------------------------------------
00147 UIntOpt::UIntOpt(const char* opt, u_int* valp,
00148                  const char* valdesc, const char* desc, bool* setp)
00149     : Opt(0, opt, valp, setp, true, valdesc, desc)
00150 {
00151 }
00152 
00153 //----------------------------------------------------------------------
00154 UIntOpt::UIntOpt(char shortopt, const char* longopt, u_int* valp,
00155                  const char* valdesc, const char* desc, bool* setp)
00156     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00157 {
00158 }
00159 
00160 //----------------------------------------------------------------------
00161 int
00162 UIntOpt::set(const char* val, size_t len)
00163 {
00164     u_int newval;
00165     char* endptr = 0;
00166 
00167     newval = strtoul(val, &endptr, 0);
00168     if (endptr != (val + len))
00169         return -1;
00170             
00171     *((u_int*)valp_) = newval;
00172     
00173     if (setp_)
00174         *setp_ = true;
00175     
00176     return 0;
00177 }
00178 
00179 //----------------------------------------------------------------------
00180 void
00181 UIntOpt::get(StringBuffer* buf)
00182 {
00183     buf->appendf("%u", *(u_int*)valp_);
00184 }
00185 
00186 //----------------------------------------------------------------------
00187 UInt64Opt::UInt64Opt(const char* opt, u_int64_t* valp,
00188                      const char* valdesc, const char* desc, bool* setp)
00189     : Opt(0, opt, valp, setp, true, valdesc, desc)
00190 {
00191 }
00192 
00193 //----------------------------------------------------------------------
00194 UInt64Opt::UInt64Opt(char shortopt, const char* longopt, u_int64_t* valp,
00195                      const char* valdesc, const char* desc, bool* setp)
00196     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00197 {
00198 }
00199 
00200 //----------------------------------------------------------------------
00201 int
00202 UInt64Opt::set(const char* val, size_t len)
00203 {
00204     u_int64_t newval;
00205     char* endptr = 0;
00206     
00207     newval = strtoull(val, &endptr, 0);
00208     if (endptr != (val + len))
00209         return -1;
00210             
00211     *((u_int64_t*)valp_) = newval;
00212     
00213     if (setp_)
00214         *setp_ = true;
00215     
00216     return 0;
00217 }
00218 
00219 //----------------------------------------------------------------------
00220 void
00221 UInt64Opt::get(StringBuffer* buf)
00222 {
00223     buf->appendf("%llu", U64FMT((*(u_int64_t*)valp_)));
00224 }
00225 
00226 //----------------------------------------------------------------------
00227 UInt16Opt::UInt16Opt(const char* opt, u_int16_t* valp,
00228                      const char* valdesc, const char* desc, bool* setp)
00229     : Opt(0, opt, valp, setp, true, valdesc, desc)
00230 {
00231 }
00232 
00233 //----------------------------------------------------------------------
00234 UInt16Opt::UInt16Opt(char shortopt, const char* longopt, u_int16_t* valp,
00235                      const char* valdesc, const char* desc, bool* setp)
00236     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00237 {
00238 }
00239 
00240 //----------------------------------------------------------------------
00241 int
00242 UInt16Opt::set(const char* val, size_t len)
00243 {
00244     u_int newval;
00245     char* endptr = 0;
00246 
00247     newval = strtoul(val, &endptr, 0);
00248     if (endptr != (val + len))
00249         return -1;
00250 
00251     if (newval > 65535)
00252         return -1;
00253             
00254     *((u_int16_t*)valp_) = (u_int16_t)newval;
00255     
00256     if (setp_)
00257         *setp_ = true;
00258     
00259     return 0;
00260 }
00261 
00262 //----------------------------------------------------------------------
00263 void
00264 UInt16Opt::get(StringBuffer* buf)
00265 {
00266     buf->appendf("%u", *(u_int16_t*)valp_);
00267 }
00268 
00269 //----------------------------------------------------------------------
00270 UInt8Opt::UInt8Opt(const char* opt, u_int8_t* valp,
00271                    const char* valdesc, const char* desc, bool* setp)
00272     : Opt(0, opt, valp, setp, true, valdesc, desc)
00273 {
00274 }
00275 
00276 //----------------------------------------------------------------------
00277 UInt8Opt::UInt8Opt(char shortopt, const char* longopt, u_int8_t* valp,
00278                    const char* valdesc, const char* desc, bool* setp)
00279     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00280 {
00281 }
00282 
00283 //----------------------------------------------------------------------
00284 int
00285 UInt8Opt::set(const char* val, size_t len)
00286 {
00287     u_int newval;
00288     char* endptr = 0;
00289 
00290     newval = strtoul(val, &endptr, 0);
00291     if (endptr != (val + len))
00292         return -1;
00293 
00294     if (newval > 65535)
00295         return -1;
00296 
00297     *((u_int8_t*)valp_) = (u_int8_t)newval;
00298 
00299     if (setp_)
00300         *setp_ = true;
00301 
00302     return 0;
00303 }
00304 
00305 //----------------------------------------------------------------------
00306 void
00307 UInt8Opt::get(StringBuffer* buf)
00308 {
00309     buf->appendf("%u", *(u_int8_t*)valp_);
00310 }
00311 
00312 //----------------------------------------------------------------------
00313 DoubleOpt::DoubleOpt(const char* opt, double* valp,
00314                      const char* valdesc, const char* desc, bool* setp)
00315     : Opt(0, opt, valp, setp, true, valdesc, desc)
00316 {
00317 }
00318 
00319 //----------------------------------------------------------------------
00320 DoubleOpt::DoubleOpt(char shortopt, const char* longopt, double* valp,
00321                      const char* valdesc, const char* desc, bool* setp)
00322     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00323 {
00324 }
00325 
00326 //----------------------------------------------------------------------
00327 int
00328 DoubleOpt::set(const char* val, size_t len)
00329 {
00330     double newval;
00331     char* endptr = 0;
00332 
00333     newval = strtod(val, &endptr);
00334     if (endptr != (val + len))
00335         return -1;
00336             
00337     *((double*)valp_) = newval;
00338     
00339     if (setp_)
00340         *setp_ = true;
00341     
00342     return 0;
00343 }
00344 
00345 //----------------------------------------------------------------------
00346 void
00347 DoubleOpt::get(StringBuffer* buf)
00348 {
00349     buf->appendf("%f", *(double*)valp_);
00350 }
00351 
00352 //----------------------------------------------------------------------
00353 StringOpt::StringOpt(const char* opt, std::string* valp,
00354                      const char* valdesc, const char* desc, bool* setp)
00355     : Opt(0, opt, valp, setp, true, valdesc, desc)
00356 {
00357 }
00358 
00359 //----------------------------------------------------------------------
00360 StringOpt::StringOpt(char shortopt, const char* longopt, std::string* valp,
00361                      const char* valdesc, const char* desc, bool* setp)
00362     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00363 {
00364 }
00365 
00366 //----------------------------------------------------------------------
00367 int
00368 StringOpt::set(const char* val, size_t len)
00369 {
00370     ((std::string*)valp_)->assign(val, len);
00371 
00372     if (setp_)
00373         *setp_ = true;
00374     
00375     return 0;
00376 }
00377 
00378 //----------------------------------------------------------------------
00379 void
00380 StringOpt::get(StringBuffer* buf)
00381 {
00382     buf->appendf("%s", ((std::string*)valp_)->c_str());
00383 }
00384 
00385 //----------------------------------------------------------------------
00386 CharBufOpt::CharBufOpt(const char* opt, char* valp, size_t* lenp, size_t buflen,
00387                        const char* valdesc, const char* desc, bool* setp)
00388     : Opt(0, opt, valp, setp, true, valdesc, desc), buflen_(buflen), lenp_(lenp)
00389 {
00390 }
00391 
00392 //----------------------------------------------------------------------
00393 CharBufOpt::CharBufOpt(char shortopt, const char* longopt,
00394                        char* valp, size_t* lenp, size_t buflen,
00395                        const char* valdesc, const char* desc, bool* setp)
00396     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc),
00397       buflen_(buflen), lenp_(lenp)
00398 {
00399 }
00400 
00401 //----------------------------------------------------------------------
00402 int
00403 CharBufOpt::set(const char* val, size_t len)
00404 {
00405     if (len > buflen_) {
00406         return -1;
00407     }
00408 
00409     memcpy(valp_, val, len);
00410 
00411     *lenp_ = len;
00412     
00413     if (setp_)
00414         *setp_ = true;
00415     
00416     return 0;
00417 }
00418 
00419 //----------------------------------------------------------------------
00420 void
00421 CharBufOpt::get(StringBuffer* buf)
00422 {
00423     buf->appendf("%.*s", (int)*lenp_, (char*)valp_);
00424 }
00425 
00426 //----------------------------------------------------------------------
00427 InAddrOpt::InAddrOpt(const char* opt, in_addr_t* valp,
00428                      const char* valdesc, const char* desc, bool* setp)
00429     : Opt(0, opt, valp, setp, true, valdesc, desc)
00430 {
00431 }
00432 
00433 //----------------------------------------------------------------------
00434 InAddrOpt::InAddrOpt(char shortopt, const char* longopt, in_addr_t* valp,
00435                      const char* valdesc, const char* desc, bool* setp)
00436     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00437 {
00438 }
00439 
00440 //----------------------------------------------------------------------
00441 int
00442 InAddrOpt::set(const char* val, size_t len)
00443 {
00444     (void)len;
00445     
00446     in_addr_t newval;
00447 
00448     if (oasys::gethostbyname(val, &newval) != 0) {
00449         return -1;
00450     }
00451         
00452     *((in_addr_t*)valp_) = newval;
00453     
00454     if (setp_)
00455         *setp_ = true;
00456     
00457     return 0;
00458 }
00459 
00460 //----------------------------------------------------------------------
00461 void
00462 InAddrOpt::get(StringBuffer* buf)
00463 {
00464     buf->appendf("%s", intoa(*((in_addr_t*)valp_)));
00465 }
00466 
00467 //----------------------------------------------------------------------
00468 EnumOpt::EnumOpt(const char* opt, Case* cases, int* valp,
00469                  const char* valdesc, const char* desc, bool* setp)
00470     : Opt(0, opt, valp, setp, true, valdesc, desc), cases_(cases)
00471 {
00472 }
00473 
00474 //----------------------------------------------------------------------
00475 EnumOpt::EnumOpt(char shortopt, const char* longopt,
00476                  Case* cases, int* valp,
00477                  const char* valdesc, const char* desc, bool* setp)
00478     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc), 
00479       cases_(cases)
00480 {
00481 }
00482 
00483 //----------------------------------------------------------------------
00484 int
00485 EnumOpt::set(const char* val, size_t len)
00486 {
00487     (void)len;
00488     
00489     size_t i = 0;
00490 
00491     while (cases_[i].key != 0)
00492     {
00493         if (!strcasecmp(cases_[i].key, val)) {
00494 
00495             (*(int*)valp_) = cases_[i].val;
00496             
00497             if (setp_)
00498                 *setp_ = true;
00499             
00500             return 0;
00501         }
00502         ++i;
00503     }
00504     
00505     return -1;
00506 }
00507 
00508 //----------------------------------------------------------------------
00509 void
00510 EnumOpt::get(StringBuffer* buf)
00511 {
00512     size_t i = 0;
00513 
00514     while (cases_[i].key != 0)
00515     {
00516         if ((*(int*)valp_) == cases_[i].val) {
00517             buf->append(cases_[i].key);
00518             return;
00519         }
00520         ++i;
00521     }
00522 }
00523 
00524 #ifdef OASYS_BLUETOOTH_ENABLED
00525 //----------------------------------------------------------------------
00526 BdAddrOpt::BdAddrOpt(const char* opt, bdaddr_t* valp,
00527                      const char* valdesc, const char* desc, bool* setp)
00528     : Opt(0, opt, valp, setp, true, valdesc, desc)
00529 {
00530 }
00531 
00532 //----------------------------------------------------------------------
00533 BdAddrOpt::BdAddrOpt(char shortopt, const char* longopt, bdaddr_t* valp,
00534                      const char* valdesc, const char* desc, bool* setp)
00535     : Opt(shortopt, longopt, valp, setp, true, valdesc, desc)
00536 {
00537 }
00538 
00539 //----------------------------------------------------------------------
00540 int
00541 BdAddrOpt::set(const char* val, size_t len)
00542 {
00543     bdaddr_t newval;
00544     (void)len;
00545 
00546     /* returns NULL on failure */
00547     if (Bluetooth::strtoba(val, &newval) == 0) {
00548         return -1;
00549     }
00550 
00551     *((bdaddr_t*)valp_) = newval;
00552 
00553     if (setp_)
00554         *setp_ = true;
00555 
00556     return 0;
00557 }
00558 
00559 //----------------------------------------------------------------------
00560 void
00561 BdAddrOpt::get(StringBuffer* buf)
00562 {
00563     buf->appendf("%s", bd2str(*((bdaddr_t*)valp_)));
00564 }
00565 
00566 #endif // OASYS_BLUETOOTH_ENABLED
00567 
00568 } // namespace oasys

Generated on Thu Jun 7 12:54:28 2007 for DTN Reference Implementation by  doxygen 1.5.1