126 {
132 long list_request;
133 char s[SDDS_MAXLINE];
134 char **filename, *ptr, *sddsOutputFile;
135 long i, i_arg, filenames, file, append_units, sddsOutput;
136 SCANNED_ARG *s_arg;
137 char *text, *contents, *delimiter, *unitsTemplate;
138 FILE *fp;
139 unsigned long pipeFlags;
140 long readAll;
141
142 list_request = -1;
143 filename = NULL;
144 sddsOutput = 0;
145 filenames = append_units = 0;
146 delimiter = sddsOutputFile = unitsTemplate = NULL;
147 pipeFlags = 0;
148 readAll = 0;
149
151 argc =
scanargs(&s_arg, argc, argv);
152
153 for (i_arg = 1; i_arg < argc; i_arg++) {
154 if (s_arg[i_arg].arg_type == OPTION) {
156 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
157 case SET_COLUMN_LIST:
158 list_request = SET_COLUMN_LIST;
159 break;
160 case SET_PARAMETER_LIST:
161 list_request = SET_PARAMETER_LIST;
162 break;
163 case SET_ASSOCIATE_LIST:
164 list_request = SET_ASSOCIATE_LIST;
165 break;
166 case SET_ARRAY_LIST:
167 list_request = SET_ARRAY_LIST;
168 break;
169 case SET_VERSION:
170 list_request = SET_VERSION;
171 break;
172 case SET_DELIMITER:
173 if (s_arg[i_arg].n_items < 2)
175 delimiter = s_arg[i_arg].list[1];
176 break;
177 case SET_APPEND_UNITS:
178 append_units = 1;
179 if (s_arg[i_arg].n_items == 2) {
180 if (strncmp(s_arg[i_arg].list[1], "bare", strlen(s_arg[i_arg].list[1])) == 0)
181 unitsTemplate = " %s";
182 else
183 SDDS_Bomb(
"invalid -appendUnits syntax");
184 } else if (s_arg[i_arg].n_items > 2)
185 SDDS_Bomb(
"invalid -appendUnits syntax");
186 else
187 unitsTemplate = " (%s)";
188 break;
189 case SET_PIPE:
190 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
192 break;
193 case SET_SDDSOUTPUT:
194 sddsOutput = 1;
195 sddsOutputFile = NULL;
196 if ((s_arg[i_arg].n_items != 1 && s_arg[i_arg].n_items != 2) ||
197 (s_arg[i_arg].n_items == 2 &&
SDDS_StringIsBlank(sddsOutputFile = s_arg[i_arg].list[1])))
199 break;
200 case SET_READALL:
201 readAll = 1;
202 break;
203 default:
204 bomb(
"unknown switch", USAGE);
205 break;
206 }
207 } else {
208 filename =
trealloc(filename,
sizeof(*filename) * (filenames + 1));
209 filename[filenames++] = s_arg[i_arg].list[0];
210 }
211 }
212
213 if (!filenames && !(pipeFlags & USE_STDIN))
215 if (pipeFlags & USE_STDIN) {
216 char **filename1;
217 filename1 =
tmalloc(
sizeof(*filename1) * (filenames + 1));
218 filename1[0] = NULL;
219 for (file = 0; file < filenames; file++)
220 filename1[file + 1] = filename[file];
221 if (filename)
222 free(filename);
223 filename = filename1;
224 filenames++;
225 }
226#ifdef DEBUG
227 fprintf(stderr, "files: ");
228 for (file = 0; file < filenames; file++)
229 fprintf(stderr, "%s ", filename[file] ? filename[file] : "NULL");
230 fprintf(stderr, "\n");
231#endif
232
233 if (sddsOutput && !InitializeSDDSHeaderOutput(&SDDSout, sddsOutputFile)) {
235 exit(EXIT_FAILURE);
236 }
237
238 for (file = 0; file < filenames; file++) {
239#ifdef DEBUG
240 fprintf(stderr, "working on file %ld\n", file);
241#endif
244 exit(EXIT_FAILURE);
245 }
246#ifdef DEBUG
247 fprintf(stderr, "file initialized\n");
248#endif
249 if (sddsOutput) {
250#ifdef DEBUG
251 fprintf(stderr, "Making SDDS header summary\n");
252#endif
253 if (!MakeSDDSHeaderSummary(&SDDSout, &SDDS_dataset, list_request, filename[file])) {
255 exit(EXIT_FAILURE);
256 }
257#ifdef SOLARIS
260 exit(EXIT_FAILURE);
261 }
262#endif
263 continue;
264 }
265 layout = &SDDS_dataset.layout;
266 if (list_request >= 0) {
267 switch (list_request) {
268 case SET_COLUMN_LIST:
269#ifdef DEBUG
270 fprintf(stderr, "printing column info\n");
271#endif
272 for (i = 0; i < layout->n_columns; i++) {
273 fputs(layout->column_definition[i].name, stdout);
274 if (append_units && layout->column_definition[i].units &&
276 fprintf(stdout, unitsTemplate, layout->column_definition[i].units);
277 fputs(delimiter ? delimiter : "\n", stdout);
278 }
279 break;
280 case SET_PARAMETER_LIST:
281#ifdef DEBUG
282 fprintf(stderr, "printing parameter info\n");
283#endif
284 for (i = 0; i < layout->n_parameters; i++) {
285 fputs(layout->parameter_definition[i].name, stdout);
286 if (append_units && layout->parameter_definition[i].units &&
288 fprintf(stdout, unitsTemplate, layout->parameter_definition[i].units);
289 fputs(delimiter ? delimiter : "\n", stdout);
290 }
291 break;
292 case SET_ASSOCIATE_LIST:
293#ifdef DEBUG
294 fprintf(stderr, "printing associate info\n");
295#endif
296 for (i = 0; i < layout->n_associates; i++) {
297 fputs(layout->associate_definition[i].filename, stdout);
298 fputs(delimiter ? delimiter : "\n", stdout);
299 }
300 break;
301 case SET_ARRAY_LIST:
302#ifdef DEBUG
303 fprintf(stderr, "printing array info\n");
304#endif
305 for (i = 0; i < layout->n_arrays; i++) {
306 fputs(layout->array_definition[i].name, stdout);
307 if (append_units && layout->array_definition[i].units &&
309 fprintf(stdout, unitsTemplate, layout->array_definition[i].units);
310 fputs(delimiter ? delimiter : "\n", stdout);
311 }
312 break;
313 case SET_VERSION:
314#ifdef DEBUG
315 fprintf(stderr, "printing version info\n");
316#endif
317 s[0] = 0;
318 if (!(fp = fopen(filename[file], "r")) || !fgets(s, SDDS_MAXLINE, fp)) {
319 puts("-1");
320 exit(EXIT_FAILURE);
321 }
322 if (strncmp(s, "SDDS", 4) != 0)
323 puts("0");
324 else {
325 if (!(ptr = strchr(s, '\n')))
326 puts("0");
327 else {
328 *ptr = 0;
329 puts(s + 4);
330 }
331 }
332 fclose(fp);
333 break;
334 default:
335 SDDS_Bomb(
"something impossible happened!");
336 break;
337 }
338 if (readAll) {
340 ;
341 }
342#ifdef SOLARIS
343 if (list_request != SET_VERSION && !
SDDS_Terminate(&SDDS_dataset)) {
345 exit(EXIT_FAILURE);
346 }
347#endif
348 exit(EXIT_SUCCESS);
349 }
350
351 fprintf(stdout, "\nfile %s is in SDDS protocol version %" PRId32 "\n",
352 filename[file] ? filename[file] : "stdin", layout->version);
355 if (text)
356 fprintf(stdout, "description: %s\n", text);
357 if (contents)
358 fprintf(stdout, "contents: %s\n", contents);
359 if (layout->data_mode.mode == SDDS_ASCII) {
360 fprintf(stdout, "\ndata is ASCII with %" PRId32 " lines per row and %" PRId32 " additional header lines expected.\n",
361 layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
362 fprintf(stdout, "row counts: %s\n", layout->data_mode.no_row_counts ? "no" : "yes");
363 } else {
364 if (!SDDS_dataset.layout.byteOrderDeclared)
365 fprintf(stdout, "data is binary (no byte order declared)\n");
367 if (SDDS_dataset.swapByteOrder)
368 fprintf(stdout, "data is little-endian binary\n");
369 else
370 fprintf(stdout, "data is big-endian binary\n");
371 } else {
372 if (SDDS_dataset.swapByteOrder)
373 fprintf(stdout, "data is big-endian binary\n");
374 else
375 fprintf(stdout, "data is little-endian binary\n");
376 }
377 }
378
379 if (layout->n_columns) {
380 fprintf(stdout, "\n%" PRId32 " columns of data:\n", layout->n_columns);
381 fprintf(stdout, "NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n");
382 fprintf(stdout, " LENGTH\n");
383 for (i = 0; i < layout->n_columns; i++) {
384 coldef = layout->column_definition + i;
385 fprintf(stdout, "%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n",
386 coldef->name ? coldef->name : "NULL",
387 coldef->units ? coldef->units : "NULL",
388 coldef->symbol ? coldef->symbol : "NULL",
389 coldef->format_string ? coldef->format_string : "NULL",
391 coldef->field_length, coldef->description ? coldef->description : "NULL");
392 }
393 }
394
395 if (layout->n_parameters) {
396 fprintf(stdout, "\n%" PRId32 " parameters:\n", layout->n_parameters);
397 fprintf(stdout, "NAME UNITS SYMBOL TYPE DESCRIPTION\n");
398 for (i = 0; i < layout->n_parameters; i++) {
399 pardef = layout->parameter_definition + i;
400 fprintf(stdout, "%-19s %-19s %-19s %-19s %s\n",
401 pardef->name ? pardef->name : "NULL",
402 pardef->units ? pardef->units : "NULL",
403 pardef->symbol ? pardef->symbol : "NULL",
405 pardef->description ? pardef->description : "NULL");
406 }
407 }
408
409 if (layout->n_arrays) {
410 fprintf(stdout, "\n%" PRId32 " arrays of data:\n", layout->n_arrays);
411 fprintf(stdout, "NAME UNITS SYMBOL FORMAT TYPE FIELD GROUP DESCRIPTION\n");
412 fprintf(stdout, " LENGTH NAME\n");
413 for (i = 0; i < layout->n_arrays; i++) {
414 arraydef = layout->array_definition + i;
415 fprintf(stdout, "%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32 " %-7" PRId32 " %-15s %s\n",
416 arraydef->name ? arraydef->name : "NULL",
417 arraydef->units ? arraydef->units : "NULL",
418 arraydef->symbol ? arraydef->symbol : "NULL",
419 arraydef->format_string ? arraydef->format_string : "NULL",
421 arraydef->dimensions,
422 arraydef->field_length,
423 arraydef->group_name ? arraydef->group_name : "NULL",
424 arraydef->description ? arraydef->description : "NULL");
425 }
426 }
427
428 if (layout->n_associates) {
429 fprintf(stdout, "\n%" PRId32 " associates:\n", layout->n_associates);
430 fprintf(stdout, "SDDS FILENAME PATH CONTENTS DESCRIPTION\n");
431 for (i = 0; i < layout->n_associates; i++)
432 fprintf(stdout, "%-5s %-19s %-29s %-19s %s\n",
433 layout->associate_definition[i].sdds ? "yes" : "no",
434 layout->associate_definition[i].filename ? layout->associate_definition[i].filename : "NULL",
435 layout->associate_definition[i].path ? layout->associate_definition[i].path : "NULL",
436 layout->associate_definition[i].contents ? layout->associate_definition[i].contents : "NULL",
437 layout->associate_definition[i].description ? layout->associate_definition[i].description : "NULL");
438 }
439 fflush(stdout);
440 if (readAll) {
442 ;
443 }
446 exit(EXIT_FAILURE);
447 }
448 }
451 exit(EXIT_FAILURE);
452 }
453 return EXIT_SUCCESS;
454}
char * SDDS_type_name[SDDS_NUM_TYPES]
Array of supported data type names.
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).
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_IsBigEndianMachine()
Determines whether the current machine uses big-endian byte ordering.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
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 processPipeOption(char **item, long items, unsigned long *flags)