home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-4.ZIP / HDR_EXMP.ZIP / EXAMPLES.ZIP / D4MORE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-15  |  7.4 KB  |  263 lines

  1.  
  2. /*  d4more.c   (c)Copyright Sequiter Software Inc., 1989. All rights reserved.
  3.  
  4.    Many beginner Code Base 4 users have difficulty with the
  5.    routine 'f4replace'.  Consequently, we have
  6.    created a set of routines which are easier to use and
  7.    simpler to understand.  The source code to these routines
  8.    can be used as an example of how other Code Base 4 routines
  9.    are used.
  10.  
  11.    Following the routine definitions is a program which demonstrates
  12.    how to use these routines.  In addition, this program shows
  13.    how to use multiple database files, multiple index files,
  14.    and 'd4seek'.
  15.  
  16.        Routine Name            Quick Description             
  17.  
  18.        f4replace_double        Assigns a 'double' to a field.
  19.        f4replace_int           Assigns a 'int' to a field.
  20.        f4replace_long          Assigns a 'long' to a field.
  21.        f4replace_char          Assigns a 'char' to a field.
  22.        f4replace_str           Copies a character array to a field.
  23.  
  24.        f4ncpy                  Copies up to 'n' bytes of a field
  25.                                to a character array.
  26.        f4double                Returns a field's value as a 'double'.
  27.        f4int                   Returns a field's value as an 'int'.
  28.        f4long                  Returns a field's value as a 'long'.
  29.        f4char                  Returns a field's value as a 'char'.
  30. */
  31.  
  32. #include "d4base.h"
  33. #include "w4.h"
  34.  
  35. #include <string.h>
  36.  
  37. void   f4ncpy( long, char *, int ) ;
  38. double f4double( long ) ;
  39. int    f4int( long ) ;
  40. long   f4long( long ) ;
  41. int    f4char( long ) ;
  42.  
  43. void   f4replace_double( long, double ) ;
  44. void   f4replace_long( long, long ) ;
  45. void   f4replace_int( long, int ) ;
  46. void   f4replace_char( long, char ) ;
  47. void   f4replace_str( long, char * ) ;
  48.  
  49.  
  50. void  f4ncpy( f_ref, ptr, n )
  51. long  f_ref ;
  52. char *ptr ;
  53. int   n ;
  54. {
  55.    /* 'f4ptr' returns a pointer to the field within the database record buffer. */
  56.    strncpy( ptr, f4ptr(f_ref), n ) ;
  57. }
  58.  
  59. double  f4double( f_ref )
  60. long  f_ref ;
  61. {
  62.    /* Convert the field data into a 'double' */
  63.    return( c4atod( f4ptr(f_ref), f4width(f_ref))  ) ;
  64. }
  65.  
  66. int  f4int( f_ref )
  67. long  f_ref ;
  68. {
  69.    /* Convert the field data into a 'int' */
  70.    return( c4atoi( f4ptr(f_ref), f4width(f_ref))  ) ;
  71. }
  72.  
  73. long f4long( f_ref )
  74. long  f_ref ;
  75. {
  76.    /* Convert the field data into a 'long' */
  77.    return( c4atol( f4ptr(f_ref), f4width(f_ref))  ) ;
  78. }
  79.  
  80. int  f4char( f_ref )
  81. long  f_ref ;
  82. {
  83.    /* Return the first character of the record buffer. */
  84.    return( *f4ptr(f_ref) ) ;
  85. }
  86.  
  87. void f4replace_double( f_ref, d_value )
  88. long    f_ref ;
  89. double  d_value ;
  90. {
  91.    char *ptr ;
  92.  
  93.    /* Convert the 'double' to ASCII and then copy it into the record buffer. */
  94.    ptr =  c4dtoa( d_value, f4width(f_ref), f4decimals(f_ref) ) ;
  95.    memcpy( f4ptr(f_ref), ptr, f4width(f_ref) ) ;
  96. }
  97.  
  98. void f4replace_long( f_ref, l_value )
  99. long    f_ref ;
  100. long    l_value ;
  101. {
  102.   f4replace_double( f_ref, (double) l_value ) ;
  103. }
  104.  
  105. void f4replace_int( f_ref, i_value )
  106. long    f_ref ;
  107. int     i_value ;
  108. {
  109.   f4replace_double( f_ref, (double) i_value ) ;
  110. }
  111.  
  112. void f4replace_char( f_ref, chr )
  113. long    f_ref ;
  114. char    chr ;
  115. {
  116.    char *ptr ;
  117.  
  118.    /* Blank the record buffer and then insert the first character. */
  119.    ptr =  f4ptr(f_ref) ;
  120.    memset( ptr, (int) ' ', f4width(f_ref) ) ;
  121.    *ptr =  chr ;
  122. }
  123.  
  124. void  f4replace_str( f_ref, str )
  125. long  f_ref ;
  126. char *str ;
  127. {
  128.    char *f_ptr ;
  129.    int   f_len, copy_bytes ;
  130.  
  131.    f_ptr =  f4ptr(f_ref) ;
  132.    f_len =  f4width(f_ref) ;
  133.    copy_bytes =  strlen(str) ;
  134.  
  135.    if ( copy_bytes > f_len )  copy_bytes =  f_len ;
  136.  
  137.    /* Copy the data into the record buffer. */
  138.    memcpy( f_ptr, str, copy_bytes ) ;
  139.  
  140.    /* Make the rest of the field blank. */
  141.    memset( f_ptr+copy_bytes, (int) ' ', f_len-copy_bytes ) ;
  142. }
  143.  
  144.  
  145. /* Example */
  146.  
  147. static FIELD  all_fields[] =
  148. {
  149.    { "NAME",       'C', 20, 0, 0 },
  150.    { "SINGLE",     'L',  1, 0, 0 },  /* Logicals always have a width of 1 */
  151.    { "HEIGHT",     'N',  8, 2, 0 },
  152.    { "BIRTH_DATE", 'D',  8, 0, 0 },  /* Dates always have a width of 8 */
  153. } ;
  154.  
  155. static FIELD  some_fields[] =
  156. {
  157.    { "BIRTH_DATE", 'D',  8, 0, 0 },  /* Dates always have a width of 8 */
  158.    { "HEIGHT",     'N', 10, 4, 0 },
  159. } ;
  160.  
  161. typedef struct  /* Define the data to copy into the database. */
  162. {
  163.    char    name_data[15] ;
  164.    double  height_data ;
  165.    char    birth_data[8] ;
  166.    char    single_data ;
  167. }  DATA ;
  168.  
  169. static DATA  field_data[] =
  170. {
  171.    { "FRED",  5.8, "19600228", 'T' },
  172.    { "JOHN",  6.3, "19621220", 'F' },
  173.    { "HARRY", 5.2, "19450715", 'T' },
  174. } ;
  175.  
  176.  
  177. main()
  178. {
  179.    int    all_ref,  some_ref, i, i_birth_ref, i_height_ref, rc ;
  180.  
  181.    long   all_name,  all_height,  all_birth_date,  all_single ;
  182.    long   some_height, some_birth_date ;
  183.  
  184.    double d ;
  185.  
  186.    /* Create 'd4all.dbf' and assign field reference numbers. */
  187.    all_ref =  d4create( "D4ALL",  4, all_fields, 0 ) ;
  188.    all_name      =  f4ref( "NAME" ) ;
  189.    all_height    =  f4ref( "HEIGHT" ) ;
  190.    all_birth_date=  f4ref( "BIRTH_DATE" ) ;
  191.    all_single    =  f4ref( "SINGLE" ) ;
  192.  
  193.    /* Create 'd4some.dbf' and assign field reference numbers. */
  194.    some_ref =  d4create( "D4SOME", 2, some_fields, 0 ) ;
  195.    some_height    =  f4ref( "HEIGHT" ) ;
  196.    some_birth_date=  f4ref( "BIRTH_DATE" ) ;
  197.  
  198.    d4select( all_ref ) ;   /* Select database 'd4some.dbf'. */ 
  199.  
  200.    /* Add 4 records to 'd4some.dbf'.  Get the data from 'field_data'. */
  201.    for ( i=0; i< 3; i++ )
  202.    {
  203.       /* These routines do not care what the type of the destination field
  204.          is.  They are only concerned with the type of the source data. */
  205.  
  206.       f4replace_str( all_name, field_data[i].name_data ) ;
  207.       f4replace_double( all_height, field_data[i].height_data ) ;
  208.       f4replace_str( all_birth_date, field_data[i].birth_data ) ;
  209.       f4replace_char( all_single, field_data[i].single_data ) ;
  210.  
  211.       d4write(0L) ;  /* Append the record buffer to disk. */
  212.    }
  213.  
  214.    /* List the new records. */
  215.    x4list() ;
  216.    w4(w4row()+1,0, "Press a key." ) ;
  217.    g4char() ;
  218.  
  219.    /* Create two index files for 'd4all.dbf' */
  220.    i_birth_ref  =  i4index( "I4BIRTH",  "BIRTH_DATE", 0, 0 ) ;
  221.    i_height_ref =  i4index( "I4HEIGHT", "HEIGHT", 0, 0 ) ;
  222.  
  223.    i4select( i_birth_ref ) ;  /* Select the 'i4birth' index file. */
  224.  
  225.    /* Skip through 'd4all.dbf' using birth date order. */
  226.    for ( rc = d4top(); rc != 3; rc = d4skip(1L) )
  227.    {
  228.       /* Copy two of the fields to database 'd4some.dbf' */
  229.       f4replace_double( some_height,  f4double(all_height) ) ;
  230.       f4replace_str( some_birth_date, f4str(all_birth_date) ) ;
  231.  
  232.       d4select(some_ref) ;
  233.       d4write(0L) ;  /* Append a record to 'd4some.dbf'. */
  234.  
  235.       d4select(all_ref) ;  /* Reselect 'd4all.dbf'. */
  236.    }
  237.  
  238.    /* List the new data in 'd4some.dbf' */
  239.    d4select( some_ref ) ;
  240.    x4list() ;
  241.    w4( w4row()+1,0, "Press a key." ) ;
  242.    g4char() ;
  243.  
  244.    /* Select 'd4all.dbf' to do some searching with routine 'd4seek'. */
  245.    d4select( all_ref ) ;
  246.  
  247.    i4select( i_birth_ref ) ;
  248.    d4seek( "19500101" ) ;   /* Represent dates as character strings for 'd4seek' */
  249.    w4display( " First birthdate, 1950 or later ", f4str(all_name), (char *) 0 ) ;
  250.  
  251.    i4select( i_height_ref ) ;
  252.  
  253.    /* When searching using numeric keys, 'd4seek' expects a pointer to a 'double'. */
  254.    d =  6.0 ;     
  255.    d4seek( &d ) ;  
  256.  
  257.    w4display( " First height. Six Feet or Taller ", f4str(all_height), (char *) 0 ) ;
  258.  
  259.    d4close_all() ;  /* Close all the database and index files. */
  260.  
  261.    w4exit(0) ;
  262. }
  263.