fileio.c

Go to the documentation of this file.
00001 /***********************************************************************
00002 
00003         LIBRARY : stmbat.lib
00004 
00005         MODULE  : fileio.c
00006 
00007         PURPOSE : File Input / Output
00008 
00009         FUNCTIONS :  FileCheck ()
00010                  FileRead  ()
00011                  FileWrite ()
00012 
00013 ************************************************************************/
00014 
00015 #include <stdio.h>
00016 #include <fcntl.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <math.h>
00020 #include "batch.h"
00021 
00022 char *getpgmhval(char *ptr, int *val);
00023 
00024 int stricmp(const char *s1, const char *s2){
00025   char *sa, *sb, *s;
00026   int ret;
00027   sa=strdup(s1);
00028   sb=strdup(s1);
00029   for(s=sa; *s; s++) *s=tolower(*s);
00030   for(s=sb; *s; s++) *s=tolower(*s);
00031   ret=strcmp(s1, s2);
00032   free(sa); 
00033   free(sb);
00034   return ret;
00035 }
00036 
00037 char *strupr(char *s1){
00038   char *s;
00039   for(s=s1; *s; s++) *s=toupper(*s);
00040   return(s1);
00041 }
00042 
00043 void *salloc (long AnzDerObj, USHORT ObjSize)
00044 /*
00045    Dies ist die genaue Nachbildung der Library Fkt. halloc ()
00046    allerdings mit einem OS2 Call
00047 */
00048 {
00049 #ifdef GNUCC
00050  SEL sel;
00051  char *retval;
00052 #endif
00053  long bytanz;
00054  bytanz = AnzDerObj * (long)ObjSize;
00055 
00056 #ifdef GNUCC
00057  if (0==(DosAllocHuge((unsigned short)(bytanz / 65536l),        /* number of segments          */
00058         (unsigned short)(bytanz % 65536l),      /* size of last segment        */
00059         &sel,                  /* address of selector          */
00060         0,                     /* sharing flag                 */
00061         0)))                   /* maximum segments for realloc */
00062  {
00063         retval = MAKEP(sel, 0);
00064         return(retval);
00065  }
00066  else
00067         return (NULL);
00068 #else
00069   return( malloc(bytanz) );
00070 #endif
00071 }
00072 
00073 void sfree (void *ptr)
00074 /* Das Gegenstück zu salloc */
00075 {
00076 #ifdef GNUCC
00077  SEL selc;
00078 
00079  selc = SELECTOROF (ptr);
00080  DosFreeSeg (selc);
00081 #else
00082  free(ptr);
00083 #endif
00084 }
00085 
00086 long FileCheck (char *fname, FILETYPE *ftyp, FILEHEAD *fhead, FILE **fh)
00087   //char fname[];               // Dateiname , voller Pfad
00088   //FILETYPE *ftyp;     // welche Art von Datei ? ,wird zurückgeliefert
00089   //FILEHEAD *fhead;    // Bildeckkoordinaten  ,werden zurückgeliefert
00090   //int *fh;            // Dateihandle
00091 /* Teste eine Datei auf Existenz,
00092           falls sie existiert, überprüfe den Typ
00093           Ist es eine Daten Datei ?, falls ja lade den Kopf
00094          sonst lade die Größe (erste zwei Words)
00095           berechne aus dem Typ den erforderlichen Speicher
00096 
00097   returns : Anzahl der Datenbytes in der Datei
00098                 -1 im Fehlerfall d.h. Datei existiert nicht
00099 */
00100 {
00101  int pc;
00102  long numofbyt, sizfac;
00103  char exstr[4];
00104  XYCoord xyanz;
00105 
00106  // if ((acc=access(fname,4 /* read permiss */)) == -1)
00107         /* access failed, fatal return */
00108  //     return (-1);
00109  /* die letzten drei Buchstaben von fname werden als extension angenommen
00110         und entsprechend ausgewertet */
00111  strcpy (exstr,fname+strlen(fname)-3);
00112  strupr(exstr);
00113  if (strstr(exstr,"DAT")) {
00114         *ftyp = DATFIL;
00115         sizfac = sizeof(short);
00116  }
00117  else if (strstr(exstr,"BYT")) {
00118     *ftyp = BYTFIL;
00119     sizfac = sizeof(char);
00120  }
00121  else if (strstr(exstr,"SHT")) {
00122     *ftyp = SHTFIL;
00123     sizfac = sizeof(short);
00124  }
00125  else if (strstr(exstr,"SPE")) {
00126     *ftyp = SPEFIL;
00127     sizfac = sizeof(short);
00128  }
00129  else if (strstr(exstr,"FLT")) {
00130         *ftyp = FLTFIL;
00131     sizfac = sizeof(float);
00132  }
00133  else if (strstr(exstr,"DBL")) {
00134     *ftyp = DBLFIL;
00135     sizfac = sizeof(double);
00136  }
00137  else if (strstr(exstr,"CPX")) {
00138     *ftyp = CPXFIL;
00139     sizfac = 2*sizeof(float);
00140  }
00141  else if (strstr(exstr,"PGM")) {
00142     *ftyp = PGMFIL;
00143     sizfac = sizeof(char);
00144  }
00145  else if (strstr(exstr,"D2D")) {
00146     *ftyp = D2DFIL;
00147     sizfac = sizeof(short);
00148  }
00149  else {   /* unknown Type */
00150     return (-1);
00151  }
00152  /* open file, lese Header oder Datenkopf */
00153  if ((*fh = fopen (fname, "rb")) == 0)
00154     return (-1);
00155  if (*ftyp == DATFIL) {
00156    fread ((char *)fhead, sizeof(HEADER),(size_t)1,*fh);
00157    numofbyt = 0; /* If Spectroscopy make Loop */
00158    if (fhead->kopf.NumOfVolts == 0) fhead->kopf.NumOfVolts++;
00159    for (pc=0; pc < fhead->kopf.NumOfVolts; ++pc)
00160      numofbyt += (long)fhead->kopf.nx[pc]*(long)fhead->kopf.ny[pc]*sizfac;
00161  }
00162  else {
00163    if (*ftyp == PGMFIL) {
00164      char *ptr, zeile[256];
00165      int value=0;
00166      fgets(zeile, 256, *fh);
00167      if(strncmp(zeile,"P5",2)){
00168        fprintf(stderr, zeile);
00169        fprintf(stderr,"\nKein PGM File, P5 Kennung wurde nicht erkannt !\n");
00170        exit(2);
00171      }
00172      ptr=zeile+2;
00173      while((ptr=getpgmhval(ptr,&value))==0) 
00174        fgets(ptr=zeile, 256, *fh);
00175      xyanz.x=(short)value;
00176 
00177      while((ptr=getpgmhval(ptr,&value))==0) 
00178        fgets(ptr=zeile, 256, *fh);
00179      xyanz.y=(short)value;
00180 
00181      while((ptr=getpgmhval(ptr,&value))==0) 
00182        fgets(ptr=zeile, 256, *fh);
00183      if(value != 255)
00184        fprintf(stderr,"ngrey=%d < 255 !\n",value);
00185 
00186      fhead->xydim.x = xyanz.x;
00187      fhead->xydim.y = xyanz.y;
00188      numofbyt = (long)xyanz.x*(long)xyanz.y*sizfac;
00189      printf("PGM Info: %dx%d = %ld\n",xyanz.x, xyanz.y, numofbyt);
00190    }
00191    else{
00192       if (*ftyp == D2DFIL) {
00193          fread ((char *)fhead, sizeof(D2D_SPA_DAT_HEADER),(size_t)1,*fh);
00194          numofbyt = (long)(fhead->d2dkopf.PointsX+1)*(long)(fhead->d2dkopf.PointsY)*sizfac;
00195          fseek(*fh,0x180, SEEK_SET); // Auf Datenanfang Positionieren
00196       }
00197       else{
00198          if(*ftyp == SPEFIL){ /* CCD Camera Format */
00199             fhead->xydim.x = xyanz.x = 512;
00200             fhead->xydim.y = xyanz.y = 512;
00201             numofbyt = (long)xyanz.x*(long)xyanz.y*sizfac;
00202             fseek(*fh,4100L, SEEK_SET);
00203             *ftyp = SHTFIL; /* Trick 17 :=) */
00204          }
00205          else{
00206             fread((char *)&xyanz, sizeof(short), (size_t)2, *fh);
00207             fhead->xydim.x = xyanz.x;
00208             fhead->xydim.y = xyanz.y;
00209             numofbyt = (long)xyanz.x*(long)xyanz.y*sizfac;
00210          }
00211       }
00212    }
00213  }
00214  /* no Fileclose */
00215  return (numofbyt);
00216 }
00217 
00218 char *getpgmhval(char *ptr, int *val){
00219   while(*ptr == ' ') ptr++;
00220   if(*ptr == '#') { 
00221     printf(ptr);
00222     return 0; 
00223   }
00224   if(*ptr == '\n') return 0;
00225   if(*ptr == 0) return 0;
00226   if((*val = atoi(ptr)) == 0) { fprintf(stderr, "Garbage in PGM-Header !\n"); exit(1); }
00227   while(*ptr >= '0' && *ptr <='9') ptr++;
00228   printf("%d\n",*val);
00229   return ptr;
00230 }
00231 
00232 int FileRead (FILE *fh, char *buffer, long nbytes)
00233 /*
00234    liest die nbytes aus der offenen Datei 'fh' (Filehandle) nach buffer
00235 
00236    benötigt vorher einen FileCheck(..) Aufruf
00237    fh: Fileptr
00238    buffer: Datenbuffer
00239    nbytes: Bytes to Read
00240 
00241    returns  :   0 bei ok
00242                -1 sonst
00243    liest aus offener Datei nbytes Bytes 
00244 */
00245 {
00246   size_t br;
00247   if((br=fread((void*)buffer, (size_t)1, (size_t)nbytes, fh)) < (size_t)nbytes) {
00248     fprintf(stderr,"Could only read %ld bytes from %ld bytes !\n", (long)br, (long)nbytes);
00249     fclose (fh);
00250     return(-1);
00251  }
00252  fclose (fh);
00253  return (0);
00254 }
00255 
00256 
00257 int FileWrite (char *fname, char *buffer, FILEHEAD *fhead)
00258 /*
00259    Öffnet Datei 'fname' und schreibt zunächst den Header
00260    und dann die Daten aus buffer in die Datei.
00261 
00262   //char *fname;           // Dateiname
00263   //char *buffer;          // den es zu schreiben gilt
00264   //FILEHEAD *fhead;   // falls Kopf geschrieben werden soll
00265 
00266    returns  :   0 bei ok
00267                -1 sonst
00268 
00269 */
00270 {
00271  FILE *fh;
00272  int pc;
00273  size_t ret;
00274  char exstr[4];
00275  long numofbyt, sizfac;
00276  FILETYPE ftyp;
00277 
00278  strcpy (exstr,fname+strlen(fname)-3);
00279  strupr(exstr);
00280  if (strstr(exstr,"DAT")) {
00281    ftyp = DATFIL;
00282    sizfac = sizeof(short);
00283  }
00284  else if (strstr(exstr,"BYT")) {
00285    ftyp = BYTFIL;
00286    sizfac = sizeof(char);
00287  }
00288  else if (strstr(exstr,"SHT")) {
00289    ftyp = SHTFIL;
00290    sizfac = sizeof(short);
00291  }
00292  else if (strstr(exstr,"FLT")) {
00293    ftyp = FLTFIL;
00294    sizfac = sizeof(float);
00295  }
00296  else if (strstr(exstr,"DBL")) {
00297    ftyp = DBLFIL;
00298    sizfac = sizeof(double);
00299  }
00300  else if (strstr(exstr,"CPX")) {
00301    ftyp = CPXFIL;
00302    sizfac = 2*sizeof(float);
00303  }
00304  else if (strstr(exstr,"PGM")) {
00305    ftyp = PGMFIL;
00306    sizfac = sizeof(char);
00307  }
00308  else {   /* unknown Type */
00309    return (-1);
00310  }
00311  /* open file, schreibe Header oder Datenkopf */
00312  if ((fh = fopen (fname, "wb"))== 0)
00313     return (-1);
00314  if (ftyp==DATFIL) {
00315     if(fwrite ( (char *)fhead, sizeof(HEADER), (size_t)1, fh) < 1) 
00316       return (-1);
00317     numofbyt = 0; /* If Spectroscopy make Loop */
00318     if (fhead->kopf.NumOfVolts == 0) fhead->kopf.NumOfVolts++;
00319     for (pc=0; pc < fhead->kopf.NumOfVolts; ++pc)
00320       numofbyt += (long)fhead->kopf.nx[pc]*(long)fhead->kopf.ny[pc]*sizfac;
00321  }
00322  else {
00323    if (ftyp==PGMFIL){
00324      fprintf(fh,"P5\n# von gnu-tools convert\n%d %d\n255\n", 
00325              (int)fhead->xydim.x, (int)fhead->xydim.y);
00326      numofbyt = (long)fhead->xydim.x*(long)fhead->xydim.y*sizfac;
00327    }
00328    else{
00329      if(fwrite ((char *)fhead, 2*sizeof(short), (size_t)1, fh) < 1)
00330        return (-1);
00331      numofbyt = (long)fhead->xydim.x*(long)fhead->xydim.y*sizfac;
00332    }
00333  }
00334  if( (ret=fwrite ((void*)buffer, (size_t)1, (size_t)numofbyt, fh)) < (size_t)numofbyt){
00335    fprintf(stderr,"ist#%ld soll#%ld\n",(long)ret,numofbyt);
00336    fclose (fh);
00337    return -1;
00338  }
00339 
00340  fclose (fh);
00341  return (int)ret;
00342 }

Generated on Sat Apr 1 09:04:25 2006 for GXSM by  doxygen 1.4.6