home *** CD-ROM | disk | FTP | other *** search
-
- /* f4.c Field Module
- (c)Copyright Sequiter Software Inc., 1987, 1988, 1989. All rights reserved.
- */
-
- #include "d4base.h"
- #include <string.h>
-
- extern BASE *v4base ;
- extern int v4cur_base ;
-
-
- /* Returns a pointer to a field name. */
-
- char * f4name( field_ref )
- long field_ref ;
- {
- BASE *base_ptr ;
- int f_num, b_ref ;
-
- b_ref = (int) (field_ref>>16) ;
- if ( b_ref < 0 ) return (char *) 0 ;
-
- base_ptr = v4base + b_ref ;
- f_num = (int) (field_ref & 0xFFFF) ;
-
- if ( f_num < 0 || f_num >= base_ptr->num_fields )
- return( (char *) 0 ) ;
-
- return( base_ptr->fields[f_num].name ) ;
- }
-
-
- /* f4value
-
- Returns the value of the corresponding field as a double.
- Only defined for 'Numeric' fields and 'Character' fields
- containing numeric data.
- */
-
-
- double f4value( field_ref)
- long field_ref ;
- {
- return( c4atod( f4ptr(field_ref), f4width(field_ref)) ) ;
- }
-
- /* f4ptr
-
- Returns a pointer to the corresponding field
- */
-
- char * f4ptr( field_ref)
- long field_ref ;
- {
- BASE *base_ptr ;
- int f_num, b_ref ;
-
- b_ref = (int) (field_ref>>16) ;
- if (b_ref < 0 ) return( (char *) 0 ) ;
-
- base_ptr = v4base + b_ref ;
- f_num = (int) (field_ref & 0xFFFF) ;
-
- if ( f_num >= base_ptr->num_fields || f_num < 0)
- return( (char *) 0 ) ;
-
- return( base_ptr->buffer + base_ptr->fields[f_num].offset ) ;
- }
-
-
- /* f4record
-
- Return a pointer to the start of the database buffer
- */
-
- void * f4record()
- {
- if ( v4cur_base < 0 ) return ( (void *) 0 ) ;
- return ( (void *) v4base[v4cur_base].buffer ) ;
- }
-
- int f4record_width()
- {
- if ( v4cur_base < 0 ) return ( -1 ) ;
- return ( v4base[v4cur_base].buffer_len ) ;
- }
-
- /* f4str
-
- Returns a pointer to static string corresponding to the field.
- This string will end in a NULL character.
- */
-
- static char buffer[258] ;
-
-
- char * f4str( field_ref)
- long field_ref ;
- {
- int width ;
-
- width = f4width( field_ref ) ;
- if (width < 0) return (char *) 0 ;
- if (width > 256) width = 256 ;
- memcpy( buffer, f4ptr( field_ref) , width ) ;
-
- buffer[width] = '\0' ;
-
- return( buffer) ;
- }
-
- /* f4decimals
-
- Returns a pointer to the corresponding field
- */
-
- f4decimals( field_ref)
- long field_ref ;
- {
- BASE *base_ptr ;
- int f_num, b_ref ;
-
- b_ref = (int) (field_ref>>16) ;
- if ( b_ref < 0 ) return -1 ;
-
- base_ptr = v4base + b_ref ;
- f_num = (int) (field_ref & 0xFFFF) ;
-
- if ( f_num >= base_ptr->num_fields || f_num < 0)
- return( -1 ) ;
-
- return( base_ptr->fields[f_num].decimals ) ;
- }
-
- /* f4width
-
- Returns a pointer to the corresponding field
- */
-
- f4width( field_ref)
- long field_ref ;
- {
- BASE *base_ptr ;
- int f_num, b_ref ;
-
- b_ref = (int) (field_ref>>16) ;
- if ( b_ref < 0 ) return -1 ;
-
- base_ptr = v4base + b_ref ;
- f_num = (int) (field_ref & 0xFFFF) ;
-
- if ( f_num >= base_ptr->num_fields || f_num < 0)
- return( -1 ) ;
-
- return( base_ptr->fields[f_num].width ) ;
- }
-
- /* f4true
-
- Returns a true or false.
-
- -1 on ERROR
- 0 on logical FALSE or numeric 0
- 1 otherwise
- */
-
- f4true( field_ref)
- long field_ref ;
- {
- char char_value ;
-
- switch( f4type( field_ref) )
- {
- case 'N':
- case 'F':
- case 'C':
- if ( f4value( field_ref) == 0.0 )
- return( 0 ) ;
- else
- return( 1 ) ;
-
- case 'L':
- char_value = *f4ptr( field_ref ) ;
- if ( char_value == 'Y' || char_value == 'y' ||
- char_value == 'T' || char_value == 't' ||
- char_value == '1')
- return( 1 ) ;
- else
- return( 0 ) ;
- }
- return( -1 ) ;
- }
-
- /* f4type
-
- Returns a pointer to the corresponding field
- */
-
- char f4type( field_ref)
- long field_ref ;
- {
- BASE *base_ptr ;
- int f_num, b_ref ;
-
- b_ref = (int) (field_ref >> 16) ;
- if (b_ref < 0 ) return( '\0' ) ;
-
- base_ptr = v4base + b_ref ;
- f_num = (int) (field_ref & 0xFFFF) ;
-
- if ( f_num >= base_ptr->num_fields || f_num < 0)
- return( '\0' ) ;
-
- return( base_ptr->fields[f_num].type ) ;
- }
-
- /* f4replace
-
- Assigns a value to a field.
- */
-
- int f4replace( field_ref, ptr )
- long field_ref ;
- void *ptr ;
- {
- int width, len ;
- double doub_val ;
- char *f_ptr, *static_ptr, f_type ;
-
- if ( ((char *) ptr) == ((char *) 0) ) return( -4 ) ;
-
- width = f4width( field_ref ) ;
- f_ptr = f4ptr( field_ref ) ;
-
- f_type = f4type( field_ref ) ;
-
- if ( f_type != 'N' && f_type != 'F' ) {
- len = strlen( (char *) ptr ) ;
- if ( len > width ) len = width ;
- }
-
- switch( f_type )
- {
- case 'C':
- memset( f_ptr, (int) ' ', (size_t) width ) ;
- memcpy( f_ptr, (char *) ptr, len ) ;
- break ;
-
- case 'D':
- memset( f_ptr, (int) ' ', (size_t) width ) ;
- if ( len != 8 ) return( -2 ) ; /* Illegal Date */
- if (c4dt_index((char *) ptr, &doub_val) < 0) return( -2 ) ;
- memcpy( f_ptr, (char *) ptr, 8 ) ;
- break ;
-
- case 'N':
- case 'F':
- static_ptr = c4dtoa( *((double*) ptr), width, f4decimals(field_ref) ) ;
- memcpy( f_ptr, static_ptr, width ) ;
- if ( *f_ptr == '*' ) return( -3 ) ;
- break ;
-
- case 'L':
- if ( *((int *)ptr) )
- f_ptr[0] = 'Y' ;
- else
- f_ptr[0] = 'N' ;
- break ;
-
- default:
- return( -5 ) ;
- }
- return( 0 ) ;
- }
-
- /* f4ref
-
- returns
- Normal: The field reference number corresponding to the field name.
- Error: -1; The field name was not located.
- */
-
- long f4ref( field_name )
- char *field_name ;
- {
- int field_on, len ;
- FIELD *field_ptr ;
- BASE *base_ptr ;
- char name[11] ;
-
- base_ptr = v4base + v4cur_base ;
-
- memcpy( name, field_name, 11) ;
- name[10] = '\0' ;
- strupr( name ) ;
-
- len = 0 ;
- while ( name[len] != ' ' && name[len] != '\0' && len < 10)
- len ++ ;
-
- if ( len == 0) return( -1) ;
-
- field_ptr = base_ptr->fields ;
-
- for ( field_on=0; field_on< base_ptr->num_fields; field_on++)
- {
- if ( memcmp( name, field_ptr->name, len) == 0)
- {
- if ( field_ptr->name[len] == ' ' || field_ptr->name[len] == '\0' || len == 10)
- return( ((long)field_on) | (((long)v4cur_base)<<16) ) ;
- }
- field_ptr++ ;
- }
-
- return( -1 ) ;
- }
-
- /* f4j_ref
-
- Returns
-
- Normal: The field reference number corresponding to the j'th field.
- Error: -1; Bad Parameter
- */
-
- long f4j_ref( j_ref )
- int j_ref ;
- {
- if ( j_ref > f4num_fields() || j_ref < 1 )
- return( -1L ) ;
- else
- return( (((long) v4cur_base) << 16) | (long) (j_ref-1) ) ;
- }
-
- /* f4num_fields
-
- Returns the number of fields in the selected database.
- */
-
- int f4num_fields()
- {
- if ( v4cur_base >= 0 )
- return( v4base[v4cur_base].num_fields ) ;
- else
- return( -1 ) ;
- }
-