/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* program: sdds2stream * purpose: spew out values for a specified column or parameters * of a SDDS data set * * Michael Borland, 1994 $Log: sdds2stream.c,v $ Revision 1.23 2005/11/07 21:48:10 soliday Updated to remove Linux compiler warnings. Revision 1.22 2004/01/13 18:30:24 borland Fixed an error in the usage text for -rows option. Revision 1.21 2003/09/04 15:44:15 soliday Fixed bug with last version. Revision 1.20 2003/09/04 15:19:56 soliday Fixed bug when printing row count when there are no rows. Revision 1.19 2003/09/02 19:16:02 soliday Cleaned up code for Linux. Revision 1.18 2002/08/14 17:12:39 soliday Added Open License Revision 1.17 2002/07/25 00:12:40 borland Now check return values from SDDS_SprintTypedValue(). Revision 1.16 2001/07/17 21:28:29 borland Reset the page counter for each file. Revision 1.15 2001/01/10 19:35:31 soliday Standardized usage message. Revision 1.14 2000/07/13 20:30:43 borland Removed an error message that didn't make sense (-pages vs. -npages). Revision 1.13 2000/07/13 20:26:53 borland Updated version numbers. Revision 1.12 2000/07/13 20:14:35 borland Per D. Blachowicz: -rows option now takes 'bare' and 'total' qualifiers. Added -npages option. Revision 1.11 1999/05/25 19:05:14 soliday Removed compiler warning on linux. Revision 1.10 1999/05/04 14:43:16 borland Added -array option to usage message. Revision 1.9 1999/04/02 17:56:45 soliday Added the -description option Revision 1.8 1999/01/06 19:54:37 borland Fixed the version number in the usage message. Revision 1.7 1996/07/17 19:47:36 borland Added -ignoreFormats option to cause printing without using header-defined formats, which are usually totally inappropriate. * Revision 1.6 1996/07/05 16:12:21 borland * Removed code that deletes quotes around strings in * -noQuotes mode. Now using SDDS_PRINT_NOQUOTES mode to * SDDS_SprintTypedValue. * * Revision 1.5 1996/02/15 00:11:38 borland * Changed SDDS_GetColumn() call to SDDS_GetInternalColumn() for better performance. * * Revision 1.4 1995/11/05 18:17:52 borland * Added -array option for output of array data. * * Revision 1.3 1995/09/06 14:56:05 saunders * First test release of SDDS1.5 * */ #include "mdb.h" #include "SDDS.h" #include "scan.h" /*#include "SDDSaps.h"*/ #define SET_COLUMNS 0 #define SET_PARAMETERS 1 /* the -table option is retained for backward compatibility */ #define SET_TABLE 2 #define SET_DELIMITER 3 #define SET_FILENAMES 4 #define SET_ROWS 5 #define SET_NOQUOTES 6 #define SET_PIPE 7 #define SET_PAGE 8 #define SET_ARRAYS 9 #define SET_IGNOREFORMATS 10 #define SET_DESCRIPTION 11 #define SET_SHOW_PAGES 12 #define N_OPTIONS 13 char *option[N_OPTIONS] = { "columns", "parameters", "table", "delimiter", "filenames", "rows", "noquotes", "pipe", "page", "arrays", "ignoreformats", "description", "npages", } ; char *USAGE = "sdds2stream [-pipe] [...]\n\ [-columns=[,...]]\n\ [-parameters=[,...]]\n\ [-arrays=[,...]]]\n\ [-page=] [-delimiter=]\n\ [-filenames] [-rows[=][,total]]\n\ [-npages=] [-noquotes]\n\ [-ignoreFormats] [-description]\n\n\ sdds2stream provides stream output to the standard output of data values from \ a group of columns or parameters. Each line of the output contains a different \ row of the tabular data or a different parameter. Values from different columns \ are separated by the delimiter string, which by default is a single space.\ If -page is not employed, all data pages are output sequentially. \ If multiple filenames are given, the files are processed sequentially in the \ order given.\n\n\ Program by Michael Borland. (This is version 5, July 2000, D. Blachowicz.)\n"; long SDDS_SprintTypedValue2(char *s, char *format, char *buffer, unsigned long mode); int main(int argc, char **argv) { SDDS_DATASET SDDS_dataset; long i, j, k, i_arg, rows, retval, description; SCANNED_ARG *s_arg; char **input; char **column_name, **parameter_name, **array_name; char **parameterFormat, **columnFormat, **arrayFormat; long column_names, parameter_names, array_names, page_number, *type, inputs; char *delimiter; void **data; char *buffer; long filenames, print_rows, print_pages, noQuotes, pipe, ignoreFormats; static char printBuffer[SDDS_MAXLINE*16]; long n_rows, n_rows_bare, n_rows_total, n_pages, n_pages_bare; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<3) bomb(NULL, USAGE); type = NULL; data = NULL; buffer = tmalloc(sizeof(char)*16); /* large enough for any data type */ delimiter = NULL; input = parameter_name = column_name = array_name = NULL; parameterFormat = columnFormat = arrayFormat = NULL; inputs = parameter_names = column_names = array_names = 0; page_number = filenames = ignoreFormats = 0; n_rows = n_rows_bare = n_rows_total = n_pages = n_pages_bare = 0; print_rows = print_pages = noQuotes = pipe = description = 0; for (i_arg=1; i_arg2) SDDS_Bomb("invalid -page syntax"); if (page_number!=0) SDDS_Bomb("invalid syntax: specify -page once only"); if (sscanf(s_arg[i_arg].list[1], "%ld", &page_number)!=1 || page_number<=0) SDDS_Bomb("invalid -page syntax or value"); break; case SET_DELIMITER: if (s_arg[i_arg].n_items<2) SDDS_Bomb("invalid -delimiter syntax"); delimiter = s_arg[i_arg].list[1]; break; case SET_FILENAMES: filenames = 1; break; case SET_ROWS: if (s_arg[i_arg].n_items>3) SDDS_Bomb("invalid -rows syntax"); else { char *rowsOutputMode[2] = {"bare", "total"}; for (i=1; i2) SDDS_Bomb("invalid -pages syntax"); else { char *pagesOutputMode[1] = {"bare"}; for (i=1; i0) { if (page_number && retval!=page_number) continue; if (print_rows) { if (n_rows_total && !(page_number)) n_rows += SDDS_CountRowsOfInterest(&SDDS_dataset); else { if (n_rows_bare) fprintf(stdout, "%" PRId32 "\n", SDDS_CountRowsOfInterest(&SDDS_dataset)); else fprintf(stdout, "%" PRId32 " rows\n", SDDS_CountRowsOfInterest(&SDDS_dataset)); } } if (column_names) { if ((rows=SDDS_CountRowsOfInterest(&SDDS_dataset))<0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (rows) { if (filenames) { fprintf(stdout, "%s%s", input[k], delimiter); if (!strchr(delimiter, '\n')) fputc('\n', stdout); } for (i=0; ielements; j++) { if (!SDDS_SprintTypedValue(array->data, j, type[i], ignoreFormats?NULL:arrayFormat[i], printBuffer, noQuotes?SDDS_PRINT_NOQUOTES:0)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } fputs(printBuffer, stdout); fprintf(stdout, delimiter); } } if (!strchr(delimiter, '\n')) fputc('\n', stdout); } else { if (filenames) fprintf(stdout, "%s%s", input[k], delimiter); for (i=0; iSDDS_PRINT_BUFLEN-3) { SDDS_SetError("Buffer size overflow (SDDS_SprintTypedValue2)"); return(0); } if (!(mode&SDDS_PRINT_NOQUOTES)) { printed = 0; if (!s || SDDS_StringIsBlank(s)) sprintf(buffer, "\"\""); else if (strchr(s, '"')) { strcpy(buffer2, s); SDDS_EscapeQuotes(buffer2, '"'); if (SDDS_HasWhitespace(buffer2)) sprintf(buffer, "\"%s\"", buffer2); else strcpy(buffer, buffer2); } else if (SDDS_HasWhitespace(s)) sprintf(buffer, "\"%s\"", s); else { sprintf(buffer, format?format:"%s", s); printed = 1; } if (!printed) { sprintf(buffer2, format?format:"%s", buffer); strcpy(buffer, buffer2); } } else { sprintf(buffer, format?format:"%s", s); } return(1); }