utils.c

Go to the documentation of this file.
00001 
00002 /*********************************************************************
00003  *
00004  * io_utils.c
00005  * 
00006  * Author:  Claudio Klein, X-Ray Research GmbH.
00007  * Version: 1.0
00008  * Date:    05/07/1996
00009  *
00010  *********************************************************************/
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <ctype.h>
00014 #include <math.h>
00015 #ifndef __sgi
00016 #include <stdlib.h>
00017 #endif
00018 
00019 /*
00020  * Definitions
00021  */
00022 #define STRING          0
00023 #define INTEGER         1
00024 #define FLOAT           2
00025 
00026 int             InputType();
00027 void            WrongType();
00028 float           GetResol(float, float, float);
00029 void            RemoveBlanks();
00030 void            swapshort();
00031 void            swaplong();
00032 
00033 /******************************************************************
00034  * Function: GetResol
00035  ******************************************************************/
00036 float
00037 GetResol(float radius, float distance, float wave)
00038 {
00039 float   res;
00040 
00041         if ( distance < 1.0 ) return 0.0;
00042 
00043         res = atan( radius/distance);   /* Two theta !!! */
00044 
00045         if ( res < 0.0001 ) return 0.0;
00046 
00047         res = wave/(2.0*sin(res/2.0)); 
00048         
00049         return( res );
00050 }
00051         
00052 /******************************************************************
00053  * Function: InputType 
00054  ******************************************************************/
00055 int 
00056 InputType(char *string)
00057 {
00058 int             i,j=0,k=0,l=0;
00059 
00060         for ( i=0 ; i<strlen(string)-1; i++ ) {
00061                 if ( isspace( string[i] ) ) {
00062                         string[i] = ' ';
00063                         j++;
00064                 }
00065                 else if ( string[i] == '.' ) {
00066                         k++;
00067                         continue;
00068                 }
00069 
00070                 else if ( string[i] == '-' || string[i] == '+' ) {
00071                         l++;
00072                         continue;
00073                 }
00074                 else if ( isdigit( string[i] ) ) {
00075                         j++;
00076                         continue;
00077                 }
00078         }
00079 
00080         if ( k == 1 && l == 0 && j == i-1 )
00081                 return( FLOAT );
00082         else if ( k == 1 && l == 1 && j == i-2 )
00083                 return( FLOAT );
00084         else if ( k == 0 && l == 1 && j == i-1 )
00085                 return( INTEGER );
00086         else if ( k == 0 && l == 0 && j == i )
00087                 return( INTEGER );
00088         else
00089                 return( STRING  );
00090 
00091 }
00092 
00093 /******************************************************************
00094  * Function: WrongType 
00095  ******************************************************************/
00096 void
00097 WrongType(int type, char *key, char *string)
00098 {
00099 
00100         fprintf(stderr,"INPUT ERROR at key '%s' and argument '%s'!\n",key,string);
00101 
00102         if ( type == FLOAT )
00103                 fprintf(stderr,"            Expected data type is: FLOAT...\n");
00104         else if ( type == INTEGER )
00105                 fprintf(stderr,"            Expected data type is: INTEGER...\n");
00106 
00107 }
00108 
00109 /******************************************************************
00110  * Function: RemoveBlanks = remove white space from string
00111  ******************************************************************/
00112 void
00113 RemoveBlanks(char *str)
00114 {
00115 int i, j=0, len=strlen(str);
00116 
00117         for(i=0;i<len;i++) {
00118                 if (!isspace(str[i]))
00119                         str[j++] = str[i];
00120         }
00121         if ( j>0)
00122                 str[j] = 0;
00123 }
00124 
00125 /******************************************************************
00126  * Function: swaplong = swaps bytes of 32 bit integers
00127  ******************************************************************/
00128 void
00129 swaplong(data, nbytes)
00130 register char *data;
00131 int nbytes;
00132 {
00133         register int i, t1, t2, t3, t4;
00134 
00135         for(i=nbytes/4;i--;) {
00136                 t1 = data[i*4+3];
00137                 t2 = data[i*4+2];
00138                 t3 = data[i*4+1];
00139                 t4 = data[i*4+0];
00140                 data[i*4+0] = t1;
00141                 data[i*4+1] = t2;
00142                 data[i*4+2] = t3;
00143                 data[i*4+3] = t4;
00144         }
00145 }
00146 
00147 /******************************************************************
00148  * Function: swapshort = swaps the two 8-bit halves of a 16-bit word
00149  ******************************************************************/
00150 void
00151 swapshort(data, n)
00152 register unsigned short *data;
00153 int n;
00154 {
00155         register int i;
00156         register unsigned short t;
00157 
00158         for(i=(n>>1)+1;--i;) {
00159                 /*t = (( (*data)&0xFF) << 8 ) | (( (*data)&0xFF00) >> 8);*/
00160                 t = (((*data) << 8) | ((*data) >> 8));
00161                 *data++ = t;
00162         }
00163 }
00164