78 {
84 long list_request;
85 char s[SDDS_MAXLINE];
86 char **filename, *ptr, *sddsOutputFile;
87 long i, i_arg, filenames, file, append_units, sddsOutput;
88 SCANNED_ARG *s_arg;
89 char *text, *contents, *delimiter, *unitsTemplate;
90 FILE *fp;
91 unsigned long pipeFlags;
92 long readAll;
93
94 list_request = -1;
95 filename = NULL;
96 sddsOutput = 0;
97 filenames = append_units = 0;
98 delimiter = sddsOutputFile = unitsTemplate = NULL;
99 pipeFlags = 0;
100 readAll = 0;
101
103 argc =
scanargs(&s_arg, argc, argv);
104
105 for (i_arg = 1; i_arg < argc; i_arg++) {
106 if (s_arg[i_arg].arg_type == OPTION) {
108 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
109 case SET_COLUMN_LIST:
110 list_request = SET_COLUMN_LIST;
111 break;
112 case SET_PARAMETER_LIST:
113 list_request = SET_PARAMETER_LIST;
114 break;
115 case SET_ASSOCIATE_LIST:
116 list_request = SET_ASSOCIATE_LIST;
117 break;
118 case SET_ARRAY_LIST:
119 list_request = SET_ARRAY_LIST;
120 break;
121 case SET_VERSION:
122 list_request = SET_VERSION;
123 break;
124 case SET_DELIMITER:
125 if (s_arg[i_arg].n_items < 2)
127 delimiter = s_arg[i_arg].list[1];
128 break;
129 case SET_APPEND_UNITS:
130 append_units = 1;
131 if (s_arg[i_arg].n_items == 2) {
132 if (strncmp(s_arg[i_arg].list[1], "bare", strlen(s_arg[i_arg].list[1])) == 0)
133 unitsTemplate = " %s";
134 else
135 SDDS_Bomb(
"invalid -appendUnits syntax");
136 } else if (s_arg[i_arg].n_items > 2)
137 SDDS_Bomb(
"invalid -appendUnits syntax");
138 else
139 unitsTemplate = " (%s)";
140 break;
141 case SET_PIPE:
142 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
144 break;
145 case SET_SDDSOUTPUT:
146 sddsOutput = 1;
147 sddsOutputFile = NULL;
148 if ((s_arg[i_arg].n_items != 1 && s_arg[i_arg].n_items != 2) ||
149 (s_arg[i_arg].n_items == 2 &&
SDDS_StringIsBlank(sddsOutputFile = s_arg[i_arg].list[1])))
151 break;
152 case SET_READALL:
153 readAll = 1;
154 break;
155 default:
156 bomb(
"unknown switch", USAGE);
157 break;
158 }
159 } else {
160 filename =
trealloc(filename,
sizeof(*filename) * (filenames + 1));
161 filename[filenames++] = s_arg[i_arg].list[0];
162 }
163 }
164
165 if (!filenames && !(pipeFlags & USE_STDIN))
167 if (pipeFlags & USE_STDIN) {
168 char **filename1;
169 filename1 =
tmalloc(
sizeof(*filename1) * (filenames + 1));
170 filename1[0] = NULL;
171 for (file = 0; file < filenames; file++)
172 filename1[file + 1] = filename[file];
173 if (filename)
174 free(filename);
175 filename = filename1;
176 filenames++;
177 }
178#ifdef DEBUG
179 fprintf(stderr, "files: ");
180 for (file = 0; file < filenames; file++)
181 fprintf(stderr, "%s ", filename[file] ? filename[file] : "NULL");
182 fprintf(stderr, "\n");
183#endif
184
185 if (sddsOutput && !InitializeSDDSHeaderOutput(&SDDSout, sddsOutputFile)) {
187 exit(EXIT_FAILURE);
188 }
189
190 for (file = 0; file < filenames; file++) {
191#ifdef DEBUG
192 fprintf(stderr, "working on file %ld\n", file);
193#endif
196 exit(EXIT_FAILURE);
197 }
198#ifdef DEBUG
199 fprintf(stderr, "file initialized\n");
200#endif
201 if (sddsOutput) {
202#ifdef DEBUG
203 fprintf(stderr, "Making SDDS header summary\n");
204#endif
205 if (!MakeSDDSHeaderSummary(&SDDSout, &SDDS_dataset, list_request, filename[file])) {
207 exit(EXIT_FAILURE);
208 }
209#ifdef SOLARIS
212 exit(EXIT_FAILURE);
213 }
214#endif
215 continue;
216 }
217 layout = &SDDS_dataset.layout;
218 if (list_request >= 0) {
219 switch (list_request) {
220 case SET_COLUMN_LIST:
221#ifdef DEBUG
222 fprintf(stderr, "printing column info\n");
223#endif
224 for (i = 0; i < layout->n_columns; i++) {
225 fputs(layout->column_definition[i].name, stdout);
226 if (append_units && layout->column_definition[i].units &&
228 fprintf(stdout, unitsTemplate, layout->column_definition[i].units);
229 fputs(delimiter ? delimiter : "\n", stdout);
230 }
231 break;
232 case SET_PARAMETER_LIST:
233#ifdef DEBUG
234 fprintf(stderr, "printing parameter info\n");
235#endif
236 for (i = 0; i < layout->n_parameters; i++) {
237 fputs(layout->parameter_definition[i].name, stdout);
238 if (append_units && layout->parameter_definition[i].units &&
240 fprintf(stdout, unitsTemplate, layout->parameter_definition[i].units);
241 fputs(delimiter ? delimiter : "\n", stdout);
242 }
243 break;
244 case SET_ASSOCIATE_LIST:
245#ifdef DEBUG
246 fprintf(stderr, "printing associate info\n");
247#endif
248 for (i = 0; i < layout->n_associates; i++) {
249 fputs(layout->associate_definition[i].filename, stdout);
250 fputs(delimiter ? delimiter : "\n", stdout);
251 }
252 break;
253 case SET_ARRAY_LIST:
254#ifdef DEBUG
255 fprintf(stderr, "printing array info\n");
256#endif
257 for (i = 0; i < layout->n_arrays; i++) {
258 fputs(layout->array_definition[i].name, stdout);
259 if (append_units && layout->array_definition[i].units &&
261 fprintf(stdout, unitsTemplate, layout->array_definition[i].units);
262 fputs(delimiter ? delimiter : "\n", stdout);
263 }
264 break;
265 case SET_VERSION:
266#ifdef DEBUG
267 fprintf(stderr, "printing version info\n");
268#endif
269 s[0] = 0;
270 if (!(fp = fopen(filename[file], "r")) || !fgets(s, SDDS_MAXLINE, fp)) {
271 puts("-1");
272 exit(EXIT_FAILURE);
273 }
274 if (strncmp(s, "SDDS", 4) != 0)
275 puts("0");
276 else {
277 if (!(ptr = strchr(s, '\n')))
278 puts("0");
279 else {
280 *ptr = 0;
281 puts(s + 4);
282 }
283 }
284 fclose(fp);
285 break;
286 default:
287 SDDS_Bomb(
"something impossible happened!");
288 break;
289 }
290 if (readAll) {
292 ;
293 }
294#ifdef SOLARIS
295 if (list_request != SET_VERSION && !
SDDS_Terminate(&SDDS_dataset)) {
297 exit(EXIT_FAILURE);
298 }
299#endif
300 exit(EXIT_SUCCESS);
301 }
302
303 fprintf(stdout, "\nfile %s is in SDDS protocol version %" PRId32 "\n",
304 filename[file] ? filename[file] : "stdin", layout->version);
307 if (text)
308 fprintf(stdout, "description: %s\n", text);
309 if (contents)
310 fprintf(stdout, "contents: %s\n", contents);
311 if (layout->data_mode.mode == SDDS_ASCII) {
312 fprintf(stdout, "\ndata is ASCII with %" PRId32 " lines per row and %" PRId32 " additional header lines expected.\n",
313 layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
314 fprintf(stdout, "row counts: %s\n", layout->data_mode.no_row_counts ? "no" : "yes");
315 } else {
316 if (!SDDS_dataset.layout.byteOrderDeclared)
317 fprintf(stdout, "data is binary (no byte order declared)\n");
319 if (SDDS_dataset.swapByteOrder)
320 fprintf(stdout, "data is little-endian binary\n");
321 else
322 fprintf(stdout, "data is big-endian binary\n");
323 } else {
324 if (SDDS_dataset.swapByteOrder)
325 fprintf(stdout, "data is big-endian binary\n");
326 else
327 fprintf(stdout, "data is little-endian binary\n");
328 }
329 }
330
331 if (layout->n_columns) {
332 fprintf(stdout, "\n%" PRId32 " columns of data:\n", layout->n_columns);
333 fprintf(stdout, "NAME UNITS SYMBOL FORMAT TYPE FIELD DESCRIPTION\n");
334 fprintf(stdout, " LENGTH\n");
335 for (i = 0; i < layout->n_columns; i++) {
336 coldef = layout->column_definition + i;
337 fprintf(stdout, "%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n",
338 coldef->name ? coldef->name : "NULL",
339 coldef->units ? coldef->units : "NULL",
340 coldef->symbol ? coldef->symbol : "NULL",
341 coldef->format_string ? coldef->format_string : "NULL",
343 coldef->field_length, coldef->description ? coldef->description : "NULL");
344 }
345 }
346
347 if (layout->n_parameters) {
348 fprintf(stdout, "\n%" PRId32 " parameters:\n", layout->n_parameters);
349 fprintf(stdout, "NAME UNITS SYMBOL TYPE DESCRIPTION\n");
350 for (i = 0; i < layout->n_parameters; i++) {
351 pardef = layout->parameter_definition + i;
352 fprintf(stdout, "%-19s %-19s %-19s %-19s %s\n",
353 pardef->name ? pardef->name : "NULL",
354 pardef->units ? pardef->units : "NULL",
355 pardef->symbol ? pardef->symbol : "NULL",
357 pardef->description ? pardef->description : "NULL");
358 }
359 }
360
361 if (layout->n_arrays) {
362 fprintf(stdout, "\n%" PRId32 " arrays of data:\n", layout->n_arrays);
363 fprintf(stdout, "NAME UNITS SYMBOL FORMAT TYPE FIELD GROUP DESCRIPTION\n");
364 fprintf(stdout, " LENGTH NAME\n");
365 for (i = 0; i < layout->n_arrays; i++) {
366 arraydef = layout->array_definition + i;
367 fprintf(stdout, "%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32 " %-7" PRId32 " %-15s %s\n",
368 arraydef->name ? arraydef->name : "NULL",
369 arraydef->units ? arraydef->units : "NULL",
370 arraydef->symbol ? arraydef->symbol : "NULL",
371 arraydef->format_string ? arraydef->format_string : "NULL",
373 arraydef->dimensions,
374 arraydef->field_length,
375 arraydef->group_name ? arraydef->group_name : "NULL",
376 arraydef->description ? arraydef->description : "NULL");
377 }
378 }
379
380 if (layout->n_associates) {
381 fprintf(stdout, "\n%" PRId32 " associates:\n", layout->n_associates);
382 fprintf(stdout, "SDDS FILENAME PATH CONTENTS DESCRIPTION\n");
383 for (i = 0; i < layout->n_associates; i++)
384 fprintf(stdout, "%-5s %-19s %-29s %-19s %s\n",
385 layout->associate_definition[i].sdds ? "yes" : "no",
386 layout->associate_definition[i].filename ? layout->associate_definition[i].filename : "NULL",
387 layout->associate_definition[i].path ? layout->associate_definition[i].path : "NULL",
388 layout->associate_definition[i].contents ? layout->associate_definition[i].contents : "NULL",
389 layout->associate_definition[i].description ? layout->associate_definition[i].description : "NULL");
390 }
391 fflush(stdout);
392 if (readAll) {
394 ;
395 }
398 exit(EXIT_FAILURE);
399 }
400 }
403 exit(EXIT_FAILURE);
404 }
405 return EXIT_SUCCESS;
406}
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)