Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALMetaIO 4.0.6.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
process_params.c
Go to the documentation of this file.
1/*
2 * process_params.c
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with with program; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23
24
25#include <metaio.h>
26
27
28#include <lal/Date.h>
29#include <lal/LALMalloc.h>
30#include <lal/LIGOLwXML.h>
31#include <lal/LIGOLwXMLRead.h>
32#include <lal/LIGOMetadataTables.h>
33#include <lal/LIGOMetadataUtils.h>
34#include <lal/XLALError.h>
35
36
37/**
38 * Create a ProcessParamsTable structure.
39 */
41 const ProcessTable *process
42)
43{
44 ProcessParamsTable *new = XLALMalloc(sizeof(*new));
45
46 if(!new)
48
49 new->next = NULL;
50 memset(new->program, 0, sizeof(new->program));
51 if(process)
52 new->process_id = process->process_id;
53 else
54 new->process_id = -1;
55 memset(new->param, 0, sizeof(new->param));
56 memset(new->type, 0, sizeof(new->type));
57 memset(new->value, 0, sizeof(new->value));
58
59 return new;
60}
61
62
63/**
64 * Destroy a ProcessParamsTable structure.
65 */
68)
69{
70 ProcessParamsTable *next = row ? row->next : NULL;
71 XLALFree(row);
72 return next;
73}
74
75
76/**
77 * Destroy a ProcessParamsTable linked list.
78 */
81)
82{
83 while(head)
85}
86
87
88/**
89 * Count the number of rows in a ProcessParamsTable linked the list.
90 */
93)
94{
95 int length;
96
97 for(length = 0; head; head = head->next)
98 length++;
99
100 return (length);
101}
102
103
104/**
105 * Read the process_params table from a LIGO Light Weight XML file into a
106 * linked list of ProcessParamsTable structures.
107 */
109 const char *filename
110)
111{
112 static const char table_name[] = "process_params";
113 int miostatus;
114 ProcessParamsTable *head = NULL;
115 ProcessParamsTable **next = &head;
116 struct MetaioParseEnvironment env;
117 struct {
118 int program;
119 int process_id;
120 int param;
121 int type;
122 int value;
123 } column_pos;
124
125 /* open the file and find table */
126
127 if(MetaioOpenFile(&env, filename)) {
128 XLALPrintError("%s(): error opening \"%s\": %s\n", __func__, filename, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
130 }
131 if(MetaioOpenTableOnly(&env, table_name)) {
132 MetaioAbort(&env);
133 XLALPrintError("%s(): cannot find %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
135 }
136
137 /* find columns */
138
140 column_pos.program = XLALLIGOLwFindColumn(&env, "program", METAIO_TYPE_LSTRING, 1);
141 column_pos.process_id = XLALLIGOLwFindColumn(&env, "process_id", METAIO_TYPE_INT_8S, 1);
142 column_pos.param = XLALLIGOLwFindColumn(&env, "param", METAIO_TYPE_LSTRING, 1);
143 column_pos.type = XLALLIGOLwFindColumn(&env, "type", METAIO_TYPE_LSTRING, 1);
144 column_pos.value = XLALLIGOLwFindColumn(&env, "value", METAIO_TYPE_LSTRING, 1);
145
146 /* check for failure (== a required column is missing) */
147
148 if(XLALGetBaseErrno()) {
149 MetaioAbort(&env);
150 XLALPrintError("%s(): failure reading %s table\n", __func__, table_name);
152 }
153
154 /* loop over the rows in the file */
155
156 while((miostatus = MetaioGetRow(&env)) > 0) {
157 /* create a new row */
158
160
161 if(!row) {
163 MetaioAbort(&env);
165 }
166
167 /* append to linked list */
168
169 *next = row;
170 next = &(*next)->next;
171
172 /* populate the columns */
173
174 strncpy(row->program, env.ligo_lw.table.elt[column_pos.program].data.lstring.data, sizeof(row->program) - 1);
175 row->process_id = env.ligo_lw.table.elt[column_pos.process_id].data.int_8s;
176 strncpy(row->param, env.ligo_lw.table.elt[column_pos.param].data.lstring.data, sizeof(row->param) - 1);
177 strncpy(row->type, env.ligo_lw.table.elt[column_pos.type].data.lstring.data, sizeof(row->type) - 1);
178 strncpy(row->value, env.ligo_lw.table.elt[column_pos.value].data.lstring.data, sizeof(row->value) - 1);
179 }
180 if(miostatus < 0) {
182 MetaioAbort(&env);
183 XLALPrintError("%s(): I/O error parsing %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
185 }
186
187 /* close file */
188
189 if(MetaioClose(&env)) {
191 XLALPrintError("%s(): error parsing document after %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
193 }
194
195 /* done */
196
197 return head;
198}
199
200
201/**
202 * Write a process_params table to an XML file.
203 */
205 LIGOLwXMLStream *xml,
206 const ProcessParamsTable *process_params
207)
208{
209 const char *row_head = "\n\t\t\t";
210
211 /* table header */
212
214 XLALFilePuts("\t<Table Name=\"process_params:table\">\n", xml->fp);
215 XLALFilePuts("\t\t<Column Name=\"program\" Type=\"lstring\"/>\n", xml->fp);
216 XLALFilePuts("\t\t<Column Name=\"process:process_id\" Type=\"int_8s\"/>\n", xml->fp);
217 XLALFilePuts("\t\t<Column Name=\"param\" Type=\"lstring\"/>\n", xml->fp);
218 XLALFilePuts("\t\t<Column Name=\"type\" Type=\"lstring\"/>\n", xml->fp);
219 XLALFilePuts("\t\t<Column Name=\"value\" Type=\"lstring\"/>\n", xml->fp);
220 XLALFilePuts("\t\t<Stream Name=\"process_params:table\" Type=\"Local\" Delimiter=\",\">", xml->fp);
221 if(XLALGetBaseErrno())
223
224 /* rows */
225
226 for(; process_params; process_params = process_params->next) {
227 if(XLALFilePrintf(xml->fp, "%s\"%s\",%ld,\"%s\",\"%s\",\"%s\"", row_head, process_params->program, process_params->process_id, process_params->param, process_params->type, process_params->value) < 0)
229 row_head = ",\n\t\t\t";
230 }
231
232 /* table footer */
233
234 if(XLALFilePuts("\n\t\t</Stream>\n\t</Table>\n", xml->fp) < 0)
236
237 /* done */
238
239 return 0;
240}
const char * program
int XLALLIGOLwFindColumn(struct MetaioParseEnvironment *env, const char *name, unsigned int type, int required)
Convenience wrapper for MetaioFindColumn(), translating to XLAL-style error reporting and printing us...
int XLALFilePuts(const char *s, LALFILE *file)
int XLALFilePrintf(LALFILE *file, const char *fmt,...)
void * XLALMalloc(size_t n)
void XLALFree(void *p)
#define XLAL_ERROR_NULL(...)
int XLALGetBaseErrno(void)
#define XLAL_ERROR(...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
int XLALClearErrno(void)
XLAL_EFUNC
XLAL_EIO
filename
ProcessParamsTable * XLALProcessParamsTableFromLIGOLw(const char *filename)
Read the process_params table from a LIGO Light Weight XML file into a linked list of ProcessParamsTa...
ProcessParamsTable * XLALCreateProcessParamsTableRow(const ProcessTable *process)
Create a ProcessParamsTable structure.
int XLALWriteLIGOLwXMLProcessParamsTable(LIGOLwXMLStream *xml, const ProcessParamsTable *process_params)
Write a process_params table to an XML file.
void XLALDestroyProcessParamsTable(ProcessParamsTable *head)
Destroy a ProcessParamsTable linked list.
int XLALCountProcessParamsTable(ProcessParamsTable *head)
Count the number of rows in a ProcessParamsTable linked the list.
ProcessParamsTable * XLALDestroyProcessParamsTableRow(ProcessParamsTable *row)
Destroy a ProcessParamsTable structure.
This structure contains the file stream and current table type for writing to LIGO lightweight XML fi...
Definition: LIGOLwXML.h:71
LALFILE * fp
Definition: LIGOLwXML.h:72
CHAR type[LIGOMETA_TYPE_MAX]
CHAR param[LIGOMETA_PARAM_MAX]
CHAR value[LIGOMETA_VALUE_MAX]
struct tagProcessParamsTable * next
CHAR program[LIGOMETA_PROGRAM_MAX]