118 {
120 long i_arg, offset = 0;
121 SCANNED_ARG *s_arg;
122 char *input = NULL, *rootname = NULL, name[500], format[100], *extension = "sdds";
123 long ascii_output = 0, binary_output = 0, retval, digits = 3;
124 long first_page = 0, last_page = 0, interval = 0;
125 unsigned long pipe_flags = 0, major_order_flag = 0;
126 char *file_parameter = NULL, *name_from_parameter = NULL, *group_parameter_name = NULL;
127 char *last_group_parameter = NULL, *this_group_parameter = NULL;
128 short column_major_order = -1, file_active = 0;
129
131 argc =
scanargs(&s_arg, argc, argv);
132 if (argc < 2) {
133 fprintf(stderr, "%s", USAGE);
134 return 1;
135 }
136
137 for (i_arg = 1; i_arg < argc; i_arg++) {
138 if (s_arg[i_arg].arg_type == OPTION) {
140 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
141 case SET_MAJOR_ORDER:
142 major_order_flag = 0;
143 s_arg[i_arg].n_items--;
144 if (s_arg[i_arg].n_items > 0 &&
145 !
scanItemList(&major_order_flag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
146 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
147 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)) {
148 fprintf(stderr, "Error: Invalid -majorOrder syntax/values\n");
149 return 1;
150 }
151 column_major_order = (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) ? 1 : 0;
152 break;
153 case SET_BINARY:
154 binary_output = 1;
155 ascii_output = 0;
156 break;
157 case SET_ASCII:
158 ascii_output = 1;
159 binary_output = 0;
160 break;
161 case SET_DIGITS:
162 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &digits) != 1 || digits <= 0) {
163 fprintf(stderr, "Error: Invalid -digits syntax\n");
164 return 1;
165 }
166 break;
167 case SET_ROOTNAME:
168 if (s_arg[i_arg].n_items != 2) {
169 fprintf(stderr, "Error: Invalid -rootname syntax\n");
170 return 1;
171 }
172 rootname = s_arg[i_arg].list[1];
173 break;
174 case SET_FIRST_PAGE:
175 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &first_page) != 1 || first_page <= 0) {
176 fprintf(stderr, "Error: Invalid -firstPage syntax\n");
177 return 1;
178 }
179 break;
180 case SET_LAST_PAGE:
181 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &last_page) != 1 || last_page <= 0) {
182 fprintf(stderr, "Error: Invalid -lastPage syntax\n");
183 return 1;
184 }
185 break;
186 case SET_INTERVAL:
187 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &interval) != 1 || interval <= 0) {
188 fprintf(stderr, "Error: Invalid -interval syntax\n");
189 return 1;
190 }
191 break;
192 case SET_EXTENSION:
193 if (s_arg[i_arg].n_items != 2) {
194 fprintf(stderr, "Error: Invalid -extension syntax\n");
195 return 1;
196 }
197 extension = s_arg[i_arg].list[1];
198 break;
199 case SET_OFFSET:
200 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &offset) != 1) {
201 fprintf(stderr, "Error: Invalid -offset syntax\n");
202 return 1;
203 }
204 break;
205 case SET_PIPE:
206 pipe_flags = USE_STDIN;
207 break;
208 case SET_NAMEPARAMETER:
209 if (s_arg[i_arg].n_items != 2) {
210 fprintf(stderr, "Error: Invalid -nameParameter syntax\n");
211 return 1;
212 }
213 file_parameter = s_arg[i_arg].list[1];
214 break;
215 case SET_GROUPPARAMETER:
216 if (s_arg[i_arg].n_items != 2) {
217 fprintf(stderr, "Error: Invalid -groupParameter syntax\n");
218 return 1;
219 }
220 group_parameter_name = s_arg[i_arg].list[1];
221 break;
222 default:
223 fprintf(stderr, "Error: Unknown switch: %s\n", s_arg[i_arg].list[0]);
224 fprintf(stderr, "%s", USAGE);
225 return 1;
226 }
227 } else {
228 if (!input) {
229 input = s_arg[i_arg].list[0];
230 } else {
231 fprintf(stderr, "Error: Too many filenames\n");
232 return 1;
233 }
234 }
235 }
236
237 if (!input && !(pipe_flags & USE_STDIN)) {
238 fprintf(stderr, "Error: Missing input filename\n");
239 return 1;
240 }
241
242 if (pipe_flags & USE_STDIN && !file_parameter && !rootname) {
243 fprintf(stderr, "Error: Provide -rootname or -nameParameter with -pipe\n");
244 return 1;
245 }
246
247 if (!rootname && !file_parameter) {
248 if ((rootname = strrchr(input, '.'))) {
249 *rootname = 0;
251 input[strlen(input)] = '.';
252 } else {
254 }
255 }
256
257 if (first_page && last_page && first_page > last_page) {
258 fprintf(stderr, "Error: firstPage > lastPage\n");
259 return 1;
260 }
261
264 return 1;
265 }
266
268 extension = NULL;
269 snprintf(format, sizeof(format), "%%s%%0%ldld", digits);
270 } else {
271 snprintf(format, sizeof(format), "%%s%%0%ldld.%s", digits, extension);
272 }
273
274 if (file_parameter &&
276 fprintf(stderr, "Error: Filename parameter not present or wrong type\n");
277 return 1;
278 }
279
280 last_group_parameter = NULL;
282 if (first_page && retval < first_page) {
283 continue;
284 }
285 if (last_page && retval > last_page) {
286 break;
287 }
288 if (interval) {
289 if (first_page) {
290 if ((retval - first_page) % interval != 0) {
291 continue;
292 }
293 } else if ((retval - 1) % interval != 0) {
294 continue;
295 }
296 }
297 if (file_parameter) {
300 return 1;
301 }
302 strncpy(name, name_from_parameter, sizeof(name) - 1);
303 name[sizeof(name) - 1] = '\0';
304 free(name_from_parameter);
305 } else {
306 snprintf(name, sizeof(name), format, rootname, retval - offset);
307 }
308 if (group_parameter_name) {
311 return 1;
312 }
313 }
314 if (!group_parameter_name || !last_group_parameter ||
315 (group_parameter_name && strcmp(this_group_parameter, last_group_parameter))) {
318 return 1;
319 }
320 file_active = 0;
323 return 1;
324 }
325 if ((ascii_output && sdds_dataset.layout.data_mode.mode != SDDS_ASCII) ||
326 (binary_output && sdds_dataset.layout.data_mode.mode != SDDS_BINARY)) {
327 sdds_dataset.layout.data_mode.mode = ascii_output ? SDDS_ASCII : SDDS_BINARY;
328 }
329 sdds_dataset.layout.data_mode.column_major = (column_major_order != -1) ? column_major_order : sdds_orig.layout.data_mode.column_major;
332 return 1;
333 }
334 file_active = 1;
335 }
336 if (group_parameter_name) {
337 free(last_group_parameter);
338 last_group_parameter = this_group_parameter;
339 this_group_parameter = NULL;
340 }
343 return 1;
344 }
345 }
346 if (retval == 0) {
348 return 1;
349 }
352 return 1;
353 }
354 return 0;
355}
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
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.
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_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
int32_t SDDS_CheckParameter(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a parameter exists in the SDDS dataset with the specified name, units, and type.
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.
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
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 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.