68 {
71 long label_parameters = 0;
74 char *inputfile = NULL;
75 char *rootname = NULL;
76 char filename[200];
77 long outputs = 0;
79 long i, i_arg;
80 int64_t n_rows;
81 long page_number, separate_pages = 0;
82 long announce_openings = 0;
83 SCANNED_ARG *s_arg;
84 char *ptr;
85 void *data[4];
86 double param[4];
87 long data_present = 0;
88 unsigned long pipe_flags = 0;
89
91 if (argc < 3) {
92 fputs(usage, stderr);
93 exit(EXIT_FAILURE);
94 }
95
96 for (i_arg = 1; i_arg < argc; i_arg++) {
97 if (s_arg[i_arg].arg_type == OPTION) {
99 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
100 case SET_ROOTNAME:
101 if (s_arg[i_arg].n_items != 2)
103 rootname = s_arg[i_arg].list[1];
104 break;
105 case SET_OUTPUT:
106 if (s_arg[i_arg].n_items < 4 || s_arg[i_arg].n_items > 6)
108 output =
trealloc(output,
sizeof(*output) * (outputs + 1));
109 if (!(output[outputs] = process_output_request(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, outputs ? output[outputs - 1] : NULL)))
111 outputs++;
112 break;
113 case SET_SEPARATE_PAGES:
114 separate_pages = 1;
115 break;
116 case SET_LABEL_PARAMETERS:
117 if (s_arg[i_arg].n_items < 2)
118 SDDS_Bomb(
"Invalid -labelparameters syntax");
119 label_parameter =
trealloc(label_parameter,
sizeof(*label_parameter) * (label_parameters + s_arg[i_arg].n_items));
120 for (i = 1; i < s_arg[i_arg].n_items; i++)
121 scan_label_parameter(label_parameter + i - 1, s_arg[i_arg].list[i]);
122 label_parameters += s_arg[i_arg].n_items - 1;
123 break;
124 case SET_ANNOUNCE_OPENINGS:
125 announce_openings = 1;
126 break;
127 case SET_PIPE:
128 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipe_flags))
130 break;
131 default:
133 break;
134 }
135 } else {
136 if (!inputfile)
137 inputfile = s_arg[i_arg].list[0];
138 else
140 }
141 }
142
143 if (!inputfile && !(pipe_flags & USE_STDIN))
145
146 if (!rootname) {
147 if (!inputfile)
148 SDDS_Bomb(
"You must give a rootname if you don't give an input filename");
150 if ((ptr = strrchr(rootname, '.')))
151 *ptr = '\0';
152 }
153
154 if (outputs <= 0)
155 SDDS_Bomb(
"No output specifications given");
156
159 exit(EXIT_FAILURE);
160 }
161
162 for (i = 0; i < outputs; i++) {
163 if (!output[i]->parameter_output) {
168 fprintf(stderr, "Error: unrecognized column name given\n");
170 exit(EXIT_FAILURE);
171 }
172 } else {
177 fprintf(stderr, "Error: unrecognized parameter name given\n");
179 exit(EXIT_FAILURE);
180 }
181 }
182 }
183
185 data_present = 1;
188 exit(EXIT_FAILURE);
189 }
191 fprintf(stderr, "Warning: no rows selected for page %" PRId32 "\n", sdds_dataset.page_number);
194 }
195 for (i = 0; i < outputs; i++) {
196 if (!output[i]->fp) {
197 if (separate_pages && !output[i]->parameter_output)
198 snprintf(filename, sizeof(filename), "%s_%03ld_%s_%s.out", rootname, output[i]->counter++, output[i]->item[0], output[i]->item[1]);
199 else
200 snprintf(filename, sizeof(filename), "%s_%s_%s.out", rootname, output[i]->item[0], output[i]->item[1]);
201 set_up_output(filename, output[i], label_parameter, label_parameters, separate_pages, announce_openings, &sdds_dataset);
202 }
203 if (!output[i]->parameter_output) {
205 data[2] = data[3] = NULL;
206 if (!(data[0] =
SDDS_GetColumn(&sdds_dataset, output[i]->item[0])) ||
208 (output[i]->columns > 2 && !(data[2] =
SDDS_GetColumn(&sdds_dataset, output[i]->item[2]))) ||
209 (output[i]->columns > 3 && !(data[3] =
SDDS_GetColumn(&sdds_dataset, output[i]->item[3])))) {
211 exit(EXIT_FAILURE);
212 }
213 for (int64_t j = 0; j < n_rows; j++) {
214 for (long k = 0; k < output[i]->columns; k++) {
215 SDDS_PrintTypedValue((
char *)data[k], j, coldef[k]->type, coldef[k]->format_string, output[i]->fp, 0);
216 if (k < output[i]->columns - 1)
217 fputc(' ', output[i]->fp);
218 }
219 fputc('\n', output[i]->fp);
220 }
221 output[i]->points += n_rows;
222 for (long k = 0; k < output[i]->columns; k++) {
223 free(data[k]);
224 }
225 } else {
227 for (long k = 0; k < output[i]->columns; k++) {
230 exit(EXIT_FAILURE);
231 }
232 }
233 output[i]->points += 1;
234 for (long k = 0; k < output[i]->columns; k++) {
235 SDDS_PrintTypedValue((
char *)¶m[k], 0, pardef[k]->type, pardef[k]->format_string, output[i]->fp, 0);
236 if (k < output[i]->columns - 1)
237 fputc(' ', output[i]->fp);
238 }
239 fputc('\n', output[i]->fp);
240 }
241 if (separate_pages && !output[i]->parameter_output) {
242 fclose(output[i]->fp);
243 output[i]->fp = NULL;
244 output[i]->points = 0;
245 }
246 }
247 }
248
249 if (page_number == 0) {
251 exit(EXIT_FAILURE);
252 }
253 if (page_number == -1 && !data_present) {
256 exit(EXIT_FAILURE);
257 }
258 fprintf(stderr, "Error: input data file is empty\n");
259 exit(EXIT_FAILURE);
260 }
261
262 for (i = 0; i < outputs; i++) {
263 if (output[i]->fp)
264 fclose(output[i]->fp);
265 }
266
267 for (i = 0; i < outputs; i++) {
268 if (!separate_pages || output[i]->parameter_output) {
269 snprintf(filename, sizeof(filename), "%s_%s_%s.out", rootname, output[i]->item[0], output[i]->item[1]);
270 fixcount(filename, output[i]->points);
271 }
272 }
273 return 0;
274}
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
int32_t SDDS_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
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.
int32_t SDDS_PrintTypedValue(void *data, int64_t index, int32_t type, char *format, FILE *fp, uint32_t mode)
Prints a data value of a specified type using an optional printf format string.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
int fixcount(char *filename, long n_points)
Updates the data point count in a specified file.
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)