"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "eas3pkg/eas3/covhelp.c" of archive eas3pkg_v1.6.3.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*-------------------------------------------------------------------------------------------------------*/
    2 /* EAS3 License                                                                                          */
    3 /*                                                                                                       */
    4 /* Copyright (c) 2006 Institut fuer Aerodynamik und Gasdynamik, Universitaet Stuttgart                   */
    5 /*                                                                                                       */
    6 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and     */
    7 /* associated documentation files (the "Software"), to deal in the Software without restriction,         */
    8 /* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, */
    9 /* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, */
   10 /* subject to the following conditions:                                                                  */
   11 /*                                                                                                       */
   12 /* The above copyright notice and this permission notice shall be included in all copies or substantial  */
   13 /* portions of the Software.										 */
   14 /*                                                                                                       */
   15 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT */
   16 /* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   */
   17 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER          */
   18 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */
   19 /* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                       */
   20 /*-------------------------------------------------------------------------------------------------------*/
   21 /* ----------------------------------------------------------------------
   22  * project: coviseio (EAS3)
   23  * file:    covhelp.c
   24  * author:  Kai Augustin <augustin@iag.uni-stuttgart.de>
   25  *          Juli 2000
   26  * ----------------------------------------------------------------------
   27  * Hilfroutinen zur Speicherverwaltung und Fehlerverwaltung, etc.
   28  * ----------------------------------------------------------------------
   29  */
   30 
   31 #include <stdlib.h>
   32 #include <stdio.h>
   33 #include <string.h>
   34 #include <stdarg.h>
   35 #include <limits.h>
   36 #include <math.h>
   37 #include <errno.h>
   38 #include <sys/types.h>
   39 #include <sys/stat.h>
   40 #include <fcntl.h>
   41 
   42 #include "coviseio.h"
   43 
   44 #if defined (_LITTLEENDIAN)
   45 /* Konvertierungsroutinen fuer Little->Big Endian einfuegen */
   46 #include <conv.h>
   47 #endif
   48 
   49 /* Maximale Anzahl Argumente fuer stdarg.h */
   50 #define MAXARGS 10
   51 
   52 /************************************************************************
   53  * C_COV : debug                                                        *
   54  * Ausgabe von Debug/Errormessage, wenn PRINTMESSAGE = 1 definiert      *
   55  ************************************************************************/
   56 void cov_debug(int nptrs, ...)
   57 {
   58   va_list ap ;
   59   Int32 ptr_no = 0 ;
   60 
   61   if (PRINTMESSAGE)
   62     {
   63       fprintf(stderr,"\n----------------------------------------------") ;
   64       fprintf(stderr,"--------------------------\n");
   65       if (nptrs > MAXARGS)
   66 	nptrs = MAXARGS;
   67       va_start(ap,nptrs) ;
   68       fprintf(stderr, "Message from libcovise in %s at line %d.\n",
   69 	      va_arg(ap, char *), va_arg(ap, Int32)) ;       ;
   70       fprintf(stderr, "Compile date: %s %s\n",
   71 	      __DATE__, __TIME__);
   72       fprintf(stderr,"----------------------------------------------") ;
   73       fprintf(stderr,"--------------------------\n");
   74       ptr_no = 2 ;
   75       while(ptr_no++ < nptrs)
   76         fprintf(stderr, "%s",va_arg(ap, char *));
   77       va_end(ap) ;
   78 
   79       if (errno != 0)
   80 	{
   81 	  fprintf(stderr, "System error string: \n%s\n", strerror(errno));
   82 	  if (errno == EBADF)
   83 	    fprintf(stderr, "Write-Lock auf RDONLY-File ") ;
   84 	    fprintf(stderr, "oder umgekehrt.\n");
   85 	}
   86       fprintf(stderr,"----------------------------------------------") ;
   87       fprintf(stderr,"--------------------------\n");
   88     }
   89 }
   90 
   91 /************************************************************************
   92  * C_COV : cov_calloc                                                   *
   93  * Speicher allokieren, Abbruch bei Fehlerausgabe                       *
   94  ************************************************************************/
   95 void *cov_calloc(const int nelem, const int elsize,
   96                  const char *file, const int line)
   97 {
   98   size_t nbytes ;
   99   void *mp = NULL;
  100 
  101   /* Anzahl der Bytes bestimmen */
  102   nbytes = (size_t) nelem * elsize ;
  103 
  104   /* Speicher allokieren */
  105   if ((mp = malloc(nbytes)) == NULL)
  106     {
  107       cov_debug(3,__FILE__, __LINE__,
  108 		"Fehler beim allokieren des Speichers, Abbruch...") ;
  109       exit(-1) ;
  110     }
  111   return(mp) ;
  112 }
  113 
  114 /************************************************************************
  115  * C_COV : cov_getsingle                                                *
  116  * Ein double Precision float-Feld in ein single Precision float-Feld   *
  117  * verwandeln                                                           *
  118  * Speicher allokieren, Overflow abchecken                              *
  119  * bei indata[i]<1E-37 => outdata[i] = 0                                *
  120  * bei indata[i]>1E37 => outdata[i] = 1E37                              *
  121  ************************************************************************/
  122 float *cov_getsingle(const Float64 *indata, const int nelem,
  123 		  const char *file, const int line, int *status)
  124 {
  125   float *outdata ;           /* Pointer fuer single Precision Feld */
  126   int   n        ;           /* Zaehlvariable                      */
  127   char  elem[80] ;           /* Hilfcharacterfeld zur Fehlerbehandlung */
  128 
  129   /* Gehen wir mal davon aus, dass alles klar geht ! */
  130   *status = 0 ;
  131 
  132   /* Speicher allokieren */
  133   outdata=(float *)cov_calloc(nelem,sizeof(float),file,line);
  134 
  135   for (n=0;n<nelem;n++)
  136     {
  137       if (indata[n] > 1.0e37)
  138 	{
  139 	  outdata[n] = 1.0e37 ;
  140 	  sprintf(elem,"%d (%le)",n,indata[n]) ;
  141 	  cov_debug(5,file,line,
  142 		    "C_COVOPEN() : Floating Point Overflow in Element n=",
  143 		    elem,"\n") ;
  144 	  *status=-16 ;
  145 	}
  146       if (indata[n] < -1.0e37)
  147 	{
  148 	  outdata[n] = -1.0e37 ;
  149 	  sprintf(elem,"%d (%le)",n,indata[n]) ;
  150 	  cov_debug(5,file,line,
  151 		    "C_COVOPEN() : Floating Point Overflow in Element n=",
  152 		    elem,"\n") ;
  153 	  *status=-16 ;
  154 	}
  155       else if ((indata[n] > 0 && indata[n] < 1.0e-37) ||
  156                (indata[n] < 0 && indata[n] > -1.0e-37) )
  157 	{
  158 	  outdata[n] = 0.0 ;
  159 	  sprintf(elem,"%d (%le)",n,indata[n]) ;
  160 	  cov_debug(5,file,line,
  161 		    "C_COVOPEN() : Floating Point Underflow in Element n=",
  162 		    elem,"\n") ;
  163 	  *status=-16 ;
  164 	}
  165       else
  166 	{
  167 	  outdata[n] = (float)indata[n] ;
  168 	}
  169     }
  170 
  171   return outdata ;
  172 }
  173 
  174 /************************************************************************
  175  * C_COV : cov_writetofile                                              *
  176  * Wrapper zum Ausfuehren des Schreibbefehls in die Datei cov_fildes    *
  177  * cov_kind ist entweder COV_ISSTRING, COV_ISINT, COV_ISFLOAT           *
  178  * Schreiben von nbytes Bytes aus dem Puffer writebuf in die Datei      *
  179  * Ggf. Little Endian -> Big Endian Konversion                          *
  180  ************************************************************************/
  181 long cov_writetofile(const int cov_fildes,
  182 		    const int cov_kind,
  183 		    const void *databuf,
  184 		    size_t numele)
  185 {
  186 
  187   size_t result, nbytes ;
  188   void *writebuf ;
  189 
  190 
  191   switch (cov_kind)
  192     {
  193     case COV_ISSTRING:
  194       nbytes = numele ;
  195 #ifdef DEBUG_C_IO
  196       fprintf(stderr,"Schreibe COV_ISSTRING: %d bytes\n",nbytes);
  197 #endif
  198       break;
  199     case COV_ISINT:
  200       nbytes = numele*sizeof(int) ;
  201 #ifdef DEBUG_C_IO
  202       fprintf(stderr,"Schreibe COV_ISINT: %d bytes\n",nbytes);
  203 #endif
  204       break;
  205     case COV_ISFLOAT:
  206       nbytes = numele*sizeof(float) ;
  207 #ifdef DEBUG_C_IO
  208       fprintf(stderr,"Schreibe COV_ISINT: %d bytes\n",nbytes);
  209 #endif
  210       break;
  211     default:
  212       cov_debug(3,__FILE__, __LINE__,
  213 		"cov_writetofile() : Unbekanntes Datenformat \n") ;
  214       return(-1) ;
  215     }
  216 
  217 #if defined (_LITTLEENDIAN)
  218 
  219   /*************************************************/
  220   /* Wir sind auf einer Little-Endian Maschine     */
  221   /* Little->Big Endian Konvertierung durchfuehren */
  222   /*************************************************/
  223   if (cov_kind == COV_ISINT || cov_kind == COV_ISFLOAT)
  224     {
  225       /* Schreibpuffer anlegen */
  226       writebuf=cov_calloc(1,nbytes,__FILE__, __LINE__) ;
  227       /* 32-bit Konvertierung durchfuehren */
  228 #ifdef DEBUG_C_IO
  229       fprintf(stderr,"Konvertiere %d 32-bit Elemente nach Big-Endian\n\n",numele) ;
  230 #endif
  231       conv32(numele,(unsigned char*)databuf,
  232 	            (unsigned char*)writebuf) ;
  233     }
  234   else
  235     {
  236       writebuf=(void*)databuf ;
  237     }
  238 
  239 #else
  240 
  241   /* Keine Konvertierung notwendig */
  242   writebuf=(void*)databuf ;
  243 
  244 #endif
  245 
  246   result = (long)write(cov_fildes,writebuf,nbytes) ;
  247 
  248 #if defined (_LITTLEENDIAN)
  249   if (cov_kind == COV_ISINT || cov_kind == COV_ISFLOAT)
  250     free(writebuf) ;
  251 #endif
  252 
  253   return result ;
  254 }