77typedef char *STRING_PAIR[2];
79long rows_equate(
SDDS_DATASET *SDDS1, int64_t row1,
SDDS_DATASET *SDDS2, int64_t row2,
long equate_columns, STRING_PAIR *equate_column);
81char *option[N_OPTIONS] = {
93 "sddsmselect [<input1>] <input2> [<output>]\n"
94 " [-pipe[=input][,output]] \n"
95 " [-match=<column-name>[=<column-name>][,...]]\n"
96 " [-equate=<column-name>[=<column-name>][,...]]\n"
98 " [-reuse[=rows][,page]]\n"
99 " [-majorOrder=row|column]\n"
102 " -pipe[=input][,output] Use pipe for input and/or output.\n"
103 " -match=<col1>=<col2>,... Specify columns to match between input1 and input2.\n"
104 " -equate=<col1>=<col2>,... Specify columns to equate between input1 and input2.\n"
105 " -invert Select rows with no matching rows in input2.\n"
106 " -reuse[=rows|page] Allow reuse of rows from input2.\n"
107 " -majorOrder=row|column Set output file order to row or column major.\n"
108 " -nowarnings Suppress warning messages.\n"
111 " sddsmselect selects data from <input1> to write to <output>\n"
112 " based on the presence or absence of matching data in <input2>.\n"
113 " If <output> is not specified, <input1> is replaced.\n"
114 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
116int main(
int argc,
char **argv) {
118 long i, i_arg, reuse, reusePage;
119 int64_t j, k, rows1, rows2, n, outputRow;
122 STRING_PAIR *match_column, *equate_column;
123 long match_columns, equate_columns;
124 char *input1, *input2, *output, *match_value;
125 long tmpfile_used, retval1, retval2;
126 long *row_used, warnings, invert;
127 unsigned long pipeFlags, majorOrderFlag;
128 short columnMajorOrder = -1;
131 argc =
scanargs(&s_arg, argc, argv);
135 input1 = input2 = output = NULL;
136 match_column = equate_column = NULL;
137 match_columns = equate_columns = reuse = reusePage = invert = 0;
142 for (i_arg = 1; i_arg < argc; i_arg++) {
143 if (s_arg[i_arg].arg_type == OPTION) {
145 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
146 case SET_MAJOR_ORDER:
148 s_arg[i_arg].n_items--;
149 if (s_arg[i_arg].n_items > 0 &&
150 (!
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items,
151 0,
"row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
152 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
153 SDDS_Bomb(
"Invalid -majorOrder syntax or values.");
154 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
155 columnMajorOrder = 1;
156 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
157 columnMajorOrder = 0;
159 case SET_MATCH_COLUMNS:
160 if (s_arg[i_arg].n_items < 2)
162 match_column =
trealloc(match_column,
sizeof(*match_column) * (match_columns + s_arg[i_arg].n_items - 1));
163 for (i = 1; i < s_arg[i_arg].n_items; i++) {
164 if ((ptr = strchr(s_arg[i_arg].list[i],
'=')))
167 ptr = s_arg[i_arg].list[i];
168 match_column[i - 1 + match_columns][0] = s_arg[i_arg].list[i];
169 match_column[i - 1 + match_columns][1] = ptr;
171 match_columns += s_arg[i_arg].n_items - 1;
173 case SET_EQUATE_COLUMNS:
174 if (s_arg[i_arg].n_items < 2)
176 equate_column =
trealloc(equate_column,
sizeof(*equate_column) * (equate_columns + s_arg[i_arg].n_items - 1));
177 for (i = 1; i < s_arg[i_arg].n_items; i++) {
178 if ((ptr = strchr(s_arg[i_arg].list[i],
'=')))
181 ptr = s_arg[i_arg].list[i];
182 equate_column[i - 1 + equate_columns][0] = s_arg[i_arg].list[i];
183 equate_column[i - 1 + equate_columns][1] = ptr;
185 equate_columns += s_arg[i_arg].n_items - 1;
188 if (s_arg[i_arg].n_items == 1)
191 char *reuseOptions[2] = {
"rows",
"page"};
192 for (i = 1; i < s_arg[i_arg].n_items; i++) {
193 switch (
match_string(s_arg[i_arg].list[i], reuseOptions, 2, 0)) {
214 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
218 fprintf(stderr,
"Error: Unknown option: %s\n", s_arg[i_arg].list[0]);
224 input1 = s_arg[i_arg].list[0];
225 else if (input2 == NULL)
226 input2 = s_arg[i_arg].list[0];
227 else if (output == NULL)
228 output = s_arg[i_arg].list[0];
230 SDDS_Bomb(
"Too many filenames provided.");
234 if (pipeFlags & USE_STDIN && input1) {
236 SDDS_Bomb(
"Too many filenames with -pipe option.");
241 processFilenames(
"sddsmselect", &input1, &output, pipeFlags, !warnings, &tmpfile_used);
243 SDDS_Bomb(
"Second input file not specified.");
245 if (!match_columns && !equate_columns)
246 SDDS_Bomb(
"Either -match or -equate must be specified.");
257 for (i = 0; i < match_columns; i++) {
260 sprintf(s,
"Error: Column '%s' not found or not of string type in file '%s'.",
261 match_column[i][0], input1 ? input1 :
"stdin");
267 sprintf(s,
"Error: Column '%s' not found or not of string type in file '%s'.",
268 match_column[i][1], input2);
273 for (i = 0; i < equate_columns; i++) {
276 sprintf(s,
"Error: Column '%s' not found or not of numeric type in file '%s'.",
277 equate_column[i][0], input1 ? input1 :
"stdin");
283 sprintf(s,
"Error: Column '%s' not found or not of numeric type in file '%s'.",
284 equate_column[i][1], input2);
290 if (output && (pipeFlags & USE_STDOUT))
291 SDDS_Bomb(
"Too many filenames with -pipe option.");
292 if (!output && !(pipeFlags & USE_STDOUT)) {
294 fprintf(stderr,
"Warning: Existing file '%s' will be replaced.\n", input1 ? input1 :
"stdin");
302 if (columnMajorOrder != -1)
303 SDDS_output.layout.data_mode.column_major = columnMajorOrder;
305 SDDS_output.layout.data_mode.column_major = SDDS_1.layout.data_mode.column_major;
314 fprintf(stderr,
"Warning: <input2> ends before <input1>.\n");
333 row_used =
SDDS_Realloc(row_used,
sizeof(*row_used) * rows2);
342 SDDS_SetError(
"Problem copying parameter or array data from first input file.");
346 for (j = 0; j < rows1; j++) {
349 for (i = 0; i < match_columns; i++) {
350 if (!
SDDS_GetValue(&SDDS_1, match_column[i][0], j, &match_value)) {
351 sprintf(s,
"Problem getting column '%s' from file '%s'.",
352 match_column[i][0], input1 ? input1 :
"stdin");
357 sprintf(s,
"Problem setting rows of interest for column '%s'.",
364 if ((!n && !invert) || (n && invert))
367 for (k = 0; k < rows2; k++) {
376 equal = rows_equate(&SDDS_1, j, &SDDS_2, k, equate_columns, equate_column);
377 if ((equal && !invert) || (!equal && invert)) {
378 row_used[k] = reuse ? 0 : 1;
383 if ((k == rows2 && !invert) || (k != rows2 && invert))
387 sprintf(s,
"Problem copying to row %" PRId64
" of output from row %" PRId64
" of data set 1.",
413long rows_equate(
SDDS_DATASET *SDDS1, int64_t row1,
SDDS_DATASET *SDDS2, int64_t row2,
long equate_columns, STRING_PAIR *equate_column) {
415 long index1, index2, size, type, i;
416 char s[SDDS_MAXLINE];
418 for (i = 0; i < equate_columns; i++) {
425 sprintf(s,
"Problem equating rows--types don't match for columns '%s' and '%s'.",
426 equate_column[i][0], equate_column[i][1]);
431 data1 = (
char *)SDDS1->data[index1] + size * row1;
432 data2 = (
char *)SDDS2->data[index2] + size * row2;
433 if (memcmp(data1, data2, size) != 0)
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
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.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_ZeroMemory(void *mem, int64_t n_bytes)
Sets a block of memory to zero.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column 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.
int32_t SDDS_GetTypeSize(int32_t type)
Retrieves the size in bytes of a specified SDDS data type.
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.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#define SDDS_STRING
Identifier for the string 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 bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
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.
char * tmpname(char *s)
Supplies a unique temporary filename.