95 {
96 char *input = NULL;
97 char *output = NULL;
98 char **copy_column_name = NULL;
99 char **users_copy_column_name = NULL;
101 int32_t copy_columns = 0;
102 long users_copy_columns = 0;
103 long groups_count = 0;
104 long i_arg, i, read_code, items;
105 int64_t rows;
106 unsigned long pipe_flags = 0;
107 SCANNED_ARG *sc_arg = NULL;
109
110
112
113
114 argc =
scanargs(&sc_arg, argc, argv);
115 if (argc < 2)
116 bomb(NULL, usage_message);
117
118
119 group = NULL;
120 copy_column_name = users_copy_column_name = NULL;
121 users_copy_columns = copy_columns = groups_count = 0;
122
123
124 for (i_arg = 1; i_arg < argc; i_arg++) {
125 if (sc_arg[i_arg].arg_type == OPTION) {
126
127 switch (
match_string(sc_arg[i_arg].list[0], option_strings, N_OPTIONS, 0)) {
128 case SET_PIPE:
129
130 if (!
processPipeOption(sc_arg[i_arg].list + 1, sc_arg[i_arg].n_items - 1, &pipe_flags))
132 break;
133
134 case SET_GROUP:
135
136 items = sc_arg[i_arg].n_items - 1;
137 if (items < 2)
139 group =
SDDS_Realloc(group,
sizeof(*group) * (groups_count + 1));
140 if (!group ||
141 !
SDDS_CopyString(&group[groups_count].new_name, sc_arg[i_arg].list[1]) ||
142 !(group[groups_count].users_old_name =
SDDS_Malloc(
sizeof(*group[groups_count].users_old_name) * (group[groups_count].users_old_names = items - 1))) ||
143 !
SDDS_CopyStringArray(group[groups_count].users_old_name, sc_arg[i_arg].list + 2, group[groups_count].users_old_names))
145 group[groups_count].old_name = NULL;
146 group[groups_count].old_names = 0;
147 groups_count++;
148 break;
149
150 case SET_COPY:
151
152 if (users_copy_columns)
154 users_copy_columns = sc_arg[i_arg].n_items - 1;
155 if (users_copy_columns < 1)
157 users_copy_column_name =
SDDS_Malloc(
sizeof(*users_copy_column_name) * users_copy_columns);
158 if (!users_copy_column_name ||
161 break;
162
163 default:
164
165 fprintf(stderr, "error: unknown/ambiguous option: %s\n", sc_arg[i_arg].list[0]);
166 exit(EXIT_FAILURE);
167 break;
168 }
169 } else {
170
171 if (!input)
172 input = sc_arg[i_arg].list[0];
173 else if (!output)
174 output = sc_arg[i_arg].list[0];
175 else
177 }
178 }
179
180 if (groups_count == 0)
182
183
185
188
189
190 if (users_copy_columns) {
192 for (i = 0; i < users_copy_columns; i++)
195 if (!copy_column_name || copy_columns == 0)
197 }
198
199
200 for (i = 0; i < groups_count; i++) {
201 long j, type = 0;
203
204 for (j = 0; j < group[i].users_old_names; j++)
206
208 if (!group[i].old_name) {
209 fprintf(stderr, "No match for group %s (sddsseparate)\n", group[i].new_name);
210 exit(EXIT_FAILURE);
211 }
212
213 if (i > 0 && group[i - 1].old_names != group[i].old_names) {
214 fprintf(stderr, "Group %s comprises %" PRId32 " columns, whereas the last group comprises %" PRId32 " (sddsseparate)\n",
215 group[i].new_name, group[i].old_names, group[i - 1].old_names);
216 exit(EXIT_FAILURE);
217 }
218
220 for (j = 1; j < group[i].old_names; j++) {
222 fprintf(stderr, "Inconsistent data types in group %s (sddsseparate)\n", group[i].new_name);
223 fprintf(stderr, "First inconsistent column is %s\n", group[i].old_name[j]);
224 exit(EXIT_FAILURE);
225 }
226 }
227 }
228
229
232 SDDS_Bomb(
"problem initializing output file");
233
234 for (i = 0; i < copy_columns; i++) {
236 SDDS_Bomb(
"problem transferring copy column definitions to output file");
237 }
238
239 for (i = 0; i < groups_count; i++) {
240 char *name;
242 fprintf(stderr, "Problem transferring column %s as %s to output file (sddsseparate)\n",
243 group[i].old_name[0], group[i].new_name);
244 exit(EXIT_FAILURE);
245 }
246
247 group[i].parameter_name =
SDDS_Malloc(
sizeof(*name) * (strlen(group[i].new_name) + 100));
248 if (!group[i].parameter_name)
250
251 sprintf(group[i].parameter_name, "%sSourceColumn", group[i].new_name);
254 }
255
257 SDDS_Bomb(
"problem writing layout to output file");
258
259
262 if (rows < 0)
264 if (rows == 0)
265 continue;
266
267 for (i = 0; i < group[0].old_names; i++) {
268 long ic, ig;
269
273
274 for (ic = 0; ic < copy_columns; ic++) {
276 if (!data ||
277 !
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, data, rows, copy_column_name[ic]))
279 }
280
281 for (ig = 0; ig < groups_count; ig++) {
282 void *data;
283
285 group[ig].parameter_name, group[ig].old_name[i], NULL))
287
289 if (!data ||
290 !
SDDS_SetColumn(&sdds_out, SDDS_SET_BY_NAME, data, rows, group[ig].new_name))
292 }
293
296 }
297 }
298
299
302 exit(EXIT_FAILURE);
303 }
304
307 exit(EXIT_FAILURE);
308 }
309
310 return 0;
311}
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)