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