GRASS Programmer's Manual  6.4.2(2012)
dspf_header.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include "viz.h"
00003 
00004 
00005 /*================= DOCUMENT RETURN VALUES! =================*/
00006 
00007 int dfwrite_header(file_info * headp)
00008 {
00009     int isize, flsize;
00010     cmndln_info *linep;
00011     FILE *fp;
00012     long Where_dataoff;
00013     long Where_lookoff;
00014 
00015     linep = &(headp->linefax);
00016     fp = headp->dspfoutfp;
00017 
00018     isize = sizeof(int);
00019     flsize = sizeof(float);
00020     /* print the header code on first line of file */
00021     if (!fwrite(DSPF_ID, strlen(DSPF_ID), 1, fp))
00022         return (-1);
00023     /* the dimensions of the data */
00024     if (1 != fwrite(&headp->xdim, isize, 1, fp))
00025         return (-1);
00026     if (1 != fwrite(&headp->ydim, isize, 1, fp))
00027         return (-1);
00028     if (1 != fwrite(&headp->zdim, isize, 1, fp))
00029         return (-1);
00030 
00031 
00032     /* print out code for min and max values */
00033     if (1 != fwrite(&headp->min, flsize, 1, fp))
00034         return (-1);
00035     if (1 != fwrite(&headp->max, flsize, 1, fp))
00036         return (-1);
00037 
00038     /* the litmodel stored for each polygon */
00039     if (1 != fwrite(&linep->litmodel, isize, 1, fp))
00040         return (-1);
00041 
00042     /* write the total number of thresholds to be searched for */
00043     if (1 != fwrite(&linep->nthres, isize, 1, fp))
00044         return (-1);
00045     /* write the array of thresholds out */
00046     if ((fwrite(linep->tvalue, flsize, linep->nthres, fp)) != linep->nthres) {
00047         fprintf(stderr, "ERROR: fwrite in dspf_header.c\n");
00048         return (-1);
00049     }
00050 
00051     /* write the offset to the lookup table */
00052     /* the first time this number is set to 0 */
00053     /*this information will be overwritten after dspf is done */
00054     /* ftell keeps track of where this information is to be placed */
00055     Where_lookoff = ftell(fp);
00056     headp->Lookoff = 0;
00057     if (1 != fwrite(&headp->Lookoff, sizeof(long), 1, fp))
00058         return (-1);
00059 
00060     /* code to determine the length of the binary file header */
00061     /* Dataoff = length of the header */
00062     /*Dataoff = strlen (DSPF_ID) + 7*isize + 5*flsize + linep->nthres*flsize; */
00063     Where_dataoff = ftell(fp);
00064     headp->Dataoff = 0;
00065     if (1 != fwrite(&headp->Dataoff, sizeof(long), 1, fp))
00066         return (-1);
00067 
00068 
00069 
00070     /* End of header,  now go back and fill in what we can */
00071     headp->Dataoff = ftell(fp);
00072     fseek(fp, Where_dataoff, 0);
00073     if (1 != fwrite(&headp->Dataoff, sizeof(long), 1, fp))
00074         return (-1);
00075 
00076     fseek(fp, headp->Dataoff, 0);       /* and return to begin writing data */
00077 
00078     /* will still have to come back once more to fill in Lookup offset */
00079 
00080     return (0);
00081 }
00082 
00083 
00084 /**************************** dfread_header **********************************/
00085 
00086 /**************************** dfread_header **********************************/
00087 
00088 /**************************** dfread_header **********************************/
00089 
00090 int dfread_header(file_info * headp)
00091 {
00092     int isize, flsize;
00093     FILE *fp;
00094     cmndln_info *linep;
00095     char buf[80];
00096     int len;
00097 
00098     fp = headp->dspfinfp;
00099 
00100 
00101     len = strlen(DSPF_ID);
00102     fseek(fp, 0L, 0);           /* rewind file */
00103     /*read in header information and store in File_info struct */
00104 
00105     if (!fread(buf, 1, len, fp))
00106         return (-1);
00107     buf[len] = 0;
00108     if (strncmp(DSPF_ID, buf, len)) {
00109         if (!strncmp("dspf003.01", buf, len))
00110             return (dfread_header_old(headp, fp));
00111 
00112         fprintf(stderr, "Error: header mismatch '%s' - '%s'\n", DSPF_ID, buf);
00113         return (-1);
00114     }
00115     linep = &(headp->linefax);
00116     isize = sizeof(int);
00117     flsize = sizeof(float);
00118 
00119     if (!fread(&headp->xdim, isize, 1, fp))
00120         return (-1);
00121     if (!fread(&headp->ydim, isize, 1, fp))
00122         return (-1);
00123     if (!fread(&headp->zdim, isize, 1, fp))
00124         return (-1);
00125     if (!fread(&headp->min, flsize, 1, fp))
00126         return (-1);
00127     if (!fread(&headp->max, flsize, 1, fp))
00128         return (-1);
00129     if (!fread(&linep->litmodel, isize, 1, fp))
00130         return (-1);
00131     if (!fread(&linep->nthres, isize, 1, fp))
00132         return (-1);
00133     if (!fread(linep->tvalue, flsize, linep->nthres, fp))
00134         return (-1);
00135     if (!fread(&headp->Lookoff, isize, 1, fp))
00136         return (-1);
00137     if (!fread(&headp->Dataoff, isize, 1, fp))
00138         return (-1);
00139 
00140     print_head_info(headp);
00141 
00142     return (1);
00143 }
00144 
00145 int dfread_header_old(file_info * headp, FILE * fp)
00146 {
00147     int isize, flsize;
00148     cmndln_info *linep;
00149     float tmp;
00150 
00151     linep = &(headp->linefax);
00152     isize = sizeof(int);
00153     flsize = sizeof(float);
00154 
00155     if (!fread(&headp->xdim, isize, 1, fp))
00156         return (-1);
00157     if (!fread(&headp->ydim, isize, 1, fp))
00158         return (-1);
00159     if (!fread(&headp->zdim, isize, 1, fp))
00160         return (-1);
00161     if (!fread(&tmp, flsize, 1, fp))
00162         return (-1);
00163     if (!fread(&tmp, flsize, 1, fp))
00164         return (-1);
00165     if (!fread(&tmp, flsize, 1, fp))
00166         return (-1);
00167     if (!fread(&headp->min, flsize, 1, fp))
00168         return (-1);
00169     if (!fread(&headp->max, flsize, 1, fp))
00170         return (-1);
00171     if (!fread(&linep->litmodel, isize, 1, fp))
00172         return (-1);
00173     if (!fread(&linep->nthres, isize, 1, fp))
00174         return (-1);
00175     if (!fread(linep->tvalue, flsize, linep->nthres, fp))
00176         return (-1);
00177     if (!fread(&headp->Lookoff, isize, 1, fp))
00178         return (-1);
00179     if (!fread(&headp->Dataoff, isize, 1, fp))
00180         return (-1);
00181 
00182     print_head_info(headp);
00183 
00184     return (1);
00185 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines