69#define PARAMETER_MODE 1
72static char *mode_name[MODES] = {
78#define COLUMN_REPLACE 0
79#define PARAMETER_REPLACE 1
80#define ARRAY_REPLACE 2
81#define REPLACE_TYPES 3
82static char *replace_type[REPLACE_TYPES] = {
83 "column",
"parameter",
"array"};
85#define PARAMETER_TRANSFER 0
86#define ARRAY_TRANSFER 1
87#define TRANSFER_TYPES 2
88static char *transfer_type[TRANSFER_TYPES] = {
89 "parameter",
"array"};
100 char **new_parameter;
103 char **orig_parameter;
117long expandTransferRequests(
char ***match, int32_t *matches,
long type,
TRANSFER_DEFINITION *transfer,
123 EDIT_NAME_REQUEST *edit_array_request,
long edit_array_requests,
long filenumber);
125char **process_editnames(
char **orig_name,
long **orig_flags,
long orig_names,
EDIT_NAME_REQUEST *edit_request,
126 long edit_requests,
long filenumber);
128 REFDATA new_data,
long columns,
char *input2);
132typedef char *STRING_PAIR[2];
134char *option[N_OPTIONS] = {
135 "take",
"leave",
"match",
"equate",
"transfer",
"reuse",
"ifnot",
136 "nowarnings",
"ifis",
"pipe",
"fillin",
"rename",
"editnames",
"wildmatch",
"majorOrder",
"replace"};
140 " sddsxref [<input1>] <input2> <input3> [<input4>...] [<output>]\n\n"
142 " -pipe[=input][,output]\n"
143 " Enable piping. Optionally specify input and/or output streams.\n\n"
144 " -ifis={column|parameter|array},<name>[,...]\n"
145 " Specify names of parameters, arrays, or columns that must exist in <input1>.\n\n"
146 " -ifnot={parameter|column|array},<name>[,...]\n"
147 " Specify names of parameters, arrays, or columns that must not exist in <input1>.\n\n"
148 " -transfer={parameter|array},<name>[,...]\n"
149 " Specify parameters or arrays to transfer from <input2>.\n\n"
150 " -take=<column-name>[,...]\n"
151 " Specify columns to take from <input2>.\n\n"
152 " -leave=<column-name>[,...]\n"
153 " Specify columns not to take from <input2>. Overrides -take for specified columns.\n"
154 " Use -leave=* to exclude all columns.\n\n"
155 " -replace=column|parameter|array,<name list>\n"
156 " Replace specified columns, parameters, or arrays in <input1> with those from subsequent input files.\n\n"
158 " Fill in NULL and 0 values in rows where no match is found. By default, such rows are omitted.\n\n"
159 " -reuse[=[rows][,page]]\n"
160 " Allow reuse of rows from <input2>. Use -reuse=page to restrict to the first page of <input2>.\n\n"
161 " -match=<column-name>[=<column-name>]\n"
162 " Specify columns to match between <input1> and <input2> for data selection and placement.\n\n"
163 " -wildMatch=<column-name>[=<column-name>]\n"
164 " Similar to -match, but allows wildcards in the matching data from <input2>.\n\n"
165 " -rename={column|parameter|array},<oldname>=<newname>[,...]\n"
166 " Rename specified columns, parameters, or arrays in the output data set.\n\n"
167 " -editnames={column|parameter|array},<wildcard-string>,<edit-string>\n"
168 " Edit names of specified entities using wildcard patterns and edit commands.\n\n"
169 " -equate=<column-name>[=<column-name>]\n"
170 " Equate columns between <input1> and <input2> for data matching based on equality.\n\n"
171 " -majorOrder=row|column\n"
172 " Specify the major order of data in the output (row or column). Defaults to the order of <input1>.\n\n"
173 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
175int main(
int argc,
char **argv) {
178 REFDATA *new_data, rename_data, *take_RefData, *replace_RefData;
179 long j, i_arg, reuse, reusePage, endWarning, k;
180 int64_t i, i1, i2, i3, rows1, rows2, rows2Max;
184 char **take_column, **leave_column, **replace_column, **replace_parameter, **replace_array, **output_column = NULL;
185 char **inputfile, **referfile;
186 char **match_column, **equate_column;
187 long take_columns, replace_columns, leave_columns, match_columns, equate_columns, leave_all_columns, replace_parameters, replace_arrays;
188 int32_t output_columns = 0;
189 char *input1, *input2, *output;
190 long tmpfile_used, retval1, retval2, inputfiles, referfiles;
196 unsigned long pipeFlags, majorOrderFlag;
197 long fillIn, keyGroups = 0;
198 KEYED_EQUIVALENT **keyGroup = NULL;
199 long outputInitialized;
200 int z, it, itm, datatype1, datatype2;
202 int firstRun, copyInput1Only;
203 char **string1, **string2;
204 double *value1, *value2;
206 short columnMajorOrder = -1;
208 EDIT_NAME_REQUEST *edit_column_request, *edit_parameter_request, *edit_array_request;
209 long edit_column_requests, edit_parameter_requests, edit_array_requests;
212 argc =
scanargs(&s_arg, argc, argv);
214 fprintf(stderr,
"%s", USAGE);
219 take_RefData = replace_RefData = NULL;
222 rename_data.columns = rename_data.parameters = rename_data.arrays = 0;
223 rename_data.new_column = rename_data.orig_column = rename_data.new_parameter = rename_data.orig_parameter = rename_data.new_array = rename_data.orig_array = NULL;
224 edit_column_request = edit_parameter_request = edit_array_request = NULL;
225 edit_column_requests = edit_parameter_requests = edit_array_requests = 0;
227 input1 = input2 = output = NULL;
228 take_column = leave_column = replace_column = replace_parameter = replace_array = NULL;
229 match_column = equate_column = NULL;
230 inputfile = referfile = NULL;
231 take_columns = leave_columns = replace_columns = match_columns = equate_columns = reuse = reusePage = replace_parameters = replace_arrays = 0;
232 tmpfile_used = inputfiles = referfiles = 0;
235 ifnot_item.items = ifis_item.items = 0;
239 outputInitialized = 0;
240 rows1 = rows2 = output_columns = 0;
241 string1 = string2 = NULL;
244 for (i_arg = 1; i_arg < argc; i_arg++) {
245 if (s_arg[i_arg].arg_type == OPTION) {
247 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
248 case SET_MAJOR_ORDER:
250 s_arg[i_arg].n_items--;
251 if (s_arg[i_arg].n_items > 0 &&
252 (!
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
253 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
254 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
255 SDDS_Bomb(
"invalid -majorOrder syntax/values");
256 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
257 columnMajorOrder = 1;
258 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
259 columnMajorOrder = 0;
261 case SET_LEAVE_COLUMNS:
262 if (s_arg[i_arg].n_items < 2)
264 leave_column =
trealloc(leave_column,
sizeof(*leave_column) * (leave_columns + s_arg[i_arg].n_items - 1));
265 for (i = 1; i < s_arg[i_arg].n_items; i++)
266 leave_column[i - 1 + leave_columns] = s_arg[i_arg].list[i];
267 leave_columns += s_arg[i_arg].n_items - 1;
269 case SET_TAKE_COLUMNS:
270 if (s_arg[i_arg].n_items < 2)
272 take_column =
trealloc(take_column,
sizeof(*take_column) * (take_columns + s_arg[i_arg].n_items - 1));
273 for (i = 1; i < s_arg[i_arg].n_items; i++)
274 take_column[i - 1 + take_columns] = s_arg[i_arg].list[i];
275 take_columns += s_arg[i_arg].n_items - 1;
280 case SET_MATCH_COLUMN:
281 if (s_arg[i_arg].n_items != 2)
282 SDDS_Bomb(
"invalid -match or -wildMatch syntax");
283 if (match_columns != 0)
284 SDDS_Bomb(
"only one -match or -wildMatch option may be given");
285 match_column =
tmalloc(
sizeof(*match_column) * 2);
286 if ((ptr = strchr(s_arg[i_arg].list[1],
'=')))
289 ptr = s_arg[i_arg].list[1];
290 match_column[0] = s_arg[i_arg].list[1];
291 match_column[1] = ptr;
294 case SET_EQUATE_COLUMN:
295 if (s_arg[i_arg].n_items != 2)
297 if (equate_columns != 0)
298 SDDS_Bomb(
"only one -equate option may be given");
299 equate_column =
tmalloc(
sizeof(*equate_column) * 2);
300 if ((ptr = strchr(s_arg[i_arg].list[1],
'=')))
303 ptr = s_arg[i_arg].list[1];
304 equate_column[0] = s_arg[i_arg].list[1];
305 equate_column[1] = ptr;
309 if (s_arg[i_arg].n_items < 3)
311 switch (
match_string(s_arg[i_arg].list[1], replace_type, REPLACE_TYPES, 0)) {
313 replace_column =
trealloc(replace_column,
sizeof(*replace_column) * (replace_columns + s_arg[i_arg].n_items - 2));
314 for (i = 2; i < s_arg[i_arg].n_items; i++)
315 replace_column[i - 2 + replace_columns] = s_arg[i_arg].list[i];
316 replace_columns += s_arg[i_arg].n_items - 2;
318 case PARAMETER_REPLACE:
319 replace_parameter =
trealloc(replace_parameter,
sizeof(*replace_parameter) * (replace_parameters + s_arg[i_arg].n_items - 2));
320 for (i = 2; i < s_arg[i_arg].n_items; i++)
321 replace_parameter[i - 2 + replace_parameters] = s_arg[i_arg].list[i];
322 replace_parameters += s_arg[i_arg].n_items - 2;
325 replace_array =
trealloc(replace_array,
sizeof(*replace_array) * (replace_arrays + s_arg[i_arg].n_items - 2));
326 for (i = 2; i < s_arg[i_arg].n_items; i++)
327 replace_array[i - 2 + replace_arrays] = s_arg[i_arg].list[i];
328 replace_arrays += s_arg[i_arg].n_items - 2;
336 if (s_arg[i_arg].n_items < 3)
338 transfer =
trealloc(transfer,
sizeof(*transfer) * (transfers + s_arg[i_arg].n_items - 2));
339 switch (
match_string(s_arg[i_arg].list[1], transfer_type, TRANSFER_TYPES, 0)) {
340 case PARAMETER_TRANSFER:
341 for (i = 2; i < s_arg[i_arg].n_items; i++) {
342 transfer[i - 2 + transfers].type = PARAMETER_TRANSFER;
343 transfer[i - 2 + transfers].name = s_arg[i_arg].list[i];
347 for (i = 2; i < s_arg[i_arg].n_items; i++) {
348 transfer[i - 2 + transfers].type = ARRAY_TRANSFER;
349 transfer[i - 2 + transfers].name = s_arg[i_arg].list[i];
356 transfers += s_arg[i_arg].n_items - 2;
359 if (s_arg[i_arg].n_items == 1)
362 char *reuseOptions[2] = {
"rows",
"page"};
363 for (i = 1; i < s_arg[i_arg].n_items; i++) {
364 switch (
match_string(s_arg[i_arg].list[i], reuseOptions, 2, 0)) {
379 if (s_arg[i_arg].n_items < 3)
381 add_ifitem(&ifnot_item, s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1);
387 if (s_arg[i_arg].n_items < 3)
389 add_ifitem(&ifis_item, s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1);
392 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
399 if (s_arg[i_arg].n_items < 3)
401 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
403 k = rename_data.columns;
404 rename_data.new_column =
trealloc(rename_data.new_column,
sizeof(
char *) * (k + s_arg[i_arg].n_items - 2));
405 rename_data.orig_column =
trealloc(rename_data.orig_column,
sizeof(
char *) * (k + s_arg[i_arg].n_items - 2));
406 for (i = 2; i < s_arg[i_arg].n_items; i++) {
407 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
410 rename_data.orig_column[k + i - 2] = s_arg[i_arg].list[i];
411 rename_data.new_column[k + i - 2] = ptr;
413 rename_data.columns += s_arg[i_arg].n_items - 2;
416 k = rename_data.parameters;
417 rename_data.new_parameter =
trealloc(rename_data.new_parameter,
sizeof(
char *) * (k + s_arg[i_arg].n_items - 2));
418 rename_data.orig_parameter =
trealloc(rename_data.orig_parameter,
sizeof(
char *) * (k + s_arg[i_arg].n_items - 2));
419 for (i = 2; i < s_arg[i_arg].n_items; i++) {
420 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
423 rename_data.orig_parameter[k + i - 2] = s_arg[i_arg].list[i];
424 rename_data.new_parameter[k + i - 2] = ptr;
426 rename_data.parameters += s_arg[i_arg].n_items - 2;
429 k = rename_data.arrays;
430 rename_data.new_array =
trealloc(rename_data.new_array,
sizeof(
char *) * (k + s_arg[i_arg].n_items - 2));
431 rename_data.orig_array =
trealloc(rename_data.orig_array,
sizeof(
char *) * (k + s_arg[i_arg].n_items - 2));
432 for (i = 2; i < s_arg[i_arg].n_items; i++) {
433 if (!(ptr = strchr(s_arg[i_arg].list[i],
'=')))
436 rename_data.orig_array[k + i - 2] = s_arg[i_arg].list[i];
437 rename_data.new_array[k + i - 2] = ptr;
439 rename_data.arrays += s_arg[i_arg].n_items - 2;
442 SDDS_Bomb(
"invalid -rename syntax: specify column, parameter, or array keyword");
447 if (s_arg[i_arg].n_items != 4)
449 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
451 edit_column_request =
trealloc(edit_column_request,
sizeof(*edit_column_request) * (edit_column_requests + 1));
452 edit_column_request[edit_column_requests].match_string = s_arg[i_arg].list[2];
453 edit_column_request[edit_column_requests].edit_string = s_arg[i_arg].list[3];
454 edit_column_requests++;
457 edit_parameter_request =
trealloc(edit_parameter_request,
sizeof(*edit_parameter_request) * (edit_parameter_requests + 1));
458 edit_parameter_request[edit_parameter_requests].match_string = s_arg[i_arg].list[2];
459 edit_parameter_request[edit_parameter_requests].edit_string = s_arg[i_arg].list[3];
460 edit_parameter_requests++;
463 edit_array_request =
trealloc(edit_array_request,
sizeof(*edit_array_request) * (edit_array_requests + 1));
464 edit_array_request[edit_array_requests].match_string = s_arg[i_arg].list[2];
465 edit_array_request[edit_array_requests].edit_string = s_arg[i_arg].list[3];
466 edit_array_requests++;
469 SDDS_Bomb(
"invalid -editnames syntax: specify column, parameter, or array keyword");
474 fprintf(stderr,
"error: unknown switch: %s\n", s_arg[i_arg].list[0]);
479 inputfile =
trealloc(inputfile,
sizeof(*inputfile) * (inputfiles + 1));
480 inputfile[inputfiles++] = s_arg[i_arg].list[0];
484 if (inputfiles == 0) {
485 SDDS_Bomb(
"too few reference files given");
487 if (!(pipeFlags & USE_STDIN) && !(pipeFlags & USE_STDOUT)) {
488 if (inputfiles < 2) {
489 SDDS_Bomb(
"too few reference files given");
490 }
else if (inputfiles == 2) {
491 input1 = output = inputfile[0];
492 referfile =
trealloc(referfile,
sizeof(*referfile) * (referfiles + 1));
493 referfile[0] = inputfile[1];
496 input1 = inputfile[0];
497 output = inputfile[--inputfiles];
498 for (z = 1; z < inputfiles; z++) {
499 referfile =
trealloc(referfile,
sizeof(*referfile) * (referfiles + 1));
500 referfile[z - 1] = inputfile[z];
504 }
else if (!(pipeFlags & USE_STDIN) && (pipeFlags & USE_STDOUT)) {
505 if (inputfiles < 2) {
506 SDDS_Bomb(
"too few reference files given");
508 input1 = inputfile[0];
509 for (z = 1; z < inputfiles; z++) {
510 referfile =
trealloc(referfile,
sizeof(*referfile) * (referfiles + 1));
511 referfile[z - 1] = inputfile[z];
515 }
else if ((pipeFlags & USE_STDIN) && !(pipeFlags & USE_STDOUT)) {
516 if (inputfiles < 2) {
517 SDDS_Bomb(
"too few reference files given");
519 output = inputfile[--inputfiles];
520 for (z = 0; z < inputfiles; z++) {
521 referfile =
trealloc(referfile,
sizeof(*referfile) * (referfiles + 1));
522 referfile[z] = inputfile[z];
527 for (z = 0; z < inputfiles; z++) {
528 referfile =
trealloc(referfile,
sizeof(*referfile) * (referfiles + 1));
529 referfile[z] = inputfile[z];
535 processFilenames(
"sddsxref", &input1, &output, pipeFlags, !warnings, &tmpfile_used);
537 if (equate_columns && match_columns)
538 SDDS_Bomb(
"only one of -equate or -match may be given");
545 if (!check_ifitems(&SDDS_1, &ifnot_item, 0, warnings) || !check_ifitems(&SDDS_1, &ifis_item, 1, warnings))
548 for (it = 0; it < ifnot_item.items; it++) {
549 switch (ifnot_item.type[it]) {
551 leave_column =
trealloc(leave_column,
sizeof(*leave_column) * (leave_columns + 1));
552 leave_column[leave_columns] = ifnot_item.name[it];
555 case PARAMETER_BASED:
556 for (itm = 0; itm < transfers; itm++) {
557 if (strcmp(transfer[itm].name, ifnot_item.name[it]) == 0) {
558 SDDS_Bomb(
"Excluded item is a part of -transfer list.");
564 for (itm = 0; itm < transfers; itm++) {
565 if (strcmp(transfer[itm].name, ifnot_item.name[it]) == 0) {
566 SDDS_Bomb(
"Excluded item is a part of -transfer list.");
572 SDDS_Bomb(
"internal error---unknown ifitem type");
579 new_data = malloc(
sizeof(*new_data) * referfiles);
580 for (z = 0; z < referfiles; z++) {
581 SDDS_ref =
trealloc(SDDS_ref,
sizeof(*SDDS_ref) * (z + 1));
582 input2 = referfile[z];
588 take_RefData =
trealloc(take_RefData,
sizeof(*take_RefData) * (z + 1));
589 take_RefData[z].columns = 0;
590 replace_RefData =
trealloc(replace_RefData,
sizeof(*replace_RefData) * (z + 1));
591 replace_RefData[z].columns = replace_RefData[z].parameters = replace_RefData[z].arrays = 0;
593 add_newnames(&SDDS_ref[z], &new_data[z], rename_data, edit_column_request, edit_column_requests,
594 edit_parameter_request, edit_parameter_requests, edit_array_request, edit_array_requests, z + 1);
600 for (i = 0; i < take_columns; i++) {
602 sprintf(s,
"error: column %s not found in file %s take_columns %ld SDDS_ref[z] %" PRId64
"\n", take_column[i], input2, take_columns, SDDS_ref[z].n_rows);
611 leave_all_columns = 0;
612 if (leave_columns == 1 && strcmp(leave_column[0],
"*") == 0)
613 leave_all_columns = 1;
617 for (i = 0; i < leave_columns; i++) {
625 if (!(take_RefData[z].orig_column = (
char **)
SDDS_GetColumnNames(&SDDS_ref[z], &take_RefData[z].columns))) {
626 SDDS_SetError(
"error: no columns selected to take from input file");
630 if (replace_columns) {
632 for (i = 0; i < replace_columns; i++) {
634 sprintf(s,
"error: column %s not found in file %s replace_columns %ld SDDS_ref[z] %" PRId64
"\n", replace_column[i], input2, replace_columns, SDDS_ref[z].n_rows);
641 if (!(replace_RefData[z].orig_column = (
char **)
SDDS_GetColumnNames(&SDDS_ref[z], &replace_RefData[z].columns))) {
642 SDDS_SetError(
"error: no columns selected to replace from input file");
649 sprintf(s,
"error: column %s not found or not string type in file %s", match_column[0], input1 ? input1 :
"stdin");
655 sprintf(s,
"error: column %s not found or not string type in file %s", match_column[1], input2);
660 if (equate_columns) {
663 sprintf(s,
"error: column %s not found or not numeric type in file %s", equate_column[0], input1 ? input1 :
"stdin");
669 sprintf(s,
"error: column %s not found or not numeric type in file %s", equate_column[1], input2);
675 take_RefData[z].columns = 0;
676 leave_all_columns = 1;
678 if (!take_RefData[z].columns && !leave_all_columns && warnings)
679 fprintf(stderr,
"warning: there are no columns being taken from %s that are not already in %s\n", input2, input1 ? input1 :
"stdin");
681 if (leave_all_columns)
682 take_RefData[z].columns = 0;
684 if (!outputInitialized) {
689 outputInitialized = 1;
690 if (columnMajorOrder != -1)
691 SDDS_output.layout.data_mode.column_major = columnMajorOrder;
693 SDDS_output.layout.data_mode.column_major = SDDS_1.layout.data_mode.column_major;
697 if (take_RefData[z].columns)
698 take_RefData[z].new_column = (
char **)malloc(
sizeof(
char *) * take_RefData[z].columns);
700 for (i = 0; i < take_RefData[z].columns; i++) {
704 if (new_data[z].columns) {
705 k =
match_string(take_RefData[z].orig_column[i], new_data[z].orig_column, new_data[z].columns, EXACT_MATCH);
707 SDDS_CopyString(&take_RefData[z].new_column[i], take_RefData[z].orig_column[i]);
709 SDDS_CopyString(&take_RefData[z].new_column[i], new_data[z].new_column[k]);
711 SDDS_CopyString(&take_RefData[z].new_column[i], take_RefData[z].orig_column[i]);
713 free(take_RefData[z].new_column[i]);
714 free(take_RefData[z].orig_column[i]);
715 for (j = i; j < take_RefData[z].columns - 1; j++)
716 take_RefData[z].orig_column[j] = take_RefData[z].orig_column[j + 1];
717 take_RefData[z].columns -= 1;
719 if (take_RefData[z].columns == 0)
728 if (!take_RefData[z].columns && !leave_all_columns && warnings)
729 fprintf(stderr,
"warning: there are no columns being taken from %s that are not already in %s\n", input2, input1 ? input1 :
"stdin");
731 if (take_RefData[z].columns &&
732 (!(output_column = (
char **)
SDDS_GetColumnNames(&SDDS_output, &output_columns)) || output_columns == 0)) {
736 if (output_columns) {
737 for (i = 0; i < output_columns; i++)
738 free(output_column[i]);
742 for (i = 0; i < replace_RefData[z].columns; i++) {
745 fprintf(stderr,
"Warning, %s replace column does not exist in the input1, ignore.\n", replace_RefData[z].orig_column[i]);
756 fprintf(stderr,
"Warning: cannot replace a numeric column with a string column, replace %s ignored.\n", replace_RefData[z].orig_column[i]);
758 fprintf(stderr,
"Warning: cannot replace a string column with a numeric column, replace %s ignored.\n", replace_RefData[z].orig_column[i]);
761 if (datatype1 != datatype2) {
763 fprintf(stderr,
"Warning, replace column %s has different data type as the column in input1; redefining the column type\n", replace_RefData[z].orig_column[i]);
765 fprintf(stderr,
"Problem redefining column type for %s\n", replace_RefData[z].orig_column[i]);
770 take_RefData[z].orig_column =
trealloc(take_RefData[z].orig_column,
sizeof(*(take_RefData[z].orig_column)) * (take_RefData[z].columns + 1));
771 take_RefData[z].new_column =
trealloc(take_RefData[z].new_column,
sizeof(*(take_RefData[z].new_column)) * (take_RefData[z].columns + 1));
772 SDDS_CopyString(&take_RefData[z].orig_column[take_RefData[z].columns], replace_RefData[z].orig_column[i]);
773 SDDS_CopyString(&take_RefData[z].new_column[take_RefData[z].columns], replace_RefData[z].orig_column[i]);
774 take_RefData[z].columns++;
777 free(replace_RefData[z].orig_column[i]);
780 take_RefData[z].parameters = take_RefData[z].arrays = 0;
782 if (!expandTransferRequests(&take_RefData[z].orig_parameter, &take_RefData[z].parameters, PARAMETER_TRANSFER, transfer, transfers, &SDDS_ref[z]) ||
783 !expandTransferRequests(&take_RefData[z].orig_array, &take_RefData[z].arrays, ARRAY_TRANSFER, transfer, transfers, &SDDS_ref[z]))
788 if (take_RefData[z].parameters)
789 take_RefData[z].new_parameter = (
char **)malloc(
sizeof(
char *) * take_RefData[z].parameters);
790 if (take_RefData[z].arrays)
791 take_RefData[z].new_array = (
char **)malloc(
sizeof(
char *) * take_RefData[z].arrays);
793 for (i = 0; i < take_RefData[z].parameters; i++) {
795 if (new_data[z].parameters) {
796 k =
match_string(take_RefData[z].orig_parameter[i], new_data[z].orig_parameter, new_data[z].parameters, EXACT_MATCH);
798 SDDS_CopyString(&take_RefData[z].new_parameter[i], new_data[z].new_parameter[k]);
800 SDDS_CopyString(&take_RefData[z].new_parameter[i], take_RefData[z].orig_parameter[i]);
802 SDDS_CopyString(&take_RefData[z].new_parameter[i], take_RefData[z].orig_parameter[i]);
804 free(take_RefData[z].orig_parameter[i]);
805 free(take_RefData[z].new_parameter[i]);
806 for (col = i; col < take_RefData[z].parameters - 1; col++)
807 take_RefData[z].orig_parameter[col] = take_RefData[z].orig_parameter[col + 1];
808 take_RefData[z].parameters -= 1;
810 if (take_RefData[z].parameters == 0)
818 for (i = 0; i < take_RefData[z].arrays; i++) {
820 if (new_data[z].arrays) {
821 k =
match_string(take_RefData[z].orig_array[i], new_data[z].orig_array, new_data[z].arrays, EXACT_MATCH);
823 SDDS_CopyString(&take_RefData[z].new_array[i], take_RefData[z].orig_array[i]);
825 SDDS_CopyString(&take_RefData[z].new_array[i], new_data[z].new_array[k]);
827 SDDS_CopyString(&take_RefData[z].new_array[i], take_RefData[z].orig_array[i]);
829 free(take_RefData[z].orig_array[i]);
830 free(take_RefData[z].new_array[i]);
831 for (col = i; col < take_RefData[z].arrays - 1; col++)
832 take_RefData[z].orig_array[col] = take_RefData[z].orig_array[col + 1];
833 take_RefData[z].arrays -= 1;
835 if (take_RefData[z].arrays == 0)
844 if (replace_parameters) {
845 for (i = 0; i < replace_parameters; i++) {
846 replace_RefData[z].parameters +=
SDDS_MatchParameters(&SDDS_ref[z], &replace_RefData[z].orig_parameter, SDDS_MATCH_STRING, FIND_ANY_TYPE, replace_parameter[i], SDDS_OR | SDDS_1_PREVIOUS);
850 for (i = 0; i < replace_RefData[z].parameters; i++) {
853 fprintf(stderr,
"Warning, parameter %s replace parameter does not exist in the input1, ignore.\n", replace_RefData[z].orig_parameter[i]);
864 fprintf(stderr,
"Warning: cannot replace a numeric parameter with a string parameter, replace %s ignored.\n", replace_RefData[z].orig_parameter[i]);
866 fprintf(stderr,
"Warning: cannot replace a string parameter with a numeric parameter, replace %s ignored.\n", replace_RefData[z].orig_parameter[i]);
869 if (datatype1 != datatype2) {
871 fprintf(stderr,
"Warning, replace parameter %s type is different from input1, redefining parameter type.\n", replace_RefData[z].orig_parameter[i]);
874 fprintf(stderr,
"Problem redefining parameter type for %s\n", replace_RefData[z].orig_parameter[i]);
879 take_RefData[z].orig_parameter =
trealloc(take_RefData[z].orig_parameter,
sizeof(*(take_RefData[z].orig_parameter)) * (take_RefData[z].parameters + 1));
880 take_RefData[z].new_parameter =
trealloc(take_RefData[z].new_parameter,
sizeof(*(take_RefData[z].new_parameter)) * (take_RefData[z].parameters + 1));
881 SDDS_CopyString(&take_RefData[z].orig_parameter[take_RefData[z].parameters], replace_RefData[z].orig_parameter[i]);
882 SDDS_CopyString(&take_RefData[z].new_parameter[take_RefData[z].parameters], replace_RefData[z].orig_parameter[i]);
883 take_RefData[z].parameters++;
886 free(replace_RefData[z].orig_parameter[i]);
889 if (replace_arrays) {
890 for (i = 0; i < replace_arrays; i++) {
891 replace_RefData[z].arrays +=
SDDS_MatchArrays(&SDDS_ref[z], &replace_RefData[z].orig_array, SDDS_MATCH_STRING, FIND_ANY_TYPE, replace_array[i], SDDS_OR | SDDS_1_PREVIOUS);
894 for (i = 0; i < replace_RefData[z].arrays; i++) {
897 fprintf(stderr,
"Warning, array %s replace array does not exist in the input, ignore.\n", replace_RefData[z].orig_array[i]);
908 fprintf(stderr,
"Warning: cannot replace a numeric array with a string array, replace %s ignored.\n", replace_RefData[z].orig_array[i]);
910 fprintf(stderr,
"Warning: cannot replace a string array with a numeric array, replace %s ignored.\n", replace_RefData[z].orig_array[i]);
913 if (datatype1 != datatype2) {
915 fprintf(stderr,
"Warning, replace array %s has different data type as the array in input1; redefining\n", replace_RefData[z].orig_array[i]);
917 fprintf(stderr,
"Problem redefining array type for %s\n", replace_RefData[z].orig_array[i]);
922 take_RefData[z].orig_array =
trealloc(take_RefData[z].orig_array,
sizeof(*(take_RefData[z].orig_array)) * (take_RefData[z].arrays + 1));
923 SDDS_CopyString(&take_RefData[z].orig_array[take_RefData[z].arrays], replace_RefData[z].orig_array[i]);
924 SDDS_CopyString(&take_RefData[z].new_array[take_RefData[z].arrays], replace_RefData[z].orig_array[i]);
925 take_RefData[z].arrays++;
928 free(replace_RefData[z].orig_array[i]);
958 SDDS_SetError(
"Problem copying parameter or array data from first input file");
962 for (z = 0; z < referfiles; z++) {
963 input2 = referfile[z];
965 if ((retval2 =
SDDS_ReadPage(&SDDS_ref[z])) <= 0 && !endWarning) {
967 fprintf(stderr,
"warning: %s ends prematurely\n", input2 ? input2 :
"stdin");
971 if (retval1 == 1 && (retval2 =
SDDS_ReadPage(&SDDS_ref[z])) <= 0) {
972 if (!endWarning && warnings)
973 fprintf(stderr,
"warning: %s has no data\n", input2 ? input2 :
"stdin");
979 if (take_RefData[z].columns &&
985 if (!CopyParametersFromSecondInput(&SDDS_output, &SDDS_ref[z], take_RefData[z])) {
986 SDDS_SetError(
"Problem copying parameter from second input file");
989 if (!CopyArraysFromSecondInput(&SDDS_output, &SDDS_ref[z], take_RefData[z])) {
990 SDDS_SetError(
"Problem copying parameter from second input file");
996 for (z = rows2Max = 0; z < referfiles; z++) {
997 input2 = referfile[z];
999 rows2Max = rows2 > rows2Max ? rows2 : rows2Max;
1005 rows1 = SDDS_RowCount(&SDDS_output);
1007 if (take_RefData[z].columns) {
1010 SDDS_SetError(
"Problem setting row flags for output file.");
1011 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1014 if (match_columns) {
1016 if (!(string1 = (
char **)
SDDS_GetColumn(&SDDS_1, match_column[0]))) {
1017 fprintf(stderr,
"Error: problem getting column %s from file %s\n", match_column[0], input1 ? input1 :
"stdin");
1018 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1021 if (!(string1 = (
char **)
SDDS_GetColumn(&SDDS_output, match_column[0]))) {
1022 fprintf(stderr,
"Error: problem getting column %s from file %s\n", match_column[0], input1 ? input1 :
"stdin");
1023 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1026 if (!(string2 = (
char **)
SDDS_GetColumn(&SDDS_ref[z], match_column[1]))) {
1027 fprintf(stderr,
"Error: problem getting column %s from file %s\n", match_column[1], input2);
1028 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1033 for (i1 = 0; i1 < rows1; i1++) {
1036 sprintf(s,
"Problem copying row %" PRId64
" of first data set", i1);
1038 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1042 if ((&SDDS_output)->row_flag[i1]) {
1047 if ((i2 =
match_string(string1[i3], string2, rows2, WILDCARD_MATCH)) >= 0)
1051 if (!CopyRowToNewColumn(&SDDS_output, i1, &SDDS_ref[z], i2, take_RefData[z], take_RefData[z].columns, input2)) {
1052 fprintf(stderr,
"error in copying data to output!\n");
1057 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1059 fprintf(stderr,
"warning: no match for row %" PRId64
" (%s = \"%s\")\n", i3, match_column[0], string1[i3]);
1066 for (i = 0; i < i3; i++)
1071 for (i = 0; i < rows2; i++)
1076 for (i = 0; i < keyGroups; i++) {
1077 free(keyGroup[i]->equivalent);
1081 }
else if (equate_columns) {
1084 fprintf(stderr,
"Error: problem getting column %s from file %s\n", equate_column[0], input1 ? input1 :
"stdin");
1085 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1089 fprintf(stderr,
"Error: problem getting column %s from file %s\n", equate_column[0], input1 ? input1 :
"stdin");
1090 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1094 fprintf(stderr,
"Error: problem getting column %s from file %s\n", equate_column[1], input2);
1095 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1100 for (i1 = 0; i1 < rows1; i1++) {
1103 sprintf(s,
"Problem copying row %" PRId64
" of first data set", i1);
1105 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1108 if ((&SDDS_output)->row_flag[i1]) {
1110 if (!CopyRowToNewColumn(&SDDS_output, i1, &SDDS_ref[z], i2, take_RefData[z], take_RefData[z].columns, input2)) {
1111 fprintf(stderr,
"error in copying data to output!\n");
1116 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1118 fprintf(stderr,
"warning: no equal for row %" PRId64
" (%s = %g)\n", i3, equate_column[0], value1[i3]);
1124 if (i3 && equate_columns)
1126 if (rows2 && equate_columns)
1128 for (i = 0; i < keyGroups; i++) {
1129 free(keyGroup[i]->equivalent);
1134 for (i1 = 0; i1 < rows1; i1++) {
1140 sprintf(s,
"Problem copying row %" PRId64
" of first data set", i1);
1142 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1146 fprintf(stderr,
"warning: no row in file 2 for row %" PRId64
" in file 1\n", i1);
1153 sprintf(s,
"Problem copying row %" PRId64
" of first data set", i1);
1155 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1158 if (take_RefData[z].columns &&
1159 !CopyRowToNewColumn(&SDDS_output, i1, &SDDS_ref[z], i2, take_RefData[z], take_RefData[z].columns, input2)) {
1160 fprintf(stderr,
"error in copying data to output!\n");
1170 if (match_columns) {
1172 if (!(string1 = (
char **)
SDDS_GetColumn(&SDDS_1, match_column[0]))) {
1173 fprintf(stderr,
"Error: problem getting column %s from file %s\n", match_column[0], input1 ? input1 :
"stdin");
1174 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1177 if (!(string1 = (
char **)
SDDS_GetColumn(&SDDS_output, match_column[0]))) {
1178 fprintf(stderr,
"Error: problem getting column %s from file %s\n", match_column[0], input1 ? input1 :
"stdin");
1179 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1182 if (!(string2 = (
char **)
SDDS_GetColumn(&SDDS_ref[z], match_column[1]))) {
1183 fprintf(stderr,
"Error: problem getting column %s from file %s\n", match_column[1], input2);
1184 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1188 for (i1 = 0; i1 < rows1; i1++) {
1191 sprintf(s,
"Problem copying row %" PRId64
" of first data set", i1);
1193 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1196 if ((&SDDS_output)->row_flag[i1]) {
1199 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1201 fprintf(stderr,
"warning: no match for row %" PRId64
" (%s = \"%s\")\n", i3, match_column[0], string1[i3]);
1208 for (i = 0; i < i3; i++)
1213 for (i = 0; i < rows2; i++)
1218 for (i = 0; i < keyGroups; i++) {
1219 free(keyGroup[i]->equivalent);
1223 }
else if (equate_columns) {
1226 fprintf(stderr,
"Error: problem getting column %s from file %s\n", equate_column[0], input1 ? input1 :
"stdin");
1227 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1231 fprintf(stderr,
"Error: problem getting column %s from file %s\n", equate_column[0], input1 ? input1 :
"stdin");
1232 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1236 fprintf(stderr,
"Error: problem getting column %s from file %s\n", equate_column[1], input2);
1237 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1241 for (i1 = 0; i1 < rows1; i1++) {
1244 sprintf(s,
"Problem copying row %" PRId64
" of first data set", i1);
1246 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1249 if ((&SDDS_output)->row_flag[i1]) {
1252 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1254 fprintf(stderr,
"warning: no equal for row %" PRId64
" (%s = %g)\n", i3, equate_column[0], value1[i3]);
1260 if (i3 && equate_columns)
1262 if (rows2 && equate_columns)
1264 for (i = 0; i < keyGroups; i++) {
1265 free(keyGroup[i]->equivalent);
1275 if ((rows2Max == 0 && fillIn) || (copyInput1Only == referfiles && !match_columns && !equate_columns)) {
1277 SDDS_SetError(
"Problem copying tabular data for output file.");
1278 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1284 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1288 for (z = 0; z < referfiles; z++) {
1291 if (take_RefData[z].columns) {
1292 for (i = 0; i < take_RefData[z].columns; i++) {
1293 free(take_RefData[z].new_column[i]);
1294 free(take_RefData[z].orig_column[i]);
1296 free(take_RefData[z].new_column);
1297 free(take_RefData[z].orig_column);
1300 if (take_RefData[z].parameters) {
1301 for (i = 0; i < take_RefData[z].parameters; i++) {
1302 free(take_RefData[z].new_parameter[i]);
1303 free(take_RefData[z].orig_parameter[i]);
1305 free(take_RefData[z].new_parameter);
1306 free(take_RefData[z].orig_parameter);
1309 if (take_RefData[z].arrays) {
1310 for (i = 0; i < take_RefData[z].arrays; i++) {
1311 free(take_RefData[z].new_array[i]);
1312 free(take_RefData[z].orig_array[i]);
1314 free(take_RefData[z].new_array);
1315 free(take_RefData[z].orig_array);
1318 if (new_data[z].columns) {
1319 for (i = 0; i < new_data[z].columns; i++) {
1320 free(new_data[z].new_column[i]);
1321 free(new_data[z].orig_column[i]);
1323 free(new_data[z].new_column);
1324 free(new_data[z].orig_column);
1327 if (new_data[z].parameters) {
1328 for (i = 0; i < new_data[z].parameters; i++) {
1329 free(new_data[z].new_parameter[i]);
1330 free(new_data[z].orig_parameter[i]);
1332 free(new_data[z].new_parameter);
1333 free(new_data[z].orig_parameter);
1335 if (new_data[z].arrays) {
1336 for (i = 0; i < new_data[z].arrays; i++) {
1337 free(new_data[z].new_array[i]);
1338 free(new_data[z].orig_array[i]);
1340 free(new_data[z].new_array);
1341 free(new_data[z].orig_array);
1347 if (edit_column_requests) {
1348 for (i = 0; i < edit_column_requests; i++) {
1350 free(edit_column_request[i].edit_string);
1352 free(edit_column_request);
1354 if (edit_parameter_requests) {
1355 for (i = 0; i < edit_parameter_requests; i++) {
1357 free(edit_parameter_request[i].edit_string);
1359 free(edit_parameter_request);
1362 if (edit_array_requests) {
1363 for (i = 0; i < edit_array_requests; i++) {
1365 free(edit_array_request[i].edit_string);
1367 free(edit_array_request);
1371 if (replace_RefData)
1372 free(replace_RefData);
1379 free(equate_column);
1386 for (z = 0; z < referfiles; z++) {
1398 return EXIT_SUCCESS;
1401long expandTransferRequests(
char ***match, int32_t *matches,
long type,
1404 int32_t (*matchRoutine)(
SDDS_DATASET *SDDS_dataset,
char ***nameReturn, int32_t matchMode, int32_t typeMode, ...);
1411 case PARAMETER_TRANSFER:
1414 case ARRAY_TRANSFER:
1418 SDDS_Bomb(
"invalid transfer type--this shouldn't happen");
1423 for (i = 0; i < transfers; i++) {
1424 if (transfer[i].type == type) {
1425 if ((*matches = (*matchRoutine)(inSet, match, SDDS_MATCH_STRING, FIND_ANY_TYPE, transfer[i].name, SDDS_OR | (first ? SDDS_0_PREVIOUS : 0))) == -1) {
1437 EDIT_NAME_REQUEST *edit_array_request,
long edit_array_requests,
long filenumber) {
1438 long i, k = 0, *orig_columnflags;
1439 int32_t columns, parameters, arrays;
1440 long *orig_parameterflags, *orig_arrayflags;
1441 char **column_names, **parameter_names, **array_names, **new_names;
1443 columns = parameters = arrays = 0;
1444 column_names = parameter_names = array_names = new_names = NULL;
1445 orig_columnflags = orig_parameterflags = orig_arrayflags = NULL;
1446 new_data->columns = new_data->parameters = new_data->arrays = 0;
1447 new_data->new_column = new_data->orig_column = NULL;
1448 new_data->new_parameter = new_data->orig_parameter = NULL;
1449 new_data->new_array = new_data->orig_array = NULL;
1452 if (!edit_column_requests && !edit_parameter_requests && !edit_array_requests &&
1453 !rename_data.columns && !rename_data.parameters && !rename_data.arrays)
1457 (*new_data).columns = rename_data.columns;
1458 (*new_data).parameters = rename_data.parameters;
1459 (*new_data).arrays = rename_data.arrays;
1460 if (rename_data.columns) {
1461 (*new_data).new_column = (
char **)malloc(
sizeof(
char *) * rename_data.columns);
1462 (*new_data).orig_column = (
char **)malloc(
sizeof(
char *) * rename_data.columns);
1463 for (i = 0; i < rename_data.columns; i++) {
1464 SDDS_CopyString(&(*new_data).new_column[i], rename_data.new_column[i]);
1465 SDDS_CopyString(&(*new_data).orig_column[i], rename_data.orig_column[i]);
1468 if (rename_data.parameters) {
1469 (*new_data).new_parameter = (
char **)malloc(
sizeof(
char *) * rename_data.parameters);
1470 (*new_data).orig_parameter = (
char **)malloc(
sizeof(
char *) * rename_data.parameters);
1471 for (i = 0; i < rename_data.parameters; i++) {
1472 SDDS_CopyString(&(*new_data).new_parameter[i], rename_data.new_parameter[i]);
1473 SDDS_CopyString(&(*new_data).orig_parameter[i], rename_data.orig_parameter[i]);
1476 if (rename_data.arrays) {
1477 (*new_data).new_array = (
char **)malloc(
sizeof(
char *) * rename_data.arrays);
1478 (*new_data).orig_array = (
char **)malloc(
sizeof(
char *) * rename_data.arrays);
1479 for (i = 0; i < rename_data.arrays; i++) {
1481 SDDS_CopyString(&(*new_data).orig_array[i], rename_data.orig_array[i]);
1500 if (edit_column_requests) {
1501 if ((new_names = process_editnames(column_names, &orig_columnflags, columns, edit_column_request, edit_column_requests, filenumber))) {
1502 for (i = 0; i < columns; i++) {
1503 if (orig_columnflags[i]) {
1504 k = (*new_data).columns;
1505 (*new_data).new_column =
trealloc((*new_data).new_column,
sizeof(
char *) * (k + 1));
1506 (*new_data).orig_column =
trealloc((*new_data).orig_column,
sizeof(
char *) * (k + 1));
1509 (*new_data).columns++;
1517 if (edit_parameter_requests) {
1518 if ((new_names = process_editnames(parameter_names, &orig_parameterflags, parameters, edit_parameter_request, edit_parameter_requests, filenumber))) {
1519 for (i = 0; i < parameters; i++) {
1520 if (orig_parameterflags[i]) {
1521 k = (*new_data).parameters;
1522 (*new_data).new_parameter =
trealloc((*new_data).new_parameter,
sizeof(
char *) * (k + 1));
1523 (*new_data).orig_parameter =
trealloc((*new_data).orig_parameter,
sizeof(
char *) * (k + 1));
1526 (*new_data).parameters++;
1534 if (edit_array_requests) {
1535 if ((new_names = process_editnames(array_names, &orig_arrayflags, arrays, edit_array_request, edit_array_requests, filenumber))) {
1536 for (i = 0; i < arrays; i++) {
1537 if (orig_arrayflags[i]) {
1538 k = (*new_data).arrays;
1539 (*new_data).new_array =
trealloc((*new_data).new_array,
sizeof(
char *) * (k + 1));
1540 (*new_data).orig_array =
trealloc((*new_data).orig_array,
sizeof(
char *) * (k + 1));
1543 (*new_data).arrays++;
1551 if (orig_columnflags)
1552 free(orig_columnflags);
1553 if (orig_parameterflags)
1554 free(orig_parameterflags);
1555 if (orig_arrayflags)
1556 free(orig_arrayflags);
1557 for (i = 0; i < columns; i++)
1558 free(column_names[i]);
1560 for (i = 0; i < parameters; i++)
1561 free(parameter_names[i]);
1562 free(parameter_names);
1563 for (i = 0; i < arrays; i++)
1564 free(array_names[i]);
1568char **process_editnames(
char **orig_name,
long **orig_flags,
long orig_names,
EDIT_NAME_REQUEST *edit_request,
1569 long edit_requests,
long filenumber) {
1570 long i, j, i1, i2 = 0, k;
1571 char **new_name, s[1024], tmpstr[1024];
1572 char *ptr, **editstr, *pch;
1573 char edit_buffer[1024];
1577 *orig_flags =
tmalloc(
sizeof(**orig_flags) * orig_names);
1578 new_name =
tmalloc(
sizeof(*new_name) * orig_names);
1580 editstr = (
char **)malloc(
sizeof(*editstr) * edit_requests);
1581 ptr = malloc(
sizeof(
char) * 256);
1582 sprintf(s,
"%ld", filenumber);
1584 for (i = 0; i < edit_requests; i++) {
1586 if (strstr(editstr[i],
"%%ld"))
1588 else if (strstr(editstr[i],
"%ld")) {
1589 sprintf(s,
"%ld", filenumber);
1591 }
else if (
wild_match(editstr[i],
"*%*ld*")) {
1594 pch = strchr(editstr[i],
'%');
1595 i1 = pch - editstr[i];
1597 for (k = 0; k < strlen(editstr[i]); k++) {
1598 if (editstr[i][k] ==
'd') {
1604 strncpy(tmpstr, pch, i2 - i1 + 1);
1605 tmpstr[i2 - i1 + 1] =
'\0';
1606 sprintf(s, tmpstr, filenumber);
1615 for (j = 0; j < orig_names; j++) {
1616 (*orig_flags)[j] = 0;
1618 for (i = 0; i < edit_requests; i++) {
1621 edit_request[i].match_string = ptr;
1623 strcpy(edit_buffer, new_name[j]);
1624 if (!edit_string(edit_buffer, editstr[i]))
1628 (*orig_flags)[j] = 1;
1633 for (i = 0; i < edit_requests; i++)
1640 REFDATA new_data,
long columns,
char *input2) {
1641 long i, j, k, type, size;
1647 for (i = 0; i < columns; i++) {
1649 sprintf(s,
"error: column %s not found in file %s\n", new_data.orig_column[i], input2);
1651 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1655 sprintf(s,
"error: column %s not defined in output\n", new_data.new_column[i]);
1657 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1662 if (!
SDDS_CopyString(((
char ***)target->data)[k] + target_row, ((
char ***)source->data)[j][source_row])) {
1663 SDDS_SetError(
"Unable to copy row--string copy failed (SDDS_CopyRow)");
1668 memcpy((
char *)target->data[k] + size * target_row, (
char *)source->data[j] + size * source_row, size);
1678 if (new_data.parameters == 0)
1680 if (new_data.parameters) {
1681 for (i = 0; i < new_data.parameters; i++) {
1687 fprintf(stderr,
"Warning, parameter %s not defined in output.\n", new_data.new_parameter[i]);
1690 if (!
SDDS_SetParameters(SDDS_target, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, k, SDDS_source->parameter[j], -1)) {
1691 sprintf(s,
"Unable to copy parameters for parameter %s", new_data.new_parameter[i]);
1704 if (new_data.arrays == 0)
1706 for (i = 0; i < new_data.arrays; i++) {
1710 sprintf(s,
"Warning, array %s not defined in output.\n", new_data.new_array[i]);
1714 if (SDDS_source->layout.array_definition[j].type != SDDS_target->layout.array_definition[k].type) {
1715 SDDS_SetError(
"Can't copy arrays between different types (SDDS_CopyArrays)");
1718 SDDS_target->array[k].definition = SDDS_target->layout.array_definition + k;
1719 SDDS_target->array[k].elements = SDDS_source->array[j].elements;
1720 if (!(SDDS_target->array[k].dimension = (int32_t *)
SDDS_Malloc(
sizeof(*SDDS_target->array[k].dimension) * SDDS_target->array[k].definition->dimensions)) ||
1721 !(SDDS_target->array[k].data =
SDDS_Realloc(SDDS_target->array[k].data,
SDDS_type_size[SDDS_target->array[k].definition->type - 1] * SDDS_target->array[k].elements))) {
1722 SDDS_SetError(
"Unable to copy arrays--allocation failure (SDDS_CopyArrays)");
1725 for (m = 0; m < SDDS_target->array[k].definition->dimensions; m++)
1726 SDDS_target->array[k].dimension[m] = SDDS_source->array[j].dimension[m];
1728 if (SDDS_target->array[k].definition->type !=
SDDS_STRING)
1729 memcpy(SDDS_target->array[k].data, SDDS_source->array[j].data,
SDDS_type_size[SDDS_target->array[k].definition->type - 1] * SDDS_target->array[k].elements);
1730 else if (!
SDDS_CopyStringArray(SDDS_target->array[k].data, SDDS_source->array[j].data, SDDS_target->array[k].elements)) {
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_CopyColumns(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyRowDirect(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_row)
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
char * SDDS_type_name[SDDS_NUM_TYPES]
Array of supported data type names.
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_ClearPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ChangeParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in a parameter definition within the SDDS dataset.
int32_t SDDS_ChangeArrayInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in an array definition within the SDDS dataset.
int32_t SDDS_ChangeColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in a column definition within 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.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
int32_t SDDS_GetArrayIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named array in the SDDS dataset.
char ** SDDS_GetParameterNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all parameters in the SDDS dataset.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
int32_t SDDS_MatchParameters(SDDS_DATASET *SDDS_dataset, char ***nameReturn, int32_t matchMode, int32_t typeMode,...)
Matches and retrieves parameter names from an SDDS dataset based on specified criteria.
int32_t SDDS_ColumnCount(SDDS_DATASET *page)
Retrieves the number of columns in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
int32_t SDDS_GetArrayType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of an array in the SDDS dataset by its index.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
int32_t SDDS_CopyStringArray(char **target, char **source, int64_t n_strings)
Copies an array of strings from source to target.
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_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
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.
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.
int32_t SDDS_MatchArrays(SDDS_DATASET *SDDS_dataset, char ***nameReturn, int32_t matchMode, int32_t typeMode,...)
Matches and retrieves array names from an SDDS dataset based on specified criteria.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric 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.
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.
int replace_string(char *t, char *s, char *orig, char *repl)
Replace all occurrences of one string with another string.
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.
KEYED_EQUIVALENT ** MakeSortedKeyGroups(long *keyGroups, long keyType, void *data, long points)
Create sorted key groups from data.
long FindMatchingKeyGroup(KEYED_EQUIVALENT **keyGroup, long keyGroups, long keyType, void *searchKeyData, long reuse)
Find a matching key group for a search key.
int has_wildcards(char *template)
Check if a template string contains any wildcard characters.
char * expand_ranges(char *template)
Expand range specifiers in a wildcard template into explicit character lists.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.