Xmipp
v3.23.11-Nereus
|
Read lists | |
These functions try to read N values of the desired type into the given structure (either a STL vector of any numerical type by adding the read values at the end or a Matrix1D of any numerical type and then the values are written at PHYSICAL positions 0 ... N-1, the Matrix1D must be big enough to hold all the data since it is not resized internally. If it is not possible to read all parameters an exception is thrown. You can provide the exception in the function call but there are default values. These functions are based in those for reading single values, so it might be possible that these other functions throw other exceptions. The different elements of the list are selected using the tokenizing functions (different elements in the string are separated by spaces), so after the application of this function to the input string, this is modified and NULL characters are introduced as delimiters of the elements. The following code is an example of doing so: getline(in_stream, line); readFloatList(NULL, 10, v2); // Read NEXT!! 10 values in v2 | |
template<typename T > | |
void | readFloatList (const char *str, int N, std::vector< T > &v) |
template<typename T > | |
void | readFloatList (const std::string &str, size_t &i, int N, std::vector< T > &v) |
template<typename T > | |
void | readFloatList (const char *str, int N, Matrix1D< T > &v, ErrorType _errno=ERR_VALUE_INCORRECT, std::string errmsg="Error reading floating list", int exit=0) |
Functions for parsing the command line | |
These functions help you to manage the command line parameters | |
const char * | getParameter (int argc, const char **argv, const char *param, const char *option=NULL) |
bool | getTwoDoubleParams (int argc, const char **argv, const char *param, double &v1, double &v2, double v1_def, double v2_def) |
bool | getThreeDoubleParams (int argc, const char **argv, const char *param, double &v1, double &v2, double &v3, double v1_def, double v2_def, double v3_def) |
bool | checkParameter (int argc, const char **argv, const char *param) |
int | paremeterPosition (int argc, const char **argv, const char *param) |
int | numComponents (const std::string &str) |
Matrix1D< double > | getVectorParameter (int argc, const char **argv, const char *param, int dim=2) |
Matrix1D< double > | getVectorParameter (FILE *fh, const char *param, int dim=2) |
void | generateCommandLine (const std::string &command_line, int &argcp, char **&argvp, char *©) |
bool | generateCommandLine (FILE *fh, const char *param, int &argcp, char **&argvp, char *©) |
std::string | getParameter (FILE *fh, const char *param, int skip=0, const char *option=NULL) |
bool | checkParameter (FILE *fh, const char *param) |
This set of functions are designed for make easier checking and reading parameters from a string. The single value readings don't modify the value of the input string, while the multiple value readings do. In general, the reading is oriented to be done with a chain of strtok (a general C function), for this reason the string itself is modified by the successive calls to strtok.
The second group (list reading) uses tokens internally, while in the single read functions the token must be given by hand. Anyway, it is not so difficult to read a set of fields from a line with all the normal checks we would like (existence of a parameter, checking for numerical correctness...)
The following is an example of its use. This code tries to read a number, a character and a list of numbers from a line. In the example you can also see how to write code for the different error handling methods.
bool checkParameter | ( | int | argc, |
const char ** | argv, | ||
const char * | param | ||
) |
Get boolean parameters from the command line.
This function assumes that the command line is structured in such a way that for each parameter a block like "-param" is defined. The label "param" can be substituted by any other one you like. It might be used to look for a boolean parameter, for instance:
-verbose means that verbose functionality is set (TRUE)
Definition at line 97 of file args.cpp.
bool checkParameter | ( | FILE * | fh, |
const char * | param | ||
) |
Check if a parameter is present in a file.
The same as the previous function, but this function only reports if a parameter is present or not in a file. Notice that boolean parameters must be defined as "parameter=". If after the parameter, "no" comes then this function returns FALSE
Definition at line 434 of file args.cpp.
void generateCommandLine | ( | const std::string & | command_line, |
int & | argcp, | ||
char **& | argvp, | ||
char *& | copy | ||
) |
Generate argc and argv for a string.
Given a string this function makes a copy of the string and divides it into tokens such that they can be used as argc and argv, as if it were a command line.
The string copy remains in "copy" and it can be freed by disposing this variable.
argvp[0] (normally the program name) is set to any value, in this case to "autom", standing for "automatically generated".
argcp==0 if now valid command line is provided, ie, the line is empty or only with blanks.
Next time the function is called it checks that argv and copy are empty (pointing to NULL), if they aren't then firstly the associated memory is freed.
Definition at line 238 of file args.cpp.
bool generateCommandLine | ( | FILE * | fh, |
const char * | param, | ||
int & | argcp, | ||
char **& | argvp, | ||
char *& | copy | ||
) |
Generate articial command line from a file.
The copy variable must be destroyed outside by "delete copy". This function takes "input_file=<input_file>" and turns it into "-input_file <input_file>" The appropriate argc, argv are also returned.
Returns TRUE if the parameter is found in the file, and FALSE if it is not
Definition at line 332 of file args.cpp.
const char* getParameter | ( | int | argc, |
const char ** | argv, | ||
const char * | param, | ||
const char * | option = NULL |
||
) |
Get parameters from the command line.
This function assumes that the command line is structured in such a way that for each parameter a block like "-param <param_value>" is defined. The label "param" can be substituted by any other one you like. If the parameter is optional then this function allows you to define a default value. If no default value is supplied and the parameter is not specified in the command line, then an exception is thrown. You may change the default exception.
You may also indicate that in case of error no exception is raised and force the program to abort (use the exit variable).
Definition at line 30 of file args.cpp.
std::string getParameter | ( | FILE * | fh, |
const char * | param, | ||
int | skip = 0 , |
||
const char * | option = NULL |
||
) |
Get parameter from file.
Parameters are supposed to be identified with an =, so any line which doesn't contain an = character cannot contain a parameter. The left part of the "=" is the identifier, and the right part the value. The value is returned without any extra space, if it is compound of several words, then the spaces in between are simplified to a single blank space.
The file position inside the file is not moved and comments are allowed starting with "#" and ";".
Parameter skip controls the number of these parameters to skip before returning the value, ie, if there are several "parameter=" tags in a file and you want the first one then you should skip 0, if you want the second the skip=1, ...
The meaning and use of the exit, errors and optional value is the same as in the command line getParameter
Definition at line 379 of file args.cpp.
bool getThreeDoubleParams | ( | int | argc, |
const char ** | argv, | ||
const char * | param, | ||
double & | v1, | ||
double & | v2, | ||
double & | v3, | ||
double | v1_def, | ||
double | v2_def, | ||
double | v3_def | ||
) |
Get 3 float parameters after a flag from the command line.
An exception is thrown if there are not enough parameters after the flag, if the message is empty then "Not enough parameters after <param>" is shown. The default values must be provided. TRUE is returned if the two values have been found
Definition at line 70 of file args.cpp.
bool getTwoDoubleParams | ( | int | argc, |
const char ** | argv, | ||
const char * | param, | ||
double & | v1, | ||
double & | v2, | ||
double | v1_def, | ||
double | v2_def | ||
) |
Get two float parameters after a flag from the command line.
An exception is thrown if there are not enough parameters after the flag, if the message is empty then "Not enough parameters after <param>" is shown. The default values must be provided. TRUE is returned if the two values have been found
Definition at line 46 of file args.cpp.
Matrix1D< double > getVectorParameter | ( | int | argc, |
const char ** | argv, | ||
const char * | param, | ||
int | dim = 2 |
||
) |
Get float vector.
A vector is defined as a "[x,y,z, ...]" of any dimension (by default 2D vectors are read). The vector must not contain blank spaces.
The vector is internally resized properly. If the dimension is -1 then all the vector components are read disregarding their dimensionality, ie,-1 is used to read vectors of an unknown dimension. If the parameter is not found when no dimensionality is given an empty vector is returned but no exception is thrown. If there is no dimensionality and a single parameter is behind the flag then no brackets are needed
Definition at line 138 of file args.cpp.
Matrix1D<double> getVectorParameter | ( | FILE * | fh, |
const char * | param, | ||
int | dim = 2 |
||
) |
Get float vector.
Same as the previous function but from a file.
Definition at line 216 of file args.cpp.
int numComponents | ( | const std::string & | str | ) |
Return the number of components of a vector argument.
A vector argument is defined as [x,y,z,...]. It returns 0 if the string does not contain a vector
int paremeterPosition | ( | int | argc, |
const char ** | argv, | ||
const char * | param | ||
) |
Returns the position where the given parameter is in the command line.
This function assumes that the command line is structured in such a way that for each parameter a block like "-param" is defined. The label "param" can be substituted by any other one you like. It returns -1 if the parameter is not found. It is used to look for parameters having a list of values behind, for instance:
-ang rot tilt psi
Definition at line 111 of file args.cpp.
void readFloatList | ( | const char * | str, |
int | N, | ||
std::vector< T > & | v | ||
) |
void readFloatList | ( | const std::string & | str, |
size_t & | i, | ||
int | N, | ||
std::vector< T > & | v | ||
) |
void readFloatList | ( | const char * | str, |
int | N, | ||
Matrix1D< T > & | v, | ||
ErrorType | _errno = ERR_VALUE_INCORRECT , |
||
std::string | errmsg = "Error reading floating list" , |
||
int | exit = 0 |
||
) |