SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
plaindata2sdds.c
Go to the documentation of this file.
1/**
2 * @file plaindata2sdds.c
3 * @brief Converts plain data files to SDDS format.
4 *
5 * This program reads plain data files in ASCII or binary format and converts them
6 * to the structured SDDS (Self Describing Data Set) format. It supports extensive
7 * configuration options for handling input and output modes, customizing separators,
8 * skipping lines, defining parameters and columns, and more.
9 *
10 * @section Usage
11 * ```
12 * plaindata2sdds <input> <output>
13 * [-pipe=[input][,output]]
14 * [-inputMode=<ascii|binary>]
15 * [-outputMode=<ascii|binary>]
16 * [-separator=<char>]
17 * [-commentCharacters=<chars>]
18 * [-noRowCount]
19 * [-binaryRows=<rowcount>]
20 * [-order=<rowMajor|columnMajor>]
21 * [-parameter=<name>,<type>[,units=<string>][,description=<string>][,symbol=<string>][,count=<integer>]...]
22 * [-column=<name>,<type>[,units=<string>][,description=<string>][,symbol=<string>][,count=<integer>]...]
23 * [-skipcolumn=<type>]
24 * [-nowarnings]
25 * [-fillin]
26 * [-skiplines=<integer>]
27 * [-eofSequence=<string>]
28 * [-majorOrder=row|column]
29 * ```
30 *
31 * @section Options
32 * | Option | Description |
33 * |-----------------------|-------------------------------------------------------------------------------------------------|
34 * | `-pipe` | Enables piping for input and/or output. Use `-pipe=input,output` for both. |
35 * | `-inputMode` | Specifies the input file format: ASCII or binary. |
36 * | `-outputMode` | Specifies the output SDDS file format: ASCII or binary. |
37 * | `-separator` | Defines the column separator in ASCII mode. Defaults to whitespace. |
38 * | `-commentCharacters` | Specifies characters that indicate comments in the input file. |
39 * | `-noRowCount` | Indicates that the input file does not include a row count. |
40 * | `-binaryRows` | Specifies the number of rows for binary input files without a row count. |
41 * | `-order` | Sets the data storage order in the input file: rowMajor or columnMajor. |
42 * | `-parameter` | Defines parameters with attributes such as name, type, units, description, and symbol. |
43 * | `-column` | Defines columns with attributes such as name, type, units, description, and symbol. |
44 * | `-skipcolumn` | Skips a specific type of column during parsing. |
45 * | `-nowarnings` | Suppresses warning messages during execution. |
46 * | `-fillin` | Fills blank entries with default values (0 for numeric, empty string for string columns). |
47 * | `-skiplines` | Skips the first `n` header lines in the input file. |
48 * | `-eofSequence` | Stops parsing when the specified sequence is found at the start of a line. |
49 * | `-majorOrder` | Specifies the major order for writing the output file: row or column. |
50 *
51 * @subsection Incompatibilities
52 * - `-binaryRows` is incompatible with:
53 * - `-noRowCount`
54 * - `-skiplines` does not work with binary input files.
55 *
56 * @subsection SR Specific Requirements
57 * - `-separator` requires a single character.
58 *
59 * @copyright
60 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
61 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
62 *
63 * @license
64 * This file is distributed under the terms of the Software License Agreement
65 * found in the file LICENSE included with this distribution.
66 *
67 * @authors
68 * R. Soliday, M. Borland, H. Shang
69 */
70
71#include "mdb.h"
72#include "SDDS.h"
73#include "scan.h"
74#include <ctype.h>
75#include <signal.h>
76#if defined(_WIN32)
77# include <io.h>
78# include <fcntl.h>
79#endif
80
81#define ASCII_MODE 0
82#define BINARY_MODE 1
83#define MODES 2
84static char *mode_name[MODES] = {
85 "ascii",
86 "binary",
87};
88
89#define TYPE_SHORT 0
90#define TYPE_LONG 1
91#define TYPE_LONG64 2
92#define TYPE_FLOAT 3
93#define TYPE_LONGDOUBLE 4
94#define TYPE_DOUBLE 5
95#define TYPE_STRING 6
96#define TYPE_CHARACTER 7
97#define DATATYPES 8
98static char *type_name[DATATYPES] = {
99 "short",
100 "long",
101 "long64",
102 "float",
103 "longdouble",
104 "double",
105 "string",
106 "character",
107};
108
109#define HEADER_UNITS 0
110#define HEADER_DESCRIPTION 1
111#define HEADER_SYMBOL 2
112#define HEADER_COUNT 3
113#define HEADERELEMENTS 4
114static char *header_elements[HEADERELEMENTS] = {
115 "units", "description", "symbol", "count"
116};
117
118typedef struct
119{
120 void *values;
121 long elements;
122 char **stringValues;
123 char *units;
124 char *description;
125 char *symbol;
126 char *name;
127 long type;
128 short skip;
130
131typedef struct
132{
133 char *units;
134 char *description;
135 char *symbol;
136 char *name;
137 long type;
139
140/* Enumeration for option types */
141enum option_type {
142 SET_INPUTMODE,
143 SET_OUTPUTMODE,
144 SET_SEPARATOR,
145 SET_NOROWCOUNT,
146 SET_PARAMETER,
147 SET_COLUMN,
148 SET_PIPE,
149 SET_NOWARNINGS,
150 SET_ORDER,
151 SET_FILLIN,
152 SET_SKIPLINES,
153 SET_SKIPCOLUMN,
154 SET_COMMENT,
155 SET_MAJOR_ORDER,
156 SET_BINARY_ROWS,
157 SET_EOF_SEQUENCE,
158 N_OPTIONS
159};
160
161char *option[N_OPTIONS] = {
162 "inputMode",
163 "outputMode",
164 "separator",
165 "noRowCount",
166 "parameter",
167 "column",
168 "pipe",
169 "nowarnings",
170 "order",
171 "fillin",
172 "skiplines",
173 "skipcolumn",
174 "commentCharacters",
175 "majorOrder",
176 "binaryRows",
177 "eofsequence"
178};
179
180#define ROW_ORDER 0
181#define COLUMN_ORDER 1
182#define ORDERS 2
183static char *order_names[ORDERS] = {
184 "rowMajor",
185 "columnMajor",
186};
187
188/* Improved Usage Message */
189char *USAGE =
190 "plaindata2sdds [<input>] [<output>]\n"
191 " [-pipe=[input][,output]]\n"
192 " [-inputMode=<ascii|binary>]\n"
193 " [-outputMode=<ascii|binary>]\n"
194 " [-separator=<char>]\n"
195 " [-commentCharacters=<chars>]\n"
196 " [-noRowCount]\n"
197 " [-binaryRows=<rowcount>]\n"
198 " [-order=<rowMajor|columnMajor>]\n"
199 " [-parameter=<name>,<type>[,units=<string>][,description=<string>][,symbol=<string>][,count=<integer>]...]\n"
200 " [-column=<name>,<type>[,units=<string>][,description=<string>][,symbol=<string>][,count=<integer>]...]\n"
201 " [-skipcolumn=<type>]\n"
202 " [-skiplines=<integer>]\n"
203 " [-eofSequence=<string>]\n"
204 " [-majorOrder=<row|column>]\n"
205 " [-fillin]\n"
206 " [-nowarnings]\n\n"
207 "Options:\n"
208 " -inputMode The plain data file can be read in ascii or binary format.\n"
209 " -outputMode The SDDS data file can be written in ascii or binary format.\n"
210 " -separator In ascii mode, columns of the plain data file are separated by the given character.\n"
211 " By default, any combination of whitespace characters is used.\n"
212 " -commentCharacters Characters that denote comments. Lines starting with these are ignored.\n"
213 " -noRowCount The number of rows is not included in the plain data file.\n"
214 " If the plain data file is binary, the row count must be set using -binaryRows.\n"
215 " -binaryRows The number of rows in a binary file without an explicit row count.\n"
216 " -order Specifies the order of data storage in the input file.\n"
217 " - rowMajor (default): Each row consists of one element from each column.\n"
218 " - columnMajor: Each column is located entirely on one row.\n"
219 " -parameter Add this option for each parameter in the plain data file.\n"
220 " -column Add this option for each column in the plain data file.\n"
221 " -skipcolumn Add this option to skip over a column in the plain data file.\n"
222 " -skiplines Add this option to skip a specified number of header lines.\n"
223 " -eofSequence Stop parsing the file when this sequence is found at the start of a line.\n"
224 " -majorOrder Specifies the major order for writing the output file (row or column).\n"
225 " -fillin Fill in blanks with default values (0 for numeric columns, empty string for string columns).\n"
226 " -nowarnings Suppress warning messages during execution.\n\n"
227 "Program by Robert Soliday. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
228
229/* Function Prototypes */
230void SetColumnData(long type, SDDS_DATASET *dataset, void *values, int32_t rows, int32_t index);
231void *AllocateColumnData(long type, void *values, int32_t rows);
232char **AllocateColumnStringData(char **values, int32_t rows, int32_t previous_rows);
233long getToken(char *s, char *buffer, long buflen, char separator, long whitespace);
234void ConvertDNotationToENotation(char *line);
235void interrupt_handler(int sig);
236
237void interrupt_handler(int sig) {
238 fprintf(stderr, "Segmentation fault. Ensure that your input file matches the -inputMode given.\n");
239 exit(EXIT_FAILURE);
240}
241
242/* ********** */
243
244int main(int argc, char **argv) {
245 FILE *fileID;
246 COLUMN_DATA_STRUCTURES *columnValues;
247 PARAMETER_DATA_STRUCTURES *parameterValues;
248
249 SDDS_DATASET SDDS_dataset;
250 SCANNED_ARG *s_arg;
251 long i, j, k, n = 0, i_arg;
252 int32_t rows, binaryRows = -1;
253 int32_t maxRows = 10000, initRows = 10000, row;
254 long par, col, page, size, readline = 1, fillin = 0;
255 int32_t ptrSize = 0;
256 char *input, *output, s[1024], *ptr, *ptr2, data[10240], temp[10240], *eofSequence;
257 unsigned long pipeFlags = 0, majorOrderFlag;
258 long noWarnings = 0, tmpfile_used = 0, columnOrder = 0, whitespace = 1;
259 short shortValue, stop;
260 int32_t longValue;
261 int64_t long64Value;
262 float floatValue;
263 long double ldoubleValue;
264 double doubleValue;
265 char stringValue[SDDS_MAXLINE];
266 char characterValue;
267 char buffer[124], buffer2[200];
268 long *parameterIndex, *columnIndex;
269
270 long binary = 0, noRowCount = 0, inputBinary = 0, count = 0;
271 char separator;
272 char commentCharacters[20];
273 short checkComment = 0;
274 short commentFound;
275 long parameters = 0, columns = 0;
276 long skiplines = 0;
277 short abort_flag = 0, recover = 1, columnMajorOrder = 0;
278
279 input = output = NULL;
280 separator = ' ';
281 eofSequence = NULL;
282 columnValues = NULL;
283 parameterValues = NULL;
284
285 parameterIndex = columnIndex = NULL;
286
288 argc = scanargs(&s_arg, argc, argv);
289 if (argc < 3)
290 bomb(NULL, USAGE);
291
292 signal(SIGSEGV, interrupt_handler);
293
294 for (i_arg = 1; i_arg < argc; i_arg++) {
295 if (s_arg[i_arg].arg_type == OPTION) {
296 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
297 case SET_MAJOR_ORDER:
298 majorOrderFlag = 0;
299 s_arg[i_arg].n_items -= 1;
300 if (s_arg[i_arg].n_items > 0 &&
301 (!scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
302 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
303 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
304 SDDS_Bomb("invalid -majorOrder syntax/values");
305 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
306 columnMajorOrder = 1;
307 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
308 columnMajorOrder = 0;
309 break;
310 case SET_OUTPUTMODE:
311 if (s_arg[i_arg].n_items != 2)
312 SDDS_Bomb("invalid -outputMode syntax");
313 switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
314 case ASCII_MODE:
315 binary = 0;
316 break;
317 case BINARY_MODE:
318 binary = 1;
319 break;
320 default:
321 SDDS_Bomb("invalid -outputMode syntax");
322 break;
323 }
324 break;
325 case SET_INPUTMODE:
326 if (s_arg[i_arg].n_items != 2)
327 SDDS_Bomb("invalid -inputMode syntax");
328 switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
329 case ASCII_MODE:
330 inputBinary = 0;
331 break;
332 case BINARY_MODE:
333 inputBinary = 1;
334 break;
335 default:
336 SDDS_Bomb("invalid -inputMode syntax");
337 break;
338 }
339 break;
340 case SET_SEPARATOR:
341 if (s_arg[i_arg].n_items != 2)
342 SDDS_Bomb("invalid -separator syntax");
343 separator = s_arg[i_arg].list[1][0];
344 whitespace = 0;
345 break;
346 case SET_COMMENT:
347 if (s_arg[i_arg].n_items != 2)
348 SDDS_Bomb("invalid -commentCharacters syntax");
349 strncpy(commentCharacters, s_arg[i_arg].list[1], sizeof(commentCharacters) - 1);
350 commentCharacters[sizeof(commentCharacters) - 1] = '\0';
351 checkComment = 1;
352 break;
353 case SET_FILLIN:
354 fillin = 1;
355 break;
356 case SET_NOROWCOUNT:
357 if (s_arg[i_arg].n_items != 1)
358 SDDS_Bomb("invalid -noRowCount syntax");
359 noRowCount = 1;
360 break;
361 case SET_ORDER:
362 if (s_arg[i_arg].n_items != 2)
363 SDDS_Bomb("invalid -order syntax");
364 switch (match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) {
365 case ROW_ORDER:
366 columnOrder = 0;
367 break;
368 case COLUMN_ORDER:
369 columnOrder = 1;
370 break;
371 default:
372 SDDS_Bomb("invalid -order syntax");
373 break;
374 }
375 break;
376 case SET_PARAMETER:
377 if (s_arg[i_arg].n_items < 3)
378 SDDS_Bomb("invalid -parameter syntax");
379 count = 1;
380 parameters++;
381 parameterValues = trealloc(parameterValues, sizeof(*parameterValues) * (parameters));
382 SDDS_CopyString(&parameterValues[parameters - 1].name, s_arg[i_arg].list[1]);
383 parameterValues[parameters - 1].units = NULL;
384 parameterValues[parameters - 1].description = NULL;
385 parameterValues[parameters - 1].symbol = NULL;
386 switch (match_string(s_arg[i_arg].list[2], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
387 case TYPE_SHORT:
388 parameterValues[parameters - 1].type = SDDS_SHORT;
389 break;
390 case TYPE_LONG:
391 parameterValues[parameters - 1].type = SDDS_LONG;
392 break;
393 case TYPE_LONG64:
394 parameterValues[parameters - 1].type = SDDS_LONG64;
395 break;
396 case TYPE_FLOAT:
397 parameterValues[parameters - 1].type = SDDS_FLOAT;
398 break;
399 case TYPE_LONGDOUBLE:
400 parameterValues[parameters - 1].type = SDDS_LONGDOUBLE;
401 break;
402 case TYPE_DOUBLE:
403 parameterValues[parameters - 1].type = SDDS_DOUBLE;
404 break;
405 case TYPE_STRING:
406 parameterValues[parameters - 1].type = SDDS_STRING;
407 break;
408 case TYPE_CHARACTER:
409 parameterValues[parameters - 1].type = SDDS_CHARACTER;
410 break;
411 default:
412 SDDS_Bomb("invalid -parameter type");
413 break;
414 }
415 for (i = 3; i < s_arg[i_arg].n_items; i++) {
416 if (!(ptr = strchr(s_arg[i_arg].list[i], '=')))
417 SDDS_Bomb("invalid -parameter syntax");
418 *ptr++ = 0;
419 switch (match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) {
420 case HEADER_UNITS:
421 SDDS_CopyString(&parameterValues[parameters - 1].units, ptr);
422 break;
423 case HEADER_DESCRIPTION:
424 SDDS_CopyString(&parameterValues[parameters - 1].description, ptr);
425 break;
426 case HEADER_SYMBOL:
427 SDDS_CopyString(&parameterValues[parameters - 1].symbol, ptr);
428 break;
429 case HEADER_COUNT:
430 if (sscanf(ptr, "%ld", &count) != 1 || count <= 0)
431 SDDS_Bomb("invalid parameter count value");
432 snprintf(buffer, sizeof(buffer), "%s", parameterValues[parameters - 1].name);
433 snprintf(buffer2, sizeof(buffer2), "%s1", buffer);
434 free(parameterValues[parameters - 1].name);
435 SDDS_CopyString(&parameterValues[parameters - 1].name, buffer2);
436 break;
437 default:
438 SDDS_Bomb("invalid -parameter syntax");
439 break;
440 }
441 }
442
443 for (i = 2; i <= count; i++) {
444 parameters++;
445 parameterValues = trealloc(parameterValues, sizeof(*parameterValues) * (parameters));
446 snprintf(buffer2, sizeof(buffer2), "%s%ld", buffer, i);
447 SDDS_CopyString(&parameterValues[parameters - 1].name, buffer2);
448 parameterValues[parameters - 1].units = NULL;
449 parameterValues[parameters - 1].description = NULL;
450 parameterValues[parameters - 1].symbol = NULL;
451 parameterValues[parameters - 1].type = parameterValues[parameters - 2].type;
452 if (parameterValues[parameters - 2].units)
453 SDDS_CopyString(&parameterValues[parameters - 1].units, parameterValues[parameters - 2].units);
454 if (parameterValues[parameters - 2].description)
455 SDDS_CopyString(&parameterValues[parameters - 1].description, parameterValues[parameters - 2].description);
456 if (parameterValues[parameters - 2].symbol)
457 SDDS_CopyString(&parameterValues[parameters - 1].symbol, parameterValues[parameters - 2].symbol);
458 }
459
460 break;
461 case SET_COLUMN:
462 if (s_arg[i_arg].n_items < 3)
463 SDDS_Bomb("invalid -column syntax");
464 count = 1;
465 columns++;
466 columnValues = trealloc(columnValues, sizeof(*columnValues) * (columns));
467 SDDS_CopyString(&columnValues[columns - 1].name, s_arg[i_arg].list[1]);
468 columnValues[columns - 1].elements = 0;
469 columnValues[columns - 1].values = NULL;
470 columnValues[columns - 1].stringValues = NULL;
471 columnValues[columns - 1].units = NULL;
472 columnValues[columns - 1].description = NULL;
473 columnValues[columns - 1].symbol = NULL;
474 columnValues[columns - 1].skip = 0;
475
476 switch (match_string(s_arg[i_arg].list[2], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
477 case TYPE_SHORT:
478 columnValues[columns - 1].type = SDDS_SHORT;
479 break;
480 case TYPE_LONG:
481 columnValues[columns - 1].type = SDDS_LONG;
482 break;
483 case TYPE_LONG64:
484 columnValues[columns - 1].type = SDDS_LONG64;
485 break;
486 case TYPE_FLOAT:
487 columnValues[columns - 1].type = SDDS_FLOAT;
488 break;
489 case TYPE_LONGDOUBLE:
490 columnValues[columns - 1].type = SDDS_LONGDOUBLE;
491 break;
492 case TYPE_DOUBLE:
493 columnValues[columns - 1].type = SDDS_DOUBLE;
494 break;
495 case TYPE_STRING:
496 columnValues[columns - 1].type = SDDS_STRING;
497 break;
498 case TYPE_CHARACTER:
499 columnValues[columns - 1].type = SDDS_CHARACTER;
500 break;
501 default:
502 SDDS_Bomb("invalid -column type");
503 break;
504 }
505 for (i = 3; i < s_arg[i_arg].n_items; i++) {
506 if (!(ptr = strchr(s_arg[i_arg].list[i], '=')))
507 SDDS_Bomb("invalid -column syntax");
508 *ptr++ = 0;
509 switch (match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) {
510 case HEADER_UNITS:
511 SDDS_CopyString(&columnValues[columns - 1].units, ptr);
512 break;
513 case HEADER_DESCRIPTION:
514 SDDS_CopyString(&columnValues[columns - 1].description, ptr);
515 break;
516 case HEADER_SYMBOL:
517 SDDS_CopyString(&columnValues[columns - 1].symbol, ptr);
518 break;
519 case HEADER_COUNT:
520 if (sscanf(ptr, "%ld", &count) != 1 || count <= 0)
521 SDDS_Bomb("invalid column count value");
522 snprintf(buffer, sizeof(buffer), "%s", columnValues[columns - 1].name);
523 snprintf(buffer2, sizeof(buffer2), "%s1", buffer);
524 free(columnValues[columns - 1].name);
525 SDDS_CopyString(&columnValues[columns - 1].name, buffer2);
526 break;
527 default:
528 SDDS_Bomb("invalid -column syntax");
529 break;
530 }
531 }
532
533 for (i = 2; i <= count; i++) {
534 columns++;
535 columnValues = trealloc(columnValues, sizeof(*columnValues) * (columns));
536 snprintf(buffer2, sizeof(buffer2), "%s%ld", buffer, i);
537 SDDS_CopyString(&columnValues[columns - 1].name, buffer2);
538 columnValues[columns - 1].elements = 0;
539 columnValues[columns - 1].values = NULL;
540 columnValues[columns - 1].stringValues = NULL;
541 columnValues[columns - 1].units = NULL;
542 columnValues[columns - 1].description = NULL;
543 columnValues[columns - 1].symbol = NULL;
544 columnValues[columns - 1].type = columnValues[columns - 2].type;
545 if (columnValues[columns - 2].units)
546 SDDS_CopyString(&columnValues[columns - 1].units, columnValues[columns - 2].units);
547 if (columnValues[columns - 2].description)
548 SDDS_CopyString(&columnValues[columns - 1].description, columnValues[columns - 2].description);
549 if (columnValues[columns - 2].symbol)
550 SDDS_CopyString(&columnValues[columns - 1].symbol, columnValues[columns - 2].symbol);
551 }
552
553 break;
554 case SET_SKIPCOLUMN:
555 if (s_arg[i_arg].n_items != 2)
556 SDDS_Bomb("invalid -skipcolumn syntax");
557 count = 1;
558 columns++;
559 columnValues = trealloc(columnValues, sizeof(*columnValues) * (columns));
560 columnValues[columns - 1].name = NULL;
561 columnValues[columns - 1].elements = 0;
562 columnValues[columns - 1].values = NULL;
563 columnValues[columns - 1].stringValues = NULL;
564 columnValues[columns - 1].units = NULL;
565 columnValues[columns - 1].description = NULL;
566 columnValues[columns - 1].symbol = NULL;
567 columnValues[columns - 1].skip = 1;
568
569 switch (match_string(s_arg[i_arg].list[1], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
570 case TYPE_SHORT:
571 columnValues[columns - 1].type = SDDS_SHORT;
572 break;
573 case TYPE_LONG:
574 columnValues[columns - 1].type = SDDS_LONG;
575 break;
576 case TYPE_LONG64:
577 columnValues[columns - 1].type = SDDS_LONG64;
578 break;
579 case TYPE_FLOAT:
580 columnValues[columns - 1].type = SDDS_FLOAT;
581 break;
582 case TYPE_LONGDOUBLE:
583 columnValues[columns - 1].type = SDDS_LONGDOUBLE;
584 break;
585 case TYPE_DOUBLE:
586 columnValues[columns - 1].type = SDDS_DOUBLE;
587 break;
588 case TYPE_STRING:
589 columnValues[columns - 1].type = SDDS_STRING;
590 break;
591 case TYPE_CHARACTER:
592 columnValues[columns - 1].type = SDDS_CHARACTER;
593 break;
594 default:
595 SDDS_Bomb("invalid -skipcolumn type");
596 break;
597 }
598 break;
599 case SET_PIPE:
600 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
601 SDDS_Bomb("invalid -pipe syntax");
602 break;
603 case SET_NOWARNINGS:
604 if (s_arg[i_arg].n_items != 1)
605 SDDS_Bomb("invalid -nowarnings syntax");
606 noWarnings = 1;
607 break;
608 case SET_SKIPLINES:
609 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &skiplines) != 1 || skiplines <= 0)
610 SDDS_Bomb("invalid -skiplines syntax");
611 break;
612 case SET_BINARY_ROWS:
613 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%" SCNd32, &binaryRows) != 1 || binaryRows < 0)
614 SDDS_Bomb("invalid -binaryRows syntax");
615 break;
616 case SET_EOF_SEQUENCE:
617 if (s_arg[i_arg].n_items != 2)
618 SDDS_Bomb("invalid -eofSequence syntax");
619 eofSequence = s_arg[i_arg].list[1];
620 break;
621 default:
622 fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]);
623 exit(EXIT_FAILURE);
624 break;
625 }
626 } else {
627 if (input == NULL) {
628 input = s_arg[i_arg].list[0];
629 } else if (output == NULL) {
630 output = s_arg[i_arg].list[0];
631 } else {
632 fprintf(stderr, "too many filenames\n");
633 exit(EXIT_FAILURE);
634 }
635 }
636 }
637
638 processFilenames("plaindata2sdds", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
639
640 if (!columns && !parameters)
641 SDDS_Bomb("you must specify one of the -column or the -parameter options");
642
643 if (skiplines && inputBinary)
644 SDDS_Bomb("-skiplines does not work with binary input files");
645
646 if (!input) {
647 if (inputBinary) {
648#if defined(_WIN32)
649 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
650 fprintf(stderr, "error: unable to set stdin to binary mode\n");
651 exit(EXIT_FAILURE);
652 }
653#endif
654 }
655 fileID = stdin;
656 } else {
657 if (!fexists(input)) {
658 fprintf(stderr, "input file not found\n");
659 exit(EXIT_FAILURE);
660 }
661 if (inputBinary) {
662 fileID = fopen(input, "rb");
663 } else {
664 fileID = fopen(input, "r");
665 }
666 }
667 if (fileID == NULL) {
668 fprintf(stderr, "unable to open input file for reading\n");
669 exit(EXIT_FAILURE);
670 }
671
672 if (!SDDS_InitializeOutput(&SDDS_dataset, binary ? SDDS_BINARY : SDDS_ASCII, 1, NULL, NULL, output))
673 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
674
675 SDDS_dataset.layout.data_mode.column_major = columnMajorOrder;
676
677 if (parameters) {
678 parameterIndex = tmalloc(sizeof(*parameterIndex) * parameters);
679 }
680 if (columns) {
681 columnIndex = tmalloc(sizeof(*columnIndex) * columns);
682 }
683
684 for (i = 0; i < parameters; i++) {
685 if ((parameterIndex[i] = SDDS_DefineParameter(&SDDS_dataset, parameterValues[i].name, parameterValues[i].symbol, parameterValues[i].units, parameterValues[i].description, NULL, parameterValues[i].type, 0)) < 0) {
686 snprintf(s, sizeof(s), "Problem defining parameter %s.", parameterValues[i].name);
687 SDDS_SetError(s);
688 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
689 }
690 }
691 for (i = 0; i < columns; i++) {
692 if (columnValues[i].skip)
693 continue;
694 if ((columnIndex[i] = SDDS_DefineColumn(&SDDS_dataset, columnValues[i].name, columnValues[i].symbol, columnValues[i].units, columnValues[i].description, NULL, columnValues[i].type, 0)) < 0) {
695 snprintf(s, sizeof(s), "Problem defining column %s.", columnValues[i].name);
696 SDDS_SetError(s);
697 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
698 }
699 }
700
701 if (!SDDS_WriteLayout(&SDDS_dataset))
702 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
703
704 if (!SDDS_StartPage(&SDDS_dataset, initRows))
705 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
706
707 row = par = col = page = 0;
708 while (inputBinary) {
709 row = par = col = 0;
710
711 if (binaryRows == -1) {
712 if (fread(&rows, sizeof(rows), 1, fileID) != 1) {
713 if (page == 0) {
714 SDDS_SetError("Unable to read number of rows");
715 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
716 } else {
717 if (!SDDS_Terminate(&SDDS_dataset))
718 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
719 for (k = 0; k < columns; k++) {
720 if (columnValues[k].type == SDDS_STRING) {
721 SDDS_FreeStringArray(columnValues[k].stringValues, columnValues[k].elements);
722 } else {
723 free(columnValues[k].values);
724 }
725 }
726 return EXIT_SUCCESS;
727 }
728 }
729 } else {
730 if (page == 0) {
731 rows = binaryRows;
732 } else {
733 if (!SDDS_Terminate(&SDDS_dataset))
734 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
735 for (k = 0; k < columns; k++) {
736 if (columnValues[k].type == SDDS_STRING) {
737 SDDS_FreeStringArray(columnValues[k].stringValues, columnValues[k].elements);
738 } else {
739 free(columnValues[k].values);
740 }
741 }
742 return EXIT_SUCCESS;
743 }
744 }
745 page++;
746
747 for (par = 0; par < parameters; par++) {
748 switch (parameterValues[par].type) {
749 case SDDS_SHORT:
750 if (fread(&shortValue, sizeof(shortValue), 1, fileID) != 1) {
751 SDDS_SetError("Unable to read short parameter");
752 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
753 }
754 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, shortValue, -1))
755 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
756 break;
757 case SDDS_LONG:
758 if (fread(&longValue, sizeof(longValue), 1, fileID) != 1) {
759 SDDS_SetError("Unable to read long parameter");
760 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
761 }
762 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, longValue, -1))
763 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
764 break;
765 case SDDS_LONG64:
766 if (fread(&long64Value, sizeof(long64Value), 1, fileID) != 1) {
767 SDDS_SetError("Unable to read long64 parameter");
768 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
769 }
770 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, long64Value, -1))
771 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
772 break;
773 case SDDS_FLOAT:
774 if (fread(&floatValue, sizeof(floatValue), 1, fileID) != 1) {
775 SDDS_SetError("Unable to read float parameter");
776 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
777 }
778 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, floatValue, -1))
779 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
780 break;
781 case SDDS_DOUBLE:
782 if (fread(&doubleValue, sizeof(doubleValue), 1, fileID) != 1) {
783 SDDS_SetError("Unable to read double parameter");
784 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
785 }
786 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, doubleValue, -1))
787 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
788 break;
789 case SDDS_LONGDOUBLE:
790 if (fread(&ldoubleValue, sizeof(ldoubleValue), 1, fileID) != 1) {
791 SDDS_SetError("Unable to read long double parameter");
792 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
793 }
794 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, ldoubleValue, -1))
795 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
796 break;
797 case SDDS_STRING:
798 if (fread(&size, sizeof(size), 1, fileID) != 1) {
799 SDDS_SetError("Unable to read string parameter");
800 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
801 }
802 if (size > SDDS_MAXLINE - 1)
803 SDDS_Bomb("String is too long");
804 if (size > 0) {
805 if (fread(&stringValue, size, 1, fileID) != 1) {
806 SDDS_SetError("Unable to read string parameter");
807 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
808 }
809 stringValue[size] = '\0';
810 } else {
811 strcpy(stringValue, "");
812 }
813 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, stringValue, -1))
814 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
815 break;
816 case SDDS_CHARACTER:
817 if (fread(&characterValue, sizeof(characterValue), 1, fileID) != 1) {
818 SDDS_SetError("Unable to read character parameter");
819 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
820 }
821 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, characterValue, -1))
822 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
823 break;
824 }
825 }
826 for (i = 0; i < columns; i++) {
827 if (columnValues[i].skip)
828 continue;
829 if (columnValues[i].type == SDDS_STRING) {
830 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
831 } else {
832 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
833 }
834 columnValues[i].elements = rows;
835 }
836 if (columnOrder) {
837 for (col = 0; col < columns; col++) {
838 switch (columnValues[col].type) {
839 case SDDS_SHORT:
840 for (i = 0; i < rows; i++) {
841 if (fread((short *)(columnValues[col].values) + i, sizeof(short), 1, fileID) != 1) {
842 SDDS_SetError("Unable to read short column");
843 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
844 }
845 }
846 break;
847 case SDDS_LONG:
848 for (i = 0; i < rows; i++) {
849 if (fread((int32_t *)(columnValues[col].values) + i, sizeof(int32_t), 1, fileID) != 1) {
850 SDDS_SetError("Unable to read long column");
851 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
852 }
853 }
854 break;
855 case SDDS_LONG64:
856 for (i = 0; i < rows; i++) {
857 if (fread((int64_t *)(columnValues[col].values) + i, sizeof(int64_t), 1, fileID) != 1) {
858 SDDS_SetError("Unable to read long64 column");
859 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
860 }
861 }
862 break;
863 case SDDS_FLOAT:
864 for (i = 0; i < rows; i++) {
865 if (fread((float *)(columnValues[col].values) + i, sizeof(float), 1, fileID) != 1) {
866 SDDS_SetError("Unable to read float column");
867 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
868 }
869 }
870 break;
871 case SDDS_DOUBLE:
872 for (i = 0; i < rows; i++) {
873 if (fread((double *)(columnValues[col].values) + i, sizeof(double), 1, fileID) != 1) {
874 SDDS_SetError("Unable to read double column");
875 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
876 }
877 }
878 break;
879 case SDDS_LONGDOUBLE:
880 for (i = 0; i < rows; i++) {
881 if (fread((long double *)(columnValues[col].values) + i, sizeof(long double), 1, fileID) != 1) {
882 SDDS_SetError("Unable to read long double column");
883 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
884 }
885 }
886 break;
887 case SDDS_STRING:
888 for (i = 0; i < rows; i++) {
889 if (fread(&size, sizeof(size), 1, fileID) != 1) {
890 SDDS_SetError("Unable to read string column");
891 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
892 }
893 if (size > SDDS_MAXLINE - 1)
894 SDDS_Bomb("String is too long");
895 columnValues[col].stringValues[i] = malloc(size + 1);
896 if (!columnValues[col].stringValues[i]) {
897 SDDS_Bomb("Memory allocation failed for string column");
898 }
899 if (size > 0) {
900 if (fread(columnValues[col].stringValues[i], size, 1, fileID) != 1) {
901 SDDS_SetError("Unable to read string column");
902 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
903 }
904 columnValues[col].stringValues[i][size] = '\0';
905 } else {
906 strcpy(columnValues[col].stringValues[i], "");
907 }
908 }
909 break;
910 case SDDS_CHARACTER:
911 for (i = 0; i < rows; i++) {
912 if (fread((char *)(columnValues[col].values) + i, sizeof(char), 1, fileID) != 1) {
913 SDDS_SetError("Unable to read character column");
914 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
915 }
916 }
917 break;
918 }
919 }
920 } else {
921 for (i = 0; i < rows; i++) {
922 for (col = 0; col < columns; col++) {
923 switch (columnValues[col].type) {
924 case SDDS_SHORT:
925 if (fread((short *)(columnValues[col].values) + i, sizeof(short), 1, fileID) != 1) {
926 SDDS_SetError("Unable to read short column");
927 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
928 }
929 break;
930 case SDDS_LONG:
931 if (fread((int32_t *)(columnValues[col].values) + i, sizeof(int32_t), 1, fileID) != 1) {
932 SDDS_SetError("Unable to read long column");
933 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
934 }
935 break;
936 case SDDS_LONG64:
937 if (fread((int64_t *)(columnValues[col].values) + i, sizeof(int64_t), 1, fileID) != 1) {
938 SDDS_SetError("Unable to read long64 column");
939 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
940 }
941 break;
942 case SDDS_FLOAT:
943 if (fread((float *)(columnValues[col].values) + i, sizeof(float), 1, fileID) != 1) {
944 SDDS_SetError("Unable to read float column");
945 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
946 }
947 break;
948 case SDDS_DOUBLE:
949 if (fread((double *)(columnValues[col].values) + i, sizeof(double), 1, fileID) != 1) {
950 SDDS_SetError("Unable to read double column");
951 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
952 }
953 break;
954 case SDDS_LONGDOUBLE:
955 if (fread((long double *)(columnValues[col].values) + i, sizeof(long double), 1, fileID) != 1) {
956 SDDS_SetError("Unable to read long double column");
957 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
958 }
959 break;
960 case SDDS_STRING:
961 if (fread(&size, sizeof(size), 1, fileID) != 1) {
962 SDDS_SetError("Unable to read string column");
963 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
964 }
965 if (size > SDDS_MAXLINE - 1)
966 SDDS_Bomb("String is too long");
967 columnValues[col].stringValues[i] = malloc(size + 1);
968 if (!columnValues[col].stringValues[i]) {
969 SDDS_Bomb("Memory allocation failed for string column");
970 }
971 if (size > 0) {
972 if (fread(columnValues[col].stringValues[i], size, 1, fileID) != 1) {
973 SDDS_SetError("Unable to read string column");
974 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
975 }
976 columnValues[col].stringValues[i][size] = '\0';
977 } else {
978 strcpy(columnValues[col].stringValues[i], "");
979 }
980 break;
981 case SDDS_CHARACTER:
982 if (fread((char *)(columnValues[col].values) + i, sizeof(char), 1, fileID) != 1) {
983 SDDS_SetError("Unable to read character column");
984 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
985 }
986 break;
987 }
988 }
989 }
990 }
991 if (rows > maxRows) {
992 if (!SDDS_LengthenTable(&SDDS_dataset, rows - maxRows)) {
993 SDDS_SetError("Unable to lengthen table");
994 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
995 }
996 maxRows = rows;
997 }
998 j = n;
999 for (i = 0; i < columns; i++) {
1000 if (columnValues[i].skip)
1001 continue;
1002 if (columnValues[i].type == SDDS_STRING) {
1003 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n);
1004 } else {
1005 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n);
1006 }
1007 n++;
1008 }
1009
1010 if (!SDDS_WritePage(&SDDS_dataset)) {
1011 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1012 }
1013 maxRows = 10000;
1014 if (!SDDS_StartPage(&SDDS_dataset, initRows))
1015 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1016 }
1017
1018 row = par = col = n = 0;
1019 rows = -1;
1020 ptr = NULL;
1021 ptr = SDDS_Malloc(sizeof(*ptr) * (ptrSize = 2048));
1022 ptr2 = SDDS_Malloc(sizeof(*ptr2) * (ptrSize = 2048));
1023 if (!ptr2) {
1024 SDDS_Bomb("Memory allocation failed for ptr2");
1025 }
1026 ptr[0] = 0;
1027 stop = 0;
1028 while (!stop) {
1029 if (readline) {
1030 while (skiplines > 0) {
1031 if (!fgets(ptr, ptrSize, fileID))
1032 break;
1033 skiplines--;
1034 }
1035 if (!fgetsSkipCommentsResize(NULL, &ptr, &ptrSize, fileID, '!'))
1036 break;
1037 commentFound = 0;
1038 if (checkComment) {
1039 for (i = 0; i < strlen(commentCharacters); i++) {
1040 if (ptr[0] == commentCharacters[i]) {
1041 commentFound = 1;
1042 break;
1043 }
1044 }
1045 }
1046 if (commentFound == 1) {
1047 continue;
1048 }
1049 if (ptr[strlen(ptr) - 1] == '\n')
1050 ptr[strlen(ptr) - 1] = '\0';
1051 strcpy(temp, ptr);
1052 /*skip empty lines */
1053 if (getToken(temp, data, 10240, separator, whitespace) < 0)
1054 continue;
1055 } else {
1056 readline = 1;
1057 }
1058 if (eofSequence && strncmp(eofSequence, ptr, strlen(eofSequence)) == 0) {
1059 stop = 1;
1060 continue;
1061 }
1062 if (par < parameters) {
1063 switch (parameterValues[par].type) {
1064 case SDDS_SHORT:
1065 if (sscanf(ptr, "%hd", &shortValue) != 1) {
1066 SDDS_SetError("Invalid short parameter");
1067 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1068 }
1069 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, shortValue, -1))
1070 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1071 break;
1072 case SDDS_LONG:
1073 if (sscanf(ptr, "%" SCNd32, &longValue) != 1) {
1074 SDDS_SetError("Invalid long parameter");
1075 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1076 }
1077 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, longValue, -1))
1078 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1079 break;
1080 case SDDS_LONG64:
1081 if (sscanf(ptr, "%" SCNd64, &long64Value) != 1) {
1082 SDDS_SetError("Invalid long64 parameter");
1083 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1084 }
1085 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, long64Value, -1))
1086 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1087 break;
1088 case SDDS_FLOAT:
1089 ConvertDNotationToENotation(ptr);
1090 if (sscanf(ptr, "%f", &floatValue) != 1) {
1091 SDDS_SetError("Invalid float parameter");
1092 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1093 }
1094 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, floatValue, -1))
1095 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1096 break;
1097 case SDDS_DOUBLE:
1098 ConvertDNotationToENotation(ptr);
1099 if (sscanf(ptr, "%lf", &doubleValue) != 1) {
1100 SDDS_SetError("Invalid double parameter");
1101 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1102 }
1103 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, doubleValue, -1))
1104 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1105 break;
1106 case SDDS_LONGDOUBLE:
1107 ConvertDNotationToENotation(ptr);
1108 if (sscanf(ptr, "%Lf", &ldoubleValue) != 1) {
1109 SDDS_SetError("Invalid long double parameter");
1110 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1111 }
1112 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, ldoubleValue, -1))
1113 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1114 break;
1115 case SDDS_STRING:
1116 SDDS_GetToken(ptr, stringValue, SDDS_MAXLINE);
1117 SDDS_InterpretEscapes(stringValue);
1118 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, stringValue, -1))
1119 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1120 break;
1121 case SDDS_CHARACTER:
1123 characterValue = ptr[0];
1124 if (!SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, characterValue, -1))
1125 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1126 break;
1127 }
1128 par++;
1129 } else if ((rows == -1) && (!noRowCount)) {
1130 if (sscanf(ptr, "%" SCNd32, &rows) != 1) {
1131 SDDS_SetError("Invalid row count");
1132 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1133 }
1134 } else if ((columns > 0) && ((row < rows) || (noRowCount))) {
1135
1136 if (columnOrder) {
1137
1138 if (noRowCount) {
1139 cp_str(&ptr2, ptr);
1140 rows = 0;
1141 while (getToken(ptr2, data, 10240, separator, whitespace) >= 0) {
1142 rows++;
1143 }
1144 free(ptr2);
1145 ptr2 = NULL;
1146 }
1147
1148 if (rows > columnValues[col].elements) {
1149 if (columnValues[col].type == SDDS_STRING) {
1150 columnValues[col].stringValues = AllocateColumnStringData(columnValues[col].stringValues, rows, columnValues[col].elements);
1151 } else {
1152 columnValues[col].values = AllocateColumnData(columnValues[col].type, columnValues[col].values, rows);
1153 }
1154 columnValues[col].elements = rows;
1155 }
1156 switch (columnValues[col].type) {
1157 case SDDS_SHORT:
1158 for (row = 0; row < rows; row++) {
1159 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1160 SDDS_SetError("Invalid short column element");
1161 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1162 }
1163 if (sscanf(data, "%hd", ((short *)(columnValues[col].values) + row)) != 1) {
1164 SDDS_SetError("Invalid short column element");
1165 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1166 }
1167 }
1168 break;
1169 case SDDS_LONG:
1170 for (row = 0; row < rows; row++) {
1171 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1172 SDDS_SetError("Invalid long column element");
1173 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1174 }
1175 if (sscanf(data, "%" SCNd32, ((int32_t *)(columnValues[col].values) + row)) != 1) {
1176 SDDS_SetError("Invalid long column element");
1177 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1178 }
1179 }
1180 break;
1181 case SDDS_LONG64:
1182 for (row = 0; row < rows; row++) {
1183 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1184 SDDS_SetError("Invalid long64 column element");
1185 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1186 }
1187 if (sscanf(data, "%" SCNd64, ((int64_t *)(columnValues[col].values) + row)) != 1) {
1188 SDDS_SetError("Invalid long64 column element");
1189 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1190 }
1191 }
1192 break;
1193 case SDDS_FLOAT:
1194 for (row = 0; row < rows; row++) {
1195 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1196 SDDS_SetError("Invalid float column element");
1197 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1198 }
1199 ConvertDNotationToENotation(data);
1200 if (sscanf(data, "%f", ((float *)(columnValues[col].values) + row)) != 1) {
1201 SDDS_SetError("Invalid float column element");
1202 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1203 }
1204 }
1205 break;
1206 case SDDS_DOUBLE:
1207 for (row = 0; row < rows; row++) {
1208 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1209 SDDS_SetError("Invalid double column element");
1210 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1211 }
1212 ConvertDNotationToENotation(data);
1213 if (sscanf(data, "%lf", ((double *)(columnValues[col].values) + row)) != 1) {
1214 SDDS_SetError("Invalid double column element");
1215 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1216 }
1217 }
1218 break;
1219 case SDDS_LONGDOUBLE:
1220 for (row = 0; row < rows; row++) {
1221 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1222 SDDS_SetError("Invalid long double column element");
1223 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1224 }
1225 ConvertDNotationToENotation(data);
1226 if (sscanf(data, "%Lf", ((long double *)(columnValues[col].values) + row)) != 1) {
1227 SDDS_SetError("Invalid long double column element");
1228 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1229 }
1230 }
1231 break;
1232 case SDDS_STRING:
1233 for (row = 0; row < rows; row++) {
1234 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1235 SDDS_SetError("Invalid string column element");
1236 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1237 }
1239 columnValues[col].stringValues[row] = malloc(strlen(data) + 1);
1240 if (!columnValues[col].stringValues[row]) {
1241 SDDS_Bomb("Memory allocation failed for string column element");
1242 }
1243 strcpy(columnValues[col].stringValues[row], data);
1244 }
1245 break;
1246 case SDDS_CHARACTER:
1247 for (row = 0; row < rows; row++) {
1248 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1249 SDDS_SetError("Invalid character column element");
1250 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1251 }
1253 *((char *)(columnValues[col].values) + row) = data[0];
1254 }
1255 break;
1256 }
1257 if (rows > maxRows) {
1258 if (!SDDS_LengthenTable(&SDDS_dataset, rows - maxRows)) {
1259 SDDS_SetError("Unable to lengthen table");
1260 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1261 }
1262 maxRows = rows;
1263 }
1264 if (columnValues[col].skip == 0) {
1265 if (columnValues[col].type == SDDS_STRING) {
1266 SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].stringValues, rows, col);
1267 } else {
1268 SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].values, rows, col);
1269 }
1270 n++;
1271 }
1272 col++;
1273 row = 0;
1274 } else {
1275 if (noRowCount) {
1276 if (row == 0) {
1277 rows = 3;
1278 } else if (row == rows - 1) {
1279 rows = rows + 3;
1280 for (i = 0; i < columns; i++) {
1281 if (rows > columnValues[i].elements) {
1282 if (columnValues[i].type == SDDS_STRING) {
1283 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
1284 } else {
1285 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
1286 }
1287 }
1288 columnValues[i].elements = rows;
1289 }
1290 }
1291 }
1292 if (row == 0)
1293 for (i = 0; i < columns; i++) {
1294 if (rows > columnValues[i].elements) {
1295 if (columnValues[i].type == SDDS_STRING) {
1296 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
1297 } else {
1298 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
1299 }
1300 }
1301 columnValues[i].elements = rows;
1302 }
1303
1304 if (noRowCount) {
1305 cp_str(&ptr2, ptr);
1306 i = 0;
1307 while (getToken(ptr2, data, 10240, separator, whitespace) >= 0) {
1308 i++;
1309 }
1310 free(ptr2);
1311 ptr2 = NULL;
1312 if ((i != columns) && (parameters > 0 && i == 1)) {
1313 if (row > 0) {
1314 if (row > maxRows) {
1315 if (!SDDS_LengthenTable(&SDDS_dataset, row - maxRows)) {
1316 SDDS_SetError("Unable to lengthen table");
1317 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1318 }
1319 maxRows = row;
1320 }
1321 n = 0;
1322 for (j = 0; j < columns; j++) {
1323 if (columnValues[j].skip)
1324 continue;
1325 if (columnValues[j].type == SDDS_STRING) {
1326 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n);
1327 } else {
1328 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n);
1329 }
1330 n++;
1331 }
1332 if (!SDDS_WritePage(&SDDS_dataset))
1333 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1334 maxRows = 10000;
1335 if (!SDDS_StartPage(&SDDS_dataset, initRows))
1336 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1337 row = par = col = 0;
1338 rows = -1;
1339 }
1340 readline = 0;
1341 continue;
1342 }
1343 }
1344
1345 for (i = 0; i < columns; i++) {
1346 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1347 if (!fillin) {
1348 fprintf(stderr, "Problem with column data: %s\n", data);
1349 SDDS_SetError("Invalid column element");
1350 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1351 } else {
1352 switch (columnValues[i].type) {
1353 case SDDS_SHORT:
1354 case SDDS_LONG:
1355 case SDDS_LONG64:
1356 case SDDS_FLOAT:
1357 case SDDS_DOUBLE:
1358 case SDDS_LONGDOUBLE:
1359 snprintf(data, sizeof(data), "0");
1360 break;
1361 case SDDS_STRING:
1362 case SDDS_CHARACTER:
1363 data[0] = '\0';
1364 break;
1365 }
1366 }
1367 }
1368
1369 switch (columnValues[i].type) {
1370 case SDDS_SHORT:
1371 if (sscanf(data, "%hd", ((short *)(columnValues[i].values) + row)) != 1) {
1372 if (recover) {
1373 abort_flag = 1;
1374 row--;
1375 } else {
1376 SDDS_SetError("Invalid short column element");
1377 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1378 }
1379 }
1380 break;
1381 case SDDS_LONG:
1382 if (sscanf(data, "%" SCNd32, ((int32_t *)(columnValues[i].values) + row)) != 1) {
1383 if (recover) {
1384 abort_flag = 1;
1385 row--;
1386 } else {
1387 SDDS_SetError("Invalid long column element");
1388 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1389 }
1390 }
1391 break;
1392 case SDDS_LONG64:
1393 if (sscanf(data, "%" SCNd64, ((int64_t *)(columnValues[i].values) + row)) != 1) {
1394 if (recover) {
1395 abort_flag = 1;
1396 row--;
1397 } else {
1398 SDDS_SetError("Invalid long64 column element");
1399 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1400 }
1401 }
1402 break;
1403 case SDDS_FLOAT:
1404 ConvertDNotationToENotation(data);
1405 if (sscanf(data, "%f", ((float *)(columnValues[i].values) + row)) != 1) {
1406 if (recover) {
1407 abort_flag = 1;
1408 row--;
1409 } else {
1410 SDDS_SetError("Invalid float column element");
1411 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1412 }
1413 }
1414 break;
1415 case SDDS_DOUBLE:
1416 ConvertDNotationToENotation(data);
1417 if (sscanf(data, "%lf", ((double *)(columnValues[i].values) + row)) != 1) {
1418 if (recover) {
1419 abort_flag = 1;
1420 row--;
1421 } else {
1422 SDDS_SetError("Invalid double column element");
1423 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1424 }
1425 }
1426 break;
1427 case SDDS_LONGDOUBLE:
1428 ConvertDNotationToENotation(data);
1429 if (sscanf(data, "%Lf", ((long double *)(columnValues[i].values) + row)) != 1) {
1430 if (recover) {
1431 abort_flag = 1;
1432 row--;
1433 } else {
1434 SDDS_SetError("Invalid long double column element");
1435 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1436 }
1437 }
1438 break;
1439 case SDDS_STRING:
1441 columnValues[i].stringValues[row] = malloc(strlen(data) + 1);
1442 if (!columnValues[i].stringValues[row]) {
1443 SDDS_Bomb("Memory allocation failed for string column element");
1444 }
1445 strcpy(columnValues[i].stringValues[row], data);
1446 break;
1447 case SDDS_CHARACTER:
1449 *((char *)(columnValues[i].values) + row) = data[0];
1450 break;
1451 }
1452
1453 if (recover && abort_flag) {
1454 abort_flag = 0;
1455 break;
1456 }
1457 }
1458 row++;
1459 if ((row == rows) && (!noRowCount)) {
1460 if (rows > maxRows) {
1461 if (!SDDS_LengthenTable(&SDDS_dataset, rows - maxRows)) {
1462 SDDS_SetError("Unable to lengthen table");
1463 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1464 }
1465 maxRows = rows;
1466 }
1467 n = 0;
1468 for (i = 0; i < columns; i++) {
1469 if (columnValues[i].skip)
1470 continue;
1471 if (columnValues[i].type == SDDS_STRING) {
1472 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n);
1473 } else {
1474 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n);
1475 }
1476 n++;
1477 }
1478 }
1479 }
1480 }
1481 if ((par == parameters) &&
1482 (((!noRowCount) &&
1483 (rows != -1)) ||
1484 (noRowCount)) &&
1485 (((columnOrder) &&
1486 (col == columns)) ||
1487 ((columns > 0) &&
1488 (row == rows)) ||
1489 (columns == 0))) {
1490 if (!SDDS_WritePage(&SDDS_dataset)) {
1491 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1492 }
1493 maxRows = 10000;
1494 if (!SDDS_StartPage(&SDDS_dataset, initRows))
1495 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1496
1497 row = par = col = 0;
1498 rows = -1;
1499 }
1500 ptr[0] = '\0';
1501 }
1502
1503 if (noRowCount) {
1504 if (row > 0) {
1505 if (row > maxRows) {
1506 if (!SDDS_LengthenTable(&SDDS_dataset, row - maxRows)) {
1507 SDDS_SetError("Unable to lengthen table");
1508 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1509 }
1510 maxRows = row;
1511 }
1512 n = 0;
1513 for (j = 0; j < columns; j++) {
1514 if (columnValues[j].skip)
1515 continue;
1516 if (columnValues[j].type == SDDS_STRING) {
1517 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n);
1518 } else {
1519 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n);
1520 }
1521 n++;
1522 }
1523 if (!SDDS_WritePage(&SDDS_dataset)) {
1524 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1525 }
1526 maxRows = 10000;
1527 }
1528 }
1529 for (i = 0; i < columns; i++) {
1530 if (columnValues[i].type == SDDS_STRING) {
1531 for (j = 0; j < columnValues[i].elements; j++) {
1532 free(columnValues[i].stringValues[j]);
1533 }
1534 free(columnValues[i].stringValues);
1535 } else {
1536 free(columnValues[i].values);
1537 }
1538 if (columnValues[i].name)
1539 free(columnValues[i].name);
1540 if (columnValues[i].units)
1541 free(columnValues[i].units);
1542 if (columnValues[i].description)
1543 free(columnValues[i].description);
1544 if (columnValues[i].symbol)
1545 free(columnValues[i].symbol);
1546 }
1547 for (i = 0; i < parameters; i++) {
1548 free(parameterValues[i].name);
1549 if (parameterValues[i].units)
1550 free(parameterValues[i].units);
1551 if (parameterValues[i].description)
1552 free(parameterValues[i].description);
1553 if (parameterValues[i].symbol)
1554 free(parameterValues[i].symbol);
1555 }
1556 if (columnValues)
1557 free(columnValues);
1558 if (parameterValues)
1559 free(parameterValues);
1560 if (columnIndex)
1561 free(columnIndex);
1562 if (parameterIndex)
1563 free(parameterIndex);
1564 if (ptr)
1565 free(ptr);
1566 if (ptr2)
1567 free(ptr2);
1568 if (!SDDS_Terminate(&SDDS_dataset))
1569 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1570 free_scanargs(&s_arg, argc);
1571 return EXIT_SUCCESS;
1572}
1573
1574void SetColumnData(long type, SDDS_DATASET *dataset, void *values, int32_t rows, int32_t index) {
1575 switch (type) {
1576 case SDDS_SHORT:
1577 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (short *)values, rows, index, NULL))
1578 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1579 break;
1580 case SDDS_LONG:
1581 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (int32_t *)values, rows, index, NULL))
1582 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1583 break;
1584 case SDDS_LONG64:
1585 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (int64_t *)values, rows, index, NULL))
1586 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1587 break;
1588 case SDDS_FLOAT:
1589 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (float *)values, rows, index, NULL))
1590 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1591 break;
1592 case SDDS_DOUBLE:
1593 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (double *)values, rows, index, NULL))
1594 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1595 break;
1596 case SDDS_LONGDOUBLE:
1597 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (long double *)values, rows, index, NULL))
1598 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1599 break;
1600 case SDDS_STRING:
1601 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (char **)values, rows, index, NULL))
1602 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1603 break;
1604 case SDDS_CHARACTER:
1605 if (!SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (char *)values, rows, index, NULL))
1606 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1607 break;
1608 }
1609}
1610
1611void *AllocateColumnData(long type, void *values, int32_t rows) {
1612 switch (type) {
1613 case SDDS_SHORT:
1614 return SDDS_Realloc(values, rows * sizeof(short));
1615 case SDDS_LONG:
1616 return SDDS_Realloc(values, rows * sizeof(int32_t));
1617 case SDDS_LONG64:
1618 return SDDS_Realloc(values, rows * sizeof(int64_t));
1619 case SDDS_FLOAT:
1620 return SDDS_Realloc(values, rows * sizeof(float));
1621 case SDDS_DOUBLE:
1622 return SDDS_Realloc(values, rows * sizeof(double));
1623 case SDDS_LONGDOUBLE:
1624 return SDDS_Realloc(values, rows * sizeof(long double));
1625 case SDDS_CHARACTER:
1626 return SDDS_Realloc(values, rows * sizeof(char));
1627 }
1628 return values;
1629}
1630
1631char **AllocateColumnStringData(char **values, int32_t rows, int32_t previous_rows) {
1632 long i;
1633 values = SDDS_Realloc(values, rows * sizeof(char *));
1634 for (i = previous_rows; i < rows; i++) {
1635 values[i] = NULL;
1636 /* values[i] = malloc(SDDS_MAXLINE); */
1637 }
1638 return values;
1639}
1640
1641long getToken(char *s, char *buffer, long buflen, char separator, long whitespace) {
1642 char *ptr0, *ptr1, *escptr;
1643 long n;
1644 /* save the pointer to the head of the string */
1645 ptr0 = s;
1646
1647 /* skip leading white-space */
1648 while (isspace(*s))
1649 s++;
1650 if (*s == 0)
1651 return (-1);
1652 ptr1 = s;
1653
1654 if (*s == '"') {
1655 /* if quoted string, skip to next quotation mark */
1656 ptr1 = s + 1; /* beginning of actual token */
1657 do {
1658 s++;
1659 escptr = NULL;
1660 if (*s == '\\' && *(s + 1) == '\\') {
1661 /* skip and remember literal \ (indicated by \\ in the string) */
1662 escptr = s + 1;
1663 s += 2;
1664 }
1665 } while (*s && (*s != '"' || (*(s - 1) == '\\' && (s - 1) != escptr)));
1666 /* replace trailing quotation mark with a space */
1667 if (*s && *s == '"')
1668 *s = ' ';
1669 n = (long)(s - ptr1);
1670 if (whitespace) {
1671 while (*s && (!isspace(*s))) {
1672 s++;
1673 }
1674 if (*s && (isspace(*s)))
1675 *s = ' ';
1676 } else {
1677 while (*s && (*s != separator)) {
1678 s++;
1679 }
1680 if (*s && (*s == separator))
1681 *s = ' ';
1682 }
1683 } else {
1684 /* skip to first separator following token */
1685 if (whitespace) {
1686 do {
1687 s++;
1688 /* imbedded quotation marks are handled here */
1689 if (*s == '"' && *(s - 1) != '\\') {
1690 while (*++s && !(*s == '"' && *(s - 1) != '\\'))
1691 ;
1692 }
1693 } while (*s && (!isspace(*s)));
1694 if (*s && (isspace(*s)))
1695 *s = ' ';
1696 } else {
1697 if (*s != separator) {
1698 do {
1699 s++;
1700 /* imbedded quotation marks are handled here */
1701 if (*s == '"' && *(s - 1) != '\\') {
1702 while (*++s && !(*s == '"' && *(s - 1) != '\\'))
1703 ;
1704 }
1705 } while (*s && (*s != separator));
1706 }
1707 if (*s && (*s == separator))
1708 *s = ' ';
1709 }
1710 n = (long)(s - ptr1);
1711 }
1712 if (n >= buflen)
1713 return (-1);
1714 strncpy(buffer, ptr1, n);
1715 buffer[n] = '\0';
1716
1717 /* update the original string to delete the token */
1718 strcpy_ss(ptr0, s);
1719
1720 /* return the string length */
1721 return (n);
1722}
1723
1724/* Description: Converts Fortran D notation to C++ e notation */
1725void ConvertDNotationToENotation(char *line) {
1726 char *ptr = line;
1727
1728 while (*ptr && (ptr = strstr(ptr, "D+"))) {
1729 *ptr = 'e';
1730 ptr++;
1731 *ptr = '+';
1732 ptr++;
1733 }
1734
1735 ptr = line;
1736 while (*ptr && (ptr = strstr(ptr, "D-"))) {
1737 *ptr = 'e';
1738 ptr++;
1739 *ptr = '-';
1740 ptr++;
1741 }
1742}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
void SDDS_InterpretEscapes(char *s)
Interprets and converts escape sequences in a string.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
Definition SDDS_utils.c:379
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_GetToken(char *s, char *buffer, int32_t buflen)
Extracts the next token from a string, handling quoted substrings and escape characters.
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
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
Definition SDDS_utils.c:677
char * fgetsSkipCommentsResize(SDDS_DATASET *SDDS_dataset, char **s, int32_t *slen, FILE *fp, char skip_char)
Reads a line from a file with dynamic buffer resizing while skipping comment lines.
#define SDDS_FLOAT
Identifier for the float data type.
Definition SDDStypes.h:43
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_SHORT
Identifier for the signed short integer data type.
Definition SDDStypes.h:73
#define SDDS_CHARACTER
Identifier for the character data type.
Definition SDDStypes.h:91
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
Definition SDDStypes.h:31
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
Definition SDDStypes.h:49
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 fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27
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 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.
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34