84static char *mode_name[MODES] = {
93#define TYPE_LONGDOUBLE 4
96#define TYPE_CHARACTER 7
98static char *type_name[DATATYPES] = {
109#define HEADER_UNITS 0
110#define HEADER_DESCRIPTION 1
111#define HEADER_SYMBOL 2
112#define HEADER_COUNT 3
113#define HEADERELEMENTS 4
114static char *header_elements[HEADERELEMENTS] = {
115 "units",
"description",
"symbol",
"count"
161char *option[N_OPTIONS] = {
181#define COLUMN_ORDER 1
183static char *order_names[ORDERS] = {
190 "plaindata2sdds [<input>] [<output>]\n"
191 " [-pipe=[input][,output]]\n"
192 " [-inputMode=<ascii|binary>]\n"
193 " [-outputMode=<ascii|binary>]\n"
194 " [-separator=<char>]\n"
195 " [-commentCharacters=<chars>]\n"
197 " [-binaryRows=<rowcount>]\n"
198 " [-order=<rowMajor|columnMajor>]\n"
199 " [-parameter=<name>,<type>[,units=<string>][,description=<string>][,symbol=<string>][,count=<integer>]...]\n"
200 " [-column=<name>,<type>[,units=<string>][,description=<string>][,symbol=<string>][,count=<integer>]...]\n"
201 " [-skipcolumn=<type>]\n"
202 " [-skiplines=<integer>]\n"
203 " [-eofSequence=<string>]\n"
204 " [-majorOrder=<row|column>]\n"
208 " -inputMode The plain data file can be read in ascii or binary format.\n"
209 " -outputMode The SDDS data file can be written in ascii or binary format.\n"
210 " -separator In ascii mode, columns of the plain data file are separated by the given character.\n"
211 " By default, any combination of whitespace characters is used.\n"
212 " -commentCharacters Characters that denote comments. Lines starting with these are ignored.\n"
213 " -noRowCount The number of rows is not included in the plain data file.\n"
214 " If the plain data file is binary, the row count must be set using -binaryRows.\n"
215 " -binaryRows The number of rows in a binary file without an explicit row count.\n"
216 " -order Specifies the order of data storage in the input file.\n"
217 " - rowMajor (default): Each row consists of one element from each column.\n"
218 " - columnMajor: Each column is located entirely on one row.\n"
219 " -parameter Add this option for each parameter in the plain data file.\n"
220 " -column Add this option for each column in the plain data file.\n"
221 " -skipcolumn Add this option to skip over a column in the plain data file.\n"
222 " -skiplines Add this option to skip a specified number of header lines.\n"
223 " -eofSequence Stop parsing the file when this sequence is found at the start of a line.\n"
224 " -majorOrder Specifies the major order for writing the output file (row or column).\n"
225 " -fillin Fill in blanks with default values (0 for numeric columns, empty string for string columns).\n"
226 " -nowarnings Suppress warning messages during execution.\n\n"
227 "Program by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
230void SetColumnData(
long type,
SDDS_DATASET *dataset,
void *values, int32_t rows, int32_t index);
231void *AllocateColumnData(
long type,
void *values, int32_t rows);
232char **AllocateColumnStringData(
char **values, int32_t rows, int32_t previous_rows);
233long getToken(
char *s,
char *buffer,
long buflen,
char separator,
long whitespace);
234void ConvertDNotationToENotation(
char *line);
235void interrupt_handler(
int sig);
237void interrupt_handler(
int sig) {
238 fprintf(stderr,
"Segmentation fault. Ensure that your input file matches the -inputMode given.\n");
244int main(
int argc,
char **argv) {
251 long i, j, k, n = 0, i_arg;
252 int32_t rows, binaryRows = -1;
253 int32_t maxRows = 10000, initRows = 10000, row;
254 long par, col, page, size, readline = 1, fillin = 0;
256 char *input, *output, s[1024], *ptr, *ptr2, data[10240], temp[10240], *eofSequence;
257 unsigned long pipeFlags = 0, majorOrderFlag;
258 long noWarnings = 0, tmpfile_used = 0, columnOrder = 0, whitespace = 1;
259 short shortValue, stop;
263 long double ldoubleValue;
265 char stringValue[SDDS_MAXLINE];
267 char buffer[124], buffer2[200];
268 long *parameterIndex, *columnIndex;
270 long binary = 0, noRowCount = 0, inputBinary = 0, count = 0;
272 char commentCharacters[20];
273 short checkComment = 0;
275 long parameters = 0, columns = 0;
277 short abort_flag = 0, recover = 1, columnMajorOrder = 0;
279 input = output = NULL;
283 parameterValues = NULL;
285 parameterIndex = columnIndex = NULL;
288 argc =
scanargs(&s_arg, argc, argv);
292 signal(SIGSEGV, interrupt_handler);
294 for (i_arg = 1; i_arg < argc; i_arg++) {
295 if (s_arg[i_arg].arg_type == OPTION) {
296 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
297 case SET_MAJOR_ORDER:
299 s_arg[i_arg].n_items -= 1;
300 if (s_arg[i_arg].n_items > 0 &&
301 (!
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
302 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
303 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
304 SDDS_Bomb(
"invalid -majorOrder syntax/values");
305 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
306 columnMajorOrder = 1;
307 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
308 columnMajorOrder = 0;
311 if (s_arg[i_arg].n_items != 2)
313 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
326 if (s_arg[i_arg].n_items != 2)
328 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
341 if (s_arg[i_arg].n_items != 2)
343 separator = s_arg[i_arg].list[1][0];
347 if (s_arg[i_arg].n_items != 2)
348 SDDS_Bomb(
"invalid -commentCharacters syntax");
349 strncpy(commentCharacters, s_arg[i_arg].list[1],
sizeof(commentCharacters) - 1);
350 commentCharacters[
sizeof(commentCharacters) - 1] =
'\0';
357 if (s_arg[i_arg].n_items != 1)
362 if (s_arg[i_arg].n_items != 2)
364 switch (
match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) {
377 if (s_arg[i_arg].n_items < 3)
381 parameterValues =
trealloc(parameterValues,
sizeof(*parameterValues) * (parameters));
382 SDDS_CopyString(¶meterValues[parameters - 1].name, s_arg[i_arg].list[1]);
383 parameterValues[parameters - 1].units = NULL;
384 parameterValues[parameters - 1].description = NULL;
385 parameterValues[parameters - 1].symbol = NULL;
386 switch (
match_string(s_arg[i_arg].list[2], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
388 parameterValues[parameters - 1].type =
SDDS_SHORT;
391 parameterValues[parameters - 1].type =
SDDS_LONG;
394 parameterValues[parameters - 1].type =
SDDS_LONG64;
397 parameterValues[parameters - 1].type =
SDDS_FLOAT;
399 case TYPE_LONGDOUBLE:
403 parameterValues[parameters - 1].type =
SDDS_DOUBLE;
406 parameterValues[parameters - 1].type =
SDDS_STRING;
415 for (i = 3; i < s_arg[i_arg].n_items; i++) {
416 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
419 switch (
match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) {
423 case HEADER_DESCRIPTION:
430 if (sscanf(ptr,
"%ld", &count) != 1 || count <= 0)
431 SDDS_Bomb(
"invalid parameter count value");
432 snprintf(buffer,
sizeof(buffer),
"%s", parameterValues[parameters - 1].name);
433 snprintf(buffer2,
sizeof(buffer2),
"%s1", buffer);
434 free(parameterValues[parameters - 1].name);
443 for (i = 2; i <= count; i++) {
445 parameterValues =
trealloc(parameterValues,
sizeof(*parameterValues) * (parameters));
446 snprintf(buffer2,
sizeof(buffer2),
"%s%ld", buffer, i);
448 parameterValues[parameters - 1].units = NULL;
449 parameterValues[parameters - 1].description = NULL;
450 parameterValues[parameters - 1].symbol = NULL;
451 parameterValues[parameters - 1].type = parameterValues[parameters - 2].type;
452 if (parameterValues[parameters - 2].units)
453 SDDS_CopyString(¶meterValues[parameters - 1].units, parameterValues[parameters - 2].units);
454 if (parameterValues[parameters - 2].description)
455 SDDS_CopyString(¶meterValues[parameters - 1].description, parameterValues[parameters - 2].description);
456 if (parameterValues[parameters - 2].symbol)
457 SDDS_CopyString(¶meterValues[parameters - 1].symbol, parameterValues[parameters - 2].symbol);
462 if (s_arg[i_arg].n_items < 3)
466 columnValues =
trealloc(columnValues,
sizeof(*columnValues) * (columns));
467 SDDS_CopyString(&columnValues[columns - 1].name, s_arg[i_arg].list[1]);
468 columnValues[columns - 1].elements = 0;
469 columnValues[columns - 1].values = NULL;
470 columnValues[columns - 1].stringValues = NULL;
471 columnValues[columns - 1].units = NULL;
472 columnValues[columns - 1].description = NULL;
473 columnValues[columns - 1].symbol = NULL;
474 columnValues[columns - 1].skip = 0;
476 switch (
match_string(s_arg[i_arg].list[2], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
481 columnValues[columns - 1].type =
SDDS_LONG;
489 case TYPE_LONGDOUBLE:
505 for (i = 3; i < s_arg[i_arg].n_items; i++) {
506 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
509 switch (
match_string(s_arg[i_arg].list[i], header_elements, HEADERELEMENTS, 0)) {
513 case HEADER_DESCRIPTION:
520 if (sscanf(ptr,
"%ld", &count) != 1 || count <= 0)
522 snprintf(buffer,
sizeof(buffer),
"%s", columnValues[columns - 1].name);
523 snprintf(buffer2,
sizeof(buffer2),
"%s1", buffer);
524 free(columnValues[columns - 1].name);
533 for (i = 2; i <= count; i++) {
535 columnValues =
trealloc(columnValues,
sizeof(*columnValues) * (columns));
536 snprintf(buffer2,
sizeof(buffer2),
"%s%ld", buffer, i);
538 columnValues[columns - 1].elements = 0;
539 columnValues[columns - 1].values = NULL;
540 columnValues[columns - 1].stringValues = NULL;
541 columnValues[columns - 1].units = NULL;
542 columnValues[columns - 1].description = NULL;
543 columnValues[columns - 1].symbol = NULL;
544 columnValues[columns - 1].type = columnValues[columns - 2].type;
545 if (columnValues[columns - 2].units)
546 SDDS_CopyString(&columnValues[columns - 1].units, columnValues[columns - 2].units);
547 if (columnValues[columns - 2].description)
548 SDDS_CopyString(&columnValues[columns - 1].description, columnValues[columns - 2].description);
549 if (columnValues[columns - 2].symbol)
550 SDDS_CopyString(&columnValues[columns - 1].symbol, columnValues[columns - 2].symbol);
555 if (s_arg[i_arg].n_items != 2)
559 columnValues =
trealloc(columnValues,
sizeof(*columnValues) * (columns));
560 columnValues[columns - 1].name = NULL;
561 columnValues[columns - 1].elements = 0;
562 columnValues[columns - 1].values = NULL;
563 columnValues[columns - 1].stringValues = NULL;
564 columnValues[columns - 1].units = NULL;
565 columnValues[columns - 1].description = NULL;
566 columnValues[columns - 1].symbol = NULL;
567 columnValues[columns - 1].skip = 1;
569 switch (
match_string(s_arg[i_arg].list[1], type_name, DATATYPES, MATCH_WHOLE_STRING)) {
574 columnValues[columns - 1].type =
SDDS_LONG;
582 case TYPE_LONGDOUBLE:
600 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
604 if (s_arg[i_arg].n_items != 1)
609 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &skiplines) != 1 || skiplines <= 0)
612 case SET_BINARY_ROWS:
613 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%" SCNd32, &binaryRows) != 1 || binaryRows < 0)
616 case SET_EOF_SEQUENCE:
617 if (s_arg[i_arg].n_items != 2)
618 SDDS_Bomb(
"invalid -eofSequence syntax");
619 eofSequence = s_arg[i_arg].list[1];
622 fprintf(stderr,
"error: unknown switch: %s\n", s_arg[i_arg].list[0]);
628 input = s_arg[i_arg].list[0];
629 }
else if (output == NULL) {
630 output = s_arg[i_arg].list[0];
632 fprintf(stderr,
"too many filenames\n");
638 processFilenames(
"plaindata2sdds", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
640 if (!columns && !parameters)
641 SDDS_Bomb(
"you must specify one of the -column or the -parameter options");
643 if (skiplines && inputBinary)
644 SDDS_Bomb(
"-skiplines does not work with binary input files");
649 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
650 fprintf(stderr,
"error: unable to set stdin to binary mode\n");
658 fprintf(stderr,
"input file not found\n");
662 fileID = fopen(input,
"rb");
664 fileID = fopen(input,
"r");
667 if (fileID == NULL) {
668 fprintf(stderr,
"unable to open input file for reading\n");
675 SDDS_dataset.layout.data_mode.column_major = columnMajorOrder;
678 parameterIndex =
tmalloc(
sizeof(*parameterIndex) * parameters);
681 columnIndex =
tmalloc(
sizeof(*columnIndex) * columns);
684 for (i = 0; i < parameters; i++) {
685 if ((parameterIndex[i] =
SDDS_DefineParameter(&SDDS_dataset, parameterValues[i].name, parameterValues[i].symbol, parameterValues[i].units, parameterValues[i].description, NULL, parameterValues[i].type, 0)) < 0) {
686 snprintf(s,
sizeof(s),
"Problem defining parameter %s.", parameterValues[i].name);
691 for (i = 0; i < columns; i++) {
692 if (columnValues[i].skip)
694 if ((columnIndex[i] =
SDDS_DefineColumn(&SDDS_dataset, columnValues[i].name, columnValues[i].symbol, columnValues[i].units, columnValues[i].description, NULL, columnValues[i].type, 0)) < 0) {
695 snprintf(s,
sizeof(s),
"Problem defining column %s.", columnValues[i].name);
707 row = par = col = page = 0;
708 while (inputBinary) {
711 if (binaryRows == -1) {
712 if (fread(&rows,
sizeof(rows), 1, fileID) != 1) {
719 for (k = 0; k < columns; k++) {
723 free(columnValues[k].values);
735 for (k = 0; k < columns; k++) {
739 free(columnValues[k].values);
747 for (par = 0; par < parameters; par++) {
748 switch (parameterValues[par].type) {
750 if (fread(&shortValue,
sizeof(shortValue), 1, fileID) != 1) {
754 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, shortValue, -1))
758 if (fread(&longValue,
sizeof(longValue), 1, fileID) != 1) {
762 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, longValue, -1))
766 if (fread(&long64Value,
sizeof(long64Value), 1, fileID) != 1) {
770 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, long64Value, -1))
774 if (fread(&floatValue,
sizeof(floatValue), 1, fileID) != 1) {
778 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, floatValue, -1))
782 if (fread(&doubleValue,
sizeof(doubleValue), 1, fileID) != 1) {
786 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, doubleValue, -1))
790 if (fread(&ldoubleValue,
sizeof(ldoubleValue), 1, fileID) != 1) {
794 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, ldoubleValue, -1))
798 if (fread(&size,
sizeof(size), 1, fileID) != 1) {
802 if (size > SDDS_MAXLINE - 1)
805 if (fread(&stringValue, size, 1, fileID) != 1) {
809 stringValue[size] =
'\0';
811 strcpy(stringValue,
"");
813 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, stringValue, -1))
817 if (fread(&characterValue,
sizeof(characterValue), 1, fileID) != 1) {
821 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, characterValue, -1))
826 for (i = 0; i < columns; i++) {
827 if (columnValues[i].skip)
830 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
832 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
834 columnValues[i].elements = rows;
837 for (col = 0; col < columns; col++) {
838 switch (columnValues[col].type) {
840 for (i = 0; i < rows; i++) {
841 if (fread((
short *)(columnValues[col].values) + i,
sizeof(
short), 1, fileID) != 1) {
848 for (i = 0; i < rows; i++) {
849 if (fread((int32_t *)(columnValues[col].values) + i,
sizeof(int32_t), 1, fileID) != 1) {
856 for (i = 0; i < rows; i++) {
857 if (fread((int64_t *)(columnValues[col].values) + i,
sizeof(int64_t), 1, fileID) != 1) {
864 for (i = 0; i < rows; i++) {
865 if (fread((
float *)(columnValues[col].values) + i,
sizeof(
float), 1, fileID) != 1) {
872 for (i = 0; i < rows; i++) {
873 if (fread((
double *)(columnValues[col].values) + i,
sizeof(
double), 1, fileID) != 1) {
880 for (i = 0; i < rows; i++) {
881 if (fread((
long double *)(columnValues[col].values) + i,
sizeof(
long double), 1, fileID) != 1) {
888 for (i = 0; i < rows; i++) {
889 if (fread(&size,
sizeof(size), 1, fileID) != 1) {
893 if (size > SDDS_MAXLINE - 1)
895 columnValues[col].stringValues[i] = malloc(size + 1);
896 if (!columnValues[col].stringValues[i]) {
897 SDDS_Bomb(
"Memory allocation failed for string column");
900 if (fread(columnValues[col].stringValues[i], size, 1, fileID) != 1) {
904 columnValues[col].stringValues[i][size] =
'\0';
906 strcpy(columnValues[col].stringValues[i],
"");
911 for (i = 0; i < rows; i++) {
912 if (fread((
char *)(columnValues[col].values) + i,
sizeof(
char), 1, fileID) != 1) {
921 for (i = 0; i < rows; i++) {
922 for (col = 0; col < columns; col++) {
923 switch (columnValues[col].type) {
925 if (fread((
short *)(columnValues[col].values) + i,
sizeof(
short), 1, fileID) != 1) {
931 if (fread((int32_t *)(columnValues[col].values) + i,
sizeof(int32_t), 1, fileID) != 1) {
937 if (fread((int64_t *)(columnValues[col].values) + i,
sizeof(int64_t), 1, fileID) != 1) {
943 if (fread((
float *)(columnValues[col].values) + i,
sizeof(
float), 1, fileID) != 1) {
949 if (fread((
double *)(columnValues[col].values) + i,
sizeof(
double), 1, fileID) != 1) {
955 if (fread((
long double *)(columnValues[col].values) + i,
sizeof(
long double), 1, fileID) != 1) {
961 if (fread(&size,
sizeof(size), 1, fileID) != 1) {
965 if (size > SDDS_MAXLINE - 1)
967 columnValues[col].stringValues[i] = malloc(size + 1);
968 if (!columnValues[col].stringValues[i]) {
969 SDDS_Bomb(
"Memory allocation failed for string column");
972 if (fread(columnValues[col].stringValues[i], size, 1, fileID) != 1) {
976 columnValues[col].stringValues[i][size] =
'\0';
978 strcpy(columnValues[col].stringValues[i],
"");
982 if (fread((
char *)(columnValues[col].values) + i,
sizeof(
char), 1, fileID) != 1) {
991 if (rows > maxRows) {
999 for (i = 0; i < columns; i++) {
1000 if (columnValues[i].skip)
1003 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n);
1005 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n);
1011 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1015 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1018 row = par = col = n = 0;
1021 ptr =
SDDS_Malloc(
sizeof(*ptr) * (ptrSize = 2048));
1022 ptr2 =
SDDS_Malloc(
sizeof(*ptr2) * (ptrSize = 2048));
1024 SDDS_Bomb(
"Memory allocation failed for ptr2");
1030 while (skiplines > 0) {
1031 if (!fgets(ptr, ptrSize, fileID))
1039 for (i = 0; i < strlen(commentCharacters); i++) {
1040 if (ptr[0] == commentCharacters[i]) {
1046 if (commentFound == 1) {
1049 if (ptr[strlen(ptr) - 1] ==
'\n')
1050 ptr[strlen(ptr) - 1] =
'\0';
1053 if (getToken(temp, data, 10240, separator, whitespace) < 0)
1058 if (eofSequence && strncmp(eofSequence, ptr, strlen(eofSequence)) == 0) {
1062 if (par < parameters) {
1063 switch (parameterValues[par].type) {
1065 if (sscanf(ptr,
"%hd", &shortValue) != 1) {
1067 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1069 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, shortValue, -1))
1070 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1073 if (sscanf(ptr,
"%" SCNd32, &longValue) != 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, longValue, -1))
1078 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1081 if (sscanf(ptr,
"%" SCNd64, &long64Value) != 1) {
1083 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1085 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, long64Value, -1))
1086 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1089 ConvertDNotationToENotation(ptr);
1090 if (sscanf(ptr,
"%f", &floatValue) != 1) {
1092 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1094 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, floatValue, -1))
1095 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1098 ConvertDNotationToENotation(ptr);
1099 if (sscanf(ptr,
"%lf", &doubleValue) != 1) {
1101 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1103 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, doubleValue, -1))
1104 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1107 ConvertDNotationToENotation(ptr);
1108 if (sscanf(ptr,
"%Lf", &ldoubleValue) != 1) {
1110 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1112 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, ldoubleValue, -1))
1113 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1118 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, stringValue, -1))
1119 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1123 characterValue = ptr[0];
1124 if (!
SDDS_SetParameters(&SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, par, characterValue, -1))
1125 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1129 }
else if ((rows == -1) && (!noRowCount)) {
1130 if (sscanf(ptr,
"%" SCNd32, &rows) != 1) {
1132 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1134 }
else if ((columns > 0) && ((row < rows) || (noRowCount))) {
1141 while (getToken(ptr2, data, 10240, separator, whitespace) >= 0) {
1148 if (rows > columnValues[col].elements) {
1150 columnValues[col].stringValues = AllocateColumnStringData(columnValues[col].stringValues, rows, columnValues[col].elements);
1152 columnValues[col].values = AllocateColumnData(columnValues[col].type, columnValues[col].values, rows);
1154 columnValues[col].elements = rows;
1156 switch (columnValues[col].type) {
1158 for (row = 0; row < rows; row++) {
1159 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1161 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1163 if (sscanf(data,
"%hd", ((
short *)(columnValues[col].values) + row)) != 1) {
1165 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1170 for (row = 0; row < rows; row++) {
1171 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1173 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1175 if (sscanf(data,
"%" SCNd32, ((int32_t *)(columnValues[col].values) + row)) != 1) {
1177 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1182 for (row = 0; row < rows; row++) {
1183 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1185 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1187 if (sscanf(data,
"%" SCNd64, ((int64_t *)(columnValues[col].values) + row)) != 1) {
1189 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1194 for (row = 0; row < rows; row++) {
1195 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1197 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1199 ConvertDNotationToENotation(data);
1200 if (sscanf(data,
"%f", ((
float *)(columnValues[col].values) + row)) != 1) {
1202 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1207 for (row = 0; row < rows; row++) {
1208 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1210 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1212 ConvertDNotationToENotation(data);
1213 if (sscanf(data,
"%lf", ((
double *)(columnValues[col].values) + row)) != 1) {
1215 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1220 for (row = 0; row < rows; row++) {
1221 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1223 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1225 ConvertDNotationToENotation(data);
1226 if (sscanf(data,
"%Lf", ((
long double *)(columnValues[col].values) + row)) != 1) {
1228 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1233 for (row = 0; row < rows; row++) {
1234 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1236 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1239 columnValues[col].stringValues[row] = malloc(strlen(data) + 1);
1240 if (!columnValues[col].stringValues[row]) {
1241 SDDS_Bomb(
"Memory allocation failed for string column element");
1243 strcpy(columnValues[col].stringValues[row], data);
1247 for (row = 0; row < rows; row++) {
1248 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1250 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1253 *((
char *)(columnValues[col].values) + row) = data[0];
1257 if (rows > maxRows) {
1260 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1264 if (columnValues[col].skip == 0) {
1266 SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].stringValues, rows, col);
1268 SetColumnData(columnValues[col].type, &SDDS_dataset, columnValues[col].values, rows, col);
1278 }
else if (row == rows - 1) {
1280 for (i = 0; i < columns; i++) {
1281 if (rows > columnValues[i].elements) {
1283 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
1285 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
1288 columnValues[i].elements = rows;
1293 for (i = 0; i < columns; i++) {
1294 if (rows > columnValues[i].elements) {
1296 columnValues[i].stringValues = AllocateColumnStringData(columnValues[i].stringValues, rows, columnValues[i].elements);
1298 columnValues[i].values = AllocateColumnData(columnValues[i].type, columnValues[i].values, rows);
1301 columnValues[i].elements = rows;
1307 while (getToken(ptr2, data, 10240, separator, whitespace) >= 0) {
1312 if ((i != columns) && (parameters > 0 && i == 1)) {
1314 if (row > maxRows) {
1317 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1322 for (j = 0; j < columns; j++) {
1323 if (columnValues[j].skip)
1326 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n);
1328 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n);
1333 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1336 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1337 row = par = col = 0;
1345 for (i = 0; i < columns; i++) {
1346 if (getToken(ptr, data, 10240, separator, whitespace) < 0) {
1348 fprintf(stderr,
"Problem with column data: %s\n", data);
1350 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1352 switch (columnValues[i].type) {
1359 snprintf(data,
sizeof(data),
"0");
1369 switch (columnValues[i].type) {
1371 if (sscanf(data,
"%hd", ((
short *)(columnValues[i].values) + row)) != 1) {
1377 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1382 if (sscanf(data,
"%" SCNd32, ((int32_t *)(columnValues[i].values) + row)) != 1) {
1388 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1393 if (sscanf(data,
"%" SCNd64, ((int64_t *)(columnValues[i].values) + row)) != 1) {
1399 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1404 ConvertDNotationToENotation(data);
1405 if (sscanf(data,
"%f", ((
float *)(columnValues[i].values) + row)) != 1) {
1411 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1416 ConvertDNotationToENotation(data);
1417 if (sscanf(data,
"%lf", ((
double *)(columnValues[i].values) + row)) != 1) {
1423 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1428 ConvertDNotationToENotation(data);
1429 if (sscanf(data,
"%Lf", ((
long double *)(columnValues[i].values) + row)) != 1) {
1435 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1441 columnValues[i].stringValues[row] = malloc(strlen(data) + 1);
1442 if (!columnValues[i].stringValues[row]) {
1443 SDDS_Bomb(
"Memory allocation failed for string column element");
1445 strcpy(columnValues[i].stringValues[row], data);
1449 *((
char *)(columnValues[i].values) + row) = data[0];
1453 if (recover && abort_flag) {
1459 if ((row == rows) && (!noRowCount)) {
1460 if (rows > maxRows) {
1463 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1468 for (i = 0; i < columns; i++) {
1469 if (columnValues[i].skip)
1472 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].stringValues, rows, n);
1474 SetColumnData(columnValues[i].type, &SDDS_dataset, columnValues[i].values, rows, n);
1481 if ((par == parameters) &&
1486 (col == columns)) ||
1491 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1495 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1497 row = par = col = 0;
1505 if (row > maxRows) {
1508 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1513 for (j = 0; j < columns; j++) {
1514 if (columnValues[j].skip)
1517 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].stringValues, row, n);
1519 SetColumnData(columnValues[j].type, &SDDS_dataset, columnValues[j].values, row, n);
1524 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1529 for (i = 0; i < columns; i++) {
1531 for (j = 0; j < columnValues[i].elements; j++) {
1532 free(columnValues[i].stringValues[j]);
1534 free(columnValues[i].stringValues);
1536 free(columnValues[i].values);
1538 if (columnValues[i].name)
1539 free(columnValues[i].name);
1540 if (columnValues[i].units)
1541 free(columnValues[i].units);
1542 if (columnValues[i].description)
1543 free(columnValues[i].description);
1544 if (columnValues[i].symbol)
1545 free(columnValues[i].symbol);
1547 for (i = 0; i < parameters; i++) {
1548 free(parameterValues[i].name);
1549 if (parameterValues[i].units)
1550 free(parameterValues[i].units);
1551 if (parameterValues[i].description)
1552 free(parameterValues[i].description);
1553 if (parameterValues[i].symbol)
1554 free(parameterValues[i].symbol);
1558 if (parameterValues)
1559 free(parameterValues);
1563 free(parameterIndex);
1569 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1571 return EXIT_SUCCESS;
1574void SetColumnData(
long type,
SDDS_DATASET *dataset,
void *values, int32_t rows, int32_t index) {
1577 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
short *)values, rows, index, NULL))
1578 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1581 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (int32_t *)values, rows, index, NULL))
1582 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1585 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (int64_t *)values, rows, index, NULL))
1586 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1589 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
float *)values, rows, index, NULL))
1590 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1593 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
double *)values, rows, index, NULL))
1594 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1597 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
long double *)values, rows, index, NULL))
1598 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1601 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
char **)values, rows, index, NULL))
1602 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1605 if (!
SDDS_SetColumn(dataset, SDDS_SET_BY_INDEX, (
char *)values, rows, index, NULL))
1606 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1611void *AllocateColumnData(
long type,
void *values, int32_t rows) {
1624 return SDDS_Realloc(values, rows *
sizeof(
long double));
1631char **AllocateColumnStringData(
char **values, int32_t rows, int32_t previous_rows) {
1634 for (i = previous_rows; i < rows; i++) {
1641long getToken(
char *s,
char *buffer,
long buflen,
char separator,
long whitespace) {
1642 char *ptr0, *ptr1, *escptr;
1660 if (*s ==
'\\' && *(s + 1) ==
'\\') {
1665 }
while (*s && (*s !=
'"' || (*(s - 1) ==
'\\' && (s - 1) != escptr)));
1667 if (*s && *s ==
'"')
1669 n = (long)(s - ptr1);
1671 while (*s && (!isspace(*s))) {
1674 if (*s && (isspace(*s)))
1677 while (*s && (*s != separator)) {
1680 if (*s && (*s == separator))
1689 if (*s ==
'"' && *(s - 1) !=
'\\') {
1690 while (*++s && !(*s ==
'"' && *(s - 1) !=
'\\'))
1693 }
while (*s && (!isspace(*s)));
1694 if (*s && (isspace(*s)))
1697 if (*s != separator) {
1701 if (*s ==
'"' && *(s - 1) !=
'\\') {
1702 while (*++s && !(*s ==
'"' && *(s - 1) !=
'\\'))
1705 }
while (*s && (*s != separator));
1707 if (*s && (*s == separator))
1710 n = (long)(s - ptr1);
1714 strncpy(buffer, ptr1, n);
1725void ConvertDNotationToENotation(
char *line) {
1728 while (*ptr && (ptr = strstr(ptr,
"D+"))) {
1736 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.