78static char *option[N_OPTIONS] = {
94 "sddssplit <inputFile> -pipe[=input]\n"
95 " [-binary | -ascii]\n"
96 " [-digits=<number>]\n"
97 " [-rootname=<string>]\n"
98 " [-firstPage=<number>]\n"
99 " [-lastPage=<number>]\n"
100 " [-interval=<number>]\n"
101 " [-extension=<string>]\n"
102 " [-groupParameter=<parameterName>]\n"
103 " [-nameParameter=<filenameParameter>]\n"
104 " [-offset=<integer>]\n"
105 " [-majorOrder=row|column]\n\n"
107 "sddssplit splits an SDDS file into many SDDS files, with each page going to a separate file.\n"
108 "The files are named <rootname><integer>.sdds, where <rootname> is either the filename for\n"
109 "the source file or the specified string, and <integer> is by default <page-number>-<offset>\n"
110 "and is printed to the number of digits given by -digits (3 is the default).\n\n"
112 "-binary, -ascii Specifies whether binary (default) or ASCII output is desired.\n"
113 "-rootname Rootname to use for output filenames. Defaults to the source filename.\n"
114 "-digits Number of digits to use in the filenames (3 is default).\n"
115 "-firstPage First page of input file to include in output (1 is default).\n"
116 "-lastPage Last page of input file to include in output (EOF is default).\n"
117 "-interval Interval between pages included in output (1 is default).\n"
118 "-extension Extension for output files (sdds is default).\n"
119 "-groupParameter Parameter of input file to use in grouping pages into output files.\n"
120 "-nameParameter Parameter of input file to use for naming the output files.\n"
121 "-offset Offset of page number to compute index for output filename.\n"
122 "-majorOrder Select row- or column-major order output (default is row).\n\n"
124 "Program by Michael Borland. ("__DATE__
126 ", SVN revision: " SVN_VERSION
")\n";
128int main(
int argc,
char **argv) {
130 long i_arg, offset = 0;
132 char *input = NULL, *rootname = NULL, name[500], format[100], *extension =
"sdds";
133 long ascii_output = 0, binary_output = 0, retval, digits = 3;
134 long first_page = 0, last_page = 0, interval = 0;
135 unsigned long pipe_flags = 0, major_order_flag = 0;
136 char *file_parameter = NULL, *name_from_parameter = NULL, *group_parameter_name = NULL;
137 char *last_group_parameter = NULL, *this_group_parameter = NULL;
138 short column_major_order = -1, file_active = 0;
141 argc =
scanargs(&s_arg, argc, argv);
143 fprintf(stderr,
"%s", USAGE);
147 for (i_arg = 1; i_arg < argc; i_arg++) {
148 if (s_arg[i_arg].arg_type == OPTION) {
150 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
151 case SET_MAJOR_ORDER:
152 major_order_flag = 0;
153 s_arg[i_arg].n_items--;
154 if (s_arg[i_arg].n_items > 0 &&
155 !
scanItemList(&major_order_flag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
156 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
157 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)) {
158 fprintf(stderr,
"Error: Invalid -majorOrder syntax/values\n");
161 column_major_order = (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) ? 1 : 0;
172 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &digits) != 1 || digits <= 0) {
173 fprintf(stderr,
"Error: Invalid -digits syntax\n");
178 if (s_arg[i_arg].n_items != 2) {
179 fprintf(stderr,
"Error: Invalid -rootname syntax\n");
182 rootname = s_arg[i_arg].list[1];
185 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &first_page) != 1 || first_page <= 0) {
186 fprintf(stderr,
"Error: Invalid -firstPage syntax\n");
191 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &last_page) != 1 || last_page <= 0) {
192 fprintf(stderr,
"Error: Invalid -lastPage syntax\n");
197 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &interval) != 1 || interval <= 0) {
198 fprintf(stderr,
"Error: Invalid -interval syntax\n");
203 if (s_arg[i_arg].n_items != 2) {
204 fprintf(stderr,
"Error: Invalid -extension syntax\n");
207 extension = s_arg[i_arg].list[1];
210 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &offset) != 1) {
211 fprintf(stderr,
"Error: Invalid -offset syntax\n");
216 pipe_flags = USE_STDIN;
218 case SET_NAMEPARAMETER:
219 if (s_arg[i_arg].n_items != 2) {
220 fprintf(stderr,
"Error: Invalid -nameParameter syntax\n");
223 file_parameter = s_arg[i_arg].list[1];
225 case SET_GROUPPARAMETER:
226 if (s_arg[i_arg].n_items != 2) {
227 fprintf(stderr,
"Error: Invalid -groupParameter syntax\n");
230 group_parameter_name = s_arg[i_arg].list[1];
233 fprintf(stderr,
"Error: Unknown switch: %s\n", s_arg[i_arg].list[0]);
234 fprintf(stderr,
"%s", USAGE);
239 input = s_arg[i_arg].list[0];
241 fprintf(stderr,
"Error: Too many filenames\n");
247 if (!input && !(pipe_flags & USE_STDIN)) {
248 fprintf(stderr,
"Error: Missing input filename\n");
252 if (pipe_flags & USE_STDIN && !file_parameter && !rootname) {
253 fprintf(stderr,
"Error: Provide -rootname or -nameParameter with -pipe\n");
257 if (!rootname && !file_parameter) {
258 if ((rootname = strrchr(input,
'.'))) {
261 input[strlen(input)] =
'.';
267 if (first_page && last_page && first_page > last_page) {
268 fprintf(stderr,
"Error: firstPage > lastPage\n");
279 snprintf(format,
sizeof(format),
"%%s%%0%ldld", digits);
281 snprintf(format,
sizeof(format),
"%%s%%0%ldld.%s", digits, extension);
284 if (file_parameter &&
286 fprintf(stderr,
"Error: Filename parameter not present or wrong type\n");
290 last_group_parameter = NULL;
292 if (first_page && retval < first_page) {
295 if (last_page && retval > last_page) {
300 if ((retval - first_page) % interval != 0) {
303 }
else if ((retval - 1) % interval != 0) {
307 if (file_parameter) {
312 strncpy(name, name_from_parameter,
sizeof(name) - 1);
313 name[
sizeof(name) - 1] =
'\0';
314 free(name_from_parameter);
316 snprintf(name,
sizeof(name), format, rootname, retval - offset);
318 if (group_parameter_name) {
324 if (!group_parameter_name || !last_group_parameter ||
325 (group_parameter_name && strcmp(this_group_parameter, last_group_parameter))) {
335 if ((ascii_output && sdds_dataset.layout.data_mode.mode != SDDS_ASCII) ||
336 (binary_output && sdds_dataset.layout.data_mode.mode != SDDS_BINARY)) {
337 sdds_dataset.layout.data_mode.mode = ascii_output ? SDDS_ASCII : SDDS_BINARY;
339 sdds_dataset.layout.data_mode.column_major = (column_major_order != -1) ? column_major_order : sdds_orig.layout.data_mode.column_major;
346 if (group_parameter_name) {
347 free(last_group_parameter);
348 last_group_parameter = this_group_parameter;
349 this_group_parameter = NULL;
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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.