57# include <sys/types.h>
72static char *option[N_OPTIONS] = {
82 " sddssortcolumn [<SDDSinput>] [<SDDSoutput>]\n"
83 " [-pipe=[input][,output]]\n"
84 " [-sortList=<list of columns in order>]\n"
87 " [-sortWith=<filename>,column=<string>]\n\n"
89 " -sortList <list of columns>\n"
90 " Specify the order of column names in a list.\n\n"
91 " -sortWith=<filename>,column=<string>\n"
92 " Sort the columns of the input based on the order defined in the\n"
93 " specified <column> of <filename>. This option overrides any other sorting order.\n\n"
95 " Sort the columns by their assumed BPM position in the storage ring.\n\n"
97 " Sort the columns in decreasing order. The default is increasing order.\n\n"
99 " Rearrange the columns of an SDDS input file into the specified order.\n\n"
100 "Program Information:\n"
101 " Program by Hairong Shang. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
103static char **column_name;
104static long increasing = 1, bpm_order = 0;
105static int32_t columns;
107static long get_bpm_suborder(
char *bpm_name) {
150static int compare_strings(
const void *vindex1,
const void *vindex2) {
151 long index1 = *(
long *)vindex1;
152 long index2 = *(
long *)vindex2;
153 long comparison, sector1, sector2, subsector1, subsector2;
156 if (sscanf(column_name[index1],
"S%ld", §or1) != 1)
158 if (sscanf(column_name[index2],
"S%ld", §or2) != 1)
160 if (sector1 == 0 && sector2 == 0) {
161 comparison = strcmp(column_name[index1], column_name[index2]);
163 if (sector1 > sector2)
165 else if (sector1 < sector2)
168 subsector1 = get_bpm_suborder(column_name[index1]);
169 subsector2 = get_bpm_suborder(column_name[index2]);
170 if (subsector1 > subsector2)
172 else if (subsector1 < subsector2)
179 comparison = strcmp(column_name[index1], column_name[index2]);
183 comparison = -comparison;
188int main(
int argc,
char **argv) {
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;
196 long tmpfile_used = 0;
197 int32_t parameters = 0;
198 unsigned long pipe_flags = 0, dummyflags;
201 argc =
scanargs(&s_arg, argc, argv);
203 fprintf(stderr,
"%s", usage);
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)) {
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");
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++) {
230 if (s_arg[i_arg].n_items != 3) {
231 fprintf(stderr,
"Invalid -sortWith option given!\n");
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) ||
239 fprintf(stderr,
"Invalid -sortWith syntax/values\n");
242 s_arg[i_arg].n_items = 3;
245 fprintf(stderr,
"Error: unknown switch: %s\n", s_arg[i_arg].list[0]);
250 input = s_arg[i_arg].list[0];
251 }
else if (output == NULL) {
252 output = s_arg[i_arg].list[0];
254 fprintf(stderr,
"Too many filenames\n");
260 processFilenames(
"sddssort", &input, &output, pipe_flags, 0, &tmpfile_used);
275 for (i = 0; i < parameters; i++) {
281 sort_column_index = malloc(
sizeof(*sort_column_index) * columns);
282 for (i = 0; i < columns; i++) {
283 sort_column_index[i] = i;
286 if (sort_file && sort_column) {
287 if (sort_list && sort_lists) {
288 for (i = 0; i < sort_lists; i++) {
305 fprintf(stderr,
"Zero rows found in sortWith file.\n");
309 if (!(sort_list = (
char **)
SDDS_GetColumn(&sdds_sort, sort_column))) {
318 sorted_column = malloc(
sizeof(*sorted_column) * columns);
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];
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];
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];
343 for (i = 0; i < columns; i++) {
356 fprintf(stderr,
"Problem starting output page\n");
383 for (i = 0; i < parameters; i++) {
384 free(parameter_name[i]);
386 free(parameter_name);
390 for (i = 0; i < columns; i++) {
391 free(column_name[i]);
397 for (i = 0; i < sort_lists; i++) {
408 free(sort_column_index);
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_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.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.