93# if defined(__BORLANDC__)
94# define _setmode(handle, amode) setmode(handle, amode)
101static char *mode_name[MODES] = {
110#define TYPE_LONGDOUBLE 4
113#define TYPE_CHARACTER 7
115static char *type_name[DATATYPES] = {
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"};
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
163#define SET_NOWARNINGS 7
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
174char *option[N_OPTIONS] = {
175 "inputMode",
"outputMode",
"separator",
"noRowCount",
176 "parameter",
"column",
"pipe",
"nowarnings",
"order",
"fillin",
177 "skiplines",
"skipcolumn",
"commentCharacters",
"majorOrder",
178 "binaryRows",
"eofsequence"};
181#define COLUMN_ORDER 1
183static char *order_names[ORDERS] = {
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";
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);
230void interrupt_handler(
int sig) {
231 fprintf(stderr,
"Segmentation fault. Ensure that your input file matches the -inputMode given.\n");
237int main(
int argc,
char **argv) {
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;
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;
256 long double ldoubleValue;
258 char stringValue[SDDS_MAXLINE];
260 char buffer[124], buffer2[200];
261 long *parameterIndex, *columnIndex;
263 long binary = 0, noRowCount = 0, inputBinary = 0, count = 0;
265 char commentCharacters[20];
266 short checkComment = 0;
268 long parameters = 0, columns = 0;
270 short abort = 0, recover = 1, columnMajorOrder = 0;
272 input = output = NULL;
276 parameterValues = NULL;
278 parameterIndex = columnIndex = NULL;
281 argc =
scanargs(&s_arg, argc, argv);
285 signal(SIGSEGV, interrupt_handler);
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:
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;
304 if (s_arg[i_arg].n_items != 2)
306 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
319 if (s_arg[i_arg].n_items != 2)
321 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
334 if (s_arg[i_arg].n_items != 2)
336 separator = s_arg[i_arg].list[1][0];
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]);
349 if (s_arg[i_arg].n_items != 1)
354 if (s_arg[i_arg].n_items != 2)
356 switch (
match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) {
369 if (s_arg[i_arg].n_items < 3)
373 parameterValues =
trealloc(parameterValues,
sizeof(*parameterValues) * (parameters));
374 SDDS_CopyString(¶meterValues[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)) {
380 parameterValues[parameters - 1].type =
SDDS_SHORT;
383 parameterValues[parameters - 1].type =
SDDS_LONG;
386 parameterValues[parameters - 1].type =
SDDS_LONG64;
389 parameterValues[parameters - 1].type =
SDDS_FLOAT;
391 case TYPE_LONGDOUBLE:
395 parameterValues[parameters - 1].type =
SDDS_DOUBLE;
398 parameterValues[parameters - 1].type =
SDDS_STRING;
407 for (i = 3; i < s_arg[i_arg].n_items; i++) {
408 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
411 switch (
match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) {
415 case HEADER_DESCRIPTION:
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);
435 for (i = 2; i <= count; i++) {
437 parameterValues =
trealloc(parameterValues,
sizeof(*parameterValues) * (parameters));
438 sprintf(buffer2,
"%s%ld", buffer, i);
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(¶meterValues[parameters - 1].units, parameterValues[parameters - 2].units);
446 if (parameterValues[parameters - 2].description)
447 SDDS_CopyString(¶meterValues[parameters - 1].description, parameterValues[parameters - 2].description);
448 if (parameterValues[parameters - 2].symbol)
449 SDDS_CopyString(¶meterValues[parameters - 1].symbol, parameterValues[parameters - 2].symbol);
454 if (s_arg[i_arg].n_items < 3)
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;
468 switch (
match_string(s_arg[i_arg].list[2], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
473 columnValues[columns - 1].type =
SDDS_LONG;
481 case TYPE_LONGDOUBLE:
497 for (i = 3; i < s_arg[i_arg].n_items; i++) {
498 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
501 switch (
match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) {
505 case HEADER_DESCRIPTION:
512 if (sscanf(ptr,
"%ld", &count) != 1 || count <= 0)
514 sprintf(buffer,
"%s", columnValues[columns - 1].name);
515 sprintf(buffer2,
"%s1", buffer);
516 free(columnValues[columns - 1].name);
525 for (i = 2; i <= count; i++) {
527 columnValues =
trealloc(columnValues,
sizeof(*columnValues) * (columns));
528 sprintf(buffer2,
"%s%ld", buffer, i);
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);
547 if (s_arg[i_arg].n_items != 2)
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;
561 switch (
match_string(s_arg[i_arg].list[1], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
566 columnValues[columns - 1].type =
SDDS_LONG;
574 case TYPE_LONGDOUBLE:
592 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
596 if (s_arg[i_arg].n_items != 1)
601 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &skiplines) != 1 || skiplines <= 0)
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)
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];
614 fprintf(stderr,
"error: unknown switch: %s\n", s_arg[i_arg].list[0]);
620 input = s_arg[i_arg].list[0];
621 }
else if (output == NULL) {
622 output = s_arg[i_arg].list[0];
624 fprintf(stderr,
"too many filenames");
630 processFilenames(
"plaindata2sdds", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
632 if (!columns && !parameters)
633 SDDS_Bomb(
"you must specify one of the -column or the -parameter options");
635 if (skiplines && inputBinary)
636 SDDS_Bomb(
"-skiplines does not work with binary input files");
641 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
642 fprintf(stderr,
"error: unable to set stdin to binary mode\n");
650 fprintf(stderr,
"input file not found\n");
654 fileID = fopen(input,
"rb");
656 fileID = fopen(input,
"r");
659 if (fileID == NULL) {
660 fprintf(stderr,
"unable to open input file for reading\n");
667 SDDS_dataset.layout.data_mode.column_major = columnMajorOrder;
670 parameterIndex =
tmalloc(
sizeof(*parameterIndex) * parameters);
673 columnIndex =
tmalloc(
sizeof(*columnIndex) * columns);
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);
683 for (i = 0; i < columns; i++) {
684 if (columnValues[i].skip)
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);
699 row = par = col = page = 0;
700 while (inputBinary) {
703 if (binaryRows == -1) {
704 if (fread(&rows,
sizeof(rows), 1, fileID) != 1) {
711 for (k = 0; k < columns; k++) {
715 free(columnValues[k].values);
727 for (k = 0; k < columns; k++) {
731 free(columnValues[k].values);
739 for (par = 0; par < parameters; par++) {
741 switch (parameterValues[par].type) {
743 if (fread(&shortValue, 2, 1, fileID) != 1) {
747 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, shortValue, -1))
751 if (fread(&longValue, 4, 1, fileID) != 1) {
755 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, longValue, -1))
759 if (fread(&long64Value, 8, 1, fileID) != 1) {
763 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, long64Value, -1))
767 if (fread(&floatValue, 4, 1, fileID) != 1) {
771 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, floatValue, -1))
775 if (fread(&doubleValue, 8, 1, fileID) != 1) {
779 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, doubleValue, -1))
783 if (fread(&ldoubleValue, 16, 1, fileID) != 1) {
787 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, ldoubleValue, -1))
791 if (fread(&size, 4, 1, fileID) != 1) {
795 if (size > SDDS_MAXLINE - 1)
798 if (fread(&stringValue, size, 1, fileID) != 1) {
802 stringValue[size] = 0;
804 strcpy(stringValue,
"");
806 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, stringValue, -1))
810 if (fread(&characterValue, 1, 1, fileID) != 1) {
814 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, characterValue, -1))
819 for (i = 0; i < columns; i++) {
820 if (rows > columnValues[i].elements) {
822 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
824 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
826 columnValues[i].elements = rows;
830 for (col = 0; col < columns; col++) {
831 switch (columnValues[col].type) {
833 for (i = 0; i < rows; i++) {
834 if (fread((
short *)(columnValues[col].values) + i, 2, 1, fileID) != 1) {
841 for (i = 0; i < rows; i++) {
842 if (fread((int32_t *)(columnValues[col].values) + i, 4, 1, fileID) != 1) {
849 for (i = 0; i < rows; i++) {
850 if (fread((int64_t *)(columnValues[col].values) + i, 8, 1, fileID) != 1) {
857 for (i = 0; i < rows; i++) {
858 if (fread((
float *)(columnValues[col].values) + i, 4, 1, fileID) != 1) {
865 for (i = 0; i < rows; i++) {
866 if (fread((
double *)(columnValues[col].values) + i, 8, 1, fileID) != 1) {
873 for (i = 0; i < rows; i++) {
874 if (fread((
long double *)(columnValues[col].values) + i, 16, 1, fileID) != 1) {
881 for (i = 0; i < rows; i++) {
882 if (fread(&size, 4, 1, fileID) != 1) {
886 if (size > SDDS_MAXLINE - 1)
888 columnValues[col].stringValues[i] = malloc(size + 1);
890 if (fread(columnValues[col].stringValues[i], size, 1, fileID) != 1) {
894 columnValues[col].stringValues[i][size] = 0;
896 strcpy(columnValues[col].stringValues[i],
"");
901 for (i = 0; i < rows; i++) {
902 if (fread((
char *)(columnValues[col].values) + i, 1, 1, fileID) != 1) {
911 for (i = 0; i < rows; i++) {
912 for (col = 0; col < columns; col++) {
913 switch (columnValues[col].type) {
915 if (fread((
short *)(columnValues[col].values) + i, 2, 1, fileID) != 1) {
921 if (fread((int32_t *)(columnValues[col].values) + i, 4, 1, fileID) != 1) {
927 if (fread((int64_t *)(columnValues[col].values) + i, 8, 1, fileID) != 1) {
933 if (fread((
float *)(columnValues[col].values) + i, 4, 1, fileID) != 1) {
939 if (fread(((
double *)(columnValues[col].values) + i), 8, 1, fileID) != 1) {
945 if (fread(((
long double *)(columnValues[col].values) + i), 16, 1, fileID) != 1) {
951 if (fread(&size, 4, 1, fileID) != 1) {
955 if (size > SDDS_MAXLINE - 1)
957 columnValues[col].stringValues[i] = malloc(size + 1);
959 if (fread(columnValues[col].stringValues[i], size, 1, fileID) != 1) {
963 columnValues[col].stringValues[i][size] = 0;
965 strcpy(columnValues[col].stringValues[i],
"");
969 if (fread(((
char *)(columnValues[col].values) + i), 1, 1, fileID) != 1) {
978 if (rows > maxRows) {
986 for (i = 0; i < columns; i++) {
987 if (columnValues[i].skip)
990 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n);
992 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n);
1002 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1005 row = par = col = n = 0;
1008 ptr =
SDDS_Malloc(
sizeof(*ptr) * (ptrSize = 2048));
1015 while (skiplines > 0) {
1016 fgets(ptr, ptrSize, fileID);
1023 for (i = 0; i < strlen(commentCharacters); i++) {
1024 if (ptr[0] == commentCharacters[i]) {
1029 if (commentFound == 1) {
1032 if (ptr[strlen(ptr) - 1] ==
'\n')
1033 ptr[strlen(ptr) - 1] = 0;
1036 if (getToken(temp, data, 10240, separator, whitespace) < 0)
1041 if (eofSequence && strncmp(eofSequence, ptr, strlen(eofSequence)) == 0) {
1045 if (par < parameters) {
1046 switch (parameterValues[par].type) {
1048 if (sscanf(ptr,
"%hd", &shortValue) != 1) {
1050 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
1056 if (sscanf(ptr,
"%" SCNd32, &longValue) != 1) {
1058 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
1064 if (sscanf(ptr,
"%" SCNd64, &long64Value) != 1) {
1066 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
1072 ConvertDNotationToENotation(ptr);
1073 if (sscanf(ptr,
"%f", &floatValue) != 1) {
1075 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
1081 ConvertDNotationToENotation(ptr);
1082 if (sscanf(ptr,
"%lf", &doubleValue) != 1) {
1084 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
1090 ConvertDNotationToENotation(ptr);
1091 if (sscanf(ptr,
"%Lf", &ldoubleValue) != 1) {
1093 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
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);
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);
1112 }
else if ((rows == -1) && (!noRowCount)) {
1113 if (sscanf(ptr,
"%" SCNd32
"", &rows) != 1) {
1115 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1117 }
else if ((columns > 0) && ((row < rows) || (noRowCount))) {
1124 while (getToken(ptr2, data, 10240, separator, whitespace) >= 0) {
1130 if (rows > columnValues[col].elements) {
1132 columnValues[col].stringValues = AllocateColumnStringData(columnValues[col].stringValues, rows, columnValues[col].elements);
1134 columnValues[col].values = AllocateColumnData(columnValues[col].type, columnValues[col].values, rows);
1136 columnValues[col].elements = rows;
1138 switch (columnValues[col].type) {
1140 for (row = 0; row < rows; row++) {
1141 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1143 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1145 if (sscanf(data,
"%hd", ((
short *)(columnValues[col].values) + row)) != 1) {
1147 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1152 for (row = 0; row < rows; row++) {
1153 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1155 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1157 if (sscanf(data,
"%" SCNd32, ((int32_t *)(columnValues[col].values) + row)) != 1) {
1159 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1164 for (row = 0; row < rows; row++) {
1165 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1167 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1169 if (sscanf(data,
"%" SCNd64, ((int64_t *)(columnValues[col].values) + row)) != 1) {
1171 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1176 for (row = 0; row < rows; row++) {
1177 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1179 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1181 ConvertDNotationToENotation(data);
1182 if (sscanf(data,
"%f", ((
float *)(columnValues[col].values) + row)) != 1) {
1184 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1189 for (row = 0; row < rows; row++) {
1190 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1192 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1194 ConvertDNotationToENotation(data);
1195 if (sscanf(data,
"%lf", ((
double *)(columnValues[col].values) + row)) != 1) {
1197 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1202 for (row = 0; row < rows; row++) {
1203 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1205 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1207 ConvertDNotationToENotation(data);
1208 if (sscanf(data,
"%Lf", ((
long double *)(columnValues[col].values) + row)) != 1) {
1210 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1215 for (row = 0; row < rows; row++) {
1216 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1218 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1221 columnValues[col].stringValues[row] = malloc(strlen(data) + 1);
1222 strcpy(columnValues[col].stringValues[row], data);
1226 for (row = 0; row < rows; row++) {
1227 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1229 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1232 *((
char *)(columnValues[col].values) + row) = data[0];
1236 if (rows > maxRows) {
1239 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1243 if (columnValues[col].skip == 0) {
1245 SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].stringValues, rows, col);
1247 SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].values, rows, col);
1257 }
else if (row == rows - 1) {
1259 for (i = 0; i < columns; i++) {
1260 if (rows > columnValues[i].elements) {
1262 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
1264 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
1267 columnValues[i].elements = rows;
1272 for (i = 0; i < columns; i++) {
1273 if (rows > columnValues[i].elements) {
1275 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
1277 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
1280 columnValues[i].elements = rows;
1286 while (getToken(ptr2, data, 10240, separator, whitespace) >= 0)
1289 if ((i != columns) && (parameters > 0 && i == 1)) {
1291 if (row > maxRows) {
1294 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1299 for (j = 0; j < columns; j++) {
1300 if (columnValues[j].skip)
1303 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n);
1305 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n);
1310 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1313 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1314 row = par = col = 0;
1322 for (i = 0; i < columns; i++) {
1323 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1325 fprintf(stderr,
"Problem with column data: %s\n", data);
1327 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1329 switch (columnValues[i].type) {
1347 switch (columnValues[i].type) {
1349 if (sscanf(data,
"%hd", ((
short *)(columnValues[i].values) + row)) != 1) {
1355 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1360 if (sscanf(data,
"%" SCNd32, ((int32_t *)(columnValues[i].values) + row)) != 1) {
1366 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1371 if (sscanf(data,
"%" SCNd64, ((int64_t *)(columnValues[i].values) + row)) != 1) {
1377 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1382 ConvertDNotationToENotation(data);
1383 if (sscanf(data,
"%f", ((
float *)(columnValues[i].values) + row)) != 1) {
1389 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1394 ConvertDNotationToENotation(data);
1395 if (sscanf(data,
"%lf", ((
double *)(columnValues[i].values) + row)) != 1) {
1401 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1406 ConvertDNotationToENotation(data);
1407 if (sscanf(data,
"%Lf", ((
long double *)(columnValues[i].values) + row)) != 1) {
1413 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1419 columnValues[i].stringValues[row] = malloc(strlen(data) + 1);
1420 strcpy(columnValues[i].stringValues[row], data);
1424 *((
char *)(columnValues[i].values) + row) = data[0];
1428 if (recover && abort) {
1433 if ((row == rows) && (!noRowCount)) {
1434 if (rows > maxRows) {
1437 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1442 for (i = 0; i < columns; i++) {
1443 if (columnValues[i].skip)
1446 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n);
1448 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n);
1455 if ((par == parameters) &&
1460 (col == columns)) ||
1465 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1469 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1471 row = par = col = 0;
1479 if (row > maxRows) {
1482 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1487 for (j = 0; j < columns; j++) {
1488 if (columnValues[j].skip)
1491 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n);
1493 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n);
1498 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1503 for (i = 0; i < columns; i++) {
1505 for (j = 0; j < columnValues[i].elements; j++) {
1506 free(columnValues[i].stringValues[j]);
1508 free(columnValues[i].stringValues);
1510 free(columnValues[i].values);
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);
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);
1532 if (parameterValues)
1533 free(parameterValues);
1537 free(parameterIndex);
1541 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1546void SetColumnData(
long type,
SDDS_DATASET *dataset,
void *values, int32_t rows, int32_t index) {
1549 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
short *)values, rows, index, NULL))
1550 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
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);
1561 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
float *)values, rows, index, NULL))
1562 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1565 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
double *)values, rows, index, NULL))
1566 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
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);
1573 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
char **)values, rows, index, NULL))
1574 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1577 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
char *)values, rows, index, NULL))
1578 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1583void *AllocateColumnData(
long type,
void *values, int32_t rows) {
1596 return SDDS_Realloc(values, rows * (
sizeof(
long double)));
1598 return SDDS_Realloc(values, rows * 2 * (
sizeof(
char)));
1603char **AllocateColumnStringData(
char **values, int32_t rows, int32_t previous_rows) {
1605 values =
SDDS_Realloc(values, rows * (
sizeof(
char *)));
1606 for (i = previous_rows; i < rows; i++) {
1613long getToken(
char *s,
char *buffer,
long buflen,
char separator,
long whitespace) {
1614 char *ptr0, *ptr1, *escptr;
1632 if (*s ==
'\\' && *(s + 1) ==
'\\') {
1637 }
while (*s && (*s !=
'"' || (*(s - 1) ==
'\\' && (s - 1) != escptr)));
1639 if (*s && *s ==
'"')
1641 n = (long)(s - ptr1);
1643 while (*s && (!isspace(*s))) {
1646 if (*s && (isspace(*s)))
1649 while (*s && (*s != separator)) {
1652 if (*s && (*s == separator))
1661 if (*s ==
'"' && *(s - 1) !=
'\\') {
1662 while (*++s && !(*s ==
'"' && *(s - 1) !=
'\\'))
1665 }
while (*s && (!isspace(*s)));
1666 if (*s && (isspace(*s)))
1669 if (*s != separator) {
1673 if (*s ==
'"' && *(s - 1) !=
'\\') {
1674 while (*++s && !(*s ==
'"' && *(s - 1) !=
'\\'))
1677 }
while (*s && (*s != separator));
1679 if (*s && (*s == separator))
1682 n = (long)(s - ptr1);
1686 strncpy(buffer, ptr1, n);
1697void ConvertDNotationToENotation(
char *line) {
1700 while (*ptr && (ptr = strstr(ptr,
"D+"))) {
1708 while (*ptr && (ptr = strstr(ptr,
"D-"))) {
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_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.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
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.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
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.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
long fexists(const char *filename)
Checks if a file exists.
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)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.