コード例 #1
0
int topen(char *fname)
{
   int         i, j, k, l, headlen, isblank;
   int         headlent, ncolt, nscan;
   char       *ptr, *kptr, *vptr;
   struct stat buf;
   char       *scan;

   tclear();

   /* Quick scan to determine table */
   /* record width                  */

   if(mtbl_maxline == 0)
   {
      if(tdebug)
      {
     printf("TDEBUG> Checking %s for header width<br>\n", fname);
     fflush(stdout);
      }

      nscan = MTBL_MAXLINE;

      while(1)
      {
     scan = (char *)malloc(nscan * sizeof(char));

     if(scan != (char *)NULL)
        break;

     if(nscan < 1024)
        return(MTBL_MALLOC);
      }

      if(tWrite)
	 tfile = fopen(fname, "r+");
      else
	 tfile = fopen(fname, "r");

      if (tfile == (FILE *)NULL)
     return(MTBL_NOFILE);

      while(1)
      {
     if(fgets(scan, nscan, tfile) == (char *)NULL)
        return (MTBL_RDERR);

     if(tdebug)
     {
        printf("TDEBUG> Scanning [%s]<br>\n", scan);
        fflush(stdout);
     }

     if(scan[0] == '|')
     {
        mtbl_maxline = 2 * strlen(scan);

        if(tdebug)
        {
           printf("TDEBUG> Scan -> max line length = %d<br>\n",
          mtbl_maxline);
           fflush(stdout);
        }

        break;
     }
      }

      if(mtbl_maxline < MTBL_MAXSTR)
      {
     mtbl_maxline = MTBL_MAXSTR;

     if(tdebug)
     {
        printf("TDEBUG> No header found -> max line length defaults to %d<br>\n",
           mtbl_maxline);
        fflush(stdout);
     }
      }

      free(scan);

      rewind(tfile);
      fclose(tfile);
   }

   if(tdebug)
   {
      printf("TDEBUG> Max line length = %d<br>\n", mtbl_maxline);
      fflush(stdout);
   }


   /* Allocate space for working strings */

   if (mtbl_linelen < mtbl_maxline)
   {
      if(tdebug)
      {
     printf("TDEBUG> Malloc %d character strings (tbl_hdr_len, etc.)<br>\n",
        mtbl_maxline);
     fflush(stdout);
      }
      tbl_rec_string = (char *)realloc(tbl_rec_string, mtbl_maxline * sizeof(char));
      tbl_hdr_string = (char *)realloc(tbl_hdr_string, mtbl_maxline * sizeof(char));
      tbl_typ_string = (char *)realloc(tbl_typ_string, mtbl_maxline * sizeof(char));
      tbl_uni_string = (char *)realloc(tbl_uni_string, mtbl_maxline * sizeof(char));
      tbl_nul_string = (char *)realloc(tbl_nul_string, mtbl_maxline * sizeof(char));
      dval           = (char *)realloc(dval,           mtbl_maxline * sizeof(char));
      mtbl_linelen   = mtbl_maxline;
   }

   strcpy(tbl_rec_string, "");
   strcpy(tbl_hdr_string, "");
   strcpy(tbl_typ_string, "");
   strcpy(tbl_uni_string, "");
   strcpy(tbl_nul_string, "");

   strcpy(dval, "");


   if(mtbl_maxcol == 0)
   {
      mtbl_maxcol = MTBL_MAXCOL;

      tbl_rec = (struct TBL_REC *)malloc(mtbl_maxcol * sizeof(struct TBL_REC));

      for(i=0; i<mtbl_maxcol; ++i)
      {
     for(k=0; k<MTBL_MAXSTR; ++k)
     {
        tbl_rec[i].name[k] = '\0';
        tbl_rec[i].type[k] = '\0';
        tbl_rec[i].unit[k] = '\0';
        tbl_rec[i].nuls[k] = '\0';
     }

     tbl_rec[i].endcol = 0;
     tbl_rec[i].colwd  = 0;
      }
   }


   /* OPEN FILE */

   if(tWrite)
      tfile = fopen(fname, "r+");
   else
      tfile = fopen(fname, "r");

   if (tfile == (FILE *)NULL)
      return(MTBL_NOFILE);


   /******************************************************/
   /* READ HEADER, find columns and extract column names */
   /******************************************************/

   headbytes =  0;
   reclen    =  0;
   nrec      = -1;
   nkey      =  0;
   nhdr      =  0;

   haveType = 0;
   haveUnit = 0;
   haveNull = 0;


   /* Parse keyword lines */

   while(FOREVER)
   {
      if(fgets(dval, mtbl_maxline, tfile) == (char *) NULL)
     break;

      if(tdebug)
      {
     printf("TDEBUG> Read keyword header line [%s]<br>\n", dval);
     fflush(stdout);
      }

      reclen     = (int)strlen(dval);
      headbytes += reclen;

      if(dval[strlen(dval) - 1] == '\n')
         dval[strlen(dval) - 1]  = '\0';

      if(dval[strlen(dval) - 1] == '\r')
         dval[strlen(dval) - 1]  = '\0';

      isblank = 1;
      for(i=0; i<strlen(dval); ++i)
      {
     if(dval[i] != ' ' && dval[i] != '\t')
     {
        isblank = 0;
        break;
     }
      }

      if(!isblank && dval[0] != '\\')
     break;

      if(nkeyalloc <= nkey)
      {
     nkeyalloc += MTBL_MAXKEY;

     keystr  = (char **)realloc(keystr,  nkeyalloc * sizeof(char *));
     keyword = (char **)realloc(keyword, nkeyalloc * sizeof(char *));
     value   = (char **)realloc(value,   nkeyalloc * sizeof(char *));

     for(i=nkey; i<nkeyalloc; ++i)
     {
        keystr [i] = (char *)malloc(MTBL_KEYLEN * sizeof(char));
        keyword[i] = (char *)malloc(MTBL_KEYLEN * sizeof(char));
        value  [i] = (char *)malloc(MTBL_KEYLEN * sizeof(char));
     }
      }

      strcpy(keystr[nhdr], dval);
      ++nhdr;

      kptr = dval + 1;
      ptr  = kptr;

      while(*ptr != ' '
     && *ptr != '='
     && *ptr != '\0')
        ++ptr;

      while(*ptr == ' ')
      {
     *ptr = '\0';
     ++ptr;
      }

      if(*ptr != '=')
     continue;

      *ptr = '\0';
      ++ptr;

      while(*ptr == ' ')
         ++ptr;

      vptr = ptr;

      for(i=strlen(vptr)-1; i>=0; --i)
      {
     if(vptr[i] == ' ')
        vptr[i] = '\0';
     else
        break;
      }

      if(strlen(kptr) > 0)
      {
     strcpy(keyword[nkey], kptr);
     strcpy(value  [nkey], vptr);
     ++nkey;
      }
   }

   strcpy(tbl_hdr_string, dval);

   if(dval[0] == '|')
      dval[0] =  ' ';

   headlen = reclen;

   if(tbl_hdr_string[headlen - 1] == '\n')
      tbl_hdr_string[headlen - 1] =  '\0';


   /* Parse the header line for column names and sizes */

   ncol = 0;

   j = 0;

   headlen = (int)strlen(dval);

   for(i=0; i<headlen; ++i)
   {
      if (dval[i] == '\\')
     break;

      if (dval[i] == '\n')
     break;

      else if (dval[i] == '|')
      {
     tbl_rec[ncol].endcol = i;
     tbl_rec[ncol].name[j] = '\0';
     ++ncol;

     if(ncol > mtbl_maxcol)
     {
        mtbl_maxcol += MTBL_MAXCOL;

        tbl_rec = (struct TBL_REC *)
           realloc(tbl_rec, mtbl_maxcol * sizeof(struct TBL_REC));

        for(k=mtbl_maxcol-MTBL_MAXCOL; k<mtbl_maxcol; ++k)
        {
           for(l=0; l<MTBL_MAXSTR; ++l)
           {
          tbl_rec[k].name[l] = '\0';
          tbl_rec[k].type[l] = '\0';
          tbl_rec[k].unit[l] = '\0';
          tbl_rec[k].nuls[l] = '\0';
           }

           tbl_rec[k].endcol = 0;
           tbl_rec[k].colwd  = 0;
        }
     }

     j = 0;

         if(i == 0)
        --ncol;
      }

      else if (dval[i] != ' ' )
      {
     tbl_rec[ncol].name[j] = dval[i];
     ++j;
      }
   }

   tbl_rec[0].colwd = tbl_rec[0].endcol + 1;

   for (i=1; i<ncol; ++i)
    tbl_rec[i].colwd = tbl_rec[i].endcol - tbl_rec[i-1].endcol;


   /* Read any additional header lines */

   while(FOREVER)
   {
      if(fgets(dval, mtbl_maxline, tfile) == (char *) NULL)
     break;

      if(tdebug)
      {
     printf("TDEBUG> Read additional header [%s]<br>\n", dval);
     fflush(stdout);
      }

      if(dval[0] != '|')
     break;

      reclen     = (int)strlen(dval);
      headbytes += reclen;

      if(!haveType)
      {
     if(dval[strlen(dval) - 1] == '\n')
        dval[strlen(dval) - 1]  = '\0';

     if(dval[strlen(dval) - 1] == '\r')
        dval[strlen(dval) - 1]  = '\0';

     haveType = 1;
     strcpy(tbl_typ_string, dval);

     j     = 0;
     ncolt = 0;

     headlent = (int)strlen(dval);

     for(i=0; i<headlent; ++i)
     {
        if (dval[i] == '\\')
           break;

        if (dval[i] == '\n')
           break;

        else if (dval[i] == '|')
        {
           ++ncolt;

           if(ncolt > MTBL_MAXCOL)
          return(MTBL_COLUMN);

           j = 0;

           if(i == 0)
          --ncolt;
        }

        else if (dval[i] != ' ' )
        {
           tbl_rec[ncolt].type[j] = dval[i];
           ++j;
        }
     }
      }

      else if(!haveUnit)
      {
     if(dval[strlen(dval) - 1] == '\n')
        dval[strlen(dval) - 1]  = '\0';

     if(dval[strlen(dval) - 1] == '\r')
        dval[strlen(dval) - 1]  = '\0';

     haveUnit = 1;
     strcpy(tbl_uni_string, dval);

     j     = 0;
     ncolt = 0;

     headlent = (int)strlen(dval);

     for(i=0; i<headlent; ++i)
     {
        if (dval[i] == '\\')
           break;

        if (dval[i] == '\n')
           break;

        else if (dval[i] == '|')
        {
           ++ncolt;

           if(ncolt > MTBL_MAXCOL)
          return(MTBL_COLUMN);

           j = 0;

           if(i == 0)
          --ncolt;
        }

        else if (dval[i] != ' ' )
        {
           tbl_rec[ncolt].unit[j] = dval[i];
           ++j;
        }
     }
      }

      else if(!haveNull)
      {
     if(dval[strlen(dval) - 1] == '\n')
        dval[strlen(dval) - 1]  = '\0';

     if(dval[strlen(dval) - 1] == '\r')
        dval[strlen(dval) - 1]  = '\0';

     haveNull = 1;
     strcpy(tbl_nul_string, dval);

     j     = 0;
     ncolt = 0;

     headlent = (int)strlen(dval);

     for(i=0; i<headlent; ++i)
     {
        if (dval[i] == '\\')
           break;

        if (dval[i] == '\n')
           break;

        else if (dval[i] == '|')
        {
           ++ncolt;

           if(ncolt > MTBL_MAXCOL)
          return(MTBL_COLUMN);

           j = 0;

           if(i == 0)
          --ncolt;
        }

        else if (dval[i] != ' ' )
        {
           tbl_rec[ncolt].nuls[j] = dval[i];
           ++j;
        }
     }
      }
   }


   /* Read the first data line to get record length */

   if(fgets(dval, mtbl_maxline, tfile) != (char *) NULL)
   {
      if(tdebug)
      {
     printf("TDEBUG> Read data line [%s]<br>\n", dval);
     fflush(stdout);
      }

      reclen = (int)strlen(dval);
   }


   if(tdebug)
   {
       printf("TDEBUG> tbl_hdr_string = [%s]<br>\n", tbl_hdr_string);
       printf("TDEBUG> tbl_typ_string = [%s]<br>\n", tbl_typ_string);
       printf("TDEBUG> tbl_uni_string = [%s]<br>\n", tbl_uni_string);
       printf("TDEBUG> tbl_nul_string = [%s]<br>\n", tbl_nul_string);
       printf("TDEBUG> firsrt record  = [%s](%d)<br>\n", dval, reclen);
       fflush(stdout);

       for (i=0; i<ncol; ++i)
       {
          printf("<br>\n");
          printf("TDEBUG> Column %d:<br>\n",    i+1);
          printf("TDEBUG> name   = [%s]<br>\n", tbl_rec[i].name);
          printf("TDEBUG> type   = [%s]<br>\n", tbl_rec[i].type);
          printf("TDEBUG> unit   = [%s]<br>\n", tbl_rec[i].unit);
          printf("TDEBUG> endcol =  %d<br>\n",  tbl_rec[i].endcol);
          printf("TDEBUG> colwd  =  %d<br>\n",  tbl_rec[i].colwd);
          fflush(stdout);
       }
   }


   /* Estimate the number of records in the file */
   /*  (correct for fixed-record table files)    */

   if(stat(fname, &buf) != 0)
      nrec = -1;

   if(reclen > 0)
      nrec = (buf.st_size - headbytes) / reclen;
   else
      nrec = -1;


   /* Reset pointer and exit */

   tseek(0);

   tbl_headbytes = headbytes;
   tbl_reclen    = reclen;

   return(ncol);
}
コード例 #2
0
ファイル: tsdatt.c プロジェクト: ArielleBassanelli/gempak
void tsdatt ( char *pname, float *xsz, float *ysz, 
		int *ileft, int *ibot, int *iright, int *itop,
		int *numclr, int *iret )
