Module for general parsing of simple ASCII-based config-files.
This module provides routines for reading formatted config-files containing definitions of the form variable = value
. The general syntax is somewhat similar to the one provided by the perl-module ConfigParser
(cf. http://www.python.org/doc/current/lib/module-ConfigParser.html)
Comments are allowed using either '#
' or %
. You can also use line-continuation using a '\
' at the end of the line. Also note that comment-signs '#%
' within double-quotes "..." are not treated as comment-characters. Semi-colons ;
are ignored, but can be used to separate several assignments on the same line. The general syntax is best illustrated using a simple example:
Note that TABS generally get replaced by a single space, which can be useful in the case of line-continuation (see example). All leading and trailing spaces in are ignore (except within double-quotes).
The general approach of reading from such a config-file, is to first call XLALParseDataFile() which loads and pre-parses the contents of the config-file into the structure LALParsedDataFile. Then one can read in config-variables either using one of the type-strict custom-wrappers XLALReadConfig<TYPE>Variable()
or the general-purpose reading function XLALReadConfigVariable().
A boolean variable read by XLALReadConfigBOOLEANVariable() can have any of the values {1, 0, yes, no, true, false}
, where the comparison is done case-insensitively, i.e. you can also use 'True' or 'FALSE'....
If one wishes a tight sytnax for the config-file, one can check that there are no illegal entries in the config-file. This is done by checking at the end that all config-file entries have been successfully parsed, using: XLALConfigFileGetUnreadEntries()
The configfile-data should be freed at the end using XLALDestroyParsedDataFile().
XLALReadConfigSTRINGVariable() read the rest of the logical line (excluding comments) as a string, and removes any surrounding quotes \' or ".
Data Structures | |
struct | LALParsedDataFile |
This structure is returned by XLALParseDataFile() and holds the contents of an ASCII data-file in a pre-parsed form, namely stripped from all comments ('#', '%'), spurious whitespaces, and separated into lines (taking into account line-continuation by '\' at the end of lines). More... | |
Macros | |
#define | DECLARE_XLALREADCONFIGVARIABLE(TYPE, CTYPE) int XLALReadConfig ##TYPE## Variable ( CTYPE *varp, LALParsedDataFile *cfgdata, const CHAR *secName, const CHAR *varName, BOOLEAN *wasRead ) |
int XLALParseDataFile | ( | LALParsedDataFile ** | cfgdata, |
const CHAR * | path | ||
) |
Parse an ASCII data-file into a pre-cleaned array of lines.
The cleaning gets rid of comments ('#', '%'), empty lines, and performs line-continuation if '\' is found at EOL
NOTE: This function can transparently detect and read gzip-compressed data-files, independently of filename-extension
NOTE2: allows passing of file-contents directly instead of filename to read by passing a string like "{ file-contents }" instead of a file-path, ie if first character == '{' and last character == '}' This is useful to allow ascii-file user inputs to be transparently passed as filenames or direct contents
[out] | cfgdata | pre-parsed data-file lines |
[in] | path | file-path of config-file to be read |
Definition at line 65 of file ConfigFile.c.
int XLALParseDataFileContent | ( | LALParsedDataFile ** | cfgdata, |
const CHAR * | string | ||
) |
[out] | cfgdata | pre-parsed data-file lines |
[in] | string | string-contents of config-file: can get modified! |
Definition at line 95 of file ConfigFile.c.
void XLALDestroyParsedDataFile | ( | LALParsedDataFile * | cfgdata | ) |
Free memory associated with a LALParsedDataFile structure.
[in] | cfgdata | config-file data |
Definition at line 147 of file ConfigFile.c.
int XLALConfigSectionExists | ( | const LALParsedDataFile * | cfgdata, |
const CHAR * | secName | ||
) |
Function to determine whether a given section secName exists in the parsed config-file contents cfgdata.
NOTE2: quite inefficient implementation, re-creates table of content each time it's called however this function also doesn't seem to be used right now and should probably be removed?
[in] | cfgdata | pre-parsed config-data |
[in] | secName | section-name to read |
Definition at line 172 of file ConfigFile.c.
LALStringVector* XLALListConfigFileSections | ( | const LALParsedDataFile * | cfgdata | ) |
Function to find all sections in given config-file contents cfgdata.
A section start is defined by a string "[ section-name ]" found at the beginning of a line The first non-section part of a config-file is referred to as the "default" section, which is included in the returned list of section-names provided it is not empty.
[in] | cfgdata | pre-parsed config-data |
Definition at line 214 of file ConfigFile.c.
UINT4Vector* XLALConfigFileGetUnreadEntries | ( | const LALParsedDataFile * | cfgdata | ) |
Return a list of unread config-file entries, NULL if none found (without error).
[in] | cfgdata | config-file data |
Definition at line 398 of file ConfigFile.c.
DECLARE_XLALREADCONFIGVARIABLE | ( | STRINGVector | , |
LALStringVector * | |||
) |
DECLARE_XLALREADCONFIGVARIABLE | ( | EPOCH | , |
LIGOTimeGPS | |||
) |
DECLARE_XLALREADCONFIGVARIABLE | ( | RAJ | , |
REAL8 | |||
) |
DECLARE_XLALREADCONFIGVARIABLE | ( | DECJ | , |
REAL8 | |||
) |
#define DECLARE_XLALREADCONFIGVARIABLE | ( | TYPE, | |
CTYPE | |||
) | int XLALReadConfig ##TYPE## Variable ( CTYPE *varp, LALParsedDataFile *cfgdata, const CHAR *secName, const CHAR *varName, BOOLEAN *wasRead ) |
Definition at line 124 of file ConfigFile.h.