STIR
6.2.0
|
A class to parse Interfile headers. More...
#include "stir/KeyParser.h"
Public Member Functions | |
bool | parse (std::istream &f, const bool write_warnings=true) |
parse() returns false if there is some error, true otherwise More... | |
bool | parse (const char *const filename, const bool write_warnings=true) |
parse() returns false if there is some error, true otherwise More... | |
bool | parse (const std::string &, const bool write_warnings=true) |
void | add_key (const std::string &keyword, float *variable_ptr) |
add a keyword. When parsing, parse its value as a float and put it in *variable_ptr | |
void | add_vectorised_key (const std::string &keyword, std::vector< float > *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as a float and put it in (*variable_ptr)[current_index] | |
void | add_key (const std::string &keyword, double *variable_ptr) |
add a keyword. When parsing, parse its value as a double and put it in *variable_ptr | |
void | add_vectorised_key (const std::string &keyword, std::vector< double > *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as a double and put it in (*variable_ptr)[current_index] | |
void | add_vectorised_key (const std::string &keyword, std::vector< std::vector< double >> *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as a list of doubles and put it in (*variable_ptr)[current_index] | |
void | add_key (const std::string &keyword, int *variable_ptr) |
add a keyword. When parsing, parse its value as a int and put it in *variable_ptr | |
void | add_key (const std::string &keyword, std::vector< int > *variable_ptr) |
add a keyword. When parsing, parse its value as a int and put it in *variable_ptr | |
void | add_vectorised_key (const std::string &keyword, std::vector< int > *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as a int and put it in (*variable_ptr)[current_index] | |
void | add_vectorised_key (const std::string &keyword, std::vector< std::vector< int >> *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as a list of ints and put it in (*variable_ptr)[current_index] | |
void | add_key (const std::string &keyword, long int *variable_ptr) |
add a keyword. When parsing, parse its value as a int and put it in *variable_ptr | |
void | add_key (const std::string &keyword, unsigned int *variable_ptr) |
add a keyword. When parsing, parse its value as a int and put it in *variable_ptr | |
void | add_vectorised_key (const std::string &keyword, std::vector< unsigned int > *variable) |
add a vectorised keyword. When parsing, parse its value as an unsigned int and put it in (*variable_ptr)[current_index] | |
void | add_key (const std::string &keyword, unsigned long *variable_ptr) |
add a keyword. When parsing, parse its value as an unsigned long and put it in *variable_ptr | |
void | add_vectorised_key (const std::string &keyword, std::vector< unsigned long > *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as an unsigned long and put it in (*variable_ptr)[current_index] | |
void | add_key (const std::string &keyword, bool *variable_ptr) |
add a keyword. When parsing, parse its value as a int and put the bool value in *variable_ptr More... | |
void | add_key (const std::string &keyword, std::vector< double > *variable_ptr) |
add a keyword. When parsing, parse its value as a list of doubles and put its value in *variable_ptr | |
void | add_key (const std::string &keyword, std::vector< std::string > *variable_ptr) |
add a keyword. When parsing, parse its value as a list of comma-separated strings and put its value in *variable_ptr | |
void | add_key (const std::string &keyword, Array< 2, float > *variable_ptr) |
add a keyword. When parsing, parse its value as a 2d array of floats and put its value in *variable_ptr | |
void | add_key (const std::string &keyword, Array< 3, float > *variable_ptr) |
add a keyword. When parsing, parse its value as a 3d array of floats and put its value in *variable_ptr | |
void | add_key (const std::string &keyword, BasicCoordinate< 3, float > *variable_ptr) |
add a keyword. When parsing, parse its value as a 3d BasicCoordinate of floats and put its value in *variable_ptr | |
void | add_key (const std::string &keyword, BasicCoordinate< 3, Array< 3, float >> *variable_ptr) |
add a keyword. When parsing, parse its value as a 3d BasicCoordinate of a 3d array of floats and put its value in *variable_ptr | |
void | add_key (const std::string &keyword, std::string *variable_ptr) |
void | add_vectorised_key (const std::string &keyword, std::vector< std::string > *variable_ptr) |
add a vectorised keyword. When parsing, parse its value as a string and put it in (*variable_ptr)[current_index] More... | |
void | add_key (const std::string &keyword, int *variable_ptr, const ASCIIlist_type *const list_of_values) |
add a keyword. When parsing, its string value is checked against a list of strings. The corresponding index is stored in variable_ptr. More... | |
void | ignore_key (const std::string &keyword) |
Add keyword this is just ignored by the parser. | |
void | add_start_key (const std::string &keyword) |
add keyword that has to occur before all others More... | |
void | add_stop_key (const std::string &keyword) |
add a keyword that when encountered, will stop the parsing | |
template<typename ParsingClass > | |
void | add_parsing_key (const std::string &keyword, ParsingClass **parsed_object_ptr_ptr) |
add keyword corresponding to an object that will parse the next keys itself More... | |
template<typename ParsingClass > | |
void | add_parsing_key (const std::string &keyword, shared_ptr< ParsingClass > *parsed_object_ptr_ptr) |
add keyword corresponding to an object that will parse the next keys itself More... | |
bool | remove_key (const std::string &keyword) |
Removes a key from the kep map. More... | |
void | add_alias_key (const std::string &keyword, const std::string &alias, bool deprecated_key=true) |
Add an alias for keyword. More... | |
void | print_keywords_to_stream (std::ostream &) const |
Prints all keywords (in random order) to the stream. | |
virtual std::string | parameter_info () const |
Returns a string with keywords and their values. More... | |
virtual void | ask_parameters () |
Ask interactively for values for all keywords. More... | |
int | find_in_ASCIIlist (const std::string &, const ASCIIlist_type &list_of_values) |
returns the index of a string in 'list_of_values', -1 if not found More... | |
void | start_parsing () |
callback function to start parsing, has to be set by first keyword | |
void | stop_parsing () |
to stop parsing | |
void | do_nothing () |
callback function for keys which do not do anything | |
void | set_variable () |
callback function that sets the variable to the value given as the value of the keyword More... | |
void | set_parsing_object () |
callback function that sets the variable by calling the parser (as stored by add_parsing_key()), with argument the value of the keyword More... | |
void | set_shared_parsing_object () |
callback function that sets the shared_ptr variable by calling the parser (as stored by add_parsing_key()), with argument the value of the keyword More... | |
Protected Types | |
typedef void(KeyParser::* | KeywordProcessor) () |
Protected Member Functions | |
virtual bool | post_processing () |
This will be called at the end of the parsing. More... | |
virtual std::string | standardise_keyword (const std::string &keyword) const |
convert 'rough' keyword into a standardised form More... | |
virtual std::string | get_keyword (const std::string &) const |
gets a keyword from a string More... | |
void | add_key (const std::string &keyword, KeyArgument::type t, KeywordProcessor function, void *variable=0, const ASCIIlist_type *const list=0) |
add a keyword to the list, together with its call_back function More... | |
void | add_key (const std::string &keyword, KeyArgument::type t, KeywordProcessor function, void *variable, const int vectorised_key_level, const ASCIIlist_type *const list=0) |
add a keyword to the list, together with its call_back function More... | |
void | add_key (const std::string &keyword, KeyArgument::type t, void *variable, const ASCIIlist_type *const list=0) |
version that defaults 'function' to set_variable More... | |
void | add_key (const std::string &keyword, KeyArgument::type t, void *variable, const int vectorised_key_level, const ASCIIlist_type *const list=0) |
version that defaults 'function' to set_variable More... | |
Friends | |
class | map_element |
A class to parse Interfile headers.
Currently, Interfile 3.3 parsing rules are hard-coded, with some extensions. Note that some functions such as get_keyword()
are virtual
such that a derived class could use non-Interfile parsing (but this might need more work).
KeyParser reads input line by line and parses each line separately. It allows for '\r' at the end of the line (as in files originating in DOS/Windows). When the line ends with a backslash '\', the next line will just be appended to the current one. That is, '\' acts as continuation marker.
Lines can have arbitrary length.
A special facility is provided for using the value of environment variables: after reading the whole line, the text is checked for occurences of the form ${some_text}$
. These text strings are then replaced by the value of the corresponding environment variables (using the std::getenv function).
Main problem: when non-trivial callback functions have to be used, you need to do it via a derived class (as KeyParser requires pointers to member functions.)
bool stir::KeyParser::parse | ( | std::istream & | f, |
const bool | write_warnings = true |
||
) |
parse() returns false if there is some error, true otherwise
if write_warnings is false
, warnings about undefined keywords will be supressed.
Referenced by stir::CListModeDataPENN::CListModeDataPENN(), stir::CListModeDataROOT::CListModeDataROOT(), stir::PoissonLLReconstructionTests< target_type >::construct_projector_pair(), stir::RigidObject3DMotionFromPolaris::post_processing(), stir::ScatterEstimation::post_processing(), distributed::receive_and_construct_exam_and_proj_data_info_ptr(), and stir::TimeFrameDefinitions::TimeFrameDefinitions().
bool stir::KeyParser::parse | ( | const char *const | filename, |
const bool | write_warnings = true |
||
) |
parse() returns false if there is some error, true otherwise
if write_warnings is false
, warnings about undefined keywords will be supressed.
References stir::warning().
bool stir::KeyParser::parse | ( | const std::string & | s, |
const bool | write_warnings = true |
||
) |
if write_warnings is false
, warnings about undefined keywords will be supressed.
void stir::KeyParser::add_key | ( | const std::string & | keyword, |
bool * | variable_ptr | ||
) |
add a keyword. When parsing, parse its value as a int and put the bool value in *variable_ptr
The integer should be 0 or 1, corresponding to false and true resp.
void stir::KeyParser::add_key | ( | const std::string & | keyword, |
std::string * | variable_ptr | ||
) |
The 'value' can contain spaces.
void stir::KeyParser::add_vectorised_key | ( | const std::string & | keyword, |
std::vector< std::string > * | variable_ptr | ||
) |
add a vectorised keyword. When parsing, parse its value as a string and put it in (*variable_ptr)[current_index]
The 'value' can contain spaces.
void stir::KeyParser::add_key | ( | const std::string & | keyword, |
int * | variable_ptr, | ||
const ASCIIlist_type *const | list_of_values | ||
) |
add a keyword. When parsing, its string value is checked against a list of strings. The corresponding index is stored in variable_ptr.
If no match is found, a warning message is issued and *variable_ptr is set to -1.
void stir::KeyParser::add_start_key | ( | const std::string & | keyword | ) |
add keyword that has to occur before all others
Example of such a key: INTERFILE
References start_parsing().
Referenced by stir::AbsTimeIntervalFromDynamicData::AbsTimeIntervalFromDynamicData(), stir::AbsTimeIntervalFromECAT7ACF::AbsTimeIntervalFromECAT7ACF(), stir::BackProjectorByBinUsingSquareProjMatrixByBin::actual_back_project(), stir::ChainedDataProcessor< DataT >::ChainedDataProcessor(), stir::CListModeDataPENN::CListModeDataPENN(), stir::CListModeDataROOT::CListModeDataROOT(), stir::FilterRootPrior< DataT >::compute_gradient(), stir::PoissonLLReconstructionTests< target_type >::construct_projector_pair(), stir::DiscretisedShape3D::DiscretisedShape3D(), stir::SeparableCartesianMetzImageFilter< elemT >::get_max_kernel_sizes(), stir::BackProjectorByBinParallelproj::initialise_keymap(), stir::ForwardProjectorByBinParallelproj::initialise_keymap(), stir::SingleScatterSimulation::initialise_keymap(), stir::BackProjectorByBinNiftyPET::initialise_keymap(), stir::ForwardProjectorByBinNiftyPET::initialise_keymap(), stir::SeparableGaussianImageFilter< elemT >::initialise_keymap(), stir::InputStreamWithRecordsFromUPENNtxt::initialise_keymap(), stir::InputStreamWithRecordsFromUPENNbin::initialise_keymap(), stir::InputStreamFromROOTFileForECATPET::initialise_keymap(), stir::InputStreamFromROOTFileForCylindricalPET::initialise_keymap(), stir::ForwardProjectorByBin::initialise_keymap(), stir::BackProjectorByBin::initialise_keymap(), stir::ScatterEstimation::initialise_keymap(), stir::RigidObject3DMotionFromPolaris::post_processing(), stir::ScatterEstimation::post_processing(), stir::ProjMatrixByBinUsingInterpolation::ProjMatrixByBinUsingInterpolation(), and stir::RigidObject3DMotionFromPolaris::synchronise().
|
inline |
add keyword corresponding to an object that will parse the next keys itself
When this keyword is encountered during parsing, the parser ParsingClass::read_registered_object(name) is called, with 'name' set to to the value of the keyword.
ParsingClass has to be derived from RegisteredObject (strictly speaking it only has to be derived from Object, and provide a static function void read_registered_object(string&)).
Example of usage:
The .par file then would look as follows
SomeReconstructionParameters := beta := 0.001 MAP a la MRP filter type := Median ; keywords here appropriate to a Median ImageFilter End SomeReconstruction Parameters:= ;
Referenced by stir::BackProjectorByBinUsingSquareProjMatrixByBin::actual_back_project(), stir::ChainedDataProcessor< DataT >::ChainedDataProcessor(), stir::CListModeDataPENN::CListModeDataPENN(), stir::CListModeDataROOT::CListModeDataROOT(), stir::FilterRootPrior< DataT >::compute_gradient(), stir::PoissonLLReconstructionTests< target_type >::construct_projector_pair(), stir::ForwardProjectorByBin::initialise_keymap(), stir::BackProjectorByBin::initialise_keymap(), stir::ScatterEstimation::initialise_keymap(), and stir::ScatterEstimation::post_processing().
|
inline |
add keyword corresponding to an object that will parse the next keys itself
As above, but with a shared_ptr
bool stir::KeyParser::remove_key | ( | const std::string & | keyword | ) |
Removes a key from the kep map.
true
if it was found, false
otherwise References stir::warning().
void stir::KeyParser::add_alias_key | ( | const std::string & | keyword, |
const std::string & | alias, | ||
bool | deprecated_key = true |
||
) |
Add an alias for keyword.
If deprecated_key
if true
, a warning will be written when the alias is encountered.
parameter_info()
will use the keyword
, not the alias
.
Referenced by stir::CListModeDataROOT::CListModeDataROOT().
|
virtual |
Returns a string with keywords and their values.
Keywords are listed in the order they are inserted in the keymap (except for start and stop keys which are listed first and last).
References start_parsing(), and stop_parsing().
|
virtual |
Ask interactively for values for all keywords.
Keywords are asked in the order they are inserted in the keymap.
References do_nothing(), start_parsing(), stop_parsing(), and stir::warning().
int stir::KeyParser::find_in_ASCIIlist | ( | const std::string & | par_ascii, |
const ASCIIlist_type & | list_of_values | ||
) |
returns the index of a string in 'list_of_values', -1 if not found
Implementation note: this function is non-static because it uses standardise_keyword
|
inlineprotectedvirtual |
This will be called at the end of the parsing.
Reimplemented in stir::InterfilePDFSHeader, stir::InterfileImageHeader, stir::InterfileHeader, and stir::InterfilePDFSHeaderSPECT.
|
protectedvirtual |
convert 'rough' keyword into a standardised form
Calls standardise_interfile_keyword(). This follows Interfile 3.3 conventions:
space
, tab
, underscore
, !
are all treated as white space and ignored. Note: in this implementation 'ignoring' white space means 'trimming' at the start and end of the keyword, and replacing repeated white space with a single space.
References stir::standardise_interfile_keyword().
|
protectedvirtual |
gets a keyword from a string
Find :=
or [
. Note that the returned keyword is not standardised yet.
|
protected |
add a keyword to the list, together with its call_back function
This provides a more flexible way to add keys with specific call_backs. Can currently only be used by derived classes, as KeywordProcessor has to be a pointer to member function.
|
protected |
add a keyword to the list, together with its call_back function
This provides a more flexible way to add keys with specific call_backs. Can currently only be used by derived classes, as KeywordProcessor has to be a pointer to member function.
|
protected |
version that defaults 'function' to set_variable
|
protected |
version that defaults 'function' to set_variable
void stir::KeyParser::set_variable | ( | ) |
callback function that sets the variable to the value given as the value of the keyword
if the keyword had no value, set_variable will do nothing
References stir::error(), and stir::warning().
void stir::KeyParser::set_parsing_object | ( | ) |
callback function that sets the variable by calling the parser (as stored by add_parsing_key()), with argument the value of the keyword
if the keyword had no value, set_variable will do nothing
References stir::error().
void stir::KeyParser::set_shared_parsing_object | ( | ) |
callback function that sets the shared_ptr variable by calling the parser (as stored by add_parsing_key()), with argument the value of the keyword
if the keyword had no value, set_variable will do nothing
References stir::error().