109 {
110 FILE *outfile;
116 char s[256];
117 char *input, *output;
118 long i, i_arg, ntable;
119 int64_t nrows, j;
120 SCANNED_ARG *s_arg;
121 char *text, *contents, *ss, *ptr, *iformat = FORMAT, *format, *rformat;
122 long verbose = 0, comments = 0, addquotes = 1;
123 short nexp;
124 double dd, ddred;
125 float ff, ffred;
126 void *data;
127 unsigned long pipeFlags;
128
130
131 input = output = NULL;
132 pipeFlags = 0;
133
134 argc =
scanargs(&s_arg, argc, argv);
135 if (argc == 1) {
136 fprintf(stderr, "%s", USAGE);
137 exit(EXIT_FAILURE);
138 }
139
140 for (i_arg = 1; i_arg < argc; i_arg++) {
141 if (s_arg[i_arg].arg_type == OPTION) {
142 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
143 case SET_COMMENTS:
144 comments = 1;
145 break;
146 case SET_FORMAT:
147 if (s_arg[i_arg].n_items < 2)
149 iformat = s_arg[i_arg].list[1];
150 break;
151 case SET_VERBOSE:
152 verbose = 1;
153 break;
154 case SET_PIPE:
155 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
157 break;
158 default:
159 fprintf(stderr, "Unknown option: %s\n", s_arg[i_arg].list[0]);
160 fprintf(stderr, "%s", USAGE);
161 exit(EXIT_FAILURE);
162 }
163 } else {
164 if (input == NULL)
165 input = s_arg[i_arg].list[0];
166 else if (output == NULL)
167 output = s_arg[i_arg].list[0];
168 else
169 SDDS_Bomb(
"Too many filenames provided.");
170 }
171 }
172
174
175 if (output) {
176 outfile = fopen(output, "w");
177 if (!outfile) {
178 fprintf(stderr, "Error: Cannot open output file '%s'\n", output);
179 exit(EXIT_FAILURE);
180 }
181 } else {
182 outfile = stdout;
183 }
184
185
186 format = (char *)calloc(256, sizeof(char));
187 if (!format) {
188 fprintf(stderr, "Memory allocation error for format.\n");
189 exit(EXIT_FAILURE);
190 }
191
192 rformat = (char *)calloc(256, sizeof(char));
193 if (!rformat) {
194 fprintf(stderr, "Memory allocation error for rformat.\n");
195 free(format);
196 exit(EXIT_FAILURE);
197 }
198
199 strcpy(format, iformat);
200 if ((ptr = strchr(format, 'E')))
201 *ptr = 'e';
202 if ((ptr = strchr(format, 'G')))
203 *ptr = 'g';
204 strcpy(rformat, format);
205 if ((ptr = strpbrk(rformat, "eg")))
206 *ptr = 'f';
207
208
211 exit(EXIT_FAILURE);
212 }
213
214 layout = &SDDS_table.layout;
215
216
217 fprintf(outfile, "{");
218
219
220 fprintf(outfile, "{");
221 if (verbose)
222 printf("\nFile '%s' is in SDDS protocol version %" PRId32 "\n", input, layout->version);
223
226
227 if (text) {
228 if (verbose)
229 printf("Description: %s\n", text);
230 fprintf(outfile, "\"%s\",", text);
231 }
232
233 if (contents) {
234 if (verbose)
235 printf("Contents: %s\n", contents);
236 fprintf(outfile, "\"%s\"", contents);
237 } else {
238 fprintf(outfile, "\"No contents\"");
239 }
240
241 if (layout->data_mode.mode == SDDS_ASCII) {
242 if (verbose)
243 printf("\nData is ASCII with %" PRId32 " lines per row and %" PRId32 " additional header lines expected.\n",
244 layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
245 if (verbose)
246 printf("Row counts: %s\n", layout->data_mode.no_row_counts ? "No" : "Yes");
247 } else if (verbose) {
248 printf("\nData is binary\n");
249 }
250
251 fprintf(outfile, "},\n");
252
253
254 fprintf(outfile, " {");
255 if (layout->n_columns) {
256 if (verbose)
257 printf("\n%" PRId32 " columns of data:\n", layout->n_columns);
258 if (verbose)
259 printf("NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n");
260 if (verbose)
261 printf(" LENGTH\n");
262 for (i = 0; i < layout->n_columns; i++) {
263 if (i > 0)
264 fprintf(outfile, ",\n ");
265 coldef = layout->column_definition + i;
266 if (verbose)
267 printf("%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n",
268 coldef->name ? coldef->name : "No name",
269 coldef->units ? coldef->units : "",
270 coldef->symbol ? coldef->symbol : "",
271 coldef->format_string ? coldef->format_string : "",
273 coldef->field_length,
274 coldef->description ? coldef->description : "No description");
275 fprintf(outfile, "{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%" PRId32 ",\"%s\"}",
276 coldef->name ? coldef->name : "No name",
277 coldef->units ? coldef->units : "",
278 coldef->symbol ? coldef->symbol : "",
279 coldef->format_string ? coldef->format_string : "",
281 coldef->field_length,
282 coldef->description ? coldef->description : "No description");
283 }
284 }
285 fprintf(outfile, "},\n");
286
287
288 fprintf(outfile, " {");
289 if (layout->n_parameters) {
290 if (verbose)
291 printf("\n%" PRId32 " parameters:\n", layout->n_parameters);
292 if (verbose)
293 printf("NAME UNITS SYMBOL TYPE DESCRIPTION\n");
294 for (i = 0; i < layout->n_parameters; i++) {
295 if (i > 0)
296 fprintf(outfile, ",\n ");
297 pardef = layout->parameter_definition + i;
298 if (verbose)
299 printf("%-19s %-19s %-19s %-19s %s\n",
300 pardef->name ? pardef->name : "No name",
301 pardef->units ? pardef->units : "",
302 pardef->symbol ? pardef->symbol : "",
304 pardef->description ? pardef->description : "No description");
305 fprintf(outfile, "{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
306 pardef->name ? pardef->name : "No name",
307 pardef->fixed_value ? pardef->fixed_value : "",
308 pardef->units ? pardef->units : "",
309 pardef->symbol ? pardef->symbol : "",
311 pardef->description ? pardef->description : "No description");
312 }
313 }
314 fprintf(outfile, "},\n");
315
316
317 fprintf(outfile, " {");
318 if (layout->n_arrays) {
319 if (verbose)
320 printf("\n%" PRId32 " arrays of data:\n", layout->n_arrays);
321 if (verbose)
322 printf("NAME UNITS SYMBOL FORMAT TYPE FIELD GROUP DESCRIPTION\n");
323 if (verbose)
324 printf(" LENGTH NAME\n");
325 for (i = 0; i < layout->n_arrays; i++) {
326 if (i > 0)
327 fprintf(outfile, ",\n ");
328 arraydef = layout->array_definition + i;
329 if (verbose)
330 printf("%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32 " %-7" PRId32 " %-15s %s\n",
331 arraydef->name ? arraydef->name : "No name",
332 arraydef->units ? arraydef->units : "",
333 arraydef->symbol ? arraydef->symbol : "",
334 arraydef->format_string ? arraydef->format_string : "",
336 arraydef->dimensions,
337 arraydef->field_length,
338 arraydef->group_name ? arraydef->group_name : "",
339 arraydef->description ? arraydef->description : "No description");
340 fprintf(outfile, "{\"%s\",\"%s\",\"%s\",\"%s\",\"%s*^%" PRId32 "\",%" PRId32 ",\"%s\",\"%s\"}",
341 arraydef->name ? arraydef->name : "No name",
342 arraydef->units ? arraydef->units : "",
343 arraydef->symbol ? arraydef->symbol : "",
344 arraydef->format_string ? arraydef->format_string : "",
346 arraydef->dimensions,
347 arraydef->field_length,
348 arraydef->group_name ? arraydef->group_name : "",
349 arraydef->description ? arraydef->description : "No description");
350 }
351 }
352 fprintf(outfile, "},\n");
353
354
355 fprintf(outfile, " {");
356 if (layout->n_associates) {
357 if (verbose)
358 printf("\n%" PRId32 " associates:\n", layout->n_associates);
359 if (verbose)
360 printf("SDDS FILENAME PATH CONTENTS DESCRIPTION\n");
361 for (i = 0; i < layout->n_associates; i++) {
362 if (i > 0)
363 fprintf(outfile, ",\n ");
364 if (verbose)
365 printf("%-5s %-19s %-29s %-19s %s\n",
366 layout->associate_definition[i].sdds ? "True" : "False",
367 layout->associate_definition[i].filename ? layout->associate_definition[i].filename : "",
368 layout->associate_definition[i].path ? layout->associate_definition[i].path : "",
369 layout->associate_definition[i].contents ? layout->associate_definition[i].contents : "",
370 layout->associate_definition[i].description ? layout->associate_definition[i].description : "No description");
371 fprintf(outfile, "{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
372 layout->associate_definition[i].sdds ? "True" : "False",
373 layout->associate_definition[i].filename ? layout->associate_definition[i].filename : "",
374 layout->associate_definition[i].path ? layout->associate_definition[i].path : "",
375 layout->associate_definition[i].contents ? layout->associate_definition[i].contents : "",
376 layout->associate_definition[i].description ? layout->associate_definition[i].description : "No description");
377 }
378 }
379 fprintf(outfile, "},\n");
380
381
382 fprintf(outfile, " {");
383 while ((ntable = SDDS_ReadTable(&SDDS_table)) > 0) {
384 if (ntable > 1)
385 fprintf(outfile, ",\n ");
386 if (comments)
387 fprintf(outfile, "(*Table %ld*)", ntable);
388 fprintf(outfile, "{\n");
389
390
391 fprintf(outfile, " {");
392 for (i = 0; i < layout->n_parameters; i++) {
393 if (i > 0)
394 fprintf(outfile, ",\n ");
395 pardef = layout->parameter_definition + i;
398 exit(EXIT_FAILURE);
399 }
400
401
402 if (comments)
403 fprintf(outfile, "(* %s *)", pardef->name);
404
405 addquotes = 1;
406 switch (pardef->type) {
408 dd = *(double *)data;
409 sprintf(s, format, dd);
410 if ((ptr = strchr(s, 'e'))) {
411 *ptr = ' ';
412 sscanf(s, "%lf %hd", &ddred, &nexp);
413 fprintf(outfile, rformat, ddred);
414 fprintf(outfile, "*10^%d", nexp);
415 } else {
416 fprintf(outfile, "%s", s);
417 }
418 break;
420 ff = *(float *)data;
421 sprintf(s, format, ff);
422 if ((ptr = strchr(s, 'e'))) {
423 *ptr = ' ';
424 sscanf(s, "%f %hd", &ffred, &nexp);
425 fprintf(outfile, rformat, ffred);
426 fprintf(outfile, "*10^%d", nexp);
427 } else {
428 fprintf(outfile, "%s", s);
429 }
430 break;
432 ss = *(char **)data;
433 if (*ss == '"')
434 addquotes = 0;
436 addquotes = 0;
438 if (addquotes)
439 fprintf(outfile, "\"");
441 if (addquotes)
442 fprintf(outfile, "\"");
443 break;
444 default:
446 break;
447 }
448 }
449 fprintf(outfile, "},\n");
450
451
452 fprintf(outfile, " {");
453 if (layout->n_columns) {
458 exit(EXIT_FAILURE);
459 }
460
461 if (nrows) {
462 for (j = 0; j < nrows; j++) {
463 if (j > 0)
464 fprintf(outfile, ",\n ");
465 fprintf(outfile, "{");
466 for (i = 0; i < layout->n_columns; i++) {
467 if (i > 0)
468 fprintf(outfile, ",");
469 coldef = layout->column_definition + i;
470 if (!(data =
SDDS_GetValue(&SDDS_table, coldef->name, j, NULL))) {
472 exit(EXIT_FAILURE);
473 }
474
475 addquotes = 1;
476 switch (coldef->type) {
478 dd = *(double *)data;
479 sprintf(s, format, dd);
480 if ((ptr = strchr(s, 'e'))) {
481 *ptr = ' ';
482 sscanf(s, "%lf %hd", &ddred, &nexp);
483 fprintf(outfile, rformat, ddred);
484 fprintf(outfile, "*10^%d", nexp);
485 } else {
486 fprintf(outfile, "%s", s);
487 }
488 break;
490 ff = *(float *)data;
491 sprintf(s, format, ff);
492 if ((ptr = strchr(s, 'e'))) {
493 *ptr = ' ';
494 sscanf(s, "%f %hd", &ffred, &nexp);
495 fprintf(outfile, rformat, ffred);
496 fprintf(outfile, "*10^%d", nexp);
497 } else {
498 fprintf(outfile, "%s", s);
499 }
500 break;
502 ss = *(char **)data;
503 if (*ss == '"')
504 addquotes = 0;
506 addquotes = 0;
508 if (addquotes)
509 fprintf(outfile, "\"");
511 if (addquotes)
512 fprintf(outfile, "\"");
513 break;
514 default:
516 break;
517 }
518 }
519 fprintf(outfile, "}");
520 }
521 }
522 }
523 fprintf(outfile, "}");
524 fprintf(outfile, "}");
525 }
526 fprintf(outfile, "\n }\n");
527
528
529 fprintf(outfile, "}\n");
530
531
532 fflush(stdout);
535 exit(EXIT_FAILURE);
536 }
537
538 free(format);
539 free(rformat);
540
541 return EXIT_SUCCESS;
542}
char * SDDS_type_name[SDDS_NUM_TYPES]
Array of supported data type names.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_PrintTypedValue(void *data, int64_t index, int32_t type, char *format, FILE *fp, uint32_t mode)
Prints a data value of a specified type using an optional printf format string.
int32_t SDDS_HasWhitespace(char *string)
Checks if a string contains any whitespace characters.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_DOUBLE
Identifier for the double data type.
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)