/*************************************************************************\ * 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. \*************************************************************************/ /* $Log: sddscast.c,v $ Revision 1.7 2006/12/14 22:21:57 soliday Updated a bunch of programs because SDDS_SaveLayout is now called by SDDS_WriteLayout and it is no longer required to be called directly. Also the AutoCheckMode is turned off by default now so I removed calls to SDDS_SetAutoCheckMode that would attempt to turn it off. It is now up to the programmer to turn it on in new programs until debugging is completed and then remove the call to SDDS_SetAutoCheckMode. Revision 1.6 2005/11/04 22:46:12 soliday Updated code to be compiled by a 64 bit processor. Revision 1.5 2003/09/02 19:16:03 soliday Cleaned up code for Linux. Revision 1.4 2002/08/14 17:12:40 soliday Added Open License Revision 1.3 2002/03/22 22:59:53 soliday Modifed free_scanargs argument. Revision 1.2 2002/01/31 00:10:27 shang added -pipe option */ #include "mdb.h" #include "SDDS.h" #include "scan.h" #define COLUMN_MODE 0 #define PARAMETER_MODE 1 #define ARRAY_MODE 2 #define MODES 3 static char *mode_name[MODES] = { "column", "parameter", "array", } ; #define SET_CAST 0 #define SET_NOWARNINGS 1 #define SET_PIPE 2 #define N_OPTIONS 3 char *option[N_OPTIONS] = {"cast", "nowarnings", "pipe",} ; #define CLO_SHORT 0 #define CLO_LONG 1 #define CLO_FLOAT 2 #define CLO_DOUBLE 3 #define TYPES 4 char *types[TYPES]={ "short","long","float","double", }; typedef struct { char **name; char **new_type; long *index; long n; } CAST_NAME; typedef struct { char *match_string; char *type_string; char *new_type; } CAST_REQUEST; char *USAGE = "sddscast [] [] [-pipe=[input][,output]] -noWarnings \n\ -cast={column|parameter|array},,,\n\ is of the form (with optional wildcards) or \n\ '(,,....)' \n\ is of the form '(long,short,double,float,*)' \n\ is long, short,double, or float\n\n\ sddscast converts the numeric columns, parameters or arrays from one datatype to another.\n\n\ Program by Hairong Shang. (This is version 1.1, Jan. 2002)\n"; void process_cast_columns(SDDS_DATASET *SDDS_input, CAST_NAME *column_cast, CAST_REQUEST *col_request, long col_requests, long noWarnings); void process_cast_parameters(SDDS_DATASET *SDDS_input, CAST_NAME *parameter_cast, CAST_REQUEST *par_request, long par_requests, long noWarnings); void process_cast_arrays(SDDS_DATASET *SDDS_input, CAST_NAME *array_cast, CAST_REQUEST *array_request, long array_requests, long noWarnings); long add_casts(char *old_type, long sdds_type, CAST_NAME *cast_name, char *add_name, char *new_type, long index,long noWarnings); void cleanUpCast(CAST_NAME *cast_name); int main(int argc, char **argv) { SDDS_DATASET SDDS_input, SDDS_output; SCANNED_ARG *s_arg; char *inputfile, *outputfile, *ptr, *buffer; long tmpfile_used,noWarnings,i,i_arg; CAST_NAME column_cast, parameter_cast, array_cast; CAST_REQUEST *col_request, *par_request, *array_request; long col_requests,par_requests,array_requests,rows; unsigned long pipeFlags; column_cast.n=parameter_cast.n=array_cast.n=0; column_cast.name=column_cast.new_type=NULL; column_cast.index=parameter_cast.index=array_cast.index=0; parameter_cast.name=parameter_cast.new_type=NULL; array_cast.name=array_cast.new_type=NULL; col_request=par_request=array_request=NULL; col_requests=par_requests=array_requests=rows=0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<3) bomb(NULL, USAGE); inputfile = outputfile = NULL; ptr=buffer=NULL; noWarnings=tmpfile_used = pipeFlags = 0; for (i_arg=1; i_arg0) { if (!SDDS_CopyPage(&SDDS_output,&SDDS_input)||!SDDS_WritePage(&SDDS_output)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile)) exit(1); /*free memory */ cleanUpCast(&column_cast); cleanUpCast(¶meter_cast); cleanUpCast(&array_cast); if (array_request) free(array_request); if (par_request) free(par_request); if (col_request) free(col_request); free_scanargs(&s_arg,argc); return 0; } void cleanUpCast(CAST_NAME *cast_name) { long i; for (i=0;in;i++) { free(cast_name->name[i]); free(cast_name->new_type[i]); cast_name->name[i]=NULL; cast_name->new_type[i]=NULL; } free(cast_name->name); free(cast_name->new_type); free(cast_name->index); cast_name->name=NULL; cast_name->new_type=NULL; cast_name->index=NULL; } void process_cast_columns(SDDS_DATASET *SDDS_input, CAST_NAME *column_cast, CAST_REQUEST *col_request, long col_requests, long noWarnings) { char *ptr,**Names, **Match_string, **type_string; long i,index,j,k,type, s1,s2; int32_t names; if (!col_requests) return; ptr=NULL; names=s1=s2=0; Names=NULL; Match_string=type_string=NULL; for (i=0;iname=SDDS_Realloc(cast_name->name,sizeof(char*)*(cast_name->n+1)); cast_name->new_type=SDDS_Realloc(cast_name->new_type, sizeof(char*)*(cast_name->n+1)); SDDS_CopyString(&cast_name->name[cast_name->n],add_name); SDDS_CopyString(&cast_name->new_type[cast_name->n],new_type); cast_name->index=SDDS_Realloc(cast_name->index,sizeof(long)*(cast_name->n+1)); cast_name->index[cast_name->n]=index; cast_name->n++; return 1; }