/************************************************************************* * *N read_next_row * *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: *P * This function reads the next row of the table. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: *A * table <input> == (vpf_table_type) vpf table structure. * return <output> == (row_type) the next row in the table. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * History: *H * Barry Michaels May 1991 Original Version DOS Turbo C * Dave Flinn July 1991 Updated for UNIX *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * External Variables: *X * None *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Functions Called: *F * void *vpfmalloc() *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Portability: *O * This module should be ANSI C compatible. *E *************************************************************************/ row_type read_next_row( vpf_table_type table ) { register ossim_int32 i,j; ossim_int32 status; char *tptr; ossim_int32 size,count; row_type row; id_triplet_type * keys; coordinate_type dummycoord; if (feof(table.fp)) { return NULL; } STORAGE_BYTE_ORDER = table.byte_order; row = (row_type)vpfmalloc((table.nfields+1) * sizeof(column_type)); for (i=0;i<table.nfields;i++) row[i].ptr = NULL; for (i=0;i<table.nfields;i++) { if (table.header[i].count < 0) { Read_Vpf_Int (&count,table.fp,1) ; } else { count = table.header[i].count; } row[i].count = count; status = 0; switch (table.header[i].type) { case 'T': if (count == 1) { row[i].ptr = (char *)vpfmalloc(sizeof(char)); Read_Vpf_Char(row[i].ptr, table.fp, 1) ; } else { size = count*sizeof(char); row[i].ptr = (char *)vpfmalloc(size+2); tptr = (char *)vpfmalloc(size+2); Read_Vpf_Char(tptr,table.fp,count) ; tptr[count] = '\0'; strcpy((char*)row[i].ptr,(char*)tptr); free(tptr); } break; case 'I': row[i].ptr = (ossim_int32 *)vpfmalloc(count*sizeof(ossim_int32)); Read_Vpf_Int (row[i].ptr, table.fp, count ) ; break; case 'S': row[i].ptr = (short int *)vpfmalloc(count*sizeof(short int)); Read_Vpf_Short (row[i].ptr, table.fp, count ) ; break; case 'F': row[i].ptr = (float *)vpfmalloc(count*sizeof(float)); Read_Vpf_Float (row[i].ptr, table.fp, count ) ; break; case 'R': row[i].ptr = (double *)vpfmalloc(count*sizeof(double)); Read_Vpf_Double (row[i].ptr, table.fp, count ) ; break; case 'D': row[i].ptr = (date_type *)vpfmalloc(count*sizeof(date_type)); Read_Vpf_Date (row[i].ptr, table.fp, count ) ; break; case 'C': /* Coordinate strings may be quite large. */ /* Allow for null coordinate string pointer if */ /* not enough memory that can be handled one */ /* coordinate at a time in higher level functions. */ row[i].ptr = (coordinate_type *)malloc(count* sizeof(coordinate_type)); if (row[i].ptr) Read_Vpf_Coordinate(row[i].ptr,table.fp,count); else for (j=0;j<count;j++) Read_Vpf_Coordinate(&dummycoord,table.fp,1); break; case 'Z': row[i].ptr = (tri_coordinate_type *)vpfmalloc(count* sizeof(tri_coordinate_type)); Read_Vpf_CoordinateZ(row[i].ptr,table.fp,count); break; case 'B': row[i].ptr = (double_coordinate_type *)vpfmalloc(count* sizeof(double_coordinate_type)); Read_Vpf_DoubleCoordinate(row[i].ptr,table.fp,count); break; case 'Y': row[i].ptr = (double_tri_coordinate_type *)vpfmalloc(count* sizeof(double_tri_coordinate_type)); Read_Vpf_DoubleCoordinateZ(row[i].ptr,table.fp,count); break; case 'K': /* ID Triplet */ row[i].ptr = (id_triplet_type *)vpfmalloc(count* sizeof(id_triplet_type)); keys = (id_triplet_type *)vpfmalloc(count* sizeof(id_triplet_type)); for (j=0;j<count;j++) { keys[j] = read_key(table); } memcpy(row[i].ptr,keys,count*sizeof(id_triplet_type)); free(keys); break; case 'X': row[i].ptr = NULL; break; default: fprintf(stderr,"\n%s%s >>> read_next_row: no such type < %c >", table.path,table.name,table.header[i].type ) ; status = 1; break ; } /* end of switch */ if (status == 1) { free_row ( row, table ) ; return (row_type) NULL; } } return row; }
/************************************************************************* * *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; }
/************************************************************************* * *N read_key * *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: *P * This function reads an id triplet key from a VPF table. * The table must be open for read. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: *A * table <input> == (vpf_table_type) VPF table. * read_key <output> == (id_triplet_type) id triplet key. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * History: *H * Barry Michaels May 1991 Original Version DOS Turbo C * Dave Flinn July 1991 Updated for UNIX *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * External Variables: *X * None *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Functions Called: *F * None *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Portability: *O * This module should be ANSI C compatible. *E *************************************************************************/ id_triplet_type read_key( vpf_table_type table ) { id_triplet_type key; unsigned char ucval; unsigned short int uival; STORAGE_BYTE_ORDER = table.byte_order; key.id = 0L; key.tile = 0L; key.exid = 0L; /* just doing this to be consistent */ Read_Vpf_Char (&(key.type),table.fp,1); switch (TYPE0(key.type)) { case 0: break; case 1: Read_Vpf_Char (&ucval, table.fp, 1 ) ; key.id = (ossim_int32)ucval; break; case 2: Read_Vpf_Short (&uival, table.fp, 1 ) ; key.id = (ossim_int32)uival; break; case 3: Read_Vpf_Int (&(key.id), table.fp, 1 ) ; break; } switch (TYPE1(key.type)) { case 0: break; case 1: Read_Vpf_Char (&ucval, table.fp, 1 ) ; key.tile = (ossim_int32)ucval; break; case 2: Read_Vpf_Short (&uival, table.fp, 1 ) ; key.tile = (ossim_int32)uival; break; case 3: Read_Vpf_Int (&(key.tile), table.fp, 1 ) ; break; } switch (TYPE2(key.type)) { case 0: break; case 1: Read_Vpf_Char (&ucval, table.fp, 1 ) ; key.exid = (ossim_int32)ucval; break; case 2: Read_Vpf_Short (&uival, table.fp, 1 ) ; key.exid = (ossim_int32)uival; break; case 3: Read_Vpf_Int (&(key.exid), table.fp, 1 ) ; break; } return key; }