50#define SET_PARAMETERS 1
53#define SET_DELIMITER 3
54#define SET_FILENAMES 4
60#define SET_IGNOREFORMATS 10
61#define SET_DESCRIPTION 11
62#define SET_SHOW_PAGES 12
65char *option[N_OPTIONS] =
82char *USAGE =
"sdds2stream [-pipe] [<SDDSinput>...]\n"
83 " [-columns=<column-name>[,...]]\n"
84 " [-parameters=<parameter-name>[,...]]\n"
85 " [-arrays=<array-name>[,...]]]\n"
86 " [-page=<pageNumber>] [-delimiter=<delimiting-string>]\n"
87 " [-filenames] [-rows[=bare][,total][,scientific]]\n"
88 " [-npages=<bare>] [-noquotes]\n"
89 " [-ignoreFormats] [-description]\n\n"
90 "sdds2stream provides stream output to the standard output of data values from "
91 "a group of columns or parameters. Each line of the output contains a different "
92 "row of the tabular data or a different parameter. Values from different columns "
93 "are separated by the delimiter string, which by default is a single space. "
94 "If -page is not employed, all data pages are output sequentially. "
95 "If multiple filenames are given, the files are processed sequentially in the "
97 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
99long SDDS_SprintTypedValue2(
char *s,
char *format,
char *buffer,
unsigned long mode);
101int main(
int argc,
char **argv) {
103 long i, k, i_arg, retval, description;
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;
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;
119 argc =
scanargs(&s_arg, argc, argv);
125 buffer =
tmalloc(
sizeof(
char) * 16);
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;
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);
141 if (s_arg[i_arg].n_items < 2)
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;
152 if (s_arg[i_arg].n_items < 2)
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;
163 if (s_arg[i_arg].n_items < 2)
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;
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");
184 if (s_arg[i_arg].n_items < 2)
186 delimiter = s_arg[i_arg].list[1];
194 if (s_arg[i_arg].n_items > 4)
197 char *rowsOutputMode[3] = {
"bare",
"total",
"scientific"};
198 for (i = 1; i < s_arg[i_arg].n_items; i++) {
200 rowsOutputMode, 3, 0);
206 n_rows_scinotation = 1;
208 SDDS_Bomb(
"unknown output mode for -rows option");
215 if (s_arg[i_arg].n_items > 2)
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);
226 SDDS_Bomb(
"unknown output mode for -npages option");
228 SDDS_Bomb(
"unknown output mode for -npages option");
242 case SET_IGNOREFORMATS:
246 case SET_DESCRIPTION:
251 fprintf(stderr,
"error: unknown switch: %s\n",
252 s_arg[i_arg].list[0]);
257 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
258 input[inputs++] = s_arg[i_arg].list[0];
266 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
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");
275 if (column_names || array_names)
284 for (k = 0; k < inputs; k++) {
294 type =
tmalloc(
sizeof(*type) * column_names);
295 data =
tmalloc(
sizeof(*data) * column_names);
296 columnFormat =
tmalloc(
sizeof(*columnFormat) * column_names);
298 for (i = 0; i < column_names; i++) {
301 fprintf(stderr,
"error: column %s does not exist\n",
309 SDDS_GET_BY_INDEX, j)) {
314 }
else if (array_names) {
316 type =
tmalloc(
sizeof(*type) * array_names);
317 data =
tmalloc(
sizeof(*data) * array_names);
318 arrayFormat =
tmalloc(
sizeof(*arrayFormat) * array_names);
321 for (i = 0; i < array_names; i++) {
324 fprintf(stderr,
"error: array %s does not exist\n",
336 }
else if (parameter_names) {
338 type =
tmalloc(
sizeof(*type) * parameter_names);
339 parameterFormat =
tmalloc(
sizeof(*parameterFormat) * parameter_names);
341 for (i = 0; i < parameter_names; i++) {
344 fprintf(stderr,
"error: parameter %s does not exist\n",
361 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.description, NULL,
363 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
368 fputs(printBuffer, stdout);
369 fprintf(stdout,
"%s", delimiter);
371 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.contents, NULL,
373 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
377 fputs(printBuffer, stdout);
378 fprintf(stdout,
"%s", delimiter);
380 if (!strchr(delimiter,
'\n'))
385 while (retval != page_number && (retval =
SDDS_ReadPage(&SDDS_dataset)) > 0) {
386 if (page_number && retval != page_number)
391 if (n_rows_total && !page_number) {
394 if (!n_rows_scinotation || counti == 0) {
396 fprintf(stdout,
"%" PRId64
"\n", (int64_t)counti);
398 fprintf(stdout,
"%" PRId64
" rows\n", (int64_t)counti);
401 double count = counti;
402 snprintf(format, 20,
"%%.%ldle%s",
403 (
long)(log10(count)),
404 n_rows_bare ?
"" :
" rows");
405 fprintf(stdout, format, count);
419 fprintf(stdout,
"%s%s", input[k], delimiter);
420 if (!strchr(delimiter,
'\n'))
423 for (i = 0; i < column_names; i++) {
431 for (j = 0; j < rows; j++) {
432 for (i = 0; i < column_names; i++) {
434 ignoreFormats ? NULL : columnFormat[i],
436 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
438 SDDS_VERBOSE_PrintErrors);
441 fputs(printBuffer, stdout);
442 if (i != column_names - 1)
443 fprintf(stdout,
"%s", delimiter);
448 }
else if (array_names) {
451 fprintf(stdout,
"%s%s", input[k], delimiter);
452 if (!strchr(delimiter,
'\n'))
455 for (i = 0; i < array_names; i++) {
461 for (j = 0; j < array->elements; j++) {
463 ignoreFormats ? NULL : arrayFormat[i],
465 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
469 fputs(printBuffer, stdout);
470 fprintf(stdout,
"%s", delimiter);
473 if (!strchr(delimiter,
'\n'))
475 }
else if (parameter_names) {
477 fprintf(stdout,
"%s%s", input[k], delimiter);
478 for (i = 0; i < parameter_names; i++) {
485 ignoreFormats ? NULL : parameterFormat[i],
487 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
491 fputs(printBuffer, stdout);
492 fprintf(stdout,
"%s", delimiter);
494 if (!strchr(delimiter,
'\n'))
505 if (print_rows && (n_rows_total || (retval == -1 && n_pages == 0)) && !page_number) {
506 if (!n_rows_scinotation || n_rows == 0) {
508 fprintf(stdout,
"%" PRId64
"\n", n_rows);
510 fprintf(stdout,
"%" PRId64
" rows\n", n_rows);
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);
523 fprintf(stdout,
"%ld\n", n_pages);
525 fprintf(stdout,
"%ld pages\n", n_pages);
536long SDDS_SprintTypedValue2(
char *s,
char *format,
char *buffer,
unsigned long mode) {
537 char buffer2[SDDS_PRINT_BUFLEN];
544 SDDS_SetError(
"Unable to print value--buffer pointer is NULL "
545 "(SDDS_SprintTypedValue2)");
548 if ((
long)strlen(s) > SDDS_PRINT_BUFLEN - 3) {
549 SDDS_SetError(
"Buffer size overflow (SDDS_SprintTypedValue2)");
553 if (!(mode & SDDS_PRINT_NOQUOTES)) {
556 sprintf(buffer,
"\"\"");
557 else if (strchr(s,
'"')) {
561 sprintf(buffer,
"\"%s\"", buffer2);
563 strcpy(buffer, buffer2);
565 sprintf(buffer,
"\"%s\"", s);
567 sprintf(buffer, format ? format :
"%s", s);
571 sprintf(buffer2, format ? format :
"%s", buffer);
572 strcpy(buffer, buffer2);
575 sprintf(buffer, format ? format :
"%s", s);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
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.
void SDDS_EscapeQuotes(char *s, char quote_char)
Escapes quote characters within a string by inserting backslashes.
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_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
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.
int32_t SDDS_HasWhitespace(char *string)
Checks if a string contains any whitespace characters.
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)