173 {
175 long i, i_arg;
176 SCANNED_ARG *s_arg;
177 char *description_text, *description_contents;
178 long tmpfile_used, noWarnings;
179 char *input, *output, *ptr, *buffer;
180 long ascii_output, binary_output, recover, recovered;
181 unsigned long pipeFlags, majorOrderFlag;
182
183 char **retain_column, **delete_column;
184 long retain_columns, delete_columns, rename_columns, edit_column_requests;
186 STRING_PAIR *rename_column;
187 char **orig_column_name, **new_column_name;
188 long *orig_column_flag = NULL;
189 int32_t orig_column_names;
190
191 char **retain_parameter, **delete_parameter;
192 long retain_parameters, delete_parameters, rename_parameters, edit_parameter_requests;
194 STRING_PAIR *rename_parameter;
195 char **orig_parameter_name, **new_parameter_name;
196 long *orig_parameter_flag = NULL;
197 int32_t orig_parameter_names;
198
199 char **retain_array, **delete_array;
200 long retain_arrays, delete_arrays, rename_arrays, edit_array_requests;
202 STRING_PAIR *rename_array;
203 char **orig_array_name, **new_array_name;
204 long *orig_array_flag = NULL;
205 int32_t orig_array_names;
206
207 long lines_per_row, pageNumber = 0, fromPage, toPage;
208 int64_t rows;
209 long max_size, output_columns;
210 long *removePage, *keepPage;
211 long removePages, keepPages, keep;
212 int64_t rowLimit;
213 short column_major = -1;
214
215 long convert_units_arrays = 0, convert_units_columns = 0, convert_units_parameters = 0;
216 CONVERT_UNITS *convert_units_array = NULL, *convert_units_column = NULL, *convert_units_parameter = NULL;
217
218 new_column_name = new_parameter_name = new_array_name = NULL;
219
221 argc =
scanargs(&s_arg, argc, argv);
222 if (argc < 3) {
223 fprintf(stderr, "%s\n", USAGE);
224 return 1;
225 }
226 input = output = NULL;
227 ascii_output = binary_output = noWarnings = 0;
228
229 retain_column = delete_column = NULL;
230 retain_columns = delete_columns = rename_columns = edit_column_requests = 0;
231 rename_column = NULL;
232 edit_column_request = NULL;
233
234 retain_parameter = delete_parameter = NULL;
235 retain_parameters = delete_parameters = rename_parameters = edit_parameter_requests = 0;
236 rename_parameter = NULL;
237 edit_parameter_request = NULL;
238
239 retain_array = delete_array = NULL;
240 retain_arrays = delete_arrays = rename_arrays = edit_array_requests = 0;
241 rename_array = NULL;
242 edit_array_request = NULL;
243
244 tmpfile_used = 0;
245 description_text = description_contents = NULL;
246 fromPage = toPage = recover = 0;
247 removePage = keepPage = NULL;
248 removePages = keepPages = 0;
249 lines_per_row = 1;
250 pipeFlags = 0;
251
252 for (i_arg = 1; i_arg < argc; i_arg++) {
253 if (s_arg[i_arg].arg_type == OPTION) {
255 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
256 case SET_MAJOR_ORDER:
257 majorOrderFlag = 0;
258 s_arg[i_arg].n_items -= 1;
259 if (s_arg[i_arg].n_items > 0 &&
261 &s_arg[i_arg].n_items, 0,
262 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
263 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER,
264 NULL)))
265 SDDS_Bomb(
"invalid -majorOrder syntax/values");
266 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
267 column_major = 1;
268 if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
269 column_major = 0;
270 break;
271 case SET_BINARY:
272 binary_output = 1;
273 ascii_output = 0;
274 break;
275 case SET_ASCII:
276 ascii_output = 1;
277 binary_output = 0;
278 break;
279 case SET_CONVERT_UNITS:
280 if ((s_arg[i_arg].n_items != 4) && (s_arg[i_arg].n_items != 5) &&
281 (s_arg[i_arg].n_items != 6)) {
282 fprintf(stderr, "Error (%s): invalid -convertUnits syntax\n", argv[0]);
283 return 1;
284 }
285 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
286 case ARRAY_MODE:
287 convert_units_array =
trealloc(convert_units_array,
288 sizeof(*convert_units_array) * (convert_units_arrays + 1));
289 convert_units_array[convert_units_arrays].name = s_arg[i_arg].list[2];
290 convert_units_array[convert_units_arrays].new_units = s_arg[i_arg].list[3];
291 if (s_arg[i_arg].n_items >= 5)
292 convert_units_array[convert_units_arrays].old_units = s_arg[i_arg].list[4];
293 else
294 convert_units_array[convert_units_arrays].old_units = NULL;
295 if (s_arg[i_arg].n_items == 6) {
296 if (sscanf(s_arg[i_arg].list[5], "%lf",
297 &(convert_units_array[convert_units_arrays].factor)) != 1) {
298 fprintf(stderr, "Error (%s): invalid -convertUnits syntax or value\n", argv[0]);
299 return 1;
300 }
301 } else
302 convert_units_array[convert_units_arrays].factor = 1.0;
303 convert_units_arrays++;
304 break;
305 case COLUMN_MODE:
306 convert_units_column =
trealloc(convert_units_column,
307 sizeof(*convert_units_column) * (convert_units_columns + 1));
308 convert_units_column[convert_units_columns].name = s_arg[i_arg].list[2];
309 convert_units_column[convert_units_columns].new_units = s_arg[i_arg].list[3];
310 if (s_arg[i_arg].n_items >= 5)
311 convert_units_column[convert_units_columns].old_units = s_arg[i_arg].list[4];
312 else
313 convert_units_column[convert_units_columns].old_units = NULL;
314 if (s_arg[i_arg].n_items == 6) {
315 if (sscanf(s_arg[i_arg].list[5], "%lf",
316 &(convert_units_column[convert_units_columns].factor)) != 1) {
317 fprintf(stderr, "Error (%s): invalid -convertUnits syntax or value\n", argv[0]);
318 return 1;
319 }
320 } else
321 convert_units_column[convert_units_columns].factor = 1.0;
322 convert_units_columns++;
323 break;
324 case PARAMETER_MODE:
325 convert_units_parameter =
trealloc(convert_units_parameter,
326 sizeof(*convert_units_parameter) * (convert_units_parameters + 1));
327 convert_units_parameter[convert_units_parameters].name = s_arg[i_arg].list[2];
328 convert_units_parameter[convert_units_parameters].new_units = s_arg[i_arg].list[3];
329 if (s_arg[i_arg].n_items >= 5)
330 convert_units_parameter[convert_units_parameters].old_units = s_arg[i_arg].list[4];
331 else
332 convert_units_parameter[convert_units_parameters].old_units = NULL;
333 if (s_arg[i_arg].n_items == 6) {
334 if (sscanf(s_arg[i_arg].list[5], "%lf",
335 &(convert_units_parameter[convert_units_parameters].factor)) != 1) {
336 fprintf(stderr, "Error (%s): invalid -convertUnits syntax or value\n", argv[0]);
337 return 1;
338 }
339 } else
340 convert_units_parameter[convert_units_parameters].factor = 1.0;
341 convert_units_parameters++;
342 break;
343 default:
344 fprintf(stderr, "Error (%s): invalid -convertUnits syntax: specify column, parameter or array\n", argv[0]);
345 return 1;
346 }
347 break;
348 case SET_DELETE:
349 if (s_arg[i_arg].n_items < 3) {
350 fprintf(stderr, "Error (%s): invalid -delete syntax\n", argv[0]);
351 return 1;
352 }
353 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
354 case COLUMN_MODE:
355 delete_column =
trealloc(delete_column,
356 sizeof(*delete_column) * (delete_columns + s_arg[i_arg].n_items - 2));
357 for (i = 2; i < s_arg[i_arg].n_items; i++)
358 delete_column[i - 2 + delete_columns] = s_arg[i_arg].list[i];
359 delete_columns += s_arg[i_arg].n_items - 2;
360 break;
361 case PARAMETER_MODE:
362 delete_parameter =
trealloc(delete_parameter,
363 sizeof(*delete_parameter) * (delete_parameters + s_arg[i_arg].n_items - 2));
364 for (i = 2; i < s_arg[i_arg].n_items; i++)
365 delete_parameter[i - 2 + delete_parameters] = s_arg[i_arg].list[i];
366 delete_parameters += s_arg[i_arg].n_items - 2;
367 break;
368 case ARRAY_MODE:
369 delete_array =
trealloc(delete_array,
370 sizeof(*delete_array) * (delete_arrays + s_arg[i_arg].n_items - 2));
371 for (i = 2; i < s_arg[i_arg].n_items; i++)
372 delete_array[i - 2 + delete_arrays] = s_arg[i_arg].list[i];
373 delete_arrays += s_arg[i_arg].n_items - 2;
374 break;
375 default:
376 fprintf(stderr, "Error (%s): invalid -delete syntax: specify column, parameter or array\n", argv[0]);
377 return 1;
378 }
379 break;
380 case SET_RETAIN:
381 if (s_arg[i_arg].n_items < 3) {
382 fprintf(stderr, "Error (%s): invalid -retain syntax\n", argv[0]);
383 return 1;
384 }
385 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
386 case COLUMN_MODE:
387 retain_column =
trealloc(retain_column,
388 sizeof(*retain_column) * (retain_columns + s_arg[i_arg].n_items - 2));
389 for (i = 2; i < s_arg[i_arg].n_items; i++)
390 retain_column[i - 2 + retain_columns] = s_arg[i_arg].list[i];
391 retain_columns += s_arg[i_arg].n_items - 2;
392 break;
393 case PARAMETER_MODE:
394 retain_parameter =
trealloc(retain_parameter,
395 sizeof(*retain_parameter) * (retain_parameters + s_arg[i_arg].n_items - 2));
396 for (i = 2; i < s_arg[i_arg].n_items; i++)
397 retain_parameter[i - 2 + retain_parameters] = s_arg[i_arg].list[i];
398 retain_parameters += s_arg[i_arg].n_items - 2;
399 break;
400 case ARRAY_MODE:
401 retain_array =
trealloc(retain_array,
402 sizeof(*retain_array) * (retain_arrays + s_arg[i_arg].n_items - 2));
403 for (i = 2; i < s_arg[i_arg].n_items; i++)
404 retain_array[i - 2 + retain_arrays] = s_arg[i_arg].list[i];
405 retain_arrays += s_arg[i_arg].n_items - 2;
406 break;
407 default:
408 fprintf(stderr, "Error (%s): invalid -retain syntax: specify column, parameter or array\n", argv[0]);
409 return 1;
410 }
411 break;
412 case SET_RENAME:
413 if (s_arg[i_arg].n_items < 3) {
414 fprintf(stderr, "Error (%s): invalid -rename syntax\n", argv[0]);
415 return 1;
416 }
417 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
418 case COLUMN_MODE:
419 rename_column =
trealloc(rename_column,
420 sizeof(*rename_column) * (rename_columns + s_arg[i_arg].n_items - 2));
421 for (i = 2; i < s_arg[i_arg].n_items; i++) {
422 if (!(ptr = strchr(s_arg[i_arg].list[i], '='))) {
423 fprintf(stderr, "Error (%s): invalid -rename syntax\n", argv[0]);
424 return 1;
425 }
426 *ptr++ = 0;
427 rename_column[i - 2 + rename_columns][0] = s_arg[i_arg].list[i];
428 rename_column[i - 2 + rename_columns][1] = ptr;
429 }
430 rename_columns += s_arg[i_arg].n_items - 2;
431 break;
432 case PARAMETER_MODE:
433 rename_parameter =
trealloc(rename_parameter,
434 sizeof(*rename_parameter) * (rename_parameters + s_arg[i_arg].n_items - 2));
435 for (i = 2; i < s_arg[i_arg].n_items; i++) {
436 if (!(ptr = strchr(s_arg[i_arg].list[i], '='))) {
437 fprintf(stderr, "Error (%s): invalid -rename syntax\n", argv[0]);
438 return 1;
439 }
440 *ptr++ = 0;
441 rename_parameter[i - 2 + rename_parameters][0] = s_arg[i_arg].list[i];
442 rename_parameter[i - 2 + rename_parameters][1] = ptr;
443 }
444 rename_parameters += s_arg[i_arg].n_items - 2;
445 break;
446 case ARRAY_MODE:
447 rename_array =
trealloc(rename_array,
448 sizeof(*rename_array) * (rename_arrays + s_arg[i_arg].n_items - 2));
449 for (i = 2; i < s_arg[i_arg].n_items; i++) {
450 if (!(ptr = strchr(s_arg[i_arg].list[i], '='))) {
451 fprintf(stderr, "Error (%s): invalid -rename syntax\n", argv[0]);
452 return 1;
453 }
454 *ptr++ = 0;
455 rename_array[i - 2 + rename_arrays][0] = s_arg[i_arg].list[i];
456 rename_array[i - 2 + rename_arrays][1] = ptr;
457 }
458 rename_arrays += s_arg[i_arg].n_items - 2;
459 break;
460 default:
461 fprintf(stderr, "Error (%s): invalid -rename syntax: specify column, parameter or array\n", argv[0]);
462 return 1;
463 }
464 break;
465 case SET_DESCRIPTION:
466 if (s_arg[i_arg].n_items != 3) {
467 fprintf(stderr, "Error (%s): invalid -description syntax\n", argv[0]);
468 return 1;
469 }
470 description_text = s_arg[i_arg].list[1];
471 description_contents = s_arg[i_arg].list[2];
472 break;
473 case SET_TABLE:
474 if (s_arg[i_arg].n_items < 2) {
475 fprintf(stderr, "Error (%s): invalid -table syntax\n", argv[0]);
476 return 1;
477 }
478 if (pageNumber != 0) {
479 fprintf(stderr, "Error (%s): specify -table once only\n", argv[0]);
480 return 1;
481 }
482 if (sscanf(s_arg[i_arg].list[1], "%ld", &pageNumber) != 1 || pageNumber <= 0) {
483 fprintf(stderr, "Error (%s): invalid -table value\n", argv[0]);
484 return 1;
485 }
486 fromPage = toPage = pageNumber;
487 break;
488 case SET_FROMPAGE:
489 if (s_arg[i_arg].n_items < 2) {
490 fprintf(stderr, "Error (%s): invalid -fromPage syntax\n", argv[0]);
491 return 1;
492 }
493 if (fromPage != 0) {
494 fprintf(stderr, "Error (%s): specify -fromPage once only\n", argv[0]);
495 return 1;
496 }
497 if (sscanf(s_arg[i_arg].list[1], "%ld", &fromPage) != 1 || fromPage <= 0) {
498 fprintf(stderr, "Error (%s): invalid -fromPage value\n", argv[0]);
499 return 1;
500 }
501 break;
502 case SET_TOPAGE:
503 if (s_arg[i_arg].n_items < 2) {
504 fprintf(stderr, "Error (%s): invalid -toPage syntax\n", argv[0]);
505 return 1;
506 }
507 if (toPage != 0) {
508 fprintf(stderr, "Error (%s): specify -toPage once only\n", argv[0]);
509 return 1;
510 }
511 if (sscanf(s_arg[i_arg].list[1], "%ld", &toPage) != 1 || toPage <= 0) {
512 fprintf(stderr, "Error (%s): invalid -toPage value\n", argv[0]);
513 return 1;
514 }
515 break;
516 case SET_REMOVEPAGES:
517 if (s_arg[i_arg].n_items < 2) {
518 fprintf(stderr, "Error (%s): invalid -removePages syntax\n", argv[0]);
519 return 1;
520 }
521 if (removePage != NULL) {
522 fprintf(stderr, "Error (%s): specify -removePages once only\n", argv[0]);
523 return 1;
524 }
525 removePages = s_arg[i_arg].n_items - 1;
526 removePage =
trealloc(removePage,
sizeof(*removePage) * (removePages));
527 for (i = 0; i < removePages; i++) {
528 if (sscanf(s_arg[i_arg].list[i + 1], "%ld", &removePage[i]) != 1 || removePage[i] <= 0) {
529 fprintf(stderr, "Error (%s): invalid -removePages value\n", argv[0]);
530 return 1;
531 }
532 }
533 break;
534 case SET_KEEPPAGES:
535 if (s_arg[i_arg].n_items < 2) {
536 fprintf(stderr, "Error (%s): invalid -keepPages syntax\n", argv[0]);
537 return 1;
538 }
539 if (keepPage != NULL) {
540 fprintf(stderr, "Error (%s): specify -keepPages once only\n", argv[0]);
541 return 1;
542 }
543 keepPages = s_arg[i_arg].n_items - 1;
544 keepPage =
trealloc(keepPage,
sizeof(*keepPage) * (keepPages));
545 for (i = 0; i < keepPages; i++) {
546 if (sscanf(s_arg[i_arg].list[i + 1], "%ld", &keepPage[i]) != 1 || keepPage[i] <= 0) {
547 fprintf(stderr, "Error (%s): invalid -keepPages value\n", argv[0]);
548 return 1;
549 }
550 }
551 break;
552 case SET_EDIT_NAMES:
553 if (s_arg[i_arg].n_items < 4) {
554 fprintf(stderr, "Error (%s): invalid -editnames syntax\n", argv[0]);
555 return 1;
556 }
557 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
558 case COLUMN_MODE:
559 edit_column_request =
trealloc(edit_column_request,
560 sizeof(*edit_column_request) * (edit_column_requests + 1));
561 edit_column_request[edit_column_requests].match_string = s_arg[i_arg].list[2];
562 edit_column_request[edit_column_requests].edit_string = s_arg[i_arg].list[3];
563 edit_column_requests++;
564 break;
565 case PARAMETER_MODE:
566 edit_parameter_request =
trealloc(edit_parameter_request,
567 sizeof(*edit_parameter_request) * (edit_parameter_requests + 1));
568 edit_parameter_request[edit_parameter_requests].match_string = s_arg[i_arg].list[2];
569 edit_parameter_request[edit_parameter_requests].edit_string = s_arg[i_arg].list[3];
570 edit_parameter_requests++;
571 break;
572 case ARRAY_MODE:
573 edit_array_request =
trealloc(edit_array_request,
574 sizeof(*edit_array_request) * (edit_array_requests + 1));
575 edit_array_request[edit_array_requests].match_string = s_arg[i_arg].list[2];
576 edit_array_request[edit_array_requests].edit_string = s_arg[i_arg].list[3];
577 edit_array_requests++;
578 break;
579 default:
580 fprintf(stderr, "Error (%s): invalid -editnames syntax\n", argv[0]);
581 return 1;
582 }
583 break;
584 case SET_LINES_PER_ROW:
585 if (s_arg[i_arg].n_items != 2 ||
586 sscanf(s_arg[i_arg].list[1], "%ld", &lines_per_row) != 1 ||
587 lines_per_row <= 0) {
588 fprintf(stderr, "Error (%s): invalid -linesperrow syntax\n", argv[0]);
589 return 1;
590 }
591 break;
592 case SET_NOWARNINGS:
593 if (s_arg[i_arg].n_items != 1) {
594 fprintf(stderr, "Error (%s): invalid -nowarnings syntax\n", argv[0]);
595 return 1;
596 }
597 noWarnings = 1;
598 break;
599 case SET_RECOVER:
600 recover = 1;
601 if (s_arg[i_arg].n_items != 1) {
602 recover = 2;
603 if (s_arg[i_arg].n_items > 2 ||
604 strncmp(s_arg[i_arg].list[1], "clip", strlen(s_arg[i_arg].list[1])) != 0) {
605 fprintf(stderr, "Error (%s): invalid -recover syntax\n", argv[0]);
606 return 1;
607 }
608 }
609 break;
610 case SET_PIPE:
611 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
612 fprintf(stderr, "Error (%s): invalid -pipe syntax\n", argv[0]);
613 return 1;
614 }
615 break;
616 case SET_ACCEPT_ALL_NAMES:
618 break;
619 case SET_ROWLIMIT:
620 if (s_arg[i_arg].n_items != 2 ||
621 sscanf(s_arg[i_arg].list[1], "%" SCNd64, &rowLimit) != 1 ||
622 rowLimit < 0) {
623 fprintf(stderr, "Error (%s): invalid -rowLimit syntax\n", argv[0]);
624 return 1;
625 }
627 break;
628 default:
629 fprintf(stderr, "Error (%s): unknown switch: %s\n", argv[0], s_arg[i_arg].list[0]);
630 return 1;
631 }
632 } else {
633 if (input == NULL)
634 input = s_arg[i_arg].list[0];
635 else if (output == NULL)
636 output = s_arg[i_arg].list[0];
637 else {
638 fprintf(stderr, "Error (%s): too many filenames\n", argv[0]);
639 return 1;
640 }
641 }
642 }
643
644 if (fromPage && toPage && fromPage > toPage) {
645 fprintf(stderr, "Error (%s): invalid -fromPage and -toPage\n", argv[0]);
646 return 1;
647 }
648
649 processFilenames(
"sddsconvert", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
650
651#ifdef DEBUG
652 fprintf(stderr, "Initializing input and output files.\n");
653#endif
656 return 1;
657 }
658
659 if (!set_units_conversion(&SDDS_orig, convert_units_array, convert_units_arrays,
660 convert_units_column, convert_units_columns,
661 convert_units_parameter, convert_units_parameters)) {
663 return 1;
664 }
665
666 if (!description_text)
668
670 ascii_output ? SDDS_ASCII : (binary_output ? SDDS_BINARY : SDDS_orig.layout.data_mode.mode),
671 lines_per_row, description_text, description_contents, output)) {
673 return 1;
674 }
675
677
678 if (column_major != -1)
679 SDDS_dataset.layout.data_mode.column_major = column_major;
680 else
681 SDDS_dataset.layout.data_mode.column_major = SDDS_orig.layout.data_mode.column_major;
682
683#ifdef DEBUG
684 fprintf(stderr, "Getting column, parameter, and array names from input file.\n");
685#endif
688 return 1;
689 }
690 if (orig_column_names &&
691 !(new_column_name = process_name_options(orig_column_name, &orig_column_flag, orig_column_names,
692 delete_column, delete_columns,
693 retain_column, retain_columns,
694 rename_column, rename_columns,
695 edit_column_request, edit_column_requests)))
696 return 1;
697
700 return 1;
701 }
702
703#ifdef DEBUG
704 fprintf(stderr, "processing name options\n");
705#endif
706 if (orig_parameter_names &&
707 !(new_parameter_name = process_name_options(orig_parameter_name, &orig_parameter_flag, orig_parameter_names,
708 delete_parameter, delete_parameters,
709 retain_parameter, retain_parameters,
710 rename_parameter, rename_parameters,
711 edit_parameter_request, edit_parameter_requests)))
712 return 1;
713
716 return 1;
717 }
718 if (orig_array_names &&
719 !(new_array_name = process_name_options(orig_array_name, &orig_array_flag, orig_array_names,
720 delete_array, delete_arrays,
721 retain_array, retain_arrays,
722 rename_array, rename_arrays,
723 edit_array_request, edit_array_requests)))
724 return 1;
725
726#ifdef DEBUG
727 for (i = 0; i < orig_parameter_names; i++)
728 fprintf(stderr, "parameter %s goes to %s\n", orig_parameter_name[i],
729 orig_parameter_flag[i] ? new_parameter_name[i] : "<deleted>");
730 for (i = 0; i < orig_array_names; i++)
731 fprintf(stderr, "array %s goes to %s\n", orig_array_name[i],
732 orig_array_flag[i] ? new_array_name[i] : "<deleted>");
733 for (i = 0; i < orig_column_names; i++)
734 fprintf(stderr, "column %s goes to %s\n", orig_column_name[i],
735 orig_column_flag[i] ? new_column_name[i] : "<deleted>");
736#endif
737
738#ifdef DEBUG
739 fprintf(stderr, "Transferring definitions to new file\n");
740#endif
741 for (i = 0; i < orig_parameter_names; i++) {
742 if (orig_parameter_flag[i]) {
744 orig_parameter_name[i], new_parameter_name[i])) {
745 fprintf(stderr, "unable to transfer parameter %s to %s\n",
746 orig_parameter_name[i], new_parameter_name[i]);
748 return 1;
749 }
750 }
751 }
752 for (i = 0; i < orig_array_names; i++) {
753 if (orig_array_flag[i]) {
755 orig_array_name[i], new_array_name[i])) {
756 fprintf(stderr, "unable to transfer array %s to %s\n",
757 orig_array_name[i], new_array_name[i]);
759 return 1;
760 }
761 }
762 }
763 output_columns = 0;
764 for (i = 0; i < orig_column_names; i++) {
765 if (orig_column_flag[i]) {
766 output_columns++;
768 orig_column_name[i], new_column_name[i])) {
769 fprintf(stderr, "unable to transfer column %s to %s\n",
770 orig_column_name[i], new_column_name[i]);
772 return 1;
773 }
774 }
775 }
776
777#ifdef DEBUG
778 fprintf(stderr, "Writing layout.\n");
779#endif
782 return 1;
783 }
784
785 max_size = 0;
789 buffer =
tmalloc(max_size *
sizeof(
char));
790
791 recovered = 0;
792#ifdef DEBUG
793 fprintf(stderr, "Reading file\n");
794#endif
795 while (!recovered && (pageNumber =
SDDS_ReadPage(&SDDS_orig)) >= 0) {
796 if (pageNumber == 0) {
797 if (!recover) {
798 fprintf(stderr, "error: SDDS data garbled--consider using -recover option\n");
799 fprintf(stderr, "warning: one or more data pages may be missing\n");
800 break;
801 } else {
803 recovered = 1;
804 else {
805 if (recover != 2)
806 fprintf(stderr, "warning: unable to recover data--pages may be missing\n");
807 break;
808 }
809 }
810 }
811 if (fromPage && pageNumber < fromPage)
812 continue;
813 if (removePages) {
814 keep = 1;
815 for (i = 0; i < removePages; i++) {
816 if (removePage[i] == pageNumber) {
817 keep = 0;
818 break;
819 }
820 }
821 if (!keep)
822 continue;
823 }
824 if (keepPages) {
825 keep = 0;
826 for (i = 0; i < keepPages; i++) {
827 if (keepPage[i] == pageNumber) {
828 keep = 1;
829 break;
830 }
831 }
832 if (!keep)
833 continue;
834 }
835 if ((rows = SDDS_RowCount(&SDDS_orig)) < 0) {
836 fprintf(stderr, "error: problem counting rows in input page\n");
838 return 1;
839 }
841 fprintf(stderr, "error: problem starting output page\n");
843 return 1;
844 }
845
846 if (!set_units_conversion(&SDDS_orig, convert_units_array, convert_units_arrays,
847 convert_units_column, convert_units_columns,
848 convert_units_parameter, convert_units_parameters)) {
850 return 1;
851 }
852
853 for (i = 0; i < orig_parameter_names; i++) {
854 if (orig_parameter_flag[i]) {
856 fprintf(stderr, "error: problem getting parameter %s\n", orig_parameter_name[i]);
858 return 1;
859 }
861 new_parameter_name[i], buffer, NULL)) {
862 fprintf(stderr, "error: problem setting parameter %s\n", new_parameter_name[i]);
864 return 1;
865 }
866 }
867 }
868 for (i = 0; i < orig_array_names; i++) {
870 if (orig_array_flag[i]) {
871 if (!(array =
SDDS_GetArray(&SDDS_orig, orig_array_name[i], NULL))) {
872 fprintf(stderr, "error: problem getting array %s\n", orig_array_name[i]);
874 return 1;
875 }
876 if (!
SDDS_SetArray(&SDDS_dataset, new_array_name[i], SDDS_CONTIGUOUS_DATA,
877 array->data, array->dimension)) {
878 fprintf(stderr, "error: problem setting array %s\n", new_array_name[i]);
880 return 1;
881 }
883 }
884 }
885
887 for (i = 0; i < orig_column_names; i++) {
888 if (orig_column_flag[i]) {
890 fprintf(stderr, "error: problem getting column %s\n", orig_column_name[i]);
892 return 1;
893 }
894 if (!
SDDS_SetColumn(&SDDS_dataset, SDDS_SET_BY_NAME, ptr, rows, new_column_name[i])) {
895 fprintf(stderr, "error: problem setting column %s\n", new_column_name[i]);
897 return 1;
898 }
899 }
900 }
901 }
903 fprintf(stderr, "error: problem writing page to file %s\n", output);
905 return 1;
906 }
907 if (toPage && pageNumber >= toPage)
908 break;
909 }
910
913 return 1;
914 }
915
917 return 1;
918
919 for (i = 0; i < orig_parameter_names; i++) {
920 if (orig_parameter_name[i])
921 free(orig_parameter_name[i]);
922 if (new_parameter_name[i])
923 free(new_parameter_name[i]);
924 }
925 if (orig_parameter_name)
926 free(orig_parameter_name);
927 if (new_parameter_name)
928 free(new_parameter_name);
929 if (orig_parameter_flag)
930 free(orig_parameter_flag);
931
932 for (i = 0; i < orig_column_names; i++) {
933 if (orig_column_name[i])
934 free(orig_column_name[i]);
935 if (new_column_name[i])
936 free(new_column_name[i]);
937 }
938 if (orig_column_name)
939 free(orig_column_name);
940 if (new_column_name)
941 free(new_column_name);
942 if (orig_column_flag)
943 free(orig_column_flag);
944
945 for (i = 0; i < orig_array_names; i++) {
946 if (orig_array_name[i])
947 free(orig_array_name[i]);
948 if (new_array_name[i])
949 free(new_array_name[i]);
950 }
951 if (orig_array_name)
952 free(orig_array_name);
953 if (new_array_name)
954 free(new_array_name);
955 if (orig_array_flag)
956 free(orig_array_flag);
957
958 return 0;
959}
int32_t SDDS_ReadRecoveryPossible(SDDS_DATASET *SDDS_dataset)
Checks if any data in an SDDS page was recovered after an error was detected.
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
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_SetArray(SDDS_DATASET *SDDS_dataset, char *array_name, int32_t mode, void *data_pointer, int32_t *dimension)
Sets the values of an array variable in the SDDS dataset using specified dimensions.
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_SetNameValidityFlags(uint32_t flags)
Sets the validity flags for parameter and column names in the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_TransferColumnDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a column definition from a source dataset to a target dataset.
int32_t SDDS_TransferArrayDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers an array definition from a source dataset to a target dataset.
int32_t SDDS_TransferParameterDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a parameter definition from a source dataset to a target dataset.
void SDDS_FreeArray(SDDS_ARRAY *array)
Frees memory allocated for an SDDS array structure.
char ** SDDS_GetParameterNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all parameters in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
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.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
char ** SDDS_GetArrayNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all arrays in the SDDS dataset.
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
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.
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
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.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
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)
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.