SDDSlib
Loading...
Searching...
No Matches
SDDS_write.c
Go to the documentation of this file.
1/**
2 * @file SDDS_write.c
3 * @brief Provides functions for writing SDDS layout headers.
4 *
5 * This file contains functions that handle writing SDDS (Self Describing Data Sets)
6 * layout headers. These functions convert the internal layout structures into the
7 * namelist format used for input/output operations.
8 *
9 * @copyright
10 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
11 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
12 *
13 * @license
14 * This file is distributed under the terms of the Software License Agreement
15 * found in the file LICENSE included with this distribution.
16 *
17 * @authors
18 * M. Borland,
19 * C. Saunders,
20 * R. Soliday
21 */
22
23#include "mdb.h"
24#include "match_string.h"
25#include "SDDS.h"
26#include "SDDS_internal.h"
27
28/**
29 * @brief Converts blank strings to NULL.
30 *
31 * This utility function checks if a given string is NULL or consists solely of
32 * whitespace characters. If so, it returns NULL; otherwise, it returns the original string.
33 *
34 * @param string The input string to check.
35 * @return Returns NULL if the string is blank or NULL; otherwise, returns the original string.
36 */
37char *SDDS_BlankToNull(char *string) {
38 if (!string || SDDS_StringIsBlank(string))
39 return NULL;
40 return string;
41}
42
43/**************************************************************************/
44/* This routine writes the protocol version of the file, and should */
45/* never be changed! */
46/**************************************************************************/
47
48/**
49 * @brief Writes the SDDS protocol version to a standard file.
50 *
51 * This function outputs the SDDS protocol version string to the provided
52 * file pointer. It is crucial that the protocol version remains unchanged
53 * to ensure compatibility.
54 *
55 * @param version_number The SDDS protocol version number to write.
56 * @param fp The file pointer where the version string will be written.
57 * @return Returns 1 on successful write, 0 if the file pointer is NULL.
58 */
59int32_t SDDS_WriteVersion(int32_t version_number, FILE *fp) {
60 if (!fp)
61 return (0);
62 fprintf(fp, "SDDS%" PRId32 "\n", version_number);
63 return (1);
64}
65
66/**
67 * @brief Writes the SDDS protocol version to an LZMA-compressed file.
68 *
69 * This function outputs the SDDS protocol version string to the provided
70 * LZMA-compressed file pointer. Maintaining the protocol version is essential
71 * for ensuring the integrity of the SDDS file format.
72 *
73 * @param version_number The SDDS protocol version number to write.
74 * @param lzmafp The LZMA-compressed file pointer where the version string will be written.
75 * @return Returns 1 on successful write, 0 if the file pointer is NULL.
76 */
77int32_t SDDS_LZMAWriteVersion(int32_t version_number, struct lzmafile *lzmafp) {
78 if (!lzmafp)
79 return (0);
80 lzma_printf(lzmafp, "SDDS%" PRId32 "\n", version_number);
81 return (1);
82}
83
84#if defined(zLib)
85/**
86 * @brief Writes the SDDS protocol version to a GZip-compressed file.
87 *
88 * This function outputs the SDDS protocol version string to the provided
89 * GZip-compressed file pointer. The protocol version must remain consistent
90 * to maintain SDDS file compatibility.
91 *
92 * @param version_number The SDDS protocol version number to write.
93 * @param gzfp The GZip-compressed file pointer where the version string will be written.
94 * @return Returns 1 on successful write, 0 if the file pointer is NULL.
95 */
96int32_t SDDS_GZipWriteVersion(int32_t version_number, gzFile gzfp) {
97 if (!gzfp)
98 return (0);
99 gzprintf(gzfp, "SDDS%" PRId32 "\n", version_number);
100 return (1);
101}
102#endif
103
104/**************************************************************/
105/* SDDS protocol version 1 routines begin here. */
106/* There are no routers for output since only the most recent */
107/* protocol will ever be emitted. */
108/**************************************************************/
109
110/**
111 * @brief Writes a namelist field to a standard file.
112 *
113 * This function formats and writes a single namelist field to the specified
114 * file. It handles escaping of double quotes and determines whether to
115 * enclose the value in quotes based on its content.
116 *
117 * @param fp The file pointer where the namelist field will be written.
118 * @param name The name of the field.
119 * @param value The value of the field.
120 * @return Returns 1 on success, 0 if the name is NULL or memory allocation fails.
121 */
122int32_t SDDS_PrintNamelistField(FILE *fp, char *name, char *value) {
123 char *buffer = NULL, *bPtr, *vPtr;
124 if (!name)
125 return 0;
126 if (!value || !strlen(name))
127 return 1;
128 if (!strlen(value))
129 fprintf(fp, "%s=\"\", ", name);
130 else {
131 if (strchr(value, '"')) {
132 if (!(buffer = SDDS_Malloc(sizeof(*buffer) * 2 * strlen(value))))
133 return 0;
134 vPtr = value;
135 bPtr = buffer;
136 while (*vPtr) {
137 if (*vPtr == '"')
138 *bPtr++ = '\\';
139 *bPtr++ = *vPtr++;
140 }
141 *bPtr = 0;
142 value = buffer;
143 }
144 if (strpbrk(value, " ,*$\t\n\b"))
145 fprintf(fp, "%s=\"%s\", ", name, value);
146 else
147 fprintf(fp, "%s=%s, ", name, value);
148 if (buffer)
149 free(buffer);
150 }
151 return 1;
152}
153
154/**
155 * @brief Writes a namelist field to an LZMA-compressed file.
156 *
157 * This function formats and writes a single namelist field to the specified
158 * LZMA-compressed file. It handles escaping of double quotes and determines
159 * whether to enclose the value in quotes based on its content.
160 *
161 * @param lzmafp The LZMA-compressed file pointer where the namelist field will be written.
162 * @param name The name of the field.
163 * @param value The value of the field.
164 * @return Returns 1 on success, 0 if the name is NULL or memory allocation fails.
165 */
166int32_t SDDS_LZMAPrintNamelistField(struct lzmafile *lzmafp, char *name, char *value) {
167 char *buffer = NULL, *bPtr, *vPtr;
168 if (!name)
169 return 0;
170 if (!value || !strlen(name))
171 return 1;
172 if (!strlen(value))
173 lzma_printf(lzmafp, "%s=\"\", ", name);
174 else {
175 if (strchr(value, '"')) {
176 if (!(buffer = SDDS_Malloc(sizeof(*buffer) * 2 * strlen(value))))
177 return 0;
178 vPtr = value;
179 bPtr = buffer;
180 while (*vPtr) {
181 if (*vPtr == '"')
182 *bPtr++ = '\\';
183 *bPtr++ = *vPtr++;
184 }
185 *bPtr = 0;
186 value = buffer;
187 }
188 if (strpbrk(value, " ,*$\t\n\b"))
189 lzma_printf(lzmafp, "%s=\"%s\", ", name, value);
190 else
191 lzma_printf(lzmafp, "%s=%s, ", name, value);
192 if (buffer)
193 free(buffer);
194 }
195 return 1;
196}
197
198#if defined(zLib)
199/**
200 * @brief Writes a namelist field to a GZip-compressed file.
201 *
202 * This function formats and writes a single namelist field to the specified
203 * GZip-compressed file. It handles escaping of double quotes and determines
204 * whether to enclose the value in quotes based on its content.
205 *
206 * @param gzfp The GZip-compressed file pointer where the namelist field will be written.
207 * @param name The name of the field.
208 * @param value The value of the field.
209 * @return Returns 1 on success, 0 if the name is NULL or memory allocation fails.
210 */
211int32_t SDDS_GZipPrintNamelistField(gzFile gzfp, char *name, char *value) {
212 char *buffer = NULL, *bPtr, *vPtr;
213 if (!name)
214 return 0;
215 if (!value || !strlen(name))
216 return 1;
217 if (!strlen(value))
218 gzprintf(gzfp, "%s=\"\", ", name);
219 else {
220 if (strchr(value, '"')) {
221 if (!(buffer = SDDS_Malloc(sizeof(*buffer) * 2 * strlen(value))))
222 return 0;
223 vPtr = value;
224 bPtr = buffer;
225 while (*vPtr) {
226 if (*vPtr == '"')
227 *bPtr++ = '\\';
228 *bPtr++ = *vPtr++;
229 }
230 *bPtr = 0;
231 value = buffer;
232 }
233 if (strpbrk(value, " ,*$\t\n\b"))
234 gzprintf(gzfp, "%s=\"%s\", ", name, value);
235 else
236 gzprintf(gzfp, "%s=%s, ", name, value);
237 if (buffer)
238 free(buffer);
239 }
240 return 1;
241}
242#endif
243
244/**
245 * @brief Writes the SDDS description section to a standard file.
246 *
247 * This function writes the description section of the SDDS layout, including
248 * optional text and contents fields. It encapsulates the data within
249 * &description and &end tags.
250 *
251 * @param text The descriptive text for the SDDS layout.
252 * @param contents The contents description for the SDDS layout.
253 * @param fp The file pointer where the description will be written.
254 * @return Returns 1 on success, 0 if the file pointer is NULL.
255 */
256int32_t SDDS_WriteDescription(char *text, char *contents, FILE *fp) {
257 if (!fp)
258 return 0;
259 if (!text && !contents)
260 return 1;
261 fputs("&description ", fp);
262 SDDS_PrintNamelistField(fp, "text", text);
263 SDDS_PrintNamelistField(fp, "contents", contents);
264 fputs("&end\n", fp);
265 return 1;
266}
267
268/**
269 * @brief Writes the SDDS description section to an LZMA-compressed file.
270 *
271 * This function writes the description section of the SDDS layout, including
272 * optional text and contents fields. It encapsulates the data within
273 * &description and &end tags in an LZMA-compressed file.
274 *
275 * @param text The descriptive text for the SDDS layout.
276 * @param contents The contents description for the SDDS layout.
277 * @param lzmafp The LZMA-compressed file pointer where the description will be written.
278 * @return Returns 1 on success, 0 if the file pointer is NULL.
279 */
280int32_t SDDS_LZMAWriteDescription(char *text, char *contents, struct lzmafile *lzmafp) {
281 if (!lzmafp)
282 return 0;
283 if (!text && !contents)
284 return 1;
285 lzma_puts("&description ", lzmafp);
286 SDDS_LZMAPrintNamelistField(lzmafp, "text", text);
287 SDDS_LZMAPrintNamelistField(lzmafp, "contents", contents);
288 lzma_puts("&end\n", lzmafp);
289 return 1;
290}
291
292#if defined(zLib)
293/**
294 * @brief Writes the SDDS description section to a GZip-compressed file.
295 *
296 * This function writes the description section of the SDDS layout, including
297 * optional text and contents fields. It encapsulates the data within
298 * &description and &end tags in a GZip-compressed file.
299 *
300 * @param text The descriptive text for the SDDS layout.
301 * @param contents The contents description for the SDDS layout.
302 * @param gzfp The GZip-compressed file pointer where the description will be written.
303 * @return Returns 1 on success, 0 if the file pointer is NULL.
304 */
305int32_t SDDS_GZipWriteDescription(char *text, char *contents, gzFile gzfp) {
306 if (!gzfp)
307 return 0;
308 if (!text && !contents)
309 return 1;
310 gzputs(gzfp, "&description ");
311 SDDS_GZipPrintNamelistField(gzfp, "text", text);
312 SDDS_GZipPrintNamelistField(gzfp, "contents", contents);
313 gzputs(gzfp, "&end\n");
314 return 1;
315}
316#endif
317
318/**
319 * @brief Writes a column definition to a standard file.
320 *
321 * This function outputs the definition of a single column in the SDDS layout.
322 * It includes fields such as name, symbol, units, description, format string,
323 * and data type. The definition is enclosed within &column and &end tags.
324 *
325 * @param column_definition Pointer to the column definition structure.
326 * @param fp The file pointer where the column definition will be written.
327 * @return Returns 1 on success, 0 if the file pointer is NULL or the column type is invalid.
328 */
329int32_t SDDS_WriteColumnDefinition(COLUMN_DEFINITION *column_definition, FILE *fp) {
330 if (!fp || column_definition->type <= 0 || column_definition->type > SDDS_NUM_TYPES)
331 return (0);
332
333 fputs("&column ", fp);
334 SDDS_PrintNamelistField(fp, "name", column_definition->name);
335 SDDS_PrintNamelistField(fp, "symbol", SDDS_BlankToNull(column_definition->symbol));
336 SDDS_PrintNamelistField(fp, "units", SDDS_BlankToNull(column_definition->units));
337 SDDS_PrintNamelistField(fp, "description", SDDS_BlankToNull(column_definition->description));
338 SDDS_PrintNamelistField(fp, "format_string", SDDS_BlankToNull(column_definition->format_string));
339 SDDS_PrintNamelistField(fp, "type", SDDS_type_name[column_definition->type - 1]);
340 fputs(" &end\n", fp);
341 return (1);
342}
343
344/**
345 * @brief Writes a column definition to an LZMA-compressed file.
346 *
347 * This function outputs the definition of a single column in the SDDS layout
348 * to an LZMA-compressed file. It includes fields such as name, symbol, units,
349 * description, format string, and data type. The definition is enclosed within
350 * &column and &end tags.
351 *
352 * @param column_definition Pointer to the column definition structure.
353 * @param lzmafp The LZMA-compressed file pointer where the column definition will be written.
354 * @return Returns 1 on success, 0 if the file pointer is NULL or the column type is invalid.
355 */
356int32_t SDDS_LZMAWriteColumnDefinition(COLUMN_DEFINITION *column_definition, struct lzmafile *lzmafp) {
357 if (!lzmafp || column_definition->type <= 0 || column_definition->type > SDDS_NUM_TYPES)
358 return (0);
359
360 lzma_puts("&column ", lzmafp);
361 SDDS_LZMAPrintNamelistField(lzmafp, "name", column_definition->name);
362 SDDS_LZMAPrintNamelistField(lzmafp, "symbol", SDDS_BlankToNull(column_definition->symbol));
363 SDDS_LZMAPrintNamelistField(lzmafp, "units", SDDS_BlankToNull(column_definition->units));
364 SDDS_LZMAPrintNamelistField(lzmafp, "description", SDDS_BlankToNull(column_definition->description));
365 SDDS_LZMAPrintNamelistField(lzmafp, "format_string", SDDS_BlankToNull(column_definition->format_string));
366 SDDS_LZMAPrintNamelistField(lzmafp, "type", SDDS_type_name[column_definition->type - 1]);
367 lzma_puts(" &end\n", lzmafp);
368 return (1);
369}
370
371#if defined(zLib)
372/**
373 * @brief Writes a column definition to a GZip-compressed file.
374 *
375 * This function outputs the definition of a single column in the SDDS layout
376 * to a GZip-compressed file. It includes fields such as name, symbol, units,
377 * description, format string, and data type. The definition is enclosed within
378 * &column and &end tags.
379 *
380 * @param column_definition Pointer to the column definition structure.
381 * @param gzfp The GZip-compressed file pointer where the column definition will be written.
382 * @return Returns 1 on success, 0 if the file pointer is NULL or the column type is invalid.
383 */
384int32_t SDDS_GZipWriteColumnDefinition(COLUMN_DEFINITION *column_definition, gzFile gzfp) {
385 if (!gzfp || column_definition->type <= 0 || column_definition->type > SDDS_NUM_TYPES)
386 return (0);
387
388 gzputs(gzfp, "&column ");
389 SDDS_GZipPrintNamelistField(gzfp, "name", column_definition->name);
390 SDDS_GZipPrintNamelistField(gzfp, "symbol", SDDS_BlankToNull(column_definition->symbol));
391 SDDS_GZipPrintNamelistField(gzfp, "units", SDDS_BlankToNull(column_definition->units));
392 SDDS_GZipPrintNamelistField(gzfp, "description", SDDS_BlankToNull(column_definition->description));
393 SDDS_GZipPrintNamelistField(gzfp, "format_string", SDDS_BlankToNull(column_definition->format_string));
394 SDDS_GZipPrintNamelistField(gzfp, "type", SDDS_type_name[column_definition->type - 1]);
395 gzputs(gzfp, " &end\n");
396 return (1);
397}
398#endif
399
400/**
401 * @brief Writes a parameter definition to a standard file.
402 *
403 * This function outputs the definition of a single parameter in the SDDS layout.
404 * It includes fields such as name, symbol, units, description, format string,
405 * data type, and fixed value. The definition is enclosed within &parameter and &end tags.
406 *
407 * @param parameter_definition Pointer to the parameter definition structure.
408 * @param fp The file pointer where the parameter definition will be written.
409 * @return Returns 1 on success, 0 if the file pointer is NULL or the parameter type is invalid.
410 */
411int32_t SDDS_WriteParameterDefinition(PARAMETER_DEFINITION *parameter_definition, FILE *fp) {
412 if (!fp || parameter_definition->type <= 0 || parameter_definition->type > SDDS_NUM_TYPES)
413 return (0);
414 fputs("&parameter ", fp);
415 SDDS_PrintNamelistField(fp, "name", parameter_definition->name);
416 SDDS_PrintNamelistField(fp, "symbol", SDDS_BlankToNull(parameter_definition->symbol));
417 SDDS_PrintNamelistField(fp, "units", SDDS_BlankToNull(parameter_definition->units));
418 SDDS_PrintNamelistField(fp, "description", SDDS_BlankToNull(parameter_definition->description));
419 SDDS_PrintNamelistField(fp, "format_string", SDDS_BlankToNull(parameter_definition->format_string));
420 SDDS_PrintNamelistField(fp, "type", SDDS_type_name[parameter_definition->type - 1]);
421 SDDS_PrintNamelistField(fp, "fixed_value", parameter_definition->fixed_value);
422 fputs("&end\n", fp);
423 return (1);
424}
425
426/**
427 * @brief Writes a parameter definition to an LZMA-compressed file.
428 *
429 * This function outputs the definition of a single parameter in the SDDS layout
430 * to an LZMA-compressed file. It includes fields such as name, symbol, units,
431 * description, format string, data type, and fixed value. The definition is
432 * enclosed within &parameter and &end tags.
433 *
434 * @param parameter_definition Pointer to the parameter definition structure.
435 * @param lzmafp The LZMA-compressed file pointer where the parameter definition will be written.
436 * @return Returns 1 on success, 0 if the file pointer is NULL or the parameter type is invalid.
437 */
438int32_t SDDS_LZMAWriteParameterDefinition(PARAMETER_DEFINITION *parameter_definition, struct lzmafile *lzmafp) {
439 if (!lzmafp || parameter_definition->type <= 0 || parameter_definition->type > SDDS_NUM_TYPES)
440 return (0);
441 lzma_puts("&parameter ", lzmafp);
442 SDDS_LZMAPrintNamelistField(lzmafp, "name", parameter_definition->name);
443 SDDS_LZMAPrintNamelistField(lzmafp, "symbol", SDDS_BlankToNull(parameter_definition->symbol));
444 SDDS_LZMAPrintNamelistField(lzmafp, "units", SDDS_BlankToNull(parameter_definition->units));
445 SDDS_LZMAPrintNamelistField(lzmafp, "description", SDDS_BlankToNull(parameter_definition->description));
446 SDDS_LZMAPrintNamelistField(lzmafp, "format_string", SDDS_BlankToNull(parameter_definition->format_string));
447 SDDS_LZMAPrintNamelistField(lzmafp, "type", SDDS_type_name[parameter_definition->type - 1]);
448 SDDS_LZMAPrintNamelistField(lzmafp, "fixed_value", parameter_definition->fixed_value);
449 lzma_puts("&end\n", lzmafp);
450 return (1);
451}
452
453#if defined(zLib)
454/**
455 * @brief Writes a parameter definition to a GZip-compressed file.
456 *
457 * This function outputs the definition of a single parameter in the SDDS layout
458 * to a GZip-compressed file. It includes fields such as name, symbol, units,
459 * description, format string, data type, and fixed value. The definition is
460 * enclosed within &parameter and &end tags.
461 *
462 * @param parameter_definition Pointer to the parameter definition structure.
463 * @param gzfp The GZip-compressed file pointer where the parameter definition will be written.
464 * @return Returns 1 on success, 0 if the file pointer is NULL or the parameter type is invalid.
465 */
466int32_t SDDS_GZipWriteParameterDefinition(PARAMETER_DEFINITION *parameter_definition, gzFile gzfp) {
467 if (!gzfp || parameter_definition->type <= 0 || parameter_definition->type > SDDS_NUM_TYPES)
468 return (0);
469 gzputs(gzfp, "&parameter ");
470 SDDS_GZipPrintNamelistField(gzfp, "name", parameter_definition->name);
471 SDDS_GZipPrintNamelistField(gzfp, "symbol", SDDS_BlankToNull(parameter_definition->symbol));
472 SDDS_GZipPrintNamelistField(gzfp, "units", SDDS_BlankToNull(parameter_definition->units));
473 SDDS_GZipPrintNamelistField(gzfp, "description", SDDS_BlankToNull(parameter_definition->description));
474 SDDS_GZipPrintNamelistField(gzfp, "format_string", SDDS_BlankToNull(parameter_definition->format_string));
475 SDDS_GZipPrintNamelistField(gzfp, "type", SDDS_type_name[parameter_definition->type - 1]);
476 SDDS_GZipPrintNamelistField(gzfp, "fixed_value", parameter_definition->fixed_value);
477 gzputs(gzfp, "&end\n");
478 return (1);
479}
480#endif
481
482/**
483 * @brief Writes an associate definition to a standard file.
484 *
485 * This function outputs the definition of an associate in the SDDS layout.
486 * It includes fields such as name, filename, contents, path, description, and
487 * an SDDS flag. The definition is enclosed within &associate and &end tags.
488 *
489 * @param associate_definition Pointer to the associate definition structure.
490 * @param fp The file pointer where the associate definition will be written.
491 * @return Returns 1 on success, 0 if the file pointer is NULL.
492 */
493int32_t SDDS_WriteAssociateDefinition(ASSOCIATE_DEFINITION *associate_definition, FILE *fp) {
494 if (!fp)
495 return (0);
496
497 fputs("&associate ", fp);
498 SDDS_PrintNamelistField(fp, "name", associate_definition->name);
499 SDDS_PrintNamelistField(fp, "filename", SDDS_BlankToNull(associate_definition->filename));
500 SDDS_PrintNamelistField(fp, "contents", SDDS_BlankToNull(associate_definition->contents));
501 SDDS_PrintNamelistField(fp, "path", SDDS_BlankToNull(associate_definition->path));
502 SDDS_PrintNamelistField(fp, "description", SDDS_BlankToNull(associate_definition->description));
503 fprintf(fp, "sdds=%" PRId32, associate_definition->sdds);
504 fputs(" &end\n", fp);
505 return (1);
506}
507
508/**
509 * @brief Writes an associate definition to an LZMA-compressed file.
510 *
511 * This function outputs the definition of an associate in the SDDS layout
512 * to an LZMA-compressed file. It includes fields such as name, filename,
513 * contents, path, description, and an SDDS flag. The definition is enclosed
514 * within &associate and &end tags.
515 *
516 * @param associate_definition Pointer to the associate definition structure.
517 * @param lzmafp The LZMA-compressed file pointer where the associate definition will be written.
518 * @return Returns 1 on success, 0 if the file pointer is NULL.
519 */
520int32_t SDDS_LZMAWriteAssociateDefinition(ASSOCIATE_DEFINITION *associate_definition, struct lzmafile *lzmafp) {
521 if (!lzmafp)
522 return (0);
523
524 lzma_puts("&associate ", lzmafp);
525 SDDS_LZMAPrintNamelistField(lzmafp, "name", associate_definition->name);
526 SDDS_LZMAPrintNamelistField(lzmafp, "filename", SDDS_BlankToNull(associate_definition->filename));
527 SDDS_LZMAPrintNamelistField(lzmafp, "contents", SDDS_BlankToNull(associate_definition->contents));
528 SDDS_LZMAPrintNamelistField(lzmafp, "path", SDDS_BlankToNull(associate_definition->path));
529 SDDS_LZMAPrintNamelistField(lzmafp, "description", SDDS_BlankToNull(associate_definition->description));
530 lzma_printf(lzmafp, "sdds=%" PRId32, associate_definition->sdds);
531 lzma_puts(" &end\n", lzmafp);
532 return (1);
533}
534
535#if defined(zLib)
536/**
537 * @brief Writes an associate definition to a GZip-compressed file.
538 *
539 * This function outputs the definition of an associate in the SDDS layout
540 * to a GZip-compressed file. It includes fields such as name, filename,
541 * contents, path, description, and an SDDS flag. The definition is enclosed
542 * within &associate and &end tags.
543 *
544 * @param associate_definition Pointer to the associate definition structure.
545 * @param gzfp The GZip-compressed file pointer where the associate definition will be written.
546 * @return Returns 1 on success, 0 if the file pointer is NULL.
547 */
548int32_t SDDS_GZipWriteAssociateDefinition(ASSOCIATE_DEFINITION *associate_definition, gzFile gzfp) {
549 if (!gzfp)
550 return (0);
551
552 gzputs(gzfp, "&associate ");
553 SDDS_GZipPrintNamelistField(gzfp, "name", associate_definition->name);
554 SDDS_GZipPrintNamelistField(gzfp, "filename", SDDS_BlankToNull(associate_definition->filename));
555 SDDS_GZipPrintNamelistField(gzfp, "contents", SDDS_BlankToNull(associate_definition->contents));
556 SDDS_GZipPrintNamelistField(gzfp, "path", SDDS_BlankToNull(associate_definition->path));
557 SDDS_GZipPrintNamelistField(gzfp, "description", SDDS_BlankToNull(associate_definition->description));
558 gzprintf(gzfp, "sdds=%" PRId32, associate_definition->sdds);
559 gzputs(gzfp, " &end\n");
560 return (1);
561}
562#endif
563
564/**
565 * @brief Writes the data mode section to a standard file.
566 *
567 * This function outputs the data mode settings of the SDDS layout to the
568 * specified file pointer. It includes settings such as mode, lines per row,
569 * row counts, endianess, column-major order, and fixed row counts.
570 * The section is enclosed within &data and &end tags.
571 *
572 * @param layout Pointer to the SDDS layout structure containing data mode settings.
573 * @param fp The file pointer where the data mode section will be written.
574 * @return Returns 1 on success, 0 if the file pointer is NULL or the data mode is invalid.
575 */
576int32_t SDDS_WriteDataMode(SDDS_LAYOUT *layout, FILE *fp) {
577 if (!fp || layout->data_mode.mode < 0 || layout->data_mode.mode > SDDS_NUM_DATA_MODES)
578 return (0);
579
580 fputs("&data ", fp);
581 SDDS_PrintNamelistField(fp, "mode", SDDS_data_mode[layout->data_mode.mode - 1]);
582 if (layout->data_mode.lines_per_row > 1)
583 fprintf(fp, "lines_per_row=%" PRId32 ", ", layout->data_mode.lines_per_row);
584 if (layout->data_mode.no_row_counts)
585 fprintf(fp, "no_row_counts=1, ");
586 if (layout->version >= 3) {
587 if (layout->data_mode.mode == SDDS_BINARY) {
588 if (layout->byteOrderDeclared == SDDS_BIGENDIAN)
589 fprintf(fp, "endian=big, ");
590 else
591 fprintf(fp, "endian=little, ");
592 if (layout->data_mode.column_major)
593 fprintf(fp, "column_major_order=1, ");
594 }
595 if (layout->data_mode.fixed_row_count)
596 fprintf(fp, "fixed_row_count=1, ");
597 }
598 fputs("&end\n", fp);
599 return (1);
600}
601
602/**
603 * @brief Writes the data mode section to an LZMA-compressed file.
604 *
605 * This function outputs the data mode settings of the SDDS layout to the
606 * specified LZMA-compressed file pointer. It includes settings such as mode,
607 * lines per row, row counts, endianess, column-major order, and fixed row counts.
608 * The section is enclosed within &data and &end tags.
609 *
610 * @param layout Pointer to the SDDS layout structure containing data mode settings.
611 * @param lzmafp The LZMA-compressed file pointer where the data mode section will be written.
612 * @return Returns 1 on success, 0 if the file pointer is NULL or the data mode is invalid.
613 */
614int32_t SDDS_LZMAWriteDataMode(SDDS_LAYOUT *layout, struct lzmafile *lzmafp) {
615 if (!lzmafp || layout->data_mode.mode < 0 || layout->data_mode.mode > SDDS_NUM_DATA_MODES)
616 return (0);
617
618 lzma_puts("&data ", lzmafp);
619 SDDS_LZMAPrintNamelistField(lzmafp, "mode", SDDS_data_mode[layout->data_mode.mode - 1]);
620 if (layout->data_mode.lines_per_row > 1)
621 lzma_printf(lzmafp, "lines_per_row=%" PRId32 ", ", layout->data_mode.lines_per_row);
622 if (layout->data_mode.no_row_counts)
623 lzma_printf(lzmafp, "no_row_counts=1, ");
624 if (layout->version >= 3) {
625 if (layout->data_mode.mode == SDDS_BINARY) {
626 if (layout->byteOrderDeclared == SDDS_BIGENDIAN)
627 lzma_printf(lzmafp, "endian=big, ");
628 else
629 lzma_printf(lzmafp, "endian=little, ");
630 if (layout->data_mode.column_major)
631 lzma_printf(lzmafp, "column_major_order=1, ");
632 }
633 if (layout->data_mode.fixed_row_count)
634 lzma_printf(lzmafp, "fixed_row_count=1, ");
635 }
636 lzma_puts("&end\n", lzmafp);
637 return (1);
638}
639
640#if defined(zLib)
641/**
642 * @brief Writes the data mode section to a GZip-compressed file.
643 *
644 * This function outputs the data mode settings of the SDDS layout to the
645 * specified GZip-compressed file pointer. It includes settings such as mode,
646 * lines per row, row counts, endianess, column-major order, and fixed row counts.
647 * The section is enclosed within &data and &end tags.
648 *
649 * @param layout Pointer to the SDDS layout structure containing data mode settings.
650 * @param gzfp The GZip-compressed file pointer where the data mode section will be written.
651 * @return Returns 1 on success, 0 if the file pointer is NULL or the data mode is invalid.
652 */
653int32_t SDDS_GZipWriteDataMode(SDDS_LAYOUT *layout, gzFile gzfp) {
654 if (!gzfp || layout->data_mode.mode < 0 || layout->data_mode.mode > SDDS_NUM_DATA_MODES)
655 return (0);
656
657 gzputs(gzfp, "&data ");
658 SDDS_GZipPrintNamelistField(gzfp, "mode", SDDS_data_mode[layout->data_mode.mode - 1]);
659 if (layout->data_mode.lines_per_row > 1)
660 gzprintf(gzfp, "lines_per_row=%" PRId32 ", ", layout->data_mode.lines_per_row);
661 if (layout->data_mode.no_row_counts)
662 gzprintf(gzfp, "no_row_counts=1, ");
663 if (layout->version >= 3) {
664 if (layout->data_mode.mode == SDDS_BINARY) {
665 if (layout->byteOrderDeclared == SDDS_BIGENDIAN)
666 gzprintf(gzfp, "endian=big, ");
667 else
668 gzprintf(gzfp, "endian=little, ");
669 if (layout->data_mode.column_major)
670 gzprintf(gzfp, "column_major_order=1, ");
671 }
672 if (layout->data_mode.fixed_row_count)
673 gzprintf(gzfp, "fixed_row_count=1, ");
674 }
675 gzputs(gzfp, "&end\n");
676 return (1);
677}
678#endif
679
680/**
681 * @brief Writes an array definition to a standard file.
682 *
683 * This function outputs the definition of a single array in the SDDS layout.
684 * It includes fields such as name, symbol, units, description, format string,
685 * group name, data type, and dimensions. The definition is enclosed within
686 * &array and &end tags.
687 *
688 * @param array_definition Pointer to the array definition structure.
689 * @param fp The file pointer where the array definition will be written.
690 * @return Returns 1 on success, 0 if the file pointer is NULL or the array type is invalid.
691 */
692int32_t SDDS_WriteArrayDefinition(ARRAY_DEFINITION *array_definition, FILE *fp) {
693 if (!fp || array_definition->type <= 0 || array_definition->type > SDDS_NUM_TYPES)
694 return (0);
695
696 fputs("&array ", fp);
697 SDDS_PrintNamelistField(fp, "name", array_definition->name);
698 SDDS_PrintNamelistField(fp, "symbol", SDDS_BlankToNull(array_definition->symbol));
699 SDDS_PrintNamelistField(fp, "units", SDDS_BlankToNull(array_definition->units));
700 SDDS_PrintNamelistField(fp, "description", SDDS_BlankToNull(array_definition->description));
701 SDDS_PrintNamelistField(fp, "format_string", SDDS_BlankToNull(array_definition->format_string));
702 SDDS_PrintNamelistField(fp, "group_name", SDDS_BlankToNull(array_definition->group_name));
703 SDDS_PrintNamelistField(fp, "type", SDDS_type_name[array_definition->type - 1]);
704 if (array_definition->dimensions != 1) /* 1 is default */
705 fprintf(fp, "dimensions=%" PRId32 ", ", array_definition->dimensions);
706 fputs(" &end\n", fp);
707 return (1);
708}
709
710/**
711 * @brief Writes an array definition to an LZMA-compressed file.
712 *
713 * This function outputs the definition of a single array in the SDDS layout
714 * to an LZMA-compressed file. It includes fields such as name, symbol, units,
715 * description, format string, group name, data type, and dimensions.
716 * The definition is enclosed within &array and &end tags.
717 *
718 * @param array_definition Pointer to the array definition structure.
719 * @param lzmafp The LZMA-compressed file pointer where the array definition will be written.
720 * @return Returns 1 on success, 0 if the file pointer is NULL or the array type is invalid.
721 */
722int32_t SDDS_LZMAWriteArrayDefinition(ARRAY_DEFINITION *array_definition, struct lzmafile *lzmafp) {
723 if (!lzmafp || array_definition->type <= 0 || array_definition->type > SDDS_NUM_TYPES)
724 return (0);
725
726 lzma_puts("&array ", lzmafp);
727 SDDS_LZMAPrintNamelistField(lzmafp, "name", array_definition->name);
728 SDDS_LZMAPrintNamelistField(lzmafp, "symbol", SDDS_BlankToNull(array_definition->symbol));
729 SDDS_LZMAPrintNamelistField(lzmafp, "units", SDDS_BlankToNull(array_definition->units));
730 SDDS_LZMAPrintNamelistField(lzmafp, "description", SDDS_BlankToNull(array_definition->description));
731 SDDS_LZMAPrintNamelistField(lzmafp, "format_string", SDDS_BlankToNull(array_definition->format_string));
732 SDDS_LZMAPrintNamelistField(lzmafp, "group_name", SDDS_BlankToNull(array_definition->group_name));
733 SDDS_LZMAPrintNamelistField(lzmafp, "type", SDDS_type_name[array_definition->type - 1]);
734 if (array_definition->dimensions != 1) /* 1 is default */
735 lzma_printf(lzmafp, "dimensions=%" PRId32 ", ", array_definition->dimensions);
736 lzma_puts(" &end\n", lzmafp);
737 return (1);
738}
739
740#if defined(zLib)
741/**
742 * @brief Writes an array definition to a GZip-compressed file.
743 *
744 * This function outputs the definition of a single array in the SDDS layout
745 * to a GZip-compressed file. It includes fields such as name, symbol, units,
746 * description, format string, group name, data type, and dimensions.
747 * The definition is enclosed within &array and &end tags.
748 *
749 * @param array_definition Pointer to the array definition structure.
750 * @param gzfp The GZip-compressed file pointer where the array definition will be written.
751 * @return Returns 1 on success, 0 if the file pointer is NULL or the array type is invalid.
752 */
753int32_t SDDS_GZipWriteArrayDefinition(ARRAY_DEFINITION *array_definition, gzFile gzfp) {
754 if (!gzfp || array_definition->type <= 0 || array_definition->type > SDDS_NUM_TYPES)
755 return (0);
756
757 gzputs(gzfp, "&array ");
758 SDDS_GZipPrintNamelistField(gzfp, "name", array_definition->name);
759 SDDS_GZipPrintNamelistField(gzfp, "symbol", SDDS_BlankToNull(array_definition->symbol));
760 SDDS_GZipPrintNamelistField(gzfp, "units", SDDS_BlankToNull(array_definition->units));
761 SDDS_GZipPrintNamelistField(gzfp, "description", SDDS_BlankToNull(array_definition->description));
762 SDDS_GZipPrintNamelistField(gzfp, "format_string", SDDS_BlankToNull(array_definition->format_string));
763 SDDS_GZipPrintNamelistField(gzfp, "group_name", SDDS_BlankToNull(array_definition->group_name));
764 SDDS_GZipPrintNamelistField(gzfp, "type", SDDS_type_name[array_definition->type - 1]);
765 if (array_definition->dimensions != 1) /* 1 is default */
766 gzprintf(gzfp, "dimensions=%" PRId32 ", ", array_definition->dimensions);
767 gzputs(gzfp, " &end\n");
768 return (1);
769}
770#endif
771
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
char * SDDS_data_mode[SDDS_NUM_DATA_MODES]
Array of supported data modes.
Definition SDDS_data.c:33
char * SDDS_type_name[SDDS_NUM_TYPES]
Array of supported data type names.
Definition SDDS_data.c:43
Internal definitions and function declarations for SDDS with LZMA support.
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
Definition SDDS_utils.c:639
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
int32_t SDDS_WriteAssociateDefinition(ASSOCIATE_DEFINITION *associate_definition, FILE *fp)
Writes an associate definition to a standard file.
Definition SDDS_write.c:493
int32_t SDDS_LZMAPrintNamelistField(struct lzmafile *lzmafp, char *name, char *value)
Writes a namelist field to an LZMA-compressed file.
Definition SDDS_write.c:166
int32_t SDDS_WriteColumnDefinition(COLUMN_DEFINITION *column_definition, FILE *fp)
Writes a column definition to a standard file.
Definition SDDS_write.c:329
int32_t SDDS_LZMAWriteDescription(char *text, char *contents, struct lzmafile *lzmafp)
Writes the SDDS description section to an LZMA-compressed file.
Definition SDDS_write.c:280
char * SDDS_BlankToNull(char *string)
Converts blank strings to NULL.
Definition SDDS_write.c:37
int32_t SDDS_LZMAWriteColumnDefinition(COLUMN_DEFINITION *column_definition, struct lzmafile *lzmafp)
Writes a column definition to an LZMA-compressed file.
Definition SDDS_write.c:356
int32_t SDDS_LZMAWriteVersion(int32_t version_number, struct lzmafile *lzmafp)
Writes the SDDS protocol version to an LZMA-compressed file.
Definition SDDS_write.c:77
int32_t SDDS_LZMAWriteArrayDefinition(ARRAY_DEFINITION *array_definition, struct lzmafile *lzmafp)
Writes an array definition to an LZMA-compressed file.
Definition SDDS_write.c:722
int32_t SDDS_PrintNamelistField(FILE *fp, char *name, char *value)
Writes a namelist field to a standard file.
Definition SDDS_write.c:122
int32_t SDDS_LZMAWriteParameterDefinition(PARAMETER_DEFINITION *parameter_definition, struct lzmafile *lzmafp)
Writes a parameter definition to an LZMA-compressed file.
Definition SDDS_write.c:438
int32_t SDDS_WriteDescription(char *text, char *contents, FILE *fp)
Writes the SDDS description section to a standard file.
Definition SDDS_write.c:256
int32_t SDDS_LZMAWriteAssociateDefinition(ASSOCIATE_DEFINITION *associate_definition, struct lzmafile *lzmafp)
Writes an associate definition to an LZMA-compressed file.
Definition SDDS_write.c:520
int32_t SDDS_WriteVersion(int32_t version_number, FILE *fp)
Writes the SDDS protocol version to a standard file.
Definition SDDS_write.c:59
int32_t SDDS_LZMAWriteDataMode(SDDS_LAYOUT *layout, struct lzmafile *lzmafp)
Writes the data mode section to an LZMA-compressed file.
Definition SDDS_write.c:614
int32_t SDDS_WriteParameterDefinition(PARAMETER_DEFINITION *parameter_definition, FILE *fp)
Writes a parameter definition to a standard file.
Definition SDDS_write.c:411
int32_t SDDS_WriteArrayDefinition(ARRAY_DEFINITION *array_definition, FILE *fp)
Writes an array definition to a standard file.
Definition SDDS_write.c:692
int32_t SDDS_WriteDataMode(SDDS_LAYOUT *layout, FILE *fp)
Writes the data mode section to a standard file.
Definition SDDS_write.c:576
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
Definition SDDStypes.h:97