Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ConfigFileTest.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2010 Larne Pekowsky
3* based on code (C) 2009 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#include <math.h>
22
23#include <lal/AVFactories.h>
24#include <lal/ConfigFile.h>
25#include <lal/StringVector.h>
26#include <lal/Date.h>
27
28/* Default parameters. */
29
30int main ( int argc, char *argv[])
31{
32 XLAL_CHECK ( argc == 1, XLAL_EINVAL, "Command-line arguments useless here \n");
33 XLAL_CHECK ( argv != NULL, XLAL_EINVAL );
34
35 LALParsedDataFile *cfgdata = NULL;
36
37 BOOLEAN testBool;
38 CHAR *string1 = NULL;
39 CHAR *string2 = NULL;
40 CHAR *string3 = NULL;
41 INT4 someint;
42 REAL8 float1, float2;
43 BOOLEAN wasRead = 0;
44
45 // ---------- TEST 1: make sure the XLAL methods can read config files without sections ----------
46 const char *cfgname = TEST_DATA_DIR "ConfigFileSample.cfg";
47
48 XLAL_CHECK ( XLALParseDataFile ( &cfgdata, cfgname ) == XLAL_SUCCESS, XLAL_EFUNC, "Failed to parse config-file '%s'", cfgname );
49
50 // check section-listing function
51 LALStringVector *sections;
52 XLAL_CHECK ( (sections = XLALListConfigFileSections ( cfgdata )) != NULL, XLAL_EFUNC );
53 XLAL_CHECK ( sections->length == 1, XLAL_EFAILED, "%s: Found %d sections instead of 1\n", cfgname, sections->length );
54 XLAL_CHECK ( strcmp ( sections->data[0], "default" ) == 0, XLAL_EFAILED, "%s: found section '%s' instead of 'default'\n", cfgname, sections->data[0] );
55 XLALDestroyStringVector ( sections );
56
57 // check config-variable reading
58 XLAL_CHECK ( XLALReadConfigREAL8Variable ( &float1, cfgdata, 0, "float1", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
59 XLAL_CHECK ( XLALReadConfigREAL8Variable ( &float2, cfgdata, 0, "float2", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
60 XLAL_CHECK ( XLALReadConfigSTRINGVariable ( &string1, cfgdata, 0, "string1", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
61 XLAL_CHECK ( XLALReadConfigINT4Variable ( &someint, cfgdata, 0, "int1", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
62
63 XLAL_CHECK ( XLALReadConfigSTRINGVariable ( &string2, cfgdata, 0, "string2", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
64 XLAL_CHECK ( XLALReadConfigSTRINGVariable ( &string3, cfgdata, 0, "string3", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
65
66 XLAL_CHECK ( XLALReadConfigBOOLEANVariable ( &testBool, cfgdata, 0, "testBool", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
67
68 LIGOTimeGPS epochGPS, epochMJDTT;
69 XLAL_CHECK ( XLALReadConfigEPOCHVariable ( &epochGPS, cfgdata, 0, "epochGPS", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
70 XLAL_CHECK ( XLALReadConfigEPOCHVariable ( &epochMJDTT, cfgdata, 0, "epochMJDTT", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
71
72 REAL8 longHMS, longRad;
73 XLAL_CHECK ( XLALReadConfigRAJVariable ( &longHMS, cfgdata, 0, "longHMS", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
74 XLAL_CHECK ( XLALReadConfigRAJVariable ( &longRad, cfgdata, 0, "longRad", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
75
76 REAL8 latDMS, latRad;
77 XLAL_CHECK ( XLALReadConfigDECJVariable ( &latDMS, cfgdata, 0, "latDMS", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
78 XLAL_CHECK ( XLALReadConfigDECJVariable ( &latRad, cfgdata, 0, "latRad", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
79
80 INT8 longInt;
81 XLAL_CHECK ( XLALReadConfigINT8Variable ( &longInt, cfgdata, 0, "longInt", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
82
83 UINT4Vector *unread = XLALConfigFileGetUnreadEntries ( cfgdata );
84 XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALConfigFileGetUnreadEntries() failed\n");
85 XLAL_CHECK ( unread == NULL, XLAL_EFAILED, "Some entries in config-file '%s' have not been parsed!\n", cfgname );
86
88 cfgdata = NULL;
89
90 // ----- now check the stuff got read-in correctly
91 XLAL_CHECK ( float1 == 1.0, XLAL_EFAILED, "%s: got float1 = %g, expected 1.0\n", cfgname, float1 );
92
93 XLAL_CHECK ( float2 == 2.0, XLAL_EFAILED, "%s: got float2 = %g, expected 2.0\n", cfgname, float2 );
94
95 XLAL_CHECK ( someint == 4, XLAL_EFAILED, "%s: someint = %d, expected 4\n", cfgname, someint );
96
97 const char *string1_ref = "some text. You can also use line-continuation";
98 XLAL_CHECK ( strcmp(string1, string1_ref) == 0, XLAL_EFAILED, "%s: got string1 = '%s', expected '%s'\n", cfgname, string1, string1_ref );
99
100 const char *string2_ref = "this is also possible, and # here does nothing; and neither does semi-colon ";
101 XLAL_CHECK ( strcmp ( string2, string2_ref ) == 0, XLAL_EFAILED, "%s: got string2 = '%s', expected '%s'\n", cfgname, string2, string2_ref );
102
103 const char *string3_ref = "how about #quotes AND line-continuation?";
104 XLAL_CHECK ( strcmp ( string3, string3_ref ) == 0, XLAL_EFAILED, "%s: got string3 = '%s', expected '%s'\n", cfgname, string3, string3_ref );
105
106 XLAL_CHECK ( testBool == 0, XLAL_EFAILED, "%s: got testBool = %d, expected 0\n", cfgname, testBool );
107
108 XLAL_CHECK ( XLALGPSCmp ( &epochGPS, &epochMJDTT ) == 0, XLAL_EFAILED, "GPS epoch {%d,%d} differs from MJD(TT) epoch {%d,%d}\n",
109 epochGPS.gpsSeconds, epochGPS.gpsNanoSeconds, epochMJDTT.gpsSeconds, epochMJDTT.gpsNanoSeconds );
110
111 REAL8 diff, tol = 3e-15;
112 XLAL_CHECK ( (diff = fabs(longHMS - longRad)) < tol, XLAL_EFAILED, "longitude(HMS) = %.16g differs from longitude(rad) = %.16g by %g > %g\n", longHMS, longRad, diff, tol );
113 XLAL_CHECK ( (diff = fabs(latDMS - latRad)) < tol, XLAL_EFAILED, "latitude(HMS) = %.16g differs from latitude(rad) = %.16g by %g > %g\n", latDMS, latRad, diff, tol );
114
115 XLAL_CHECK ( longInt == 4294967294, XLAL_EFAILED, "Failed to read an INT8: longInt = %" LAL_INT8_FORMAT " != 4294967294", longInt );
116
117 XLALFree (string1);
118 XLALFree (string2);
119 XLALFree (string3);
120 string1 = string2 = string3 = NULL;
121
122 // ---------- TEST 2: read some values from different sections ----------
123
124 cfgname = TEST_DATA_DIR "ConfigFileSample2.cfg";
125 XLAL_CHECK ( XLALParseDataFile ( &cfgdata, cfgname ) == XLAL_SUCCESS, XLAL_EFUNC, "Failed to parse config-file '%s'", cfgname );
126
127 // check section-listing function
128 XLAL_CHECK ( (sections = XLALListConfigFileSections ( cfgdata )) != NULL, XLAL_EFUNC );
129 XLAL_CHECK ( sections->length == 3, XLAL_EFAILED, "%s: found %d sections instead of 3\n", cfgname, sections->length );
130 XLAL_CHECK ( strcmp ( sections->data[0], "section1" ) == 0, XLAL_EFAILED, "%s: 1st section found is '%s' instead of 'section1'\n", cfgname, sections->data[0] );
131 XLAL_CHECK ( strcmp ( sections->data[1], "section2" ) == 0, XLAL_EFAILED, "%s: 2nd section found is '%s' instead of 'section2'\n", cfgname, sections->data[1] );
132 XLAL_CHECK ( strcmp ( sections->data[2], "section3" ) == 0, XLAL_EFAILED, "%s: 3rd section found is '%s' instead of 'section3'\n", cfgname, sections->data[2] );
133 XLALDestroyStringVector ( sections );
134
135 // Check for a section we have and one we don't
136 XLAL_CHECK ( XLALConfigSectionExists ( cfgdata, "section1" ), XLAL_EFAILED );
137 XLAL_CHECK ( !XLALConfigSectionExists ( cfgdata, "section5" ), XLAL_EFAILED );
138
139 // check config-variable reading
140 XLAL_CHECK ( XLALReadConfigREAL8Variable (&float1, cfgdata, "section1", "float1", &wasRead) == XLAL_SUCCESS, XLAL_EFUNC );
141 XLAL_CHECK ( XLALReadConfigSTRINGVariable (&string1, cfgdata, "section1", "string1", &wasRead) == XLAL_SUCCESS, XLAL_EFUNC );
142
143 XLAL_CHECK ( XLALReadConfigINT4Variable (&someint, cfgdata, "section1", "int1", &wasRead) == XLAL_SUCCESS, XLAL_EFUNC );
144
145 XLAL_CHECK ( XLALReadConfigSTRINGVariable(&string2, cfgdata, "section2", "string2", &wasRead) == XLAL_SUCCESS, XLAL_EFUNC );
146 XLAL_CHECK ( XLALReadConfigSTRINGVariable(&string3, cfgdata, "section3", "string3", &wasRead) == XLAL_SUCCESS, XLAL_EFUNC );
147
148 XLAL_CHECK ( XLALReadConfigBOOLEANVariable (&testBool, cfgdata, "section3", "testBool", &wasRead) == XLAL_SUCCESS, XLAL_EFUNC );
149
150
151 XLAL_CHECK ( XLALReadConfigEPOCHVariable ( &epochGPS, cfgdata, "section2", "epochGPS", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
152 XLAL_CHECK ( XLALReadConfigEPOCHVariable ( &epochMJDTT, cfgdata, "section3", "epochMJDTT", &wasRead ) == XLAL_SUCCESS, XLAL_EFUNC );
153
154 unread = XLALConfigFileGetUnreadEntries ( cfgdata );
155 XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALConfigFileGetUnreadEntries() failed\n");
156 XLAL_CHECK ( unread == NULL, XLAL_EFAILED, "Some entries in config-file '%s' have not been parsed!\n", cfgname );
157
159 cfgdata = NULL;
160
161 // ----- now check the stuff got read-in correctly
162 XLAL_CHECK ( float1 == 1.0, XLAL_EFAILED, "%s: got float1 = %g, expected 1.0\n", cfgname, float1 );
163
164 XLAL_CHECK ( float2 == 2.0, XLAL_EFAILED, "%s: got float2 = %g, expected 2.0\n", cfgname, float2 );
165
166 XLAL_CHECK ( someint == 4, XLAL_EFAILED, "%s: someint = %d, expected 4\n", cfgname, someint );
167
168 XLAL_CHECK ( strcmp(string1, string1_ref) == 0, XLAL_EFAILED, "%s: got string1 = '%s', expected '%s'\n", cfgname, string1, string1_ref );
169
170 XLAL_CHECK ( strcmp ( string2, string2_ref ) == 0, XLAL_EFAILED, "%s: got string2 = '%s', expected '%s'\n", cfgname, string2, string2_ref );
171
172 XLAL_CHECK ( strcmp ( string3, string3_ref ) == 0, XLAL_EFAILED, "%s: got string3 = '%s', expected '%s'\n", cfgname, string3, string3_ref );
173
174 XLAL_CHECK ( testBool == 0, XLAL_EFAILED, "%s: got testBool = %d, expected 0\n", cfgname, testBool );
175
176 XLAL_CHECK ( XLALGPSCmp ( &epochGPS, &epochMJDTT ) == 0, XLAL_EFAILED, "GPS epoch {%d,%d} differs from MJD(TT) epoch {%d,%d}\n",
177 epochGPS.gpsSeconds, epochGPS.gpsNanoSeconds, epochMJDTT.gpsSeconds, epochMJDTT.gpsNanoSeconds );
178
179 // ---------- TEST 3: check reading of compressed files ----------
180 cfgname = TEST_DATA_DIR "ConfigFileSample3.cfg.gz";
181 XLAL_CHECK ( XLALParseDataFile ( &cfgdata, cfgname ) == XLAL_SUCCESS, XLAL_EFUNC );
182
183 XLAL_CHECK ( cfgdata->lines->nTokens == 4, XLAL_EFAILED );
184 XLAL_CHECK ( strcmp(cfgdata->lines->tokens[0], "a") == 0, XLAL_EFAILED );
185 XLAL_CHECK ( strcmp(cfgdata->lines->tokens[1], "b") == 0, XLAL_EFAILED );
186 XLAL_CHECK ( strcmp(cfgdata->lines->tokens[2], "c") == 0, XLAL_EFAILED );
187 XLAL_CHECK ( strcmp(cfgdata->lines->tokens[3], "d") == 0, XLAL_EFAILED );
189 cfgdata = NULL;
190
191 XLALFree (string1);
192 XLALFree (string2);
193 XLALFree (string3);
194
195 // -----
197
198 return XLAL_SUCCESS;
199
200} // main()
int XLALReadConfigSTRINGVariable(CHAR **varp, LALParsedDataFile *cfgdata, const CHAR *secName, const CHAR *varName, BOOLEAN *wasRead)
String parser for config-file: can read config-variables of the form VARIABLE [=:] VALUE.
Definition: ConfigFile.c:277
int main(int argc, char *argv[])
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
int XLALParseDataFile(LALParsedDataFile **cfgdata, const CHAR *path)
Parse an ASCII data-file into a pre-cleaned array of lines.
Definition: ConfigFile.c:65
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 *cfgdata, const CHAR *secName)
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.
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.
int32_t INT4
Four-byte signed integer.
#define XLALFree(p)
Definition: LALMalloc.h:47
#define LAL_INT8_FORMAT
Definition: LALStdio.h:128
void XLALDestroyStringVector(LALStringVector *vect)
XLAL-interface: Free a string-vector ;)
Definition: StringVector.c:204
#define xlalErrno
Modifiable lvalue containing the XLAL error number.
Definition: XLALError.h:571
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EINVAL
Invalid argument.
Definition: XLALError.h:409
@ XLAL_EFAILED
Generic failure.
Definition: XLALError.h:418
int XLALGPSCmp(const LIGOTimeGPS *t0, const LIGOTimeGPS *t1)
Compares two GPS times.
Definition: XLALTime.c:174
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
Vector of type CHAR*, ie 'strings', see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:82
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:86
CHAR ** data
Pointer to the data array.
Definition: LALDatatypes.h:87
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
INT4 gpsSeconds
Seconds since 0h UTC 6 Jan 1980.
Definition: LALDatatypes.h:459
INT4 gpsNanoSeconds
Residual nanoseconds.
Definition: LALDatatypes.h:460
CHAR ** tokens
A list of pointers to the individual tokens; the elements tokens[0..nTokens-1] point to tokens,...
Definition: StringInput.h:137
UINT4 nTokens
The number of tokens in the list.
Definition: StringInput.h:136
Vector of type UINT4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:118