89 {
90 char *input = NULL;
91 char *output = NULL;
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;
99 int64_t rows;
100 unsigned long pipe_flags = 0;
101 SCANNED_ARG *sc_arg = NULL;
103
104
106
107
108 argc =
scanargs(&sc_arg, argc, argv);
109 if (argc < 2)
110 bomb(usage_message, NULL);
111
112
113 group = NULL;
114 copy_column_name = users_copy_column_name = NULL;
115 users_copy_columns = copy_columns = groups_count = 0;
116
117
118 for (i_arg = 1; i_arg < argc; i_arg++) {
119 if (sc_arg[i_arg].arg_type == OPTION) {
120
121 switch (
match_string(sc_arg[i_arg].list[0], option_strings, N_OPTIONS, 0)) {
122 case SET_PIPE:
123
124 if (!
processPipeOption(sc_arg[i_arg].list + 1, sc_arg[i_arg].n_items - 1, &pipe_flags))
126 break;
127
128 case SET_GROUP:
129
130 items = sc_arg[i_arg].n_items - 1;
131 if (items < 2)
133 group =
SDDS_Realloc(group,
sizeof(*group) * (groups_count + 1));
134 if (!group ||
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;
141 groups_count++;
142 break;
143
144 case SET_COPY:
145
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 ||
155 break;
156
157 default:
158
159 fprintf(stderr, "error: unknown/ambiguous option: %s\n", sc_arg[i_arg].list[0]);
160 exit(EXIT_FAILURE);
161 break;
162 }
163 } else {
164
165 if (!input)
166 input = sc_arg[i_arg].list[0];
167 else if (!output)
168 output = sc_arg[i_arg].list[0];
169 else
171 }
172 }
173
174 if (groups_count == 0)
176
177
179
182
183
184 if (users_copy_columns) {
186 for (i = 0; i < users_copy_columns; i++)
189 if (!copy_column_name || copy_columns == 0)
191 }
192
193
194 for (i = 0; i < groups_count; i++) {
195 long j, type = 0;
197
198 for (j = 0; j < group[i].users_old_names; j++)
200
202 if (!group[i].old_name) {
203 fprintf(stderr, "No match for group %s (sddsseparate)\n", group[i].new_name);
204 exit(EXIT_FAILURE);
205 }
206
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);
210 exit(EXIT_FAILURE);
211 }
212
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]);
218 exit(EXIT_FAILURE);
219 }
220 }
221 }
222
223
226 SDDS_Bomb(
"problem initializing output file");
227
228 for (i = 0; i < copy_columns; i++) {
230 SDDS_Bomb(
"problem transferring copy column definitions to output file");
231 }
232
233 for (i = 0; i < groups_count; i++) {
234 char *name;
236 fprintf(stderr, "Problem transferring column %s as %s to output file (sddsseparate)\n",
237 group[i].old_name[0], group[i].new_name);
238 exit(EXIT_FAILURE);
239 }
240
241 group[i].parameter_name =
SDDS_Malloc(
sizeof(*name) * (strlen(group[i].new_name) + 100));
242 if (!group[i].parameter_name)
244
245 sprintf(group[i].parameter_name, "%sSourceColumn", group[i].new_name);
248 }
249
251 SDDS_Bomb(
"problem writing layout to output file");
252
253
256 if (rows < 0)
258 if (rows == 0)
259 continue;
260
261 for (i = 0; i < group[0].old_names; i++) {
262 long ic, ig;
263
267
268 for (ic = 0; ic < copy_columns; ic++) {
270 if (!data ||
271 !
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, data, rows, copy_column_name[ic]))
273 }
274
275 for (ig = 0; ig < groups_count; ig++) {
276 void *data;
277
279 group[ig].parameter_name, group[ig].old_name[i], NULL))
281
283 if (!data ||
284 !
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, data, rows, group[ig].new_name))
286 }
287
290 }
291 }
292
293
296 exit(EXIT_FAILURE);
297 }
298
301 exit(EXIT_FAILURE);
302 }
303
304 return 0;
305}
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)