/* Main operations with RSF files. */ /* Copyright (C) 2004 University of Texas at Austin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include "file.h" #include "getpar.h" #include "alloc.h" #include "error.h" #include "simtab.h" #include "_bool.h" #ifndef _sf_file_h typedef struct sf_File *sf_file; #endif struct sf_File { FILE *stream; char *dataname; sf_simtab pars; }; static const int tabsize=10; static bool error=true; static void sf_input_error(sf_file file, const char* message, const char* name); /*----------------------------------------------------------------------------------*/ void sf_file_error(bool err) /*< set error on opening files >*/ { error = err; } /*----------------------------------------------------------------------------------*/ static void sf_input_error(sf_file file, const char* message, const char* name) { if (error) sf_error ("%s: %s %s:",__FILE__,message,name); sf_fileclose(file); } /*----------------------------------------------------------------------------------*/ sf_file sf_input (char* filename) //(const char* tag) /*< Create an input file structure >*/ { // int esize; sf_file file; size_t len; file = (sf_file) sf_alloc(1,sizeof(*file)); file->dataname = NULL; fprintf(stderr,"Input file=%s\n",filename); if (NULL == filename) { sf_input_error(file,"Cannot read header file name",filename); return NULL; } file->stream = fopen(filename,"r"); if (NULL == file->stream) { sf_input_error(file,"Cannot read header file",filename); return NULL; } /* create a parameter table */ file->pars = sf_simtab_init (tabsize); /* read the parameter table from the file */ sf_simtab_input (file->pars,file->stream,NULL); filename = sf_histstring(file,"in"); fprintf(stderr,"Velocity file in=%s\n",filename); if (NULL == filename) { sf_input_error (file,"No in= in file",filename); //tag return NULL; } len = strlen(filename)+1; file->dataname = sf_charalloc(len); memcpy(file->dataname,filename,len); if (0 != strcmp(filename,"stdin")) { file->stream = freopen(filename,"rb",file->stream); if (NULL == file->stream) { sf_input_error(file,"Cannot read data file",filename); return NULL; } } free (filename); return file; } void sf_fileclose (sf_file file) /*< close a file and free allocated space >*/ { if (NULL == file) return; if (file->stream != stdin && file->stream != stdout && file->stream != NULL) { (void) fclose (file->stream); file->stream = NULL; } if (NULL != file->pars) { sf_simtab_close (file->pars); file->pars = NULL; } if (NULL != file->dataname) { free (file->dataname); file->dataname = NULL; } } bool sf_histint (sf_file file, const char* key,/*@out@*/ int* par) /*< read an int parameter from file >*/ { return sf_simtab_getint (file->pars,key,par); } bool sf_histfloat (sf_file file, const char* key,/*@out@*/ float* par) /*< read a float parameter from file >*/ { return sf_simtab_getfloat (file->pars,key,par); } char* sf_histstring (sf_file file, const char* key) /*< read a string parameter from file (returns NULL on failure) >*/ { return sf_simtab_getstring (file->pars,key); } void sf_putint (sf_file file, const char* key, int par) /*< put an int parameter to a file >*/ { char val[256]; int coun; if (NULL == file->dataname) sf_warning("%s: putint to a closed file",__FILE__); coun=sprintf(val,"%d",par); sf_simtab_enter (file->pars,key,val); } void sf_floatread (/*@out@*/ float* arr, size_t size, sf_file file) /*< read a float array arr[size] from file >*/ { size_t got; got = fread(arr,sizeof(float),size,file->stream); if (got != size) sf_error ("%s: trouble reading: %d of %d",__FILE__,got,size); } /*--------------------------------------------------------------*/ int current_read (/*@out@*/ float* arrx, float* arry, float* arrz, size_t size, sf_file file) /*< read a float array arr[size] from file >*/ { FILE *stream; size_t got; char *filename; char f[100]=""; char fz[100]=""; char fy[100]=""; filename = sf_histstring(file,"incur"); if (NULL == filename || strlen(filename)==0) { fprintf(stderr,"%s\n","No input current file"); return(0); } //---------------- current x-component --------------------- strncpy(f,filename,strlen(filename)-4); strcat(f,"_x"); strcat(f,filename+strlen(filename)-4 ); if (NULL == f) { fprintf(stderr,"%s %s\n","No input current file",f); // return(0); } if ( (stream = fopen( f, "rb" )) == NULL ) { fprintf(stderr,"%s %s\n","No current data file!",f); // return(0); } else { got = fread(arrx,sizeof(float),size,stream); if (got != size) sf_error ("%s: trouble reading: %d of %d",__FILE__,got,size); fclose( stream ); } //---------------- current y-component --------------------- strncpy(fy,filename,strlen(filename)-4); strcat(fy,"_y"); strcat(fy,filename+strlen(filename)-4 ); if (NULL == fy) { fprintf(stderr,"%s %s\n","No input current file",fy); // return(0); } if ( (stream = fopen( fy, "rb" )) == NULL ) { fprintf(stderr,"%s %s\n","No current data file",fy); // return(0); } else { got = fread(arry,sizeof(float),size,stream); if (got != size) sf_error ("%s: trouble reading: %d of %d",__FILE__,got,size); fclose( stream ); return(got); } //---------------- current z-component --------------------- strncpy(fz,filename,strlen(filename)-4); strcat(fz,"_z"); strcat(fz,filename+strlen(filename)-4 ); fprintf(stderr,"fz = %s\n",fz); if (NULL == fz) { fprintf(stderr,"%s %s\n","No input current file",fz); return(0); } if ( (stream = fopen( fz, "rb" )) == NULL ) { fprintf(stderr,"%s %s\n","No current data file",fz); return(0); } else { got = fread(arrz,sizeof(float),size,stream); if (got != size) sf_error ("%s: trouble reading: %d of %d",__FILE__,got,size); fclose( stream ); return(got); } return(got); }