/*************************************************************************\ * 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: sddsbinarystring.c * purpose: convert integer type columns into binary string * future capabilities: convert to octal and hex * convert back to binary * create separate string columns for sections of the binary data. $Log: sddsbinarystring.c,v $ Revision 1.4 2009/01/28 16:03:20 shang modified to give usage message when no arguments is given and replaced sddsmakebinarystring by sddsbinarystring Revision 1.3 2007/03/28 22:59:45 emery Allows SDDS_ULONG as integer type for conversion. Revision 1.2 2006/08/08 17:06:12 emery Fixed the cvs $log comment. Removed all but the *data variable from the int32_t type statement. Initialized character values with 0 instead of NULL (to remove complication warnings.) Revision 1.1 2006/08/08 16:38:21 emery Shang's first version. */ #include "mdb.h" #include "SDDS.h" #include "scan.h" #include "SDDSutils.h" #include "ctype.h" #define SET_COLUMN 0 #define SET_PIPE 1 #define N_OPTIONS 2 char *option[N_OPTIONS] = { "column", "pipe", } ; char *USAGE = "sddsbinarystring [] []\n\ [-pipe=[input][,output]] [-column=] \n\n\ sddsbinarystring converts the integer columns into binary string values.\n\ -column list of columns to be convert, wildcards accepted. The binary string will be \n\ written into BinaryString columns. \n\n\ Program by Hairong. (This is version 1, August 2006.)\n"; int main(int argc, char **argv) { SDDS_DATASET SDDS_dataset, SDDS_orig; long i, j, i_row, i_arg; SCANNED_ARG *s_arg; long tmpfile_used, noWarnings, columnMatches, integerColumns, digits; long index, type, rows, columns; int32_t *data; short *integerType; char *input, *output, **columnName, **columnMatch, **integerColumn, buff[1024], **binaryString; unsigned long pipeFlags; tmpfile_used = 0; pipeFlags = 0; input = output = NULL; data = NULL; noWarnings = 0; binaryString = NULL; buff[0] = 0; columns = columnMatches = integerColumns = 0; columnName = columnMatch = integerColumn = NULL; integerType = NULL; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2) bomb(NULL, USAGE); for (i_arg=1; i_arg0) { if ((rows = SDDS_CountRowsOfInterest(&SDDS_orig))<0) continue; binaryString = malloc(sizeof(*binaryString)*rows); if (!SDDS_CopyPage(&SDDS_dataset, &SDDS_orig)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i> j & 0x1) ? '1':'0'; binaryString[i_row][digits] = 0; } sprintf(buff, "%sBinaryString", integerColumn[i]); if (!SDDS_SetColumn(&SDDS_dataset, SDDS_BY_NAME, binaryString, rows, buff)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free(data); data = NULL; SDDS_FreeStringArray(binaryString, rows); } free(binaryString); binaryString = NULL; if (!SDDS_WritePage(&SDDS_dataset)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDS_orig) || !SDDS_Terminate(&SDDS_dataset)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (tmpfile_used && !replaceFileAndBackUp(input, output)) exit(1); SDDS_FreeStringArray(integerColumn, integerColumns); free(integerColumn); free_scanargs(&s_arg, argc); return 0; }