SDDSlib
Loading...
Searching...
No Matches
sdds2plaindata.c
Go to the documentation of this file.
1/**
2 * @file sdds2plaindata.c
3 * @brief Converts SDDS (Self Describing Data Sets) files to plain data format.
4 *
5 * This program reads an SDDS input file and writes its contents to a plain data file
6 * in either ASCII or binary format. It provides various options to control the output
7 * format, including setting the output mode, specifying separators, controlling row counts,
8 * selecting specific parameters or columns, and more.
9 *
10 * ## Usage
11 * ```
12 * sdds2plaindata <input> <output>
13 * [-pipe=[input][,output]]
14 * [-outputMode={ascii|binary}]
15 * [-separator=<string>]
16 * [-noRowCount]
17 * [-order={rowMajor|columnMajor}]
18 * [-parameter=<name>[,format=<string>]...]
19 * [-column=<name>[,format=<string>]...]
20 * [-labeled]
21 * [-nowarnings]
22 * ```
23 *
24 * ### Options
25 * - `-pipe=[input][,output]`
26 * Use pipes for input and/or output.
27 *
28 * - `-outputMode={ascii|binary}`
29 * Specify the output format as ASCII or binary. Default is ASCII.
30 *
31 * - `-separator=<string>`
32 * Define the string used to separate columns in ASCII mode.
33 *
34 * - `-noRowCount`
35 * Exclude the number of rows from the plain data file. Note: In binary mode, the row count is always included.
36 *
37 * - `-order={rowMajor|columnMajor}`
38 * Set the data ordering. `rowMajor` writes one element from each column per row (default).
39 * `columnMajor` writes each column entirely on one row.
40 *
41 * - `-parameter=<name>[,format=<string>]...`
42 * Include specified parameters in the output. Optional format can be provided.
43 *
44 * - `-column=<name>[,format=<string>]...`
45 * Include specified columns in the output. Supports wildcards. Optional format can be provided.
46 *
47 * - `-labeled`
48 * Add labels for each parameter or column in ASCII mode. It's recommended to specify a separator when using this option.
49 *
50 * - `-nowarnings`
51 * Suppress warning messages.
52 *
53 * @copyright
54 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
55 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
56 *
57 * @license
58 * This file is distributed under the terms of the Software License Agreement
59 * found in the file LICENSE included with this distribution.
60 *
61 * @author R. Soliday, M. Borland, H. Shang
62 */
63
64#include "mdb.h"
65#include "SDDS.h"
66#include "scan.h"
67
68#if defined(_WIN32)
69# include <fcntl.h>
70# include <io.h>
71# if defined(__BORLANDC__)
72# define _setmode(handle, amode) setmode(handle, amode)
73# endif
74#endif
75
76#define ASCII_MODE 0
77#define BINARY_MODE 1
78#define MODES 2
79static char *mode_name[MODES] = {
80 "ascii",
81 "binary",
82};
83
84#define ROW_ORDER 0
85#define COLUMN_ORDER 1
86#define ORDERS 2
87static char *order_names[ORDERS] = {
88 "rowMajor",
89 "columnMajor",
90};
91
92/* Enumeration for option types */
93enum option_type {
94 SET_OUTPUTMODE,
95 SET_SEPARATOR,
96 SET_NOROWCOUNT,
97 SET_PARAMETER,
98 SET_COLUMN,
99 SET_PIPE,
100 SET_NOWARNINGS,
101 SET_ORDER,
102 SET_LABELED,
103 N_OPTIONS
104};
105
106char *option[N_OPTIONS] = {
107 "outputMode",
108 "separator",
109 "noRowCount",
110 "parameter",
111 "column",
112 "pipe",
113 "nowarnings",
114 "order",
115 "labeled",
116};
117
118char *USAGE =
119 "Usage: sdds2plaindata <input> <output>\n"
120 " [-pipe=[input][,output]]\n"
121 " [-outputMode={ascii|binary}]\n"
122 " [-separator=<string>]\n"
123 " [-noRowCount]\n"
124 " [-order={rowMajor|columnMajor}]\n"
125 " [-parameter=<name>[,format=<string>]...]\n"
126 " [-column=<name>[,format=<string>]...]\n"
127 " [-labeled]\n"
128 " [-nowarnings]\n\n"
129 "Options:\n"
130 " -outputMode Specify output format: ascii or binary.\n"
131 " -separator Define the column separator string in ASCII mode.\n"
132 " -noRowCount Exclude the number of rows from the output file.\n"
133 " (Note: Binary mode always includes row count.)\n"
134 " -order Set data ordering: rowMajor (default) or columnMajor.\n"
135 " -parameter Include specified parameters in the output. Optionally specify a format.\n"
136 " -column Include specified columns in the output. Supports wildcards and optional format.\n"
137 " -labeled Add labels for each parameter or column in ASCII mode.\n"
138 " -nowarnings Suppress warning messages.\n\n"
139 "Program by Robert Soliday. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
140
141/* ********** */
142
143int main(int argc, char **argv) {
144 FILE *fileID;
145 SDDS_LAYOUT *layout = NULL;
146 SDDS_FILEBUFFER *fBuffer = NULL;
147
148 SDDS_DATASET SDDS_dataset, SDDS_dummy;
149 SCANNED_ARG *s_arg;
150 long i_arg, retval, page_number = 0, size, columnOrder = 0;
151 int64_t i, j;
152 int64_t rows = 0;
153 int32_t rows32;
154 char *input, *output;
155 unsigned long pipeFlags = 0, flags = 0;
156 long noWarnings = 0, tmpfile_used = 0;
157 long labeled = 0;
158
159 long *parameterType, *columnType, *parameterIndex;
160 int32_t *columnIndex;
161 char **parameterUnits;
162 void **columnData;
163 char *buffer;
164 static char printBuffer[SDDS_MAXLINE * 16];
165 static char formatbuffer[100], formatbuffer2[100];
166
167 long binary = 0, noRowCount = 0;
168 char *separator, *ptr = NULL;
169 char **parameter, **parameterFormat;
170 char **column, **columnFormat, **columnUnits, **columnMatch, **columnMatchFormat, **columnName;
171 long parameters = 0, columns = 0, columnMatches = 0;
172 int32_t columnNames = 0;
173
174 input = output = NULL;
175 parameter = column = parameterFormat = columnFormat = columnMatch = columnMatchFormat = columnName = NULL;
176 separator = NULL;
177
178 parameterType = columnType = parameterIndex = NULL;
179 columnIndex = NULL;
180 columnData = NULL;
181 parameterUnits = columnUnits = NULL;
182
183 buffer = tmalloc(sizeof(char) * 16);
184
186 argc = scanargs(&s_arg, argc, argv);
187 if (argc < 3)
188 bomb(NULL, USAGE);
189
190 for (i_arg = 1; i_arg < argc; i_arg++) {
191 if (s_arg[i_arg].arg_type == OPTION) {
192 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
193 case SET_OUTPUTMODE:
194 if (s_arg[i_arg].n_items != 2)
195 SDDS_Bomb("invalid -outputMode syntax");
196 switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
197 case ASCII_MODE:
198 binary = 0;
199 break;
200 case BINARY_MODE:
201 binary = 1;
202 break;
203 default:
204 SDDS_Bomb("invalid -outputMode syntax");
205 break;
206 }
207 break;
208 case SET_SEPARATOR:
209 if (s_arg[i_arg].n_items != 2)
210 SDDS_Bomb("invalid -separator syntax");
211 separator = s_arg[i_arg].list[1];
212 break;
213 case SET_NOROWCOUNT:
214 if (s_arg[i_arg].n_items != 1)
215 SDDS_Bomb("invalid -noRowCount syntax");
216 noRowCount = 1;
217 break;
218 case SET_ORDER:
219 if (s_arg[i_arg].n_items != 2)
220 SDDS_Bomb("invalid -order syntax");
221 switch (match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) {
222 case ROW_ORDER:
223 columnOrder = 0;
224 break;
225 case COLUMN_ORDER:
226 columnOrder = 1;
227 break;
228 default:
229 SDDS_Bomb("invalid -order syntax");
230 break;
231 }
232 break;
233 case SET_PARAMETER:
234 if ((s_arg[i_arg].n_items != 2) && (s_arg[i_arg].n_items != 4))
235 SDDS_Bomb("invalid -parameter syntax");
236 parameter = trealloc(parameter, sizeof(*parameter) * (++parameters));
237 parameterFormat = trealloc(parameterFormat, sizeof(*parameterFormat) * (parameters));
238 parameter[parameters - 1] = s_arg[i_arg].list[1];
239 parameterFormat[parameters - 1] = NULL;
240 s_arg[i_arg].n_items -= 2;
241 if (!scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0, "format", SDDS_STRING, &(parameterFormat[parameters - 1]), 1, 0, NULL))
242 SDDS_Bomb("invalid -parameter syntax");
243 if (parameterFormat[parameters - 1]) {
244 replaceString(formatbuffer, parameterFormat[parameters - 1], "ld", PRId32, -1, 0);
245 replaceString(formatbuffer2, formatbuffer, "lu", PRIu32, -1, 0);
246 parameterFormat[parameters - 1] = malloc(sizeof(*(parameterFormat[parameters - 1])) * (strlen(formatbuffer2) + 1));
247 sprintf(parameterFormat[parameters - 1], "%s", formatbuffer2);
248 }
249 break;
250 case SET_COLUMN:
251 if ((s_arg[i_arg].n_items < 2))
252 SDDS_Bomb("invalid -column syntax");
253 if (has_wildcards(s_arg[i_arg].list[1])) {
254 columnMatch = trealloc(columnMatch, sizeof(*columnMatch) * (++columnMatches));
255 columnMatchFormat = trealloc(columnMatchFormat, sizeof(*columnMatchFormat) * (columnMatches));
256 SDDS_CopyString(&columnMatch[columnMatches - 1], s_arg[i_arg].list[1]);
257 columnMatchFormat[columnMatches - 1] = NULL;
258 s_arg[i_arg].n_items -= 2;
259 if (!scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0, "format", SDDS_STRING, &(columnMatchFormat[columnMatches - 1]), 1, 0, NULL))
260 SDDS_Bomb("invalid -columns syntax");
261 if (columnMatchFormat[columnMatches - 1]) {
262 replaceString(formatbuffer, columnMatchFormat[columnMatches - 1], "ld", PRId32, -1, 0);
263 replaceString(formatbuffer2, formatbuffer, "lu", PRIu32, -1, 0);
264 columnMatchFormat[columnMatches - 1] = malloc(sizeof(*(columnMatchFormat[columnMatches - 1])) * (strlen(formatbuffer2) + 1));
265 sprintf(columnMatchFormat[columnMatches - 1], "%s", formatbuffer2);
266 }
267 } else {
268 column = trealloc(column, sizeof(*column) * (++columns));
269 columnFormat = trealloc(columnFormat, sizeof(*columnFormat) * (columns));
270 SDDS_CopyString(&column[columns - 1], s_arg[i_arg].list[1]);
271 columnFormat[columns - 1] = NULL;
272 s_arg[i_arg].n_items -= 2;
273 if (!scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0, "format", SDDS_STRING, &(columnFormat[columns - 1]), 1, 0, NULL))
274 SDDS_Bomb("invalid -columns syntax");
275 if (columnFormat[columns - 1]) {
276 replaceString(formatbuffer, columnFormat[columns - 1], "ld", PRId32, -1, 0);
277 replaceString(formatbuffer2, formatbuffer, "lu", PRIu32, -1, 0);
278 columnFormat[columns - 1] = malloc(sizeof(*(columnFormat[columns - 1])) * (strlen(formatbuffer2) + 1));
279 sprintf(columnFormat[columns - 1], "%s", formatbuffer2);
280 }
281 }
282 break;
283 case SET_PIPE:
284 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
285 SDDS_Bomb("invalid -pipe syntax");
286 break;
287 case SET_NOWARNINGS:
288 if (s_arg[i_arg].n_items != 1)
289 SDDS_Bomb("invalid -nowarnings syntax");
290 noWarnings = 1;
291 break;
292 case SET_LABELED:
293 labeled = 1;
294 break;
295 default:
296 fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]);
297 exit(EXIT_FAILURE);
298 break;
299 }
300 } else {
301 if (input == NULL) {
302 input = s_arg[i_arg].list[0];
303 } else if (output == NULL) {
304 output = s_arg[i_arg].list[0];
305 } else {
306 fprintf(stderr, "error: too many filenames provided.\n");
307 exit(EXIT_FAILURE);
308 }
309 }
310 }
311
312 processFilenames("sdds2plaindata", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
313
314 if (!columns && !columnMatches && !parameters)
315 SDDS_Bomb("error: you must specify at least one of the -column or -parameter options.");
316 if (!separator) {
317 cp_str(&separator, "");
318 }
319
320 if (!SDDS_InitializeInput(&SDDS_dataset, input)) {
321 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
322 exit(EXIT_FAILURE);
323 }
324 if (columnMatches) {
325 if (!(columnName = SDDS_GetColumnNames(&SDDS_dataset, &columnNames)))
326 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
327 for (i = 0; i < columnMatches; i++) {
328 ptr = expand_ranges(columnMatch[i]);
329 for (j = 0; j < columnNames; j++) {
330 if (wild_match(columnName[j], ptr)) {
331 if (columns > 0) {
332 if (match_string(columnName[j], column, columns, EXACT_MATCH) < 0) {
333 column = trealloc(column, sizeof(*column) * (columns + 1));
334 SDDS_CopyString(&column[columns], columnName[j]);
335 columnFormat = trealloc(columnFormat, sizeof(*columnFormat) * (columns + 1));
336 columnFormat[columns] = NULL;
337 if (columnMatchFormat[i])
338 SDDS_CopyString(&columnFormat[columns], columnMatchFormat[i]);
339 columns++;
340 }
341 } else {
342 column = trealloc(column, sizeof(*column) * (columns + 1));
343 SDDS_CopyString(&column[columns], columnName[j]);
344 columnFormat = trealloc(columnFormat, sizeof(*columnFormat) * (columns + 1));
345 columnFormat[columns] = NULL;
346 if (columnMatchFormat[i])
347 SDDS_CopyString(&columnFormat[columns], columnMatchFormat[i]);
348 columns++;
349 }
350 }
351 }
352 }
353 SDDS_FreeStringArray(columnName, columnNames);
354 free(columnName);
355 SDDS_FreeStringArray(columnMatch, columnMatches);
356 free(columnMatch);
357 SDDS_FreeStringArray(columnMatchFormat, columnMatches);
358 free(columnMatchFormat);
359 }
360
361 if (parameters) {
362 parameterType = tmalloc(sizeof(*parameterType) * parameters);
363 parameterIndex = tmalloc(sizeof(*parameterIndex) * parameters);
364 parameterUnits = tmalloc(sizeof(*parameterUnits) * parameters);
365 for (i = 0; i < parameters; i++) {
366 if ((parameterIndex[i] = SDDS_GetParameterIndex(&SDDS_dataset, parameter[i])) < 0) {
367 fprintf(stderr, "error: parameter '%s' does not exist.\n", parameter[i]);
368 exit(EXIT_FAILURE);
369 }
370 if ((parameterType[i] = SDDS_GetParameterType(&SDDS_dataset, parameterIndex[i])) <= 0 ||
371 SDDS_GetParameterInformation(&SDDS_dataset, "units", &parameterUnits[i], SDDS_GET_BY_INDEX, parameterIndex[i]) != SDDS_STRING) {
372 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
373 exit(EXIT_FAILURE);
374 }
375 }
376 }
377 if (columns) {
378 columnType = tmalloc(sizeof(*columnType) * columns);
379 columnIndex = tmalloc(sizeof(*columnIndex) * columns);
380 columnData = tmalloc(sizeof(*columnData) * columns);
381 columnUnits = tmalloc(sizeof(*columnUnits) * columns);
382 for (i = 0; i < columns; i++) {
383 if ((columnIndex[i] = SDDS_GetColumnIndex(&SDDS_dataset, column[i])) < 0) {
384 fprintf(stderr, "error: column '%s' does not exist.\n", column[i]);
385 exit(EXIT_FAILURE);
386 }
387 if ((columnType[i] = SDDS_GetColumnType(&SDDS_dataset, columnIndex[i])) <= 0 ||
388 SDDS_GetColumnInformation(&SDDS_dataset, "units", &columnUnits[i], SDDS_GET_BY_INDEX, columnIndex[i]) != SDDS_STRING) {
389 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
390 exit(EXIT_FAILURE);
391 }
392 }
393 }
394
395 if (!output) {
396 if (binary) {
397#if defined(_WIN32)
398 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
399 fprintf(stderr, "error: unable to set stdout to binary mode.\n");
400 exit(EXIT_FAILURE);
401 }
402#endif
403 }
404 fileID = stdout;
405 } else {
406 if (binary) {
407 fileID = fopen(output, "wb");
408 } else {
409 fileID = fopen(output, "w");
410 }
411 }
412 if (fileID == NULL) {
413 fprintf(stderr, "error: unable to open output file for writing.\n");
414 exit(EXIT_FAILURE);
415 }
416
417 if (binary) {
418 layout = &SDDS_dataset.layout;
419 fBuffer = &SDDS_dummy.fBuffer;
420 fBuffer->buffer = NULL;
421 if (!fBuffer->buffer) {
422 if (!(fBuffer->buffer = fBuffer->data = SDDS_Malloc(sizeof(char) * SDDS_FILEBUFFER_SIZE))) {
423 fprintf(stderr, "error: unable to allocate buffer for binary output.\n");
424 exit(EXIT_FAILURE);
425 }
426 fBuffer->bufferSize = SDDS_FILEBUFFER_SIZE;
427 fBuffer->bytesLeft = SDDS_FILEBUFFER_SIZE;
428 }
429 }
430
431 retval = -1;
432 while (retval != page_number && (retval = SDDS_ReadPage(&SDDS_dataset)) > 0) {
433 if (page_number && retval != page_number)
434 continue;
435 if (columns != 0) {
436 if ((rows = SDDS_CountRowsOfInterest(&SDDS_dataset)) < 0) {
437 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
438 exit(EXIT_FAILURE);
439 }
440 }
441 if ((binary) && (!noRowCount)) {
442 if (rows > INT32_MAX) {
443 rows32 = INT32_MIN;
444 if (!SDDS_BufferedWrite(&rows32, sizeof(rows32), fileID, fBuffer)) {
445 fprintf(stderr, "error: failed to write row count (overflow).\n");
446 exit(EXIT_FAILURE);
447 }
448 if (!SDDS_BufferedWrite(&rows, sizeof(rows), fileID, fBuffer)) {
449 fprintf(stderr, "error: failed to write row count.\n");
450 exit(EXIT_FAILURE);
451 }
452 } else {
453 rows32 = rows;
454 if (!SDDS_BufferedWrite(&rows32, sizeof(rows32), fileID, fBuffer)) {
455 fprintf(stderr, "error: failed to write row count.\n");
456 exit(EXIT_FAILURE);
457 }
458 }
459 }
460
461 for (i = 0; i < parameters; i++) {
462 if (binary) {
463 /*
464 if (layout->parameter_definition[i].fixed_value)
465 continue;
466 */
467 if (parameterType[i] == SDDS_STRING) {
468 if (!SDDS_WriteBinaryString(*((char **)SDDS_dataset.parameter[parameterIndex[i]]), fileID, fBuffer)) {
469 fprintf(stderr, "error: failed to write string parameter.\n");
470 exit(EXIT_FAILURE);
471 }
472 } else if (!SDDS_BufferedWrite(SDDS_dataset.parameter[parameterIndex[i]], SDDS_type_size[layout->parameter_definition[parameterIndex[i]].type - 1], fileID, fBuffer)) {
473 fprintf(stderr, "error: failed to write parameter value.\n");
474 exit(EXIT_FAILURE);
475 }
476 } else {
477 if (!SDDS_GetParameter(&SDDS_dataset, parameter[i], buffer)) {
478 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
479 exit(EXIT_FAILURE);
480 }
481 SDDS_SprintTypedValue(buffer, 0, parameterType[i], parameterFormat[i], printBuffer, 0);
482 if (labeled) {
483 fputs(parameter[i], fileID);
484 fputs(separator, fileID);
485 if (parameterUnits[i])
486 fputs(parameterUnits[i], fileID);
487 fputs(separator, fileID);
488 }
489 fputs(printBuffer, fileID);
490 fputc('\n', fileID);
491 }
492 }
493
494 if (!binary) {
495 if (!noRowCount)
496 fprintf(fileID, "\t%" PRId64 "\n", rows);
497 if (labeled && columns) {
498 for (j = 0; j < columns; j++) {
499 fputs(column[j], fileID);
500 if (j != (columns - 1))
501 fputs(separator, fileID);
502 }
503 fputc('\n', fileID);
504 for (j = 0; j < columns; j++) {
505 if (columnUnits[j])
506 fputs(columnUnits[j], fileID);
507 if (j != (columns - 1))
508 fputs(separator, fileID);
509 }
510 fputc('\n', fileID);
511 }
512 }
513
514 if (columns) {
515 if (rows) {
516 if (binary) {
517 if (columnOrder) {
518 for (j = 0; j < columns; j++) {
519 if (columnType[j] == SDDS_STRING) {
520 for (i = 0; i < rows; i++) {
521 if (!SDDS_WriteBinaryString(*((char **)SDDS_dataset.data[columnIndex[j]] + i), fileID, fBuffer)) {
522 fprintf(stderr, "error: failed to write string data for column '%s'.\n", column[j]);
523 exit(EXIT_FAILURE);
524 }
525 }
526 } else {
527 size = SDDS_type_size[columnType[j] - 1];
528 for (i = 0; i < rows; i++) {
529 if (!SDDS_BufferedWrite((char *)SDDS_dataset.data[columnIndex[j]] + i * size, size, fileID, fBuffer)) {
530 fprintf(stderr, "error: failed to write data for column '%s'.\n", column[j]);
531 exit(EXIT_FAILURE);
532 }
533 }
534 }
535 }
536 } else {
537 for (i = 0; i < rows; i++) {
538 for (j = 0; j < columns; j++) {
539 if (columnType[j] == SDDS_STRING) {
540 if (!SDDS_WriteBinaryString(*((char **)SDDS_dataset.data[columnIndex[j]] + i), fileID, fBuffer)) {
541 fprintf(stderr, "error: failed to write string data for column '%s'.\n", column[j]);
542 exit(EXIT_FAILURE);
543 }
544 } else {
545 size = SDDS_type_size[columnType[j] - 1];
546 if (!SDDS_BufferedWrite((char *)SDDS_dataset.data[columnIndex[j]] + i * size, size, fileID, fBuffer)) {
547 fprintf(stderr, "error: failed to write data for column '%s'.\n", column[j]);
548 exit(EXIT_FAILURE);
549 }
550 }
551 }
552 }
553 }
554 } else {
555 for (i = 0; i < columns; i++) {
556 if (!(columnData[i] = SDDS_GetInternalColumn(&SDDS_dataset, column[i]))) {
557 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
558 exit(EXIT_FAILURE);
559 }
560 }
561 if (columnOrder) {
562 for (i = 0; i < columns; i++) {
563 for (j = 0; j < rows; j++) {
564 SDDS_SprintTypedValue(columnData[i], j, columnType[i], columnFormat[i], printBuffer, 0);
565 fputs(printBuffer, fileID);
566 if (j != rows - 1)
567 fprintf(fileID, "%s", separator);
568 }
569 fputc('\n', fileID);
570 }
571 } else {
572 for (j = 0; j < rows; j++) {
573 for (i = 0; i < columns; i++) {
574 SDDS_SprintTypedValue(columnData[i], j, columnType[i], columnFormat[i], printBuffer, 0);
575 fputs(printBuffer, fileID);
576 if (i != columns - 1)
577 fprintf(fileID, "%s", separator);
578 }
579 fputc('\n', fileID);
580 }
581 }
582 }
583 }
584 }
585
586 if (retval == 0) {
587 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
588 exit(EXIT_FAILURE);
589 }
590 }
591
592 if (binary) {
593 if (!SDDS_FlushBuffer(fileID, fBuffer)) {
594 SDDS_SetError("Unable to write page--buffer flushing problem (SDDS_WriteBinaryPage)");
595 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
596 exit(EXIT_FAILURE);
597 }
598 } else {
599 fflush(fileID);
600 }
601 fclose(fileID);
602
603 if (columns) {
604 SDDS_FreeStringArray(column, columns);
605 free(column);
606 SDDS_FreeStringArray(columnFormat, columns);
607 free(columnFormat);
608 }
609 if (parameters) {
610 SDDS_FreeStringArray(parameter, parameters);
611 free(parameter);
612 SDDS_FreeStringArray(parameterFormat, parameters);
613 free(parameterFormat);
614 }
615 free_scanargs(&s_arg, argc);
616 if (!SDDS_Terminate(&SDDS_dataset)) {
617 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
618 exit(EXIT_FAILURE);
619 }
620
621 exit(EXIT_SUCCESS);
622}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_BufferedWrite(void *target, int64_t targetSize, FILE *fp, SDDS_FILEBUFFER *fBuffer)
int32_t SDDS_WriteBinaryString(char *string, FILE *fp, SDDS_FILEBUFFER *fBuffer)
Writes a binary string to a file with buffering.
int32_t SDDS_FlushBuffer(FILE *fp, SDDS_FILEBUFFER *fBuffer)
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
Definition SDDS_data.c:62
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
void * SDDS_GetInternalColumn(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves an internal pointer to the data of a specified column, including all rows.
void * SDDS_GetParameter(SDDS_DATASET *SDDS_dataset, char *parameter_name, void *memory)
Retrieves the value of a specified parameter from the current data table of a data set.
int32_t SDDS_GetParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified parameter in the SDDS dataset.
Definition SDDS_info.c:117
int32_t SDDS_GetColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified column in the SDDS dataset.
Definition SDDS_info.c:41
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
Definition SDDS_utils.c:379
int32_t SDDS_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_SprintTypedValue(void *data, int64_t index, int32_t type, const char *format, char *buffer, uint32_t mode)
Formats a data value of a specified type into a string buffer using an optional printf format string.
Definition SDDS_utils.c:151
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
Definition SDDS_utils.c:639
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
Definition SDDS_utils.c:856
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int replaceString(char *t, char *s, char *orig, char *repl, long count_limit, long here)
Replace occurrences of one string with another string with additional options.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
void free_scanargs(SCANNED_ARG **scanned, int argc)
Definition scanargs.c:584
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
int has_wildcards(char *template)
Check if a template string contains any wildcard characters.
Definition wild_match.c:498
char * expand_ranges(char *template)
Expand range specifiers in a wildcard template into explicit character lists.
Definition wild_match.c:429
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.
Definition wild_match.c:49