home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / F4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  6.9 KB  |  349 lines

  1.  
  2. /* f4.c  Field Module   
  3.    (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  4. */
  5.  
  6. #include "d4base.h"
  7. #include <string.h>
  8.  
  9. extern  BASE  *v4base ;
  10. extern  int    v4cur_base ;
  11.  
  12.  
  13. /*  Returns a pointer to a field name. */
  14.  
  15. char * f4name( field_ref )
  16. long       field_ref ;
  17. {
  18.    BASE  *base_ptr  ;
  19.    int    f_num, b_ref ;
  20.  
  21.    b_ref    =  (int) (field_ref>>16) ;
  22.    if ( b_ref < 0 )  return (char *) 0 ;
  23.  
  24.    base_ptr =  v4base +  b_ref ;
  25.    f_num    =  (int) (field_ref & 0xFFFF) ;
  26.  
  27.    if ( f_num < 0  || f_num >= base_ptr->num_fields )
  28.     return( (char *) 0 ) ;
  29.  
  30.    return(  base_ptr->fields[f_num].name ) ;
  31. }
  32.  
  33.  
  34. /* f4value   
  35.  
  36.    Returns the value of the corresponding field as a double.
  37.    Only defined for 'Numeric' fields and 'Character' fields
  38.    containing numeric data.
  39. */
  40.  
  41.  
  42. double f4value( field_ref)
  43. long        field_ref ;
  44. {
  45.    return( c4atod( f4ptr(field_ref), f4width(field_ref))  ) ;
  46. }
  47.  
  48. /* f4ptr  
  49.  
  50.    Returns a pointer to the corresponding field
  51. */
  52.  
  53. char * f4ptr( field_ref)
  54. long      field_ref ;
  55. {
  56.    BASE  *base_ptr  ;
  57.    int    f_num, b_ref ;
  58.  
  59.    b_ref    =  (int) (field_ref>>16) ;
  60.    if (b_ref < 0 ) return( (char *) 0 ) ;
  61.  
  62.    base_ptr =  v4base +  b_ref ;
  63.    f_num    =  (int) (field_ref & 0xFFFF) ;
  64.  
  65.    if ( f_num >= base_ptr->num_fields  || f_num < 0)
  66.     return( (char *) 0 ) ;
  67.  
  68.    return( base_ptr->buffer + base_ptr->fields[f_num].offset ) ;
  69. }
  70.  
  71.  
  72. /* f4record
  73.  
  74.    Return a pointer to the start of the database buffer   
  75. */
  76.  
  77. void *  f4record()
  78. {
  79.    if ( v4cur_base < 0 )  return ( (void *) 0 ) ;
  80.    return ( (void *) v4base[v4cur_base].buffer ) ;
  81. }
  82.  
  83. int  f4record_width()
  84. {
  85.    if ( v4cur_base < 0 )  return ( -1 ) ;
  86.    return ( v4base[v4cur_base].buffer_len ) ;
  87. }
  88.  
  89. /* f4str
  90.  
  91.    Returns a pointer to static string corresponding to the field.
  92.    This string will end in a NULL character.
  93. */
  94.  
  95. static  char   buffer[258] ;
  96.  
  97.  
  98. char * f4str( field_ref)
  99. long      field_ref ;
  100. {
  101.    int width ;
  102.  
  103.    width =  f4width( field_ref ) ;
  104.    if (width < 0)  return (char *) 0 ;
  105.    if (width > 256) width = 256 ;
  106.    memcpy( buffer, f4ptr( field_ref) , width ) ;
  107.  
  108.    buffer[width] = '\0' ;
  109.  
  110.    return( buffer) ;
  111. }
  112.  
  113. /* f4decimals 
  114.  
  115.    Returns a pointer to the corresponding field
  116. */
  117.  
  118. f4decimals( field_ref)
  119. long    field_ref ;
  120. {
  121.    BASE  *base_ptr  ;
  122.    int    f_num, b_ref ;
  123.  
  124.    b_ref =  (int) (field_ref>>16) ;
  125.    if ( b_ref < 0 )  return -1 ;
  126.  
  127.    base_ptr =  v4base + b_ref ;
  128.    f_num    =  (int) (field_ref & 0xFFFF) ;
  129.  
  130.    if ( f_num >= base_ptr->num_fields  || f_num < 0)
  131.     return( -1 ) ;
  132.  
  133.    return( base_ptr->fields[f_num].decimals ) ;
  134. }
  135.  
  136. /* f4width   
  137.  
  138.    Returns a pointer to the corresponding field
  139. */
  140.  
  141. f4width( field_ref)
  142. long     field_ref ;
  143. {
  144.    BASE  *base_ptr ;
  145.    int    f_num, b_ref ;
  146.  
  147.    b_ref    =  (int) (field_ref>>16) ;
  148.    if ( b_ref < 0 )  return -1 ;
  149.  
  150.    base_ptr =  v4base +  b_ref ;
  151.    f_num    =  (int) (field_ref & 0xFFFF) ;
  152.  
  153.    if ( f_num >= base_ptr->num_fields  || f_num < 0)
  154.     return( -1 ) ;
  155.  
  156.    return( base_ptr->fields[f_num].width ) ;
  157. }
  158.  
  159. /* f4true   
  160.  
  161.    Returns a true or false.
  162.  
  163.       -1 on ERROR
  164.        0 on logical FALSE  or  numeric 0
  165.        1 otherwise
  166. */
  167.  
  168. f4true( field_ref)
  169. long    field_ref ;
  170. {
  171.    char char_value ;
  172.  
  173.    switch( f4type( field_ref)  )
  174.    {
  175.       case  'N':
  176.       case  'F':
  177.       case  'C':
  178.      if ( f4value( field_ref) == 0.0 )
  179.         return( 0 ) ;
  180.      else
  181.         return( 1 ) ;
  182.  
  183.       case  'L':
  184.      char_value =  *f4ptr( field_ref ) ;
  185.      if ( char_value == 'Y'  ||  char_value == 'y'  ||
  186.           char_value == 'T'  ||  char_value == 't'  ||
  187.           char_value == '1')
  188.           return( 1 ) ;
  189.      else
  190.           return( 0 ) ;
  191.    }
  192.    return( -1 ) ;
  193. }
  194.  
  195. /* f4type   
  196.  
  197.    Returns a pointer to the corresponding field
  198. */
  199.  
  200. char f4type( field_ref)
  201. long    field_ref ;
  202. {
  203.    BASE  *base_ptr  ;
  204.    int    f_num, b_ref ;
  205.  
  206.    b_ref    =  (int) (field_ref >> 16) ;
  207.    if (b_ref < 0 ) return( '\0' ) ;
  208.  
  209.    base_ptr =  v4base +  b_ref ;
  210.    f_num    =  (int) (field_ref & 0xFFFF) ;
  211.  
  212.    if ( f_num >= base_ptr->num_fields  || f_num < 0)
  213.     return( '\0' ) ;
  214.  
  215.    return( base_ptr->fields[f_num].type ) ;
  216. }
  217.  
  218. /* f4replace
  219.  
  220.    Assigns a value to a field.
  221. */
  222.  
  223. int f4replace( field_ref, ptr ) 
  224. long   field_ref ;
  225. void  *ptr ;
  226. {
  227.    int     width, len ;
  228.    double  doub_val ;
  229.    char   *f_ptr, *static_ptr, f_type ;
  230.  
  231.    if ( ((char *) ptr) == ((char *) 0) )  return( -4 ) ;
  232.  
  233.    width =  f4width( field_ref ) ;
  234.    f_ptr =  f4ptr(   field_ref ) ;
  235.  
  236.    f_type =  f4type( field_ref ) ;
  237.  
  238.    if ( f_type != 'N' && f_type != 'F' ) {
  239.       len   =  strlen( (char *) ptr ) ;
  240.       if ( len > width )  len =  width ;
  241.    }
  242.  
  243.    switch( f_type )
  244.    {
  245.       case 'C':
  246.      memset( f_ptr, (int) ' ', (size_t) width ) ;
  247.      memcpy( f_ptr, (char *) ptr, len ) ;
  248.      break ;
  249.  
  250.       case 'D':
  251.      memset( f_ptr, (int) ' ', (size_t) width ) ;
  252.      if ( len != 8 )  return( -2 ) ;  /* Illegal Date */
  253.      if (c4dt_index((char *) ptr, &doub_val) < 0)  return( -2 ) ;
  254.      memcpy( f_ptr, (char *) ptr, 8 ) ;
  255.      break ;
  256.  
  257.       case 'N':
  258.       case 'F':
  259.      static_ptr = c4dtoa( *((double*) ptr), width, f4decimals(field_ref) ) ;
  260.      memcpy( f_ptr, static_ptr, width ) ;
  261.      if ( *f_ptr == '*' )  return( -3 ) ;
  262.      break ;
  263.  
  264.       case 'L':
  265.      if ( *((int *)ptr) )
  266.         f_ptr[0] = 'Y' ;
  267.      else
  268.         f_ptr[0] = 'N' ;
  269.      break ;
  270.  
  271.       default:
  272.      return( -5 ) ;
  273.    }
  274.    return( 0 ) ;
  275. }
  276.  
  277. /*  f4ref   
  278.  
  279.     returns
  280.        Normal:  The field reference number corresponding to the field name.
  281.        Error:   -1;  The field name was not located.
  282. */
  283.  
  284. long  f4ref( field_name )
  285. char *field_name ;
  286. {
  287.    int    field_on, len ;
  288.    FIELD *field_ptr ;
  289.    BASE  *base_ptr  ;
  290.    char   name[11]  ;
  291.  
  292.    base_ptr =  v4base +  v4cur_base ;
  293.  
  294.    memcpy( name, field_name, 11) ;
  295.    name[10] = '\0' ;
  296.    strupr( name )  ;
  297.  
  298.    len = 0 ;
  299.    while ( name[len] != ' '  &&  name[len] != '\0'  && len < 10)
  300.        len ++ ;
  301.  
  302.    if ( len == 0)  return( -1) ;
  303.  
  304.    field_ptr =  base_ptr->fields ;
  305.  
  306.    for ( field_on=0;  field_on< base_ptr->num_fields; field_on++)
  307.    {
  308.       if ( memcmp( name, field_ptr->name, len) == 0)
  309.       {
  310.      if ( field_ptr->name[len] == ' ' || field_ptr->name[len] == '\0' ||  len == 10)
  311.           return( ((long)field_on) | (((long)v4cur_base)<<16) ) ;
  312.       }
  313.       field_ptr++ ;
  314.    }
  315.  
  316.    return( -1 ) ;
  317. }
  318.  
  319. /*  f4j_ref 
  320.  
  321.     Returns
  322.  
  323.        Normal:  The field reference number corresponding to the j'th field.
  324.        Error:   -1;  Bad Parameter
  325. */
  326.  
  327. long  f4j_ref( j_ref )
  328. int     j_ref ;
  329. {
  330.    if ( j_ref >  f4num_fields() ||  j_ref < 1  )  
  331.       return( -1L ) ;
  332.    else
  333.       return( (((long) v4cur_base) << 16)  |   (long) (j_ref-1) ) ;
  334. }
  335.  
  336. /* f4num_fields 
  337.  
  338.    Returns the number of fields in the selected database.
  339. */
  340.  
  341. int f4num_fields()
  342. {
  343.    if ( v4cur_base >= 0 ) 
  344.       return( v4base[v4cur_base].num_fields ) ;
  345.    else
  346.       return( -1 ) ;
  347. }
  348.  
  349.