// // ******************************************************************** // * License and Disclaimer * // * * // * The GAMOS software is copyright of the Copyright Holders of * // * the GAMOS Collaboration. It is provided under the terms and * // * conditions of the GAMOS Software License, included in the file * // * LICENSE and available at http://fismed.ciemat.es/GAMOS/license .* // * These include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GAMOS collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the GAMOS Software license. * // ******************************************************************** // /* * Copyright 2000-2003 Virginia Commonwealth University * ----------------------------------------------------------------------------- * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is furnished * to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * *----------------------------------------------------------------------------- * * AUTHORS: * * Jeffrey Vincent Siebers * e-mail: jsiebers@vcu.edu * Virginia Commonwealth University * 401 College Street, P.O.Box 980058 * Richmond, Viriginia 23298-0058 * Phone: +1-804-6287771 * * */ /* General Utilities for CPP programs File Created: 18-December-1995: Combined ok_check.cpp and some open_file Modification History: 01-Feb-1996: JVS: filename used in open_file has extension only in openfile 09-Feb-1996: JVS: Add eprintf: outputs to screen and a buffer called pbuffer pbuffer is a global whose memory must be allocated this is useful for creating a "history" file 17-June-1996: JVS: change latex_string so will work with win95/bc5.0 cannot have for(int i=0,j=0; ). j will not increment. 05-Sept-1996: JVS: Add allocate_pbuffer and print_runtime_info 23-April-1997: JVS: Add interpolate 06-Jan-1998: JVS: Add array_read 11-June-1998: jvs: add eprintf and view_errors 22-Sept-1998: JVS: fix memory leak in eprintf 07-Dec-1998: JVS: Add clean_name 18-Feb-1999: JVS: eliminate atof in array_read because of failures 25-Feb-1999: JVS: array_read will now read numbers that start with . 26-Feb-1999: JVS: Add array_read for strings 02-March-1999: JVS: Add global eprint_mode so can quite eprintf statements 24-March-1999: JVS: Modify clean_name so names cannot have *'s in them July 20, 1999: JVS: eprintf modified to use fprintf(stdout), rather than printf Dec 3, 1999: JVS: Add check_byte_order Jan 11, 2000: JVS: Modify clean_name so names cannot have / in them Jun 16, 2000: JVS: Change open_file so will read in .extension properly when a . is in the path name April 25, 2001: JVS: Add cp(SourceFile,DestinationFile) May 29, 2001: JVS: clean_name now removes & as well May 31, 2002: add reverse_short_byte_order Feb 18, 2004: JVS: Add writeBigEndianBinaryFile() Feb 10, 2005: JVS: Add writeLittleEndianBinaryFile() and writeBinaryFile() Feb 11, 2005: JVS: Add reverse_int_byte_order April 21, 2005: JVS: Add readBinaryDataFromFile */ #include #include #include #include #include #include #include "iaea_utilities.hh" /* ************************************************************************** */ int reverse_int_byte_order(int xold) { int xnew; char *pn = (char *) &xnew; char *po = (char *) &xold; pn[0] = po[3]; pn[1] = po[2]; pn[2] = po[1]; pn[3] = po[0]; return(xnew); } /* *************************************************************************** */ float reverse_float_byte_order(float xold) { float xnew; char *pn = (char *) &xnew; char *po = (char *) &xold; pn[0] = po[3]; pn[1] = po[2]; pn[2] = po[1]; pn[3] = po[0]; return(xnew); } /* **************************************************************************** */ short reverse_short_byte_order(short xold) { short xnew; char *pn = (char *) &xnew; char *po = (char *) &xold; pn[0] = po[1]; pn[1] = po[0]; return(xnew); } /* **************************************************************************** */ int check_byte_order() { /* Determine the byte order on this machine */ float ftest=1.0f; /* assign a float to 1.0 */ char *pf = (char *) &ftest; // printf("\n \t %x %x %x %x", pf[0],pf[1],pf[2],pf[3]); if(pf[0] == 0 && pf[3] != 0) { printf("\n\n Byte order: INTEL / ALPHA,LINUX -> LITLE_ENDIAN \n"); return(LITTLE_ENDIAN); }else if(pf[0] != 0 && pf[3] == 0) { printf("\n\n Byte order: OTHER (SGI,SUN-SOLARIS) -> BIG_ENDIAN \n "); return(BIG_ENDIAN); } else { printf("\n\n ERROR: indeterminate byte order"); printf("\n \t %x %x %x %x", pf[0],pf[1],pf[2],pf[3]); return(UNKNOWN_ENDIAN); } } /* ************************************************** */ void print_runtime_info(int argc, char *argv[]) { // print file header stuff printf("\n Command Line: "); for(int i=0; i len) return(FAIL); /* return 0 when fails */ return (OK); } /* ********************************************************************** */ int my_isascii( int c ) { return( !(c < 0 || c > 0177) ); } /* ********************************************************************* */ int clean_name(char *name) { int len = strlen(name); char *tname = (char *) calloc(len+1, sizeof(char)); if(tname == NULL) { eprintf("\n ERROR: memory allocation error"); return(FAIL); } strcpy(tname, name); if(clean_name(tname, name)!= OK) { eprintf("\n ERROR: cleaning Name"); return(FAIL); } free(tname); return(OK); } /* ********************************************************************* */ int clean_name(char *tmp_path, char *opath) { /* remove spaces, *'s, :'s &'s and commas from the name */ int len = strlen(tmp_path); int o_index=0; for(int i=0; i ",access); fgets(string,MAX_STR_LEN,stdin); sscanf(string,"%s",filename); printf(" FILE %s opened \n", filename); } int len=strlen(filename); if( len + strlen(extension) >= MAX_STR_LEN) { printf("\n ERROR: String Length of %s.%s Exceeds Maximum", filename, extension); return(NULL); } //GAMOS char *filename1 = new char[len+strlen(extension)+1]; char *filename1 = new char[len+strlen(extension)+1]; strcpy(filename1,filename); // temp filename for appending extension /* check if file name has .extension */ /* if it does not, add .extension to it */ int i=len-1; while(i > 0 && filename[i--] != '.'); // printf("\n Comparing %s to %s", extension, filename+i+1); if(strcmp(extension, filename+i+1) ) strcat(filename1,extension); if( (strm = fopen(filename1, access) ) == NULL ) { printf("\n ERROR OPENING FILE %s (mode %s)", filename1,access); } delete(filename1); return(strm); } /* *********************************************************************** */ int ok_check(void) /* GETS RESPONSE FROM USER */ { /* IF OK TO DO SOMETHING */ char reply[MAX_STR_LEN]; /* RETURNS 1 ONLY IF REPLY Y */ /* OR y ELSE RETURNS 0 */ fgets(reply,MAX_STR_LEN,stdin); if( ( strncmp(reply,"Y",1)==0 )|| ( strncmp(reply,"y",1)==0 )) return(1); return(0); } /* *********************************************************************** */ int ok_checks(char *string) { printf("\n %s", string); return(ok_check()); } /* ********************************************************************** */ #include // for va function // GAMOS int pprintf(char *fmt, ... ) // GAMOS { // GAMOS va_list argptr; /* Argument list pointer */ // GAMOS char str[MAX_STR_LEN]; /* Buffer to build sting into */ // GAMOS int cnt; /* Result of SPRINTF for return */ // GAMOS va_start( argptr, fmt ); /* Initialize va_ functions */ // GAMOS cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */ // GAMOS if(str[0] == '\0') return(0); // GAMOS printf("%s", str); /* Send to screen */ // GAMOS if(pbuffer != NULL && strlen(pbuffer) + strlen(str) < MAX_BUFFER_SIZE) // GAMOS strcat(pbuffer,str); // GAMOS else // GAMOS printf("\n ERROR: pbuffer is full"); // GAMOS va_end( argptr ); /* Close va_ functions */ // GAMOS return( cnt ); /* Return the conversion count */ // GAMOS } /* *********************************************************************** */ /* eprintf: for buffering error reports, writes error messages to a buffer, and, also can echo them to the screen (if set at compile time) at first instance, allocates memory for the error buffer */ static char *ebuffer = NULL; //GAMOS int eprintf(char *fmt, ... ) int eprintf(const char *fmt, ... ) { va_list argptr; /* Argument list pointer */ char str[MAX_STR_LEN]; /* Buffer to build sting into */ int cnt; /* Result of SPRINTF for return */ va_start( argptr, fmt ); /* Initialize va_ functions */ cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */ if(str[0] == '\0') return(0); //GAMOS if(eprintf_mode==ON) fprintf(stdout,"%s", str); /* Send to screen */ // allocate memory for the error message int ilen = 0; if(ebuffer != NULL) { ilen+=strlen(ebuffer); ebuffer = (char *) realloc(ebuffer, (ilen+strlen(str)+1)*sizeof(char)); } else ebuffer = (char *) calloc(ilen+strlen(str)+1,sizeof(char)); if(ebuffer == NULL) { printf("\n ERROR: ebuffer cannot be allocated in eprintf"); } else strcat(ebuffer,str); va_end( argptr ); /* Close va_ functions */ return( cnt ); /* Return the conversion count */ } int view_errors(void) { printf("\n%s\n",ebuffer); return(OK); } /* ************************************************************************** */ int latex_string(char *string, char *nstring) { // adds \\ in front of % so % will show up in the comment when printed // with LaTeX // must change all %'s to \% for latex output // also, must do the same for $, &, # _ { and } int len = strlen(string); int sval=0; int j; while(isspace(string[sval]) )sval++; // remove space from start of string while(isspace(string[len-1]))len--; // remove space from end to string j=0; for(int i=sval;i' ) { nstring[j++]='$'; } nstring[j++] = string[i]; if(string[i]=='<' || string[i]=='>' ) { nstring[j++]='$'; } } nstring[j]='\0'; /* printf("\n string: %s", string); printf("\n nstring: %d %s",j, nstring); */ return(OK); } /* ************************************************************************** */ float interpolate(float xh, float xl, float xm, float yh, float yl) { return(yh - (xh-xm)/(xh-xl)*(yh-yl)); } /* *********************************************************************** */ // #define DEBUG_ARRAY /* ********************************************************************** */ int array_read(char *in_string, float *array, int max_array) { char delimeter_string[MAX_STR_LEN]; sprintf(delimeter_string," ,\t"); /* spaces, commas, and tabs */ char *p; /* pointer to string read in */ p = strtok(in_string,delimeter_string); int i=0; if(p!=NULL) { array[i++]=(float)atof(p); /* get the first value */ // if( sscanf(p,"%f",&array[i]) == 1) i++; // sscanf rounds values.... do{ /* get remaining values */ p = strtok(NULL,delimeter_string); if(p!=NULL) { //array[i++] = atof(p); if( sscanf(p,"%f",&array[i]) == 1) i++; // printf("\n Got Value of %f", array[i-1]); } }while(p!=NULL && i < max_array); } #ifdef DEBUG_ARRAY printf("\n atof %d", i); for(int j=0; jilen) /* if advance past end of string, get a new one */ { if(fgets(istring, Max_Str_Len, fspec) == NULL) /* output warning if not a valid read */ { printf ("\nERROR: Reading File, looking for end of comment %s",comment_stop); // fclose(fspec); free(istring); return(FAIL); } ilen = strlen(istring); /* get length of this new string */ /* null terminate the string */ istring[ilen]='\0'; icnt = 0; /* reset the counter to the start of the string */ } } else { icnt+=clen; /* advance past comment delimiter */ } }while(check != 0); /* end of comment found */ } /* end if */ else /* check if comment is in c++ format */ { check = strncmp(istring+icnt, "//",2); if(check == 0) /* c++ style comment found */ { /* skip till end of string */ icnt = ilen; string[olen++]='\n'; string[olen]='\0'; } else /* is a valid character for the string */ { string[olen++] = istring[icnt++]; /* append value to the string */ string[olen]='\0'; } } }while(icnt < ilen && /* do till end of string */ olen < Max_Str_Len); /* and output string not too long */ /* check for only carriage return (should have been caught above) */ if(olen == 1 && string[0] == '\n') olen = 0; } /* end else */ }while(olen == 0); /* do till read in a string */ if(olen == Max_Str_Len) { printf ("\nERROR: Input line too long"); // fclose(fspec); free(istring); return(FAIL); } free(istring); return(OK); }