예제 #1
0
long int VpfWrite ( void *from, VpfDataType type, long int count, FILE *to )
{
  long int retval=0 , i ;

  switch ( type ) {
  case VpfChar:
    retval = (long)fwrite ( from, sizeof (char), count, to ) ;
    break ;
  case VpfShort:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	short int stemp ,
		*sptr = (short *) from ;
	for ( i=0; i < count; i++, sptr++ ) {
	   swap_two ( (char*)sptr, (char*)&stemp ) ;
	   retval = (long)fwrite ( &stemp, sizeof (short), 1, to ) ;
	}
      } else {
	retval = (long)fwrite ( from, sizeof (short), count, to ) ;
      }
    }
    break ;
  case VpfInteger:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	 long int itemp,
	   *iptr = (long int *) from ;
	 for ( i=0; i < count; i++, iptr++ ) {
	   swap_four ( (char*)iptr, (char*)&itemp ) ;
	   retval = (long)fwrite ( &itemp, sizeof (long int), 1, to ) ;
	 }
      } else {
	 retval = (long)fwrite ( from, sizeof (long int), count, to ) ;
      }
    }
    break ;
  case VpfFloat:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	 float ftemp ,
	    *fptr = (float *) from ;
	 for ( i=0; i < count; i++, fptr++ ) {
	   swap_four ( (char*)fptr, (char*)&ftemp ) ;
	   retval = (long)fwrite ( &ftemp, sizeof (float), 1, to ) ;
	 }
      } else {
	 retval = (long)fwrite ( from, sizeof (long int), count, to ) ;
      }
    }
    break ;
  case VpfDouble:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	 double dtemp ,
	     *dptr = (double *) from ;
	 for ( i=0; i < count; i++, dptr++ ) {
	   swap_eight ( (char*)dptr, (char*)&dtemp ) ;
	   retval = (long)fwrite ( &dtemp, sizeof (double), 1, to ) ;
	 }
      } else {
	 retval = (long)fwrite ( from, sizeof (double), count, to ) ;
      }
    }
    break ;
  case VpfDate:	/* only write out 20, not 21 chars */
    retval = (long)fwrite ( from, sizeof ( date_type ) - 1, count, to ) ;
    break ;
  case VpfCoordinate:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	 coordinate_type ctemp ,
		      *cptr = (coordinate_type *) from ;
	 for ( i=0; i < count; i++, cptr++ ) {
	   swap_four ( (char*)&cptr->x, (char*)&ctemp.x ) ;
	   swap_four ( (char*)&cptr->y, (char*)&ctemp.y ) ;
	   retval = (long)fwrite ( &ctemp, sizeof (coordinate_type), 1, to ) ;
	 }
      } else {
	 retval = (long)fwrite ( from, sizeof (coordinate_type), count, to ) ;
      }
    }
    break ;
  case VpfDoubleCoordinate:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	 double_coordinate_type dctemp ,
			     *dcptr = (double_coordinate_type *) from ;
	 for ( i=0; i < count; i++, dcptr++ ) {
	   swap_eight ( (char*)&dcptr->x, (char*)&dctemp.x ) ;
	   swap_eight ( (char*)&dcptr->y, (char*)&dctemp.y ) ;
	   retval = (long)fwrite ( &dctemp, sizeof (double_coordinate_type),
	                     1, to ) ;
	 }
      } else {
	 retval = (long)fwrite ( from, sizeof (double_coordinate_type),
			   count, to ) ;
      }
    }
    break ;
  case VpfTriCoordinate:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	tri_coordinate_type ttemp ,
			  *tptr = (tri_coordinate_type *) from ;
	for ( i=0; i < count; i++, tptr++ ) {
	   swap_four ( (char*)&tptr->x, (char*)&ttemp.x ) ;
	   swap_four ( (char*)&tptr->y, (char*)&ttemp.y ) ;
	   swap_four ( (char*)&tptr->z, (char*)&ttemp.z ) ;
	   retval = (long)fwrite ( &ttemp, sizeof (tri_coordinate_type), 1, to ) ;
	}
      } else {
	retval = (long)fwrite ( from, sizeof (tri_coordinate_type), count, to ) ;
      }
    }
    break ;
  case VpfDoubleTriCoordinate:
    {
      if (MACHINE_BYTE_ORDER != STORAGE_BYTE_ORDER) {
	double_tri_coordinate_type dttemp ,
		    *dtptr = (double_tri_coordinate_type *) from ;
	for ( i=0; i < count; i++, dtptr++ ) {
	   swap_eight ( (char*)&dtptr->x, (char*)&dttemp.x ) ;
	   swap_eight ( (char*)&dtptr->y, (char*)&dttemp.y ) ;
	   swap_eight ( (char*)&dtptr->z, (char*)&dttemp.z ) ;
	   retval = (long)fwrite ( &dttemp,sizeof (double_tri_coordinate_type),
			     1, to);
	}
      } else {
	retval = (long)fwrite ( from,sizeof (double_tri_coordinate_type),
			  count, to);
      }
    }
    break ;
  case VpfNull:
    /* Do Nothing */
    break ;
  default:
    break ;
  }

  return retval;
}
예제 #2
0
/*************************************************************************
 *
 *N  parse_data_def
 *
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Purpose:
 *P
 *     This function parses a table's data definition and creates a header
 *     in memory that is associated with the table.
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Parameters:
 *A
 *    table <inout> == (vpf_table_type *) vpf table structure.
 *    ddlen <input> == (ossim_int32) length of the table's data definition.
 *
 *    return value is the record length if all items are fixed length, or
 *    -1 if the record contains variable length items
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   History:
 *H
 *    Barry Michaels   April 1991                        DOS Turbo C
 *    Mody Buchbinder  May 1991 - Modified for new table header.
 *    Dave Flinn       July 1991 - updated for UNIX
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   External Variables:
 *X
 *    None
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Functions Called:
 *F
 *    void *vpfmalloc()                                  VPFMISC.C
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Portability:
 *O
 *    This module should be ANSI C compatible.
 *E
 *************************************************************************/
ossim_int32 parse_data_def( vpf_table_type *table )
{
   register ossim_int32 n,i;
   ossim_int32 p, k;
   char *buf,*des,*nar,*vdt, *tdx, *doc, byte ;/*temporary storage */
   char end_of_rec;
   int status;
   ossim_int32 ddlen;
   ossim_int32 reclen = 0;
#if UNIX
   /* just for backward compat check */
   ossim_int32 bkcompat = 1 ;
#endif

   if ( table->mode == Read ) {
     fread(&ddlen,sizeof(ddlen),1,table->fp);

     /* Check the next byte to see if the byte order is specified */
     fread(&byte,1,1,table->fp);
     p=0;
     table->byte_order = LEAST_SIGNIFICANT; /* default */
     switch (toupper(byte)) {
	case 'L':
	   p++;
	   break;
	case 'M':
	   table->byte_order = MOST_SIGNIFICANT;
	   p++;
	   break;
     }
     if (MACHINE_BYTE_ORDER != table->byte_order) {
	k = ddlen;
	swap_four((char *)&k,(char *)&ddlen);
     }
     if ( ddlen < 0 ) {
       return (ossim_int32)0 ;
     }

     STORAGE_BYTE_ORDER = table->byte_order;

     /* header without first 4 bytes */
     table->ddlen = ddlen + sizeof (ossim_int32) ;
     buf = (char *)vpfmalloc((ddlen+3)*sizeof(char));
     buf[0] = byte; /* already have the first byte of the buffer */
     Read_Vpf_Char(&buf[1],table->fp,ddlen-1) ;
   } else {
     table->ddlen = (long)strlen ( table->defstr ) ;
     ddlen = table->ddlen ;
     buf = (char *)vpfmalloc((ddlen+3)*sizeof(char));
     strncpy ( buf, table->defstr, ddlen ) ;
     p=0;
     table->byte_order = LEAST_SIGNIFICANT; /* default */
     byte = buf[0];
     switch (toupper(byte)) {
	case 'L':
	   p++;
	   break;
	case 'M':
	   table->byte_order = MOST_SIGNIFICANT;
	   p++;
	   break;
     }
     STORAGE_BYTE_ORDER = table->byte_order;
   }

   buf[ddlen-1] = '\0'; /* mark end of string for reading functions */
   if ( buf[p] == ';' )
     p++; /* buf[p] is semi-colon */
   des = get_string(&p,buf,COMPONENT_SEPERATOR );
   strncpy(table->description,des,80);
   free(des);
   nar = get_string(&p,buf,COMPONENT_SEPERATOR );
   strncpy(table->narrative ,nar,12);
   free(nar);
   n = 0 ;
   /* get number of fields */
   for (i=p; i < ddlen;i++)
     if ( buf[i] == LINE_CONTINUE )
       i++ ;	/* skip past line continue, and next character */
     else if (buf[i] == END_OF_FIELD ) 		/* Found end of field */
	n++;					/* increment nfields */
     else if (buf[i] == COMMENT )		/* skip past comments */
       while ( buf[i] != LINE_CONTINUE &&
	       buf[i] != END_OF_FIELD &&
	       buf[i] != '\0')
	 i++ ;					/* increment i */

   table->nfields = n ;
   table->header = (header_type)vpfmalloc((n+1)*sizeof(header_cell));

   for(i=0;i<n;i++) {
     end_of_rec = FALSE;
     table->header[i].name  = get_string(&p,buf, FIELD_COUNT);  /*****/
     rightjust(table->header[i].name);
     table->header[i].type  = toupper(vpf_get_char  (&p,buf));
     table->header[i].count = get_number(&p,buf,FIELD_SEPERATOR );

     if ( i == 0 )
       if ( ossim_strcasecmp ( table->header[0].name, "ID" ) ) {
        free(buf);
	      return (ossim_int32)0 ;
       }

     if(table->header[i].count == -1)
       reclen = -1;			/* set reclen to variable len flag */

     /* Now set null values and add up record length, if fixed length */

     status = 0;

     switch (table->header[i].type) {
     case 'I':
       if ( reclen >= 0 )
	 reclen += (sizeof(ossim_int32)*table->header[i].count);
       table->header[i].nullval.Int = NULLINT ;
       break;
     case 'S':
       if ( reclen >= 0 )
	 reclen += (sizeof(short int)*table->header[i].count);
       table->header[i].nullval.Short = NULLSHORT ;
       break;
     case 'F':
       if ( reclen >= 0 )
	 reclen += (sizeof(float)*table->header[i].count);
       table->header[i].nullval.Float = NULLFLOAT ;
       break;
     case 'R':
       if ( reclen >= 0 )
	 reclen += (sizeof(double)*table->header[i].count);
       table->header[i].nullval.Double = NULLDOUBLE ;
       break;
     case 'T':
       if ( reclen >= 0 ) { 		/* if fixed length */
	 reclen += (sizeof(char)*table->header[i].count);
	 table->header[i].nullval.Char =
	   (char *) vpfmalloc ( table->header[i].count + 1 ) ;
	 for ( k=0; k < table->header[i].count; k++ )
	   table->header[i].nullval.Char[k] = NULLCHAR ;
	 table->header[i].nullval.Char[k] = '\0';
       } else {			/* variable length */
	 table->header[i].nullval.Char =
	   (char *) vpfmalloc ( VARIABLE_STRING_NULL_LENGTH + 1 ) ;
	 for ( k=0; k < VARIABLE_STRING_NULL_LENGTH ; k++ )
	   table->header[i].nullval.Char[k] = NULLCHAR ;
	 table->header[i].nullval.Char[k] = '\0';
       }
       break;
     case 'C':
       if ( reclen >= 0 )
	 reclen += (sizeof(coordinate_type)*table->header[i].count);
       table->header[i].nullval.Other = '\0';
       break;
     case 'Z':
       if ( reclen >= 0 )
	 reclen += (sizeof(tri_coordinate_type)*table->header[i].count);
       table->header[i].nullval.Other = '\0' ;
       break;
     case 'B':
       if ( reclen >= 0 )
	 reclen += (sizeof(double_coordinate_type)*table->header[i].count);
       table->header[i].nullval.Other = '\0' ;
       break;
     case 'Y':
       if ( reclen >= 0 )
	 reclen +=
	   (sizeof(double_tri_coordinate_type)*table->header[i].count);
       table->header[i].nullval.Other ='\0';
       break;
     case 'D':
       if ( reclen >= 0 )
	 reclen += ((sizeof(date_type)-1)*table->header[i].count);
       strcpy ( table->header[i].nullval.Date, NULLDATE ) ;
       break;
     case 'K':
       reclen = -1;
       table->header[i].nullval.Other = '\0' ;
       break;
     case 'X':
       /* do nothing */
       table->header[i].nullval.Other = '\0' ;
       break ;
     default:
       status = 1;
       break ;
     } /** switch type **/

     if (status)
     {
        free(buf);
        return (ossim_int32)0;
     } 

     table->header[i].keytype     = vpf_get_char  (&p,buf);
     des = get_string(&p,buf, FIELD_SEPERATOR );
     rightjust(des);
     strncpy(table->header[i].description,des,80);
     free(des);
     vdt = get_string(&p,buf, FIELD_SEPERATOR );
     strncpy(table->header[i].vdt,vdt,12);
     free(vdt);
#if UNIX
     /* This is for backward compatability qc checking */
     if ( bkcompat && buf[p] == END_OF_FIELD ) {
       bkcompat= 0 ;
     }
#endif
     tdx = get_string(&p,buf, FIELD_SEPERATOR ) ;
     if ( ! strcmp ( tdx, "" ) ) {
       table->header[i].tdx = (char *) NULL ;
       if (buf[p] == ':')
	 end_of_rec = TRUE;
     } else {
       if (strcmp(tdx,"-") != 0) {
	  table->header[i].tdx =(char*) vpfmalloc ( (unsigned long)strlen ( tdx ) +1 ) ;
	  strcpy (table->header[i].tdx, tdx );
       } else table->header[i].tdx = (char *)NULL;
     }
     free(tdx);
     if (!end_of_rec) {
	doc = get_string(&p,buf, FIELD_SEPERATOR ) ;
	if ( ! strcmp ( doc, "" ) ) {
	  table->header[i].narrative = (char *) NULL ;
	  end_of_rec = TRUE;
	} else {
	  if (strcmp(doc,"-") != 0) {
	     table->header[i].narrative = (char*)vpfmalloc ( (unsigned long)strlen(doc) +1) ;
	     strcpy (table->header[i].narrative, doc );
	  } else table->header[i].narrative = (char *)NULL;
	}
	free(doc);
     } else table->header[i].narrative = (char *)NULL;
     p += 1; /** eat semicolon **/
    }
   free(buf);
   return reclen;
}
예제 #3
0
ossim_int32 VpfRead ( void *to, VpfDataType type, ossim_int32 count, FILE *from )
{
  ossim_int32 retval=0 , i ;

  switch ( type ) {
  case VpfChar:
    retval = (long)fread ( to, sizeof (char), count, from ) ;
    break ;
  case VpfShort:
    {
      short int stemp ,
                *sptr = (short *) to ;
      for ( i=0; i < count; i++ ) {
	retval = (long)fread ( &stemp, sizeof (short), 1, from ) ;
	if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER)
	   swap_two ( (char*)&stemp, (char*)sptr ) ;
	else
	   *sptr = stemp;
        sptr++ ;
      }
    }  
    break ;
  case VpfInteger:
    {
      if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER) {
	ossim_int32 itemp,
	  *iptr = (ossim_int32 *) to ;
	for ( i=0; i < count; i++ ) {
	  retval = (long)fread ( &itemp, sizeof (ossim_int32), 1, from ) ;
	  swap_four ( (char*)&itemp, (char*)iptr ) ;
	  iptr++ ;
	}
      } else {
	retval = (long)fread ( to, sizeof (ossim_int32), count, from ) ;
      }
    }  
    break ;
  case VpfFloat:
    {
      float ftemp ,
            *fptr = (float *) to ;
      for ( i=0; i < count; i++ ) {
        retval = (long)fread ( &ftemp, sizeof (float), 1, from ) ;
	if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER)
	   swap_four ( (char*)&ftemp, (char*)fptr ) ;
	else
	   *fptr = ftemp;
	fptr++ ;
      }
    }
    break ;
  case VpfDouble:
    {
      double dtemp ,
             *dptr = (double *) to ;
      for ( i=0; i < count; i++ ) {
        retval = (long)fread ( &dtemp, sizeof (double), 1, from ) ;
	if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER)
	   swap_eight ( (char*)&dtemp, (char*)dptr ) ;
	else
	   *dptr = dtemp;
	dptr++ ;
      }
    }
    break ;
  case VpfDate:
    {
      date_type *dp = (date_type *) to ;
      retval = (long)fread(dp,sizeof(date_type)-1,count,from);
    }
    break ;
  case VpfCoordinate:
    {
      if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER) {
	 coordinate_type ctemp ,
		      *cptr = (coordinate_type *) to ;
	 for ( i=0; i < count; i++ ) {
	   retval = (long)fread ( &ctemp, sizeof (coordinate_type), 1, from ) ;
	   swap_four ( (char*)&ctemp.x, (char*)&cptr->x ) ;
	   swap_four ( (char*)&ctemp.y, (char*)&cptr->y ) ;
	   cptr++ ;
	 }
      } else {
	 retval = (long)fread ( to, sizeof (coordinate_type), count, from ) ;
      }
    }  
    break ;
  case VpfDoubleCoordinate:
    {
      double_coordinate_type dctemp ,
                             *dcptr = (double_coordinate_type *) to ;
      for ( i=0; i < count; i++ ) {
        retval = (long)fread ( &dctemp, sizeof (double_coordinate_type), 1, from ) ;
	if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER) {
	   swap_eight ( (char*)&dctemp.x, (char*)&dcptr->x ) ;
	   swap_eight ( (char*)&dctemp.y, (char*)&dcptr->y ) ;
	} else {
	   dcptr->x = dctemp.x;
	   dcptr->y = dctemp.y;
	}
	dcptr++ ;
      }
    }
    break ;
  case VpfTriCoordinate:
    {
      tri_coordinate_type ttemp ,
                          *tptr = (tri_coordinate_type *) to ;
      for ( i=0; i < count; i++ ) {
        retval = (long)fread ( &ttemp, sizeof (tri_coordinate_type), 1, from ) ;
	if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER) {
	   swap_four ( (char*)&ttemp.x, (char*)&tptr->x ) ;
	   swap_four ( (char*)&ttemp.y, (char*)&tptr->y ) ;
	   swap_four ( (char*)&ttemp.z, (char*)&tptr->z ) ;
	} else {
	   tptr->x = ttemp.x;
	   tptr->y = ttemp.y;
	   tptr->z = ttemp.z;
	}
	tptr++ ;
      }
    }
    break ;
  case VpfDoubleTriCoordinate:
    {
      double_tri_coordinate_type dttemp ,
                                 *dtptr = (double_tri_coordinate_type *) to ;
      for ( i=0; i < count; i++ ) {
        retval = (long)fread ( &dttemp, sizeof (double_tri_coordinate_type), 1, from);
	if (STORAGE_BYTE_ORDER != MACHINE_BYTE_ORDER) {
	   swap_eight ( (char*)&dttemp.x, (char*)&dtptr->x ) ;
	   swap_eight ( (char*)&dttemp.y, (char*)&dtptr->y ) ;
	   swap_eight ( (char*)&dttemp.z, (char*)&dtptr->z ) ;
	} else {
	   dtptr->x = dttemp.x;
	   dtptr->y = dttemp.y;
	   dtptr->z = dttemp.z;
	}
	dtptr++ ;
      }
    }  
    break ;
  case VpfNull:
    /* Do Nothing */
    break ;
  default:
    break ;
  }   /* end of switch */

  return retval ;       /* whatever fread returns */

}