135 {
137 long i, k, i_arg, retval, description;
138 int64_t j, rows;
139 SCANNED_ARG *s_arg;
140 char **input;
141 char **column_name, **parameter_name, **array_name;
142 char **parameterFormat, **columnFormat, **arrayFormat;
143 long column_names, parameter_names, array_names, page_number, *type, inputs;
144 char *delimiter;
145 void **data;
146 char *buffer;
147 long filenames, print_rows, print_pages, noQuotes, pipe, ignoreFormats;
148 static char printBuffer[SDDS_MAXLINE * 16];
149 long n_rows_bare, n_rows_total, n_pages, n_pages_bare, n_rows_scinotation;
150 int64_t n_rows;
151
153 argc =
scanargs(&s_arg, argc, argv);
154 if (argc < 3)
156
157 type = NULL;
158 data = NULL;
159 buffer =
tmalloc(
sizeof(
char) * 16);
160 delimiter = NULL;
161 input = parameter_name = column_name = array_name = NULL;
162 parameterFormat = columnFormat = arrayFormat = NULL;
163 inputs = parameter_names = column_names = array_names = 0;
164 page_number = filenames = ignoreFormats = 0;
165 n_rows = n_rows_bare = n_rows_total = n_pages = n_pages_bare = 0;
166 print_rows = print_pages = noQuotes = pipe = description = 0;
167 n_rows_scinotation = 0;
168
169
170 for (i_arg = 1; i_arg < argc; i_arg++) {
171 if (s_arg[i_arg].arg_type == OPTION) {
172 int optIndex =
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0);
173 switch (optIndex) {
174 case SET_COLUMNS:
175 if (s_arg[i_arg].n_items < 2)
177 if (column_names)
178 SDDS_Bomb(
"invalid syntax: specify -columns once only");
179 column_name =
tmalloc(
sizeof(*column_name) * (s_arg[i_arg].n_items - 1));
180 for (i = 1; i < s_arg[i_arg].n_items; i++)
181 column_name[i - 1] = s_arg[i_arg].list[i];
182 column_names = s_arg[i_arg].n_items - 1;
183 break;
184
185 case SET_PARAMETERS:
186 if (s_arg[i_arg].n_items < 2)
188 if (parameter_names)
189 SDDS_Bomb(
"invalid syntax: specify -parameters once only");
190 parameter_name =
tmalloc(
sizeof(*parameter_name) * (s_arg[i_arg].n_items - 1));
191 for (i = 1; i < s_arg[i_arg].n_items; i++)
192 parameter_name[i - 1] = s_arg[i_arg].list[i];
193 parameter_names = s_arg[i_arg].n_items - 1;
194 break;
195
196 case SET_ARRAYS:
197 if (s_arg[i_arg].n_items < 2)
199 if (array_names)
200 SDDS_Bomb(
"invalid syntax: specify -arrays once only");
201 array_name =
tmalloc(
sizeof(*array_name) * (s_arg[i_arg].n_items - 1));
202 for (i = 1; i < s_arg[i_arg].n_items; i++)
203 array_name[i - 1] = s_arg[i_arg].list[i];
204 array_names = s_arg[i_arg].n_items - 1;
205 break;
206
207 case SET_TABLE:
208 case SET_PAGE:
209 if (s_arg[i_arg].n_items < 2 || s_arg[i_arg].n_items > 2)
211 if (page_number != 0)
212 SDDS_Bomb(
"invalid syntax: specify -page once only");
213 if (sscanf(s_arg[i_arg].list[1], "%ld", &page_number) != 1 || page_number <= 0)
214 SDDS_Bomb(
"invalid -page syntax or value");
215 break;
216
217 case SET_DELIMITER:
218 if (s_arg[i_arg].n_items < 2)
220 delimiter = s_arg[i_arg].list[1];
221 break;
222
223 case SET_FILENAMES:
224 filenames = 1;
225 break;
226
227 case SET_ROWS:
228 if (s_arg[i_arg].n_items > 4)
230 else {
231 char *rowsOutputMode[3] = {"bare", "total", "scientific"};
232 for (i = 1; i < s_arg[i_arg].n_items; i++) {
234 rowsOutputMode, 3, 0);
235 if (rm == 0)
236 n_rows_bare = 1;
237 else if (rm == 1)
238 n_rows_total = 1;
239 else if (rm == 2)
240 n_rows_scinotation = 1;
241 else
242 SDDS_Bomb(
"unknown output mode for -rows option");
243 }
244 }
245 print_rows = 1;
246 break;
247
248 case SET_SHOW_PAGES:
249 if (s_arg[i_arg].n_items > 2)
251 else {
252 char *pagesOutputMode[1] = {"bare"};
253 for (i = 1; i < s_arg[i_arg].n_items; i++) {
254 if (strcmp(s_arg[i_arg].list[i], "") != 0) {
256 pagesOutputMode, 1, 0);
257 if (pm == 0)
258 n_pages_bare = 1;
259 else
260 SDDS_Bomb(
"unknown output mode for -npages option");
261 } else
262 SDDS_Bomb(
"unknown output mode for -npages option");
263 }
264 }
265 print_pages = 1;
266 break;
267
268 case SET_NOQUOTES:
269 noQuotes = 1;
270 break;
271
272 case SET_PIPE:
273 pipe = 1;
274 break;
275
276 case SET_IGNOREFORMATS:
277 ignoreFormats = 1;
278 break;
279
280 case SET_DESCRIPTION:
281 description = 1;
282 break;
283
284 default:
285 fprintf(stderr, "error: unknown switch: %s\n",
286 s_arg[i_arg].list[0]);
287 exit(1);
288 break;
289 }
290 } else {
291 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
292 input[inputs++] = s_arg[i_arg].list[0];
293 }
294 }
295
296 if (!inputs) {
297 if (!pipe)
299 inputs = 1;
300 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
301 input[0] = NULL;
302 }
303
304 if (!column_names && !parameter_names && !array_names && !print_rows && !description && !print_pages)
305 SDDS_Bomb(
"you must specify one of -columns, -parameters, "
306 "-arrays, -rows or -description");
307
308 if (!delimiter) {
309 if (column_names || array_names)
311 else
313 }
314
316
317
318 for (k = 0; k < inputs; k++) {
321 exit(1);
322 }
323 n_pages = 0;
324
325
326 if (column_names) {
327 if (k == 0) {
328 type =
tmalloc(
sizeof(*type) * column_names);
329 data =
tmalloc(
sizeof(*data) * column_names);
330 columnFormat =
tmalloc(
sizeof(*columnFormat) * column_names);
331 }
332 for (i = 0; i < column_names; i++) {
334 if (j < 0) {
335 fprintf(stderr, "error: column %s does not exist\n",
336 column_name[i]);
337 exit(1);
338 }
341 "format_string",
342 columnFormat + i,
343 SDDS_GET_BY_INDEX, j)) {
345 exit(1);
346 }
347 }
348 } else if (array_names) {
349 if (k == 0) {
350 type =
tmalloc(
sizeof(*type) * array_names);
351 data =
tmalloc(
sizeof(*data) * array_names);
352 arrayFormat =
tmalloc(
sizeof(*arrayFormat) * array_names);
353 }
354
355 for (i = 0; i < array_names; i++) {
357 if (j < 0) {
358 fprintf(stderr, "error: array %s does not exist\n",
359 array_name[i]);
360 exit(1);
361 }
364 arrayFormat + i,
365 SDDS_BY_INDEX, j)) {
367 exit(1);
368 }
369 }
370 } else if (parameter_names) {
371 if (k == 0) {
372 type =
tmalloc(
sizeof(*type) * parameter_names);
373 parameterFormat =
tmalloc(
sizeof(*parameterFormat) * parameter_names);
374 }
375 for (i = 0; i < parameter_names; i++) {
377 if (j < 0) {
378 fprintf(stderr, "error: parameter %s does not exist\n",
379 parameter_name[i]);
380 exit(1);
381 }
384 "format_string",
385 parameterFormat + i,
386 SDDS_BY_INDEX, j)) {
388 exit(1);
389 }
390 }
391 }
392
393
394 if (description) {
395 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.description, NULL,
396 printBuffer,
397 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
399 exit(1);
400 }
401
402 fputs(printBuffer, stdout);
403 fprintf(stdout, "%s", delimiter);
404
405 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.contents, NULL,
406 printBuffer,
407 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
409 exit(1);
410 }
411 fputs(printBuffer, stdout);
412 fprintf(stdout, "%s", delimiter);
413
414 if (!strchr(delimiter, '\n'))
415 fputc('\n', stdout);
416 }
417
418 retval = -1;
419 while (retval != page_number && (retval =
SDDS_ReadPage(&SDDS_dataset)) > 0) {
420 if (page_number && retval != page_number)
421 continue;
422
423 if (print_rows) {
425 if (n_rows_total && !page_number) {
426 n_rows += counti;
427 } else {
428 if (!n_rows_scinotation || counti == 0) {
429 if (n_rows_bare)
430 fprintf(stdout, "%" PRId64 "\n", (int64_t)counti);
431 else
432 fprintf(stdout, "%" PRId64 " rows\n", (int64_t)counti);
433 } else {
434 char format[20];
435 double count = counti;
436 snprintf(format, 20, "%%.%ldle%s",
437 (long)(log10(count)),
438 n_rows_bare ? "" : " rows");
439 fprintf(stdout, format, count);
440 fputc('\n', stdout);
441 }
442 }
443 }
444
445 if (column_names) {
447 if (rows < 0) {
449 exit(1);
450 }
451 if (rows) {
452 if (filenames) {
453 fprintf(stdout, "%s%s", input[k], delimiter);
454 if (!strchr(delimiter, '\n'))
455 fputc('\n', stdout);
456 }
457 for (i = 0; i < column_names; i++) {
459 column_name[i]);
460 if (!data[i]) {
462 exit(1);
463 }
464 }
465 for (j = 0; j < rows; j++) {
466 for (i = 0; i < column_names; i++) {
468 ignoreFormats ? NULL : columnFormat[i],
469 printBuffer,
470 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
472 SDDS_VERBOSE_PrintErrors);
473 exit(1);
474 }
475 fputs(printBuffer, stdout);
476 if (i != column_names - 1)
477 fprintf(stdout, "%s", delimiter);
478 }
479 fputc('\n', stdout);
480 }
481 }
482 } else if (array_names) {
484 if (filenames) {
485 fprintf(stdout, "%s%s", input[k], delimiter);
486 if (!strchr(delimiter, '\n'))
487 fputc('\n', stdout);
488 }
489 for (i = 0; i < array_names; i++) {
491 if (!array) {
493 exit(1);
494 }
495 for (j = 0; j < array->elements; j++) {
497 ignoreFormats ? NULL : arrayFormat[i],
498 printBuffer,
499 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
501 exit(1);
502 }
503 fputs(printBuffer, stdout);
504 fprintf(stdout, "%s", delimiter);
505 }
506 }
507 if (!strchr(delimiter, '\n'))
508 fputc('\n', stdout);
509 } else if (parameter_names) {
510 if (filenames)
511 fprintf(stdout, "%s%s", input[k], delimiter);
512 for (i = 0; i < parameter_names; i++) {
514 buffer)) {
516 exit(1);
517 }
519 ignoreFormats ? NULL : parameterFormat[i],
520 printBuffer,
521 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
523 exit(1);
524 }
525 fputs(printBuffer, stdout);
526 fprintf(stdout, "%s", delimiter);
527 }
528 if (!strchr(delimiter, '\n'))
529 fputc('\n', stdout);
530 }
531
532 n_pages++;
533 }
534
535 if (retval == 0) {
537 exit(1);
538 } else {
539 if (print_rows && (n_rows_total || (retval == -1 && n_pages == 0)) && !page_number) {
540 if (!n_rows_scinotation || n_rows == 0) {
541 if (n_rows_bare)
542 fprintf(stdout, "%" PRId64 "\n", n_rows);
543 else
544 fprintf(stdout, "%" PRId64 " rows\n", n_rows);
545 } else {
546 char format[20];
547 double count = n_rows;
548 snprintf(format, 20, "%%.%ldle%s",
549 (long)(log10(count)),
550 n_rows_bare ? "" : " rows");
551 fprintf(stdout, format, count);
552 fputc('\n', stdout);
553 }
554 }
555 if (print_pages) {
556 if (n_pages_bare)
557 fprintf(stdout, "%ld\n", n_pages);
558 else
559 fprintf(stdout, "%ld pages\n", n_pages);
560 }
561 }
564 exit(1);
565 }
566 }
567 return 0;
568}
int32_t SDDS_GetArrayInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified array in the SDDS dataset.
int32_t SDDS_GetParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified parameter in the SDDS dataset.
int32_t SDDS_GetColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified column in the SDDS dataset.
int32_t SDDS_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
int32_t SDDS_GetArrayIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named array in the SDDS dataset.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_SprintTypedValue(void *data, int64_t index, int32_t type, const char *format, char *buffer, uint32_t mode)
Formats a data value of a specified type into a string buffer using an optional printf format string.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
int32_t SDDS_GetArrayType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of an array in the SDDS dataset by its index.
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_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
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 * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
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)