LAL 7.7.0.1-678514e
ConfigFile.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Reinhard Prix
3 * Copyright (C) 2004 Reinhard Prix
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with with program; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 */
20
21#ifndef _CONFIGFILE_H /* Double-include protection. */
22#define _CONFIGFILE_H
23
24#include <lal/LALDatatypes.h>
25#include <lal/StringInput.h>
26
27/* C++ protection. */
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * \defgroup ConfigFile_h Header ConfigFile.h
34 * \ingroup lal_support
35 * \author Reinhard Prix
36 * \brief Module for general parsing of simple ASCII-based config-files.
37 *
38 * ### Description ###
39 *
40 * This module provides routines for reading formatted
41 * config-files containing definitions of the form <tt>variable = value</tt>.
42 * The general syntax is somewhat similar to the one provided by the
43 * perl-module <tt>ConfigParser</tt> (cf.
44 * http://www.python.org/doc/current/lib/module-ConfigParser.html)
45 *
46 * Comments are allowed using either '<tt>\#</tt>' or <tt>\%</tt>.
47 * You can also use line-continuation using a '<tt>\</tt>' at the end of the line.
48 * Also note that comment-signs '<tt>\#\%</tt>' within double-quotes &quot;...&quot;
49 * are <em>not</em> treated as comment-characters.
50 * Semi-colons <tt>;</tt> are ignored, but can be used to separate several assignments on the same line.
51 * The general syntax is best illustrated
52 * using a simple example:
53 * \code
54 * # comment line
55 * var1 = 1.0; var2 = 3.1; ## several assignments on a line, separated by ';'
56 * somevar = some text.\
57 * You can also use\
58 * line-continuation
59 * var3 = 4 # whatever that means
60 * note = "this is also possible, and # here does nothing"
61 * a_switch = true #possible values: 0,1,true,false,yes,no, case insensitive
62 * ...
63 * \endcode
64 *
65 * Note that TABS generally get replaced by a single space, which can be
66 * useful in the case of line-continuation (see example). All leading and
67 * trailing spaces in are ignore (except within double-quotes).
68 *
69 * The general approach of reading from such a config-file, is to first
70 * call XLALParseDataFile() which loads and pre-parses the contents of the
71 * config-file into the structure LALParsedDataFile. Then one can read in
72 * config-variables either using one of the type-strict custom-wrappers
73 * <tt>XLALReadConfig<TYPE>Variable()</tt> or the general-purpose reading function
74 * XLALReadConfigVariable().
75 *
76 * A boolean variable read by XLALReadConfigBOOLEANVariable() can have any of the values
77 * <tt>{1, 0, yes, no, true, false}</tt>, where the comparison is done
78 * <em>case-insensitively</em>, i.e. you can also use 'True' or 'FALSE'....
79 *
80 * If one wishes a tight sytnax for the config-file, one can check
81 * that there are no illegal entries in the config-file. This is done
82 * by checking at the end that all config-file entries have been
83 * successfully parsed, using: XLALConfigFileGetUnreadEntries()
84 *
85 * The configfile-data should be freed at the end using
86 * XLALDestroyParsedDataFile().
87 *
88 * \par Notes
89 *
90 * XLALReadConfigSTRINGVariable() read the <em>rest</em> of the logical line (excluding comments) as a string,
91 * and removes any surrounding quotes \' or \".
92 *
93 *
94 * \note instead of using these functions directly, it might be
95 * more convenient to use the \ref UserInput_h.
96 *
97 */
98/** @{ */
99
100/**
101 * This structure is returned by XLALParseDataFile() and holds the contents of an
102 * ASCII data-file in a pre-parsed form, namely stripped from all comments ('\#', '\%'),
103 * spurious whitespaces, and separated into lines (taking into account line-continuation
104 * by '\\' at the end of lines).
105 * This is used as the input structure in the config-variable reading routines.
106 */
107typedef struct tagLALParsedDataFile {
108 TokenList *lines; /**< list of pre-parsed data-file lines */
109 BOOLEAN *wasRead; /**< keep track of successfully read lines */
111
112
113/* Function prototypes */
114int XLALParseDataFile (LALParsedDataFile **cfgdata, const CHAR *fname);
115int XLALParseDataFileContent (LALParsedDataFile **cfgdata, const CHAR *string );
116
118
122
123// ---------- type-specific parser prototypes generated via template
124#define DECLARE_XLALREADCONFIGVARIABLE(TYPE,CTYPE) \
125 int XLALReadConfig ##TYPE## Variable ( CTYPE *varp, LALParsedDataFile *cfgdata, const CHAR *secName, const CHAR *varName, BOOLEAN *wasRead )
126
138// ------------------------------------------------------------
139
140
141
142/** @} */
143
144
145/* C++ protection. */
146#ifdef __cplusplus
147}
148#endif
149
150#endif /* Double-include protection. */
#define STRING(a)
Definition: PrintVector.c:12
int XLALParseDataFileContent(LALParsedDataFile **cfgdata, const CHAR *string)
Definition: ConfigFile.c:95
int XLALParseDataFile(LALParsedDataFile **cfgdata, const CHAR *fname)
Parse an ASCII data-file into a pre-cleaned array of lines.
Definition: ConfigFile.c:65
#define DECLARE_XLALREADCONFIGVARIABLE(TYPE, CTYPE)
Definition: ConfigFile.h:124
UINT4Vector * XLALConfigFileGetUnreadEntries(const LALParsedDataFile *cfgdata)
Return a list of unread config-file entries, NULL if none found (without error).
Definition: ConfigFile.c:398
LALStringVector * XLALListConfigFileSections(const LALParsedDataFile *cfgdata)
Function to find all sections in given config-file contents cfgdata.
Definition: ConfigFile.c:214
int XLALConfigSectionExists(const LALParsedDataFile *, const CHAR *)
Function to determine whether a given section secName exists in the parsed config-file contents cfgda...
Definition: ConfigFile.c:172
void XLALDestroyParsedDataFile(LALParsedDataFile *cfgdata)
Free memory associated with a LALParsedDataFile structure.
Definition: ConfigFile.c:147
unsigned char BOOLEAN
Boolean logical type, see Headers LAL(Atomic)Datatypes.h for more details.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
double REAL8
Double precision real floating-point number (8 bytes).
int64_t INT8
Eight-byte signed integer; on some platforms this is equivalent to long int instead.
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
int32_t INT4
Four-byte signed integer.
This structure is returned by XLALParseDataFile() and holds the contents of an ASCII data-file in a p...
Definition: ConfigFile.h:107
TokenList * lines
list of pre-parsed data-file lines
Definition: ConfigFile.h:108
BOOLEAN * wasRead
keep track of successfully read lines
Definition: ConfigFile.h:109
Vector of type CHAR*, ie 'strings', see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:82
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
This structure stores a number of null-terminated strings of arbitrary length.
Definition: StringInput.h:135
Vector of type UINT4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:118