/************************************************************************
 * tsdatt								*
 * 									*
 * This subroutine defines the device attributes.			*
 * 									*
 * tsdatt ( pname, xsz, ysz, ileft, ibot, iright, itop,	numclr, iret )	*
 *									*
 * Input parameters:							*
 *	*pname		char		Name of file as output		*
 *	*xsz		float		X size in inches or pixels	*
 *	*ysz		float		Y size in inches or pixels	*
 *									*
 * Output parameters:							*
 *	*ileft		int		Left device coordinate		*
 *	*ibot		int		Bottom device coordinate	*
 *	*iright		int		Right device coordinate		*
 *	*itop		int		Top device coordinate		*
 *	*numclr		int		Max number of colors for device	*
 * 	*iret		int		Return code			*
 **									*
 * Log:									*
 * S. Jacobs/NCEP	12/98						*
 * R. Tian/SAIC         05/02   Modified to display FAX as image        *
 * T. Piper/SAIC	02/04	Removed lenf parameter			*
 ***********************************************************************/
{

	int	ier;
	char	tmpfil[133], pnuc[133], descr[41];
	int	newfile = 0;

/*---------------------------------------------------------------------*/
	*iret = G_NORMAL;

/*
 *      Save the file name.
 */
	cst_lcuc ( pname, pnuc, &ier );
	sprintf ( tmpfil, "%s.tiff", pnuc );

	if  ( strcmp ( filnam, tmpfil ) != 0 )  {
/*
 *	    If the passed in filename is different from the global filename,
 *	    change the name after closing the old file.
 */
	    tclosp ( &ier );
/*
 *	    Find the requested product in the FAX product
 *	    definition table.
 */
	    ctb_tiff ( pnuc, descr, &kbit, &klin, &krot, &ier );
	    if  ( ier != G_NORMAL )  {
		*iret = G_NOPROD;
		return;
	    }
	    newfile = 1;
	}
	else if ( strncmp ( pnuc, "FAX", 3 ) == 0 ) {
/*
 *          There is no entry for FAX product in the TIFF product
 *          definition table. The size is passed in.
 */
	    tclosp ( &ier );

            kbit = (int)(*xsz);
            klin = (int)(*ysz);
            krot = 0;
	    newfile = 1;
	}

	if ( newfile ) {
/*
 *	    Make sure that there are enough bytes per raster line. If the
 *	    number of bits is not divisible by 8 then add enough bits to
 *	    make the number divisible by 8.
 */
	    if  ( kbit % 8 != 0 )  {
		kbit = kbit + (8 - kbit%8);
	    }

/*
 *	    Compute the number of bytes for this raster image.
 *	    If the size is larger than the maximum, return with an error.
 */
	    msize = (kbit/8) * klin;

	    if  ( msize > MAXSIZ )  {
		*iret = G_NIDSIZ;
		return;
	    }

/*
 *	    Clear the entire image.
 */
	    tclear ( &ier );

/*
 *	    Set the device bounds.
 */
	    if  ( ( krot == 0 ) || ( krot == 180 ) )  {
		*ileft  = 1;
		*ibot   = klin;
		*iright = kbit;
		*itop   = 1;
	    }
	    else if  ( ( krot == 90 ) || ( krot == 270 ) )  {
		*ileft  = 1;
		*ibot   = kbit;
		*iright = klin;
		*itop   = 1;
	    }

/*
 *	    Set file to initially closed.
 */
	    opnfil = G_FALSE;

/*
 *	    If the new file name is not empty, set the current file name.
 */
	    if  ( tmpfil[0] != CHNULL )  {
                strcpy ( filnam, tmpfil );
		*iret = G_NEWWIN;
	    }


/*
 *	    Set the number of colors to be returned to DEVCHR.
 *
 *	    nncolr (numclr) = number of device colors
 *		( A maximum of MXCLNM = 32 may be initialized. )
 */
	    nncolr  = 1;
	    *numclr = nncolr;

	}

}