47 char **users_old_name;
63static char *option_strings[N_OPTIONS] = {
70static char *usage_message =
71 "Usage: sddsseparate [<inputfile>] [<outputfile>]\n"
72 " [-pipe=[input][,output]]\n"
73 " [-group=<newName>,<listOfOldNames>] [-copy=<listOfNames>]\n"
76 " Reorganizes the column data in the input so that data from different\n"
77 " columns ends up on different pages.\n"
78 " For each -group option, a column is created in the output that contains\n"
79 " data from the columns <listOfOldNames> on sequential pages.\n"
80 " Columns named with the -copy option are duplicated on each page.\n"
83 " Group columns A, B, C under a new name 'Group1' and copy column D:\n"
84 " sddsseparate input.sdds output.sdds -group=Group1,A,B,C -copy=D\n"
86 "Program by Michael Borland.\n"
87 "(" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
89int main(
int argc,
char **argv) {
92 char **copy_column_name = NULL;
93 char **users_copy_column_name = NULL;
95 int32_t copy_columns = 0;
96 long users_copy_columns = 0;
97 long groups_count = 0;
98 long i_arg, i, read_code, items;
100 unsigned long pipe_flags = 0;
101 SCANNED_ARG *sc_arg = NULL;
108 argc =
scanargs(&sc_arg, argc, argv);
110 bomb(usage_message, NULL);
114 copy_column_name = users_copy_column_name = NULL;
115 users_copy_columns = copy_columns = groups_count = 0;
118 for (i_arg = 1; i_arg < argc; i_arg++) {
119 if (sc_arg[i_arg].arg_type == OPTION) {
121 switch (
match_string(sc_arg[i_arg].list[0], option_strings, N_OPTIONS, 0)) {
124 if (!
processPipeOption(sc_arg[i_arg].list + 1, sc_arg[i_arg].n_items - 1, &pipe_flags))
130 items = sc_arg[i_arg].n_items - 1;
133 group =
SDDS_Realloc(group,
sizeof(*group) * (groups_count + 1));
135 !
SDDS_CopyString(&group[groups_count].new_name, sc_arg[i_arg].list[1]) ||
136 !(group[groups_count].users_old_name =
SDDS_Malloc(
sizeof(*group[groups_count].users_old_name) * (group[groups_count].users_old_names = items - 1))) ||
137 !
SDDS_CopyStringArray(group[groups_count].users_old_name, sc_arg[i_arg].list + 2, group[groups_count].users_old_names))
139 group[groups_count].old_name = NULL;
140 group[groups_count].old_names = 0;
146 if (users_copy_columns)
148 users_copy_columns = sc_arg[i_arg].n_items - 1;
149 if (users_copy_columns < 1)
151 users_copy_column_name =
SDDS_Malloc(
sizeof(*users_copy_column_name) * users_copy_columns);
152 if (!users_copy_column_name ||
159 fprintf(stderr,
"error: unknown/ambiguous option: %s\n", sc_arg[i_arg].list[0]);
166 input = sc_arg[i_arg].list[0];
168 output = sc_arg[i_arg].list[0];
174 if (groups_count == 0)
184 if (users_copy_columns) {
186 for (i = 0; i < users_copy_columns; i++)
189 if (!copy_column_name || copy_columns == 0)
194 for (i = 0; i < groups_count; i++) {
198 for (j = 0; j < group[i].users_old_names; j++)
202 if (!group[i].old_name) {
203 fprintf(stderr,
"No match for group %s (sddsseparate)\n", group[i].new_name);
207 if (i > 0 && group[i - 1].old_names != group[i].old_names) {
208 fprintf(stderr,
"Group %s comprises %" PRId32
" columns, whereas the last group comprises %" PRId32
" (sddsseparate)\n",
209 group[i].new_name, group[i].old_names, group[i - 1].old_names);
214 for (j = 1; j < group[i].old_names; j++) {
216 fprintf(stderr,
"Inconsistent data types in group %s (sddsseparate)\n", group[i].new_name);
217 fprintf(stderr,
"First inconsistent column is %s\n", group[i].old_name[j]);
226 SDDS_Bomb(
"problem initializing output file");
228 for (i = 0; i < copy_columns; i++) {
230 SDDS_Bomb(
"problem transferring copy column definitions to output file");
233 for (i = 0; i < groups_count; i++) {
236 fprintf(stderr,
"Problem transferring column %s as %s to output file (sddsseparate)\n",
237 group[i].old_name[0], group[i].new_name);
241 group[i].parameter_name =
SDDS_Malloc(
sizeof(*name) * (strlen(group[i].new_name) + 100));
242 if (!group[i].parameter_name)
245 sprintf(group[i].parameter_name,
"%sSourceColumn", group[i].new_name);
251 SDDS_Bomb(
"problem writing layout to output file");
261 for (i = 0; i < group[0].old_names; i++) {
268 for (ic = 0; ic < copy_columns; ic++) {
271 !
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, data, rows, copy_column_name[ic]))
275 for (ig = 0; ig < groups_count; ig++) {
279 group[ig].parameter_name, group[ig].old_name[i], NULL))
284 !
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, data, rows, group[ig].new_name))
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_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
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_DefineSimpleParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data parameter 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_TransferAllParameterDefinitions(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
Transfers all parameter definitions from a source dataset to a target dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column 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.
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.
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.
#define SDDS_STRING
Identifier for the string data type.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
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 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)