188 {
190 char *input = NULL, *output = NULL;
191 char **sort_list = NULL, **parameter_name = NULL;
192 char *sort_file = NULL, *sort_column = NULL, **sorted_column = NULL;
193 long i_arg, *sort_column_index = NULL, index;
194 int64_t i, sort_lists = 0, rows, count;
195 SCANNED_ARG *s_arg;
196 long tmpfile_used = 0;
197 int32_t parameters = 0;
198 unsigned long pipe_flags = 0, dummyflags;
199
201 argc =
scanargs(&s_arg, argc, argv);
202 if (argc < 2) {
203 fprintf(stderr, "%s", usage);
204 return EXIT_FAILURE;
205 }
206
207 for (i_arg = 1; i_arg < argc; i_arg++) {
208 if (s_arg[i_arg].arg_type == OPTION) {
209 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
210 case SET_PIPE:
211 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags)) {
212 fprintf(stderr, "Invalid -pipe syntax\n");
213 return EXIT_FAILURE;
214 }
215 break;
216 case SET_DECREASING:
217 increasing = 0;
218 break;
219 case SET_BPMORDER:
220 bpm_order = 1;
221 break;
222 case SET_SORTLIST:
223 sort_lists = s_arg[i_arg].n_items - 1;
224 sort_list = malloc(sizeof(*sort_list) * sort_lists);
225 for (i = 0; i < sort_lists; i++) {
227 }
228 break;
229 case SET_SORTWITH:
230 if (s_arg[i_arg].n_items != 3) {
231 fprintf(stderr, "Invalid -sortWith option given!\n");
232 return EXIT_FAILURE;
233 }
234 sort_file = s_arg[i_arg].list[1];
235 s_arg[i_arg].n_items = 1;
236 if (!
scanItemList(&dummyflags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
237 "column",
SDDS_STRING, &sort_column, 1, 0, NULL) ||
238 !sort_column) {
239 fprintf(stderr, "Invalid -sortWith syntax/values\n");
240 return EXIT_FAILURE;
241 }
242 s_arg[i_arg].n_items = 3;
243 break;
244 default:
245 fprintf(stderr, "Error: unknown switch: %s\n", s_arg[i_arg].list[0]);
246 return EXIT_FAILURE;
247 }
248 } else {
249 if (input == NULL) {
250 input = s_arg[i_arg].list[0];
251 } else if (output == NULL) {
252 output = s_arg[i_arg].list[0];
253 } else {
254 fprintf(stderr, "Too many filenames\n");
255 return EXIT_FAILURE;
256 }
257 }
258 }
259
260 processFilenames(
"sddssort", &input, &output, pipe_flags, 0, &tmpfile_used);
261
264 return EXIT_FAILURE;
265 }
266
269 return EXIT_FAILURE;
270 }
271
274
275 for (i = 0; i < parameters; i++) {
278 }
279 }
280
281 sort_column_index = malloc(sizeof(*sort_column_index) * columns);
282 for (i = 0; i < columns; i++) {
283 sort_column_index[i] = i;
284 }
285
286 if (sort_file && sort_column) {
287 if (sort_list && sort_lists) {
288 for (i = 0; i < sort_lists; i++) {
289 free(sort_list[i]);
290 }
291 free(sort_list);
292 }
293 sort_lists = 0;
294 sort_list = NULL;
295
298 }
299
302 }
303
305 fprintf(stderr, "Zero rows found in sortWith file.\n");
306 return EXIT_FAILURE;
307 }
308
309 if (!(sort_list = (
char **)
SDDS_GetColumn(&sdds_sort, sort_column))) {
311 }
312
315 }
316 }
317
318 sorted_column = malloc(sizeof(*sorted_column) * columns);
319 count = 0;
320
321 if (sort_list) {
322 for (i = 0; i < sort_lists; i++) {
323 if ((index =
match_string(sort_list[i], column_name, columns, EXACT_MATCH)) >= 0) {
324 sorted_column[count] = sort_list[i];
325 count++;
326 }
327 }
328
329 for (i = 0; i < columns; i++) {
330 if (
match_string(column_name[i], sort_list, sort_lists, EXACT_MATCH) < 0) {
331 sorted_column[count] = column_name[i];
332 count++;
333 }
334 }
335 } else {
336 qsort((void *)sort_column_index, columns, sizeof(*sort_column_index), compare_strings);
337 for (i = 0; i < columns; i++) {
338 index = sort_column_index[i];
339 sorted_column[i] = column_name[index];
340 }
341 }
342
343 for (i = 0; i < columns; i++) {
346 }
347 }
348
351 }
352
356 fprintf(stderr, "Problem starting output page\n");
358 }
359
362 }
363
366 }
367
370 }
371 }
372
375 return EXIT_FAILURE;
376 }
377
379 return EXIT_FAILURE;
380 }
381
382 if (parameters) {
383 for (i = 0; i < parameters; i++) {
384 free(parameter_name[i]);
385 }
386 free(parameter_name);
387 }
388
389 if (columns) {
390 for (i = 0; i < columns; i++) {
391 free(column_name[i]);
392 }
393 free(column_name);
394 }
395
396 if (sort_lists) {
397 for (i = 0; i < sort_lists; i++) {
398 free(sort_list[i]);
399 }
400 free(sort_list);
401 }
402
403 if (sort_column) {
404 free(sort_column);
405 }
406
407 free(sorted_column);
408 free(sort_column_index);
410
411 return EXIT_SUCCESS;
412}
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_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_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_TransferParameterDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a parameter definition from a source dataset to a target dataset.
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.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
#define SDDS_STRING
Identifier for the string 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.
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)
void free_scanargs(SCANNED_ARG **scanned, int argc)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.