78#define SET_PARAMETERS 1
81#define SET_DELIMITER 3
82#define SET_FILENAMES 4
88#define SET_IGNOREFORMATS 10
89#define SET_DESCRIPTION 11
90#define SET_SHOW_PAGES 12
93char *option[N_OPTIONS] =
111 " sdds2stream [<SDDSinput>...]\n"
113 " [-columns=<column-name>[,...]]\n"
114 " [-parameters=<parameter-name>[,...]]\n"
115 " [-arrays=<array-name>[,...]]\n"
116 " [-page=<pageNumber>] \n"
117 " [-delimiter=<delimiting-string>]\n"
119 " [-rows[=bare][,total][,scientific]]\n"
120 " [-npages[=<bare>]] \n"
122 " [-ignoreFormats] \n"
124 "sdds2stream provides stream output to the standard output of data values from "
125 "a group of columns or parameters. Each line of the output contains a different "
126 "row of the tabular data or a different parameter. Values from different columns "
127 "are separated by the delimiter string, which by default is a single space. "
128 "If -page is not employed, all data pages are output sequentially. "
129 "If multiple filenames are given, the files are processed sequentially in the "
131 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
133long SDDS_SprintTypedValue2(
char *s,
char *format,
char *buffer,
unsigned long mode);
135int main(
int argc,
char **argv) {
137 long i, k, i_arg, retval, description;
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;
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;
153 argc =
scanargs(&s_arg, argc, argv);
159 buffer =
tmalloc(
sizeof(
char) * 16);
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;
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);
175 if (s_arg[i_arg].n_items < 2)
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;
186 if (s_arg[i_arg].n_items < 2)
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;
197 if (s_arg[i_arg].n_items < 2)
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;
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");
218 if (s_arg[i_arg].n_items < 2)
220 delimiter = s_arg[i_arg].list[1];
228 if (s_arg[i_arg].n_items > 4)
231 char *rowsOutputMode[3] = {
"bare",
"total",
"scientific"};
232 for (i = 1; i < s_arg[i_arg].n_items; i++) {
234 rowsOutputMode, 3, 0);
240 n_rows_scinotation = 1;
242 SDDS_Bomb(
"unknown output mode for -rows option");
249 if (s_arg[i_arg].n_items > 2)
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);
260 SDDS_Bomb(
"unknown output mode for -npages option");
262 SDDS_Bomb(
"unknown output mode for -npages option");
276 case SET_IGNOREFORMATS:
280 case SET_DESCRIPTION:
285 fprintf(stderr,
"error: unknown switch: %s\n",
286 s_arg[i_arg].list[0]);
291 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
292 input[inputs++] = s_arg[i_arg].list[0];
300 input =
trealloc(input,
sizeof(*input) * (inputs + 1));
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");
309 if (column_names || array_names)
318 for (k = 0; k < inputs; k++) {
328 type =
tmalloc(
sizeof(*type) * column_names);
329 data =
tmalloc(
sizeof(*data) * column_names);
330 columnFormat =
tmalloc(
sizeof(*columnFormat) * column_names);
332 for (i = 0; i < column_names; i++) {
335 fprintf(stderr,
"error: column %s does not exist\n",
343 SDDS_GET_BY_INDEX, j)) {
348 }
else if (array_names) {
350 type =
tmalloc(
sizeof(*type) * array_names);
351 data =
tmalloc(
sizeof(*data) * array_names);
352 arrayFormat =
tmalloc(
sizeof(*arrayFormat) * array_names);
355 for (i = 0; i < array_names; i++) {
358 fprintf(stderr,
"error: array %s does not exist\n",
370 }
else if (parameter_names) {
372 type =
tmalloc(
sizeof(*type) * parameter_names);
373 parameterFormat =
tmalloc(
sizeof(*parameterFormat) * parameter_names);
375 for (i = 0; i < parameter_names; i++) {
378 fprintf(stderr,
"error: parameter %s does not exist\n",
395 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.description, NULL,
397 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
402 fputs(printBuffer, stdout);
403 fprintf(stdout,
"%s", delimiter);
405 if (!SDDS_SprintTypedValue2(SDDS_dataset.layout.contents, NULL,
407 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
411 fputs(printBuffer, stdout);
412 fprintf(stdout,
"%s", delimiter);
414 if (!strchr(delimiter,
'\n'))
419 while (retval != page_number && (retval =
SDDS_ReadPage(&SDDS_dataset)) > 0) {
420 if (page_number && retval != page_number)
425 if (n_rows_total && !page_number) {
428 if (!n_rows_scinotation || counti == 0) {
430 fprintf(stdout,
"%" PRId64
"\n", (int64_t)counti);
432 fprintf(stdout,
"%" PRId64
" rows\n", (int64_t)counti);
435 double count = counti;
436 snprintf(format, 20,
"%%.%ldle%s",
437 (
long)(log10(count)),
438 n_rows_bare ?
"" :
" rows");
439 fprintf(stdout, format, count);
453 fprintf(stdout,
"%s%s", input[k], delimiter);
454 if (!strchr(delimiter,
'\n'))
457 for (i = 0; i < column_names; i++) {
465 for (j = 0; j < rows; j++) {
466 for (i = 0; i < column_names; i++) {
468 ignoreFormats ? NULL : columnFormat[i],
470 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
472 SDDS_VERBOSE_PrintErrors);
475 fputs(printBuffer, stdout);
476 if (i != column_names - 1)
477 fprintf(stdout,
"%s", delimiter);
482 }
else if (array_names) {
485 fprintf(stdout,
"%s%s", input[k], delimiter);
486 if (!strchr(delimiter,
'\n'))
489 for (i = 0; i < array_names; i++) {
495 for (j = 0; j < array->elements; j++) {
497 ignoreFormats ? NULL : arrayFormat[i],
499 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
503 fputs(printBuffer, stdout);
504 fprintf(stdout,
"%s", delimiter);
507 if (!strchr(delimiter,
'\n'))
509 }
else if (parameter_names) {
511 fprintf(stdout,
"%s%s", input[k], delimiter);
512 for (i = 0; i < parameter_names; i++) {
519 ignoreFormats ? NULL : parameterFormat[i],
521 noQuotes ? SDDS_PRINT_NOQUOTES : 0)) {
525 fputs(printBuffer, stdout);
526 fprintf(stdout,
"%s", delimiter);
528 if (!strchr(delimiter,
'\n'))
539 if (print_rows && (n_rows_total || (retval == -1 && n_pages == 0)) && !page_number) {
540 if (!n_rows_scinotation || n_rows == 0) {
542 fprintf(stdout,
"%" PRId64
"\n", n_rows);
544 fprintf(stdout,
"%" PRId64
" rows\n", n_rows);
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);
557 fprintf(stdout,
"%ld\n", n_pages);
559 fprintf(stdout,
"%ld pages\n", n_pages);
570long SDDS_SprintTypedValue2(
char *s,
char *format,
char *buffer,
unsigned long mode) {
571 char buffer2[SDDS_PRINT_BUFLEN];
578 SDDS_SetError(
"Unable to print value--buffer pointer is NULL "
579 "(SDDS_SprintTypedValue2)");
582 if ((
long)strlen(s) > SDDS_PRINT_BUFLEN - 3) {
583 SDDS_SetError(
"Buffer size overflow (SDDS_SprintTypedValue2)");
587 if (!(mode & SDDS_PRINT_NOQUOTES)) {
590 sprintf(buffer,
"\"\"");
591 else if (strchr(s,
'"')) {
595 sprintf(buffer,
"\"%s\"", buffer2);
597 strcpy(buffer, buffer2);
599 sprintf(buffer,
"\"%s\"", s);
601 sprintf(buffer, format ? format :
"%s", s);
605 sprintf(buffer2, format ? format :
"%s", buffer);
606 strcpy(buffer, buffer2);
609 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)