191 {
193 char *input = NULL, *output = NULL;
194 char **sort_list = NULL, **parameter_name = NULL;
195 char *sort_file = NULL, *sort_column = NULL, **sorted_column = NULL;
196 long i_arg, *sort_column_index = NULL, index;
197 int64_t i, sort_lists = 0, rows, count;
198 SCANNED_ARG *s_arg;
199 long tmpfile_used = 0;
200 int32_t parameters = 0;
201 unsigned long pipe_flags = 0, dummyflags;
202
204 argc =
scanargs(&s_arg, argc, argv);
205 if (argc < 2) {
206 fprintf(stderr, "%s", usage);
207 return EXIT_FAILURE;
208 }
209
210 for (i_arg = 1; i_arg < argc; i_arg++) {
211 if (s_arg[i_arg].arg_type == OPTION) {
212 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
213 case SET_PIPE:
214 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags)) {
215 fprintf(stderr, "Invalid -pipe syntax\n");
216 return EXIT_FAILURE;
217 }
218 break;
219 case SET_DECREASING:
220 increasing = 0;
221 break;
222 case SET_BPMORDER:
223 bpm_order = 1;
224 break;
225 case SET_SORTLIST:
226 sort_lists = s_arg[i_arg].n_items - 1;
227 sort_list = malloc(sizeof(*sort_list) * sort_lists);
228 for (i = 0; i < sort_lists; i++) {
230 }
231 break;
232 case SET_SORTWITH:
233 if (s_arg[i_arg].n_items != 3) {
234 fprintf(stderr, "Invalid -sortWith option given!\n");
235 return EXIT_FAILURE;
236 }
237 sort_file = s_arg[i_arg].list[1];
238 s_arg[i_arg].n_items = 1;
239 if (!
scanItemList(&dummyflags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
240 "column",
SDDS_STRING, &sort_column, 1, 0, NULL) ||
241 !sort_column) {
242 fprintf(stderr, "Invalid -sortWith syntax/values\n");
243 return EXIT_FAILURE;
244 }
245 s_arg[i_arg].n_items = 3;
246 break;
247 default:
248 fprintf(stderr, "Error: unknown switch: %s\n", s_arg[i_arg].list[0]);
249 return EXIT_FAILURE;
250 }
251 } else {
252 if (input == NULL) {
253 input = s_arg[i_arg].list[0];
254 } else if (output == NULL) {
255 output = s_arg[i_arg].list[0];
256 } else {
257 fprintf(stderr, "Too many filenames\n");
258 return EXIT_FAILURE;
259 }
260 }
261 }
262
263 processFilenames(
"sddssort", &input, &output, pipe_flags, 0, &tmpfile_used);
264
267 return EXIT_FAILURE;
268 }
269
272 return EXIT_FAILURE;
273 }
274
277
278 for (i = 0; i < parameters; i++) {
281 }
282 }
283
284 sort_column_index = malloc(sizeof(*sort_column_index) * columns);
285 for (i = 0; i < columns; i++) {
286 sort_column_index[i] = i;
287 }
288
289 if (sort_file && sort_column) {
290 if (sort_list && sort_lists) {
291 for (i = 0; i < sort_lists; i++) {
292 free(sort_list[i]);
293 }
294 free(sort_list);
295 }
296 sort_lists = 0;
297 sort_list = NULL;
298
301 }
302
305 }
306
308 fprintf(stderr, "Zero rows found in sortWith file.\n");
309 return EXIT_FAILURE;
310 }
311
312 if (!(sort_list = (
char **)
SDDS_GetColumn(&sdds_sort, sort_column))) {
314 }
315
318 }
319 }
320
321 sorted_column = malloc(sizeof(*sorted_column) * columns);
322 count = 0;
323
324 if (sort_list) {
325 for (i = 0; i < sort_lists; i++) {
326 if ((index =
match_string(sort_list[i], column_name, columns, EXACT_MATCH)) >= 0) {
327 sorted_column[count] = sort_list[i];
328 count++;
329 }
330 }
331
332 for (i = 0; i < columns; i++) {
333 if (
match_string(column_name[i], sort_list, sort_lists, EXACT_MATCH) < 0) {
334 sorted_column[count] = column_name[i];
335 count++;
336 }
337 }
338 } else {
339 qsort((void *)sort_column_index, columns, sizeof(*sort_column_index), compare_strings);
340 for (i = 0; i < columns; i++) {
341 index = sort_column_index[i];
342 sorted_column[i] = column_name[index];
343 }
344 }
345
346 for (i = 0; i < columns; i++) {
349 }
350 }
351
354 }
355
359 fprintf(stderr, "Problem starting output page\n");
361 }
362
365 }
366
369 }
370
373 }
374 }
375
378 return EXIT_FAILURE;
379 }
380
382 return EXIT_FAILURE;
383 }
384
385 if (parameters) {
386 for (i = 0; i < parameters; i++) {
387 free(parameter_name[i]);
388 }
389 free(parameter_name);
390 }
391
392 if (columns) {
393 for (i = 0; i < columns; i++) {
394 free(column_name[i]);
395 }
396 free(column_name);
397 }
398
399 if (sort_lists) {
400 for (i = 0; i < sort_lists; i++) {
401 free(sort_list[i]);
402 }
403 free(sort_list);
404 }
405
406 if (sort_column) {
407 free(sort_column);
408 }
409
410 free(sorted_column);
411 free(sort_column_index);
413
414 return EXIT_SUCCESS;
415}
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.