xorp

test_args.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8 sw=4:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/contrib/olsr/test_args.hh,v 1.3 2008/10/02 21:56:35 bms Exp $
00022 
00023 #ifndef __OSPF_TEST_ARGS_HH__
00024 #define __OSPF_TEST_ARGS_HH__
00025 
00026 #include "libxorp/tokenize.hh"
00027 
00031 class Args {
00032  public:
00033     Args(string& line) : _line(line), _pos(0) {
00034     tokenize(line, _words);
00035     }
00036 
00041     bool get_next(string& word) {
00042     if (_pos >= _words.size())
00043         return false;
00044 
00045     word = _words[_pos++];
00046 
00047     return true;
00048     }
00049 
00050     void push_back() {
00051     if (_pos > 1)
00052         _pos--;
00053     }
00054 
00055     const string& original_line() const {
00056     return _line;
00057     }
00058 
00059  private:
00060     const string _line;
00061     vector<string> _words;
00062     size_t _pos;
00063 };
00064 
00065 
00073 inline
00074 uint32_t
00075 get_number(const string& word)
00076     throw(InvalidString)
00077 {
00078     char *endptr;
00079     
00080     uint32_t number = strtoul(word.c_str(), &endptr, 0);
00081     if (0 != *endptr)
00082     xorp_throw(InvalidString,
00083            c_format("<%s> is not a number", word.c_str()));
00084 
00085     return number;
00086 }
00087 
00095 inline
00096 IPv4
00097 get_ipv4(const string& word)
00098     throw(InvalidString)
00099 {
00100     IPv4 addr;
00101     try {
00102     addr = IPv4(word.c_str());
00103     } catch (...) {
00104     xorp_throw(InvalidString,
00105            c_format("<%s> is not an IPv4 address", word.c_str()));
00106     }
00107 
00108     return addr;
00109 }
00110 
00118 inline
00119 IPv4Net
00120 get_ipv4_net(const string& word)
00121     throw(InvalidString)
00122 {
00123     IPv4Net v4net;
00124     try {
00125     v4net = IPv4Net(word.c_str());
00126     } catch (...) {
00127     xorp_throw(InvalidString,
00128            c_format("<%s> is not an IPv4 network address",
00129                 word.c_str()));
00130     }
00131 
00132     return v4net;
00133 }
00134 
00142 inline
00143 bool
00144 get_bool(const string& word)
00145     throw(InvalidString)
00146 {
00147     bool value = false;
00148 
00149     try {
00150     int i_value = get_number(word);
00151     if (i_value == 1) {
00152         value = true;
00153     } else if (i_value == 1) {
00154         value = false;
00155     }
00156     } catch (InvalidString is) {
00157     if (0 == strcasecmp(word.c_str(), "true")) {
00158         value = true;
00159     } else if (0 == strcasecmp(word.c_str(), "false")) {
00160         value = false;
00161     } else {
00162         // re-throw exception with appropriate error message
00163         xorp_throw(InvalidString,
00164                c_format("<%s> is not a boolean", word.c_str()));
00165     }
00166     }
00167 
00168     return value;
00169 }
00170 
00179 inline
00180 string
00181 get_next_word(Args& args, const string& varname)
00182     throw(InvalidString)
00183 {
00184     string var;
00185     if (!args.get_next(var))
00186     xorp_throw(InvalidString,
00187            c_format("No argument to %s. [%s]",
00188                 varname.c_str(),
00189                 args.original_line().c_str()));
00190 
00191     return var;
00192 }
00193 
00194 inline
00195 uint32_t
00196 get_next_number(Args& args, const string& varname)
00197     throw(InvalidString)
00198 {
00199     return get_number(get_next_word(args, varname));
00200 }
00201 
00202 inline
00203 IPv4
00204 get_next_ipv4(Args& args, const string& varname)
00205     throw(InvalidString)
00206 {
00207     return get_ipv4(get_next_word(args, varname));
00208 }
00209 
00210 inline
00211 IPv4Net
00212 get_next_ipv4_net(Args& args, const string& varname)
00213     throw(InvalidString)
00214 {
00215     return get_ipv4_net(get_next_word(args, varname));
00216 }
00217 
00218 inline
00219 bool
00220 get_next_bool(Args& args, const string& varname)
00221     throw(InvalidString)
00222 {
00223     return get_bool(get_next_word(args, varname));
00224 }
00225 
00226 #endif // __OSPF_TEST_ARGS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations