68static char *option[N_OPTIONS] = {
84 "sddssplit <inputFile> -pipe[=input]\n"
85 " [-binary | -ascii]\n"
86 " [-digits=<number>]\n"
87 " [-rootname=<string>]\n"
88 " [-firstPage=<number>]\n"
89 " [-lastPage=<number>]\n"
90 " [-interval=<number>]\n"
91 " [-extension=<string>]\n"
92 " [-groupParameter=<parameterName>]\n"
93 " [-nameParameter=<filenameParameter>]\n"
94 " [-offset=<integer>]\n"
95 " [-majorOrder=row|column]\n\n"
97 "sddssplit splits an SDDS file into many SDDS files, with each page going to a separate file.\n"
98 "The files are named <rootname><integer>.sdds, where <rootname> is either the filename for\n"
99 "the source file or the specified string, and <integer> is by default <page-number>-<offset>\n"
100 "and is printed to the number of digits given by -digits (3 is the default).\n\n"
102 "-binary, -ascii Specifies whether binary (default) or ASCII output is desired.\n"
103 "-rootname Rootname to use for output filenames. Defaults to the source filename.\n"
104 "-digits Number of digits to use in the filenames (3 is default).\n"
105 "-firstPage First page of input file to include in output (1 is default).\n"
106 "-lastPage Last page of input file to include in output (EOF is default).\n"
107 "-interval Interval between pages included in output (1 is default).\n"
108 "-extension Extension for output files (sdds is default).\n"
109 "-groupParameter Parameter of input file to use in grouping pages into output files.\n"
110 "-nameParameter Parameter of input file to use for naming the output files.\n"
111 "-offset Offset of page number to compute index for output filename.\n"
112 "-majorOrder Select row- or column-major order output (default is row).\n\n"
114 "Program by Michael Borland. ("__DATE__
116 ", SVN revision: " SVN_VERSION
")\n";
118int main(
int argc,
char **argv) {
120 long i_arg, offset = 0;
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;
131 argc =
scanargs(&s_arg, argc, argv);
133 fprintf(stderr,
"%s", USAGE);
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");
151 column_major_order = (major_order_flag & SDDS_COLUMN_MAJOR_ORDER) ? 1 : 0;
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");
168 if (s_arg[i_arg].n_items != 2) {
169 fprintf(stderr,
"Error: Invalid -rootname syntax\n");
172 rootname = s_arg[i_arg].list[1];
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");
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");
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");
193 if (s_arg[i_arg].n_items != 2) {
194 fprintf(stderr,
"Error: Invalid -extension syntax\n");
197 extension = s_arg[i_arg].list[1];
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");
206 pipe_flags = USE_STDIN;
208 case SET_NAMEPARAMETER:
209 if (s_arg[i_arg].n_items != 2) {
210 fprintf(stderr,
"Error: Invalid -nameParameter syntax\n");
213 file_parameter = s_arg[i_arg].list[1];
215 case SET_GROUPPARAMETER:
216 if (s_arg[i_arg].n_items != 2) {
217 fprintf(stderr,
"Error: Invalid -groupParameter syntax\n");
220 group_parameter_name = s_arg[i_arg].list[1];
223 fprintf(stderr,
"Error: Unknown switch: %s\n", s_arg[i_arg].list[0]);
224 fprintf(stderr,
"%s", USAGE);
229 input = s_arg[i_arg].list[0];
231 fprintf(stderr,
"Error: Too many filenames\n");
237 if (!input && !(pipe_flags & USE_STDIN)) {
238 fprintf(stderr,
"Error: Missing input filename\n");
242 if (pipe_flags & USE_STDIN && !file_parameter && !rootname) {
243 fprintf(stderr,
"Error: Provide -rootname or -nameParameter with -pipe\n");
247 if (!rootname && !file_parameter) {
248 if ((rootname = strrchr(input,
'.'))) {
251 input[strlen(input)] =
'.';
257 if (first_page && last_page && first_page > last_page) {
258 fprintf(stderr,
"Error: firstPage > lastPage\n");
269 snprintf(format,
sizeof(format),
"%%s%%0%ldld", digits);
271 snprintf(format,
sizeof(format),
"%%s%%0%ldld.%s", digits, extension);
274 if (file_parameter &&
276 fprintf(stderr,
"Error: Filename parameter not present or wrong type\n");
280 last_group_parameter = NULL;
282 if (first_page && retval < first_page) {
285 if (last_page && retval > last_page) {
290 if ((retval - first_page) % interval != 0) {
293 }
else if ((retval - 1) % interval != 0) {
297 if (file_parameter) {
302 strncpy(name, name_from_parameter,
sizeof(name) - 1);
303 name[
sizeof(name) - 1] =
'\0';
304 free(name_from_parameter);
306 snprintf(name,
sizeof(name), format, rootname, retval - offset);
308 if (group_parameter_name) {
314 if (!group_parameter_name || !last_group_parameter ||
315 (group_parameter_name && strcmp(this_group_parameter, last_group_parameter))) {
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;
329 sdds_dataset.layout.data_mode.column_major = (column_major_order != -1) ? column_major_order : sdds_orig.layout.data_mode.column_major;
336 if (group_parameter_name) {
337 free(last_group_parameter);
338 last_group_parameter = this_group_parameter;
339 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.