xorp

test_args.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
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/ospf/test_args.hh,v 1.6 2008/10/02 21:57:49 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 
00069 inline
00070 uint32_t
00071 get_number(const string& word) throw(InvalidString)
00072 {
00073     char *endptr;
00074     
00075     uint32_t number = strtoul(word.c_str(), &endptr, 0);
00076     if (0 != *endptr)
00077     xorp_throw(InvalidString,
00078            c_format("<%s> is not a number", word.c_str()));
00079 
00080     return number;
00081 }
00082 
00086 inline
00087 string
00088 get_next_word(Args& args, const string& varname) throw(InvalidString)
00089 {
00090     string var;
00091     if (!args.get_next(var))
00092     xorp_throw(InvalidString,
00093            c_format("No argument to %s. [%s]",
00094                 varname.c_str(),
00095                 args.original_line().c_str()));
00096 
00097     return var;
00098 }
00099 
00100 inline
00101 uint32_t
00102 get_next_number(Args& args, const string& varname) throw(InvalidString)
00103 {
00104     return get_number(get_next_word(args, varname));
00105 }
00106 
00107 
00108 #endif // __OSPF_TEST_ARGS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations