/usr/include/itpp/base/parser.h is in libitpp-dev 4.3.1-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | /*!
* \file
* \brief Definition of an argument parser class
* \author Thomas Eriksson, Pal Frenger and Johan Bergman
*
* Thanks to Svante Signell for valuable input
*
* -------------------------------------------------------------------------
*
* Copyright (C) 1995-2010 (see AUTHORS file for a list of contributors)
*
* This file is part of IT++ - a C++ library of mathematical, signal
* processing, speech processing, and communications classes and functions.
*
* IT++ is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with IT++. If not, see <http://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#ifndef PARSER_H
#define PARSER_H
// #define MAX_STR_LEN 4096
#include <itpp/base/vec.h>
#include <itpp/base/mat.h>
#include <itpp/base/array.h>
#include <iostream>
#include <itpp/itexports.h>
namespace itpp
{
//! \cond
#if (defined(_MSC_VER) && defined(ITPP_SHARED_LIB))
//MSVC explicitely instantiate required template while building the shared library
template class ITPP_EXPORT Array<std::string>;
#endif
//! \endcond
/*!
\addtogroup parser
\brief Argument Parser
\author Thomas Eriksson and Pal Frenger (Thanks to Svante Signell for
valuable input), modifications by Johan Bergman
This class parses strings to variables. The syntax is compatible with Matlab
and Octave. It can be used in several different ways. The following test
program and test data file gives several examples:
<ul>
<li> Use the Parser class on a parameter file </li>
<li> Use the Parser class on the command line input </li>
<li> Use the Parser class on a char pointer (usually the command line input) </li>
<li> Use the Parser class on a parameter file and a char pointer </li>
<li> Use the Parser class on an Array of strings </li>
<li> Use the Parser::get() method on a parameter file </li>
<li> Use the Parser::exist() method to check if variables exist </li>
</ul>
The test program looks as follows:
\include parser_test.cpp
The data file \c parser_test_data.txt looks as follows:
\include parser_test_data.txt
Beside the type-specific get methods, like get_int(), there is a templated
get method called get(), that can handle all variable types that have an
istream operator defined, e.g. Arrays. The get() method takes the variable
value as an input/output parameter. If the variable name cannot be found,
the old value is kept. Here is an example:
\code
// Declare and initialize a variable
Array<ivec> var;
set_array(var, "{[1] [2 3] [4 5 6]}");
// Let the Parser p get the variable named my_var_name
bool my_var_name_was_found = p.get(var, "my_var_name");
\endcode
In the above example, if \c my_var_name was not found, \c var keeps its old
value {[1] [2 3] [4 5 6]}. In non-silent mode, the get() method echoes the
variable values to the standard output in Matlab/Octave m-file style.
*/
/*!
\ingroup parser
\brief Argument Parser Class
\author Thomas Eriksson and Pal Frenger (Thanks to Svante Signell for
valuable input)
This class parses strings to variables. The syntax is compatible with Matlab
and Octave. It can be used in several different ways. See the Detailed Description
in the \ref parser module.
*/
class ITPP_EXPORT Parser
{
public:
//! Default Constructor
Parser();
//! Constructor. Sets input file name.
Parser(const std::string &filename);
//! Constructor. Uses argc and argv (command line arguments)
Parser(int argc, char *argv[]);
//! Constructor. Sets input file name and uses argc and argv (command line arguments)
Parser(const std::string &filename, int argc, char *argv[]);
//! Constructor. Sets and Array of strings
Parser(const Array<std::string> &setup);
//! Initialization function. Sets input file name.
void init(const std::string &filename);
//! Initialization function. Uses argc and argv (command line arguments)
void init(int argc, char *argv[]);
//! Initialization function. Sets input file name and uses argc and argv (command line arguments)
void init(const std::string &filename, int argc, char *argv[]);
//! Initialization function. Sets and Array of strings
void init(const Array<std::string> &setup);
//! Sets silent mode if true, or verbose mode if false
void set_silentmode(bool v = true);
//! Check is \a name exists in the file. Returns \c true if the \a name is found and \c false otherwise.
bool exist(const std::string &name);
//! Get variable value if \a name can be found (and return true), otherwise keep old value (and return false)
template<class T>
bool get(T &var, const std::string &name, int num = -1);
//! Interpret variable \a name as a bool
bool get_bool(const std::string &name, int num = -1);
//! Interpret variable \a name as an integer
int get_int(const std::string &name, int num = -1);
//! Interpret variable \a name as a double
double get_double(const std::string &name, int num = -1);
//! Interpret variable \a name as a string
std::string get_string(const std::string &name, int num = -1);
//! Interpret variable \a name as a vec
vec get_vec(const std::string &name, int num = -1);
//! Interpret variable \a name as a ivec
ivec get_ivec(const std::string &name, int num = -1);
//! Interpret variable \a name as a svec
svec get_svec(const std::string &name, int num = -1);
//! Interpret variable \a name as a bvec
bvec get_bvec(const std::string &name, int num = -1);
//! Interpret variable \a name as a mat
mat get_mat(const std::string &name, int num = -1);
//! Interpret variable \a name as a imat
imat get_imat(const std::string &name, int num = -1);
//! Interpret variable \a name as a smat
smat get_smat(const std::string &name, int num = -1);
//! Interpret variable \a name as a bmat
bmat get_bmat(const std::string &name, int num = -1);
protected:
private:
//! Find the string \c name
std::string findname(const std::string &name,
bool &error_flag,
bool &print_flag,
int num = 0,
bool keep_brackets = false);
void pre_parsing(void);
Array<std::string> SetupStrings;
bool VERBOSE;
};
// ----------------------- Implementation starts here -----------------------
template<class T>
bool Parser::get(T &var, const std::string &name, int num)
{
bool error_flag, print_flag;
std::string str = findname(name, error_flag, print_flag, num, true);
std::istringstream buffer(str);
if (error_flag) {
if (VERBOSE) {
std::cout << name << " = " << var << ";" << std::endl;
}
}
else {
buffer >> var;
if (print_flag) {
std::cout << name << " = " << var << std::endl;
}
else if (VERBOSE) {
std::cout << name << " = " << var << ";" << std::endl;
}
}
return !error_flag;
}
//! Specialization or \c get() for std::string
template<>
ITPP_EXPORT bool Parser::get(std::string &var, const std::string &name, int num);
//! Specialization of \c get() for int
template<>
ITPP_EXPORT bool Parser::get(int &var, const std::string &name, int num);
//! Specialization of \c get() for bool
template<>
ITPP_EXPORT bool Parser::get(bool &var, const std::string &name, int num);
} // namespace itpp
#endif // #ifndef PARSER_H
|