101 {
103 long i, k, i_arg, retval, description;
104 int64_t j, rows;
105 SCANNED_ARG *s_arg;
106 char **input;
107 char **column_name, **parameter_name, **array_name;
108 char **parameterFormat, **columnFormat, **arrayFormat;
109 long column_names, parameter_names, array_names, page_number, *type, inputs;
110 char *delimiter;
111 void **data;
112 char *buffer;
113 long filenames, print_rows, print_pages, noQuotes, pipe, ignoreFormats;
114 static char printBuffer[SDDS_MAXLINE * 16];
115 long n_rows_bare, n_rows_total, n_pages, n_pages_bare, n_rows_scinotation;
116 int64_t n_rows;
117
119 argc =
scanargs(&s_arg, argc, argv);
120 if (argc < 3)
122
123 type = NULL;
124 data = NULL;
125 buffer =
tmalloc(
sizeof(
char) * 16);
126 delimiter = NULL;
127 input = parameter_name = column_name = array_name = NULL;
128 parameterFormat = columnFormat = arrayFormat = NULL;
129 inputs = parameter_names = column_names = array_names = 0;
130 page_number = filenames = ignoreFormats = 0;
131 n_rows = n_rows_bare = n_rows_total = n_pages = n_pages_bare = 0;
132 print_rows = print_pages = noQuotes = pipe = description = 0;
133 n_rows_scinotation = 0;
134
135
136 for (i_arg = 1; i_arg < argc; i_arg++) {
137 if (s_arg[i_arg].arg_type == OPTION) {
138 int optIndex =
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0);
139 switch (optIndex) {
140 case SET_COLUMNS:
141 if (s_arg[i_arg].n_items < 2)
143 if (column_names)
144 SDDS_Bomb(
"invalid syntax: specify -columns once only");
145 column_name =
tmalloc(
sizeof(*column_name) * (s_arg[i_arg].n_items - 1));
146 for (i = 1; i < s_arg[i_arg].n_items; i++)
147 column_name[i - 1] = s_arg[i_arg].list[i];
148 column_names = s_arg[i_arg].n_items - 1;
149 break;
150
151 case SET_PARAMETERS:
152 if (s_arg[i_arg].n_items < 2)
154 if (parameter_names)
155 SDDS_Bomb(
"invalid syntax: specify -parameters once only");
156 parameter_name =
tmalloc(
sizeof(*parameter_name) * (s_arg[i_arg].n_items - 1));
157 for (i = 1; i < s_arg[i_arg].n_items; i++)
158 parameter_name[i - 1] = s_arg[i_arg].list[i];
159 parameter_names = s_arg[i_arg].n_items - 1;
160 break;
161
162 case SET_ARRAYS:
163 if (s_arg[i_arg].n_items < 2)
165 if (array_names)
166 SDDS_Bomb(
"invalid syntax: specify -arrays once only");
167 array_name =
tmalloc(
sizeof(*array_name) * (s_arg[i_arg].n_items - 1));
168 for (i = 1; i < s_arg[i_arg].n_items; i++)
169 array_name[i - 1] = s_arg[i_arg].list[i];
170 array_names = s_arg[i_arg].n_items - 1;
171 break;
172
173 case SET_TABLE:
174 case SET_PAGE:
175 if (s_arg[i_arg].n_items < 2 || s_arg[i_arg].n_items > 2)
177 if (page_number != 0)
178 SDDS_Bomb(
"invalid syntax: specify -page once only");
179 if (sscanf(s_arg[i_arg].list[1], "%ld", &page_number) != 1 || page_number <= 0)
180 SDDS_Bomb(
"invalid -page syntax or value");
181 break;
182
183 case SET_DELIMITER:
184 if (s_arg[i_arg].n_items < 2)
186 delimiter = s_arg[i_arg].list[1];
187 break;
188
189 case SET_FILENAMES:
190 filenames = 1;
191 break;
192
193 case SET_ROWS:
194 if (s_arg[i_arg].n_items > 4)
196 else {
197 char *rowsOutputMode[3] = {"bare", "total", "scientific"};
198 for (i = 1; i < s_arg[i_arg].n_items; i++) {
200 rowsOutputMode, 3, 0);
201 if (rm == 0)
202 n_rows_bare = 1;
203 else if (rm == 1)
204 n_rows_total = 1;
205 else if (rm == 2)
206 n_rows_scinotation = 1;
207 else
208 SDDS_Bomb(
"unknown output mode for -rows option");
209 }
210 }
211 print_rows = 1;
212 break;
213
214 case SET_SHOW_PAGES:
215 if (s_arg[i_arg].n_items > 2)
217 else {
218 char *pagesOutputMode[1] = {"bare"};
219 for (i = 1; i < s_arg[i_arg].n_items; i++) {
220 if (strcmp(s_arg[i_arg].list[i], "") != 0) {
222 pagesOutputMode, 1, 0);
223 if (pm == 0)
224 n_pages_bare = 1;
225 else
226 SDDS_Bomb(
"unknown output mode for -npages option");
227 } else
228 SDDS_Bomb(
"unknown output mode for -npages option");
229 }
230 }
231 print_pages = 1;
232 break;
233
234 case SET_NOQUOTES:
235 noQuotes = 1;
236 break;
237
238 case SET_PIPE:
239 pipe = 1;
240 break;
241
242 case SET_IGNOREFORMATS:
243 ignoreFormats = 1;
244 break;
245
246 case SET_DESCRIPTION:
247 description = 1;
248 break;
249
250 default:
251 fprintf(stderr, "error: unknown switch: %s\n",
252 s_arg[i_arg].list[0]);
253 exit(1);
254 break;
255 }
256 } else {
257 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
258 input[inputs++] = s_arg[i_arg].list[0];
259 }
260 }
261
262 if (!inputs) {
263 if (!pipe)
265 inputs = 1;
266 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
267 input[0] = NULL;
268 }
269
270 if (!column_names && !parameter_names && !array_names && !print_rows && !description && !print_pages)
271 SDDS_Bomb(
"you must specify one of -columns, -parameters, "
272 "-arrays, -rows or -description");
273
274 if (!delimiter) {
275 if (column_names || array_names)
277 else
279 }
280
282
283
284 for (k = 0; k < inputs; k++) {
287 exit(1);
288 }
289 n_pages = 0;
290
291
292 if (column_names) {
293 if (k == 0) {
294 type =
tmalloc(
sizeof(*type) * column_names);
295 data =
tmalloc(
sizeof(*data) * column_names);
296 columnFormat =
tmalloc(
sizeof(*columnFormat) * column_names);
297 }
298 for (i = 0; i < column_names; i++) {
300 if (j < 0) {
301 fprintf(stderr, "error: column %s does not exist\n",
302 column_name[i]);
303 exit(1);
304 }
307 "format_string",
308 columnFormat + i,
309 SDDS_GET_BY_INDEX, j)) {
311 exit(1);
312 }
313 }
314 } else if (array_names) {
315 if (k == 0) {
316 type =
tmalloc(
sizeof(*type) * array_names);
317 data =
tmalloc(
sizeof(*data) * array_names);
318 arrayFormat =
tmalloc(
sizeof(*arrayFormat) * array_names);
319 }
320
321 for (i = 0; i < array_names; i++) {
323 if (j < 0) {
324 fprintf(stderr, "error: array %s does not exist\n",
325 array_name[i]);
326 exit(1);
327 }
330 arrayFormat + i,
331 SDDS_BY_INDEX, j)) {
333 exit(1);
334 }
335 }
336 } else if (parameter_names) {
337 if (k == 0) {
338 type =
tmalloc(
sizeof(*type) * parameter_names);
339 parameterFormat =
tmalloc(
sizeof(*parameterFormat) * parameter_names);
340 }
341 for (i = 0; i < parameter_names; i++) {
343 if (j < 0) {
344 fprintf(stderr, "error: parameter %s does not exist\n",
345 parameter_name[i]);
346 exit(1);
347 }
350 "format_string",
351 parameterFormat + i,
352 SDDS_BY_INDEX, j)) {
354 exit(1);
355 }
356 }
357 }
358
359
360 if (description) {
361 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.description, NULL,
362 printBuffer,
363 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
365 exit(1);
366 }
367
368 fputs(printBuffer, stdout);
369 fprintf(stdout, "%s", delimiter);
370
371 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.contents, NULL,
372 printBuffer,
373 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
375 exit(1);
376 }
377 fputs(printBuffer, stdout);
378 fprintf(stdout, "%s", delimiter);
379
380 if (!strchr(delimiter, '\n'))
381 fputc('\n', stdout);
382 }
383
384 retval = -1;
385 while (retval != page_number && (retval =
SDDS_ReadPage(&SDDS_dataset)) > 0) {
386 if (page_number && retval != page_number)
387 continue;
388
389 if (print_rows) {
391 if (n_rows_total && !page_number) {
392 n_rows += counti;
393 } else {
394 if (!n_rows_scinotation || counti == 0) {
395 if (n_rows_bare)
396 fprintf(stdout, "%" PRId64 "\n", (int64_t)counti);
397 else
398 fprintf(stdout, "%" PRId64 " rows\n", (int64_t)counti);
399 } else {
400 char format[20];
401 double count = counti;
402 snprintf(format, 20, "%%.%ldle%s",
403 (long)(log10(count)),
404 n_rows_bare ? "" : " rows");
405 fprintf(stdout, format, count);
406 fputc('\n', stdout);
407 }
408 }
409 }
410
411 if (column_names) {
413 if (rows < 0) {
415 exit(1);
416 }
417 if (rows) {
418 if (filenames) {
419 fprintf(stdout, "%s%s", input[k], delimiter);
420 if (!strchr(delimiter, '\n'))
421 fputc('\n', stdout);
422 }
423 for (i = 0; i < column_names; i++) {
425 column_name[i]);
426 if (!data[i]) {
428 exit(1);
429 }
430 }
431 for (j = 0; j < rows; j++) {
432 for (i = 0; i < column_names; i++) {
434 ignoreFormats ? NULL : columnFormat[i],
435 printBuffer,
436 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
438 SDDS_VERBOSE_PrintErrors);
439 exit(1);
440 }
441 fputs(printBuffer, stdout);
442 if (i != column_names - 1)
443 fprintf(stdout, "%s", delimiter);
444 }
445 fputc('\n', stdout);
446 }
447 }
448 } else if (array_names) {
450 if (filenames) {
451 fprintf(stdout, "%s%s", input[k], delimiter);
452 if (!strchr(delimiter, '\n'))
453 fputc('\n', stdout);
454 }
455 for (i = 0; i < array_names; i++) {
457 if (!array) {
459 exit(1);
460 }
461 for (j = 0; j < array->elements; j++) {
463 ignoreFormats ? NULL : arrayFormat[i],
464 printBuffer,
465 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
467 exit(1);
468 }
469 fputs(printBuffer, stdout);
470 fprintf(stdout, "%s", delimiter);
471 }
472 }
473 if (!strchr(delimiter, '\n'))
474 fputc('\n', stdout);
475 } else if (parameter_names) {
476 if (filenames)
477 fprintf(stdout, "%s%s", input[k], delimiter);
478 for (i = 0; i < parameter_names; i++) {
480 buffer)) {
482 exit(1);
483 }
485 ignoreFormats ? NULL : parameterFormat[i],
486 printBuffer,
487 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
489 exit(1);
490 }
491 fputs(printBuffer, stdout);
492 fprintf(stdout, "%s", delimiter);
493 }
494 if (!strchr(delimiter, '\n'))
495 fputc('\n', stdout);
496 }
497
498 n_pages++;
499 }
500
501 if (retval == 0) {
503 exit(1);
504 } else {
505 if (print_rows && (n_rows_total || (retval == -1 && n_pages == 0)) && !page_number) {
506 if (!n_rows_scinotation || n_rows == 0) {
507 if (n_rows_bare)
508 fprintf(stdout, "%" PRId64 "\n", n_rows);
509 else
510 fprintf(stdout, "%" PRId64 " rows\n", n_rows);
511 } else {
512 char format[20];
513 double count = n_rows;
514 snprintf(format, 20, "%%.%ldle%s",
515 (long)(log10(count)),
516 n_rows_bare ? "" : " rows");
517 fprintf(stdout, format, count);
518 fputc('\n', stdout);
519 }
520 }
521 if (print_pages) {
522 if (n_pages_bare)
523 fprintf(stdout, "%ld\n", n_pages);
524 else
525 fprintf(stdout, "%ld pages\n", n_pages);
526 }
527 }
530 exit(1);
531 }
532 }
533 return 0;
534}
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)