home *** CD-ROM | disk | FTP | other *** search
-
- /* d4more.c (c)Copyright Sequiter Software Inc., 1989. All rights reserved.
-
- Many beginner Code Base 4 users have difficulty with the
- routine 'f4replace'. Consequently, we have
- created a set of routines which are easier to use and
- simpler to understand. The source code to these routines
- can be used as an example of how other Code Base 4 routines
- are used.
-
- Following the routine definitions is a program which demonstrates
- how to use these routines. In addition, this program shows
- how to use multiple database files, multiple index files,
- and 'd4seek'.
-
- Routine Name Quick Description
-
- f4replace_double Assigns a 'double' to a field.
- f4replace_int Assigns a 'int' to a field.
- f4replace_long Assigns a 'long' to a field.
- f4replace_char Assigns a 'char' to a field.
- f4replace_str Copies a character array to a field.
-
- f4ncpy Copies up to 'n' bytes of a field
- to a character array.
- f4double Returns a field's value as a 'double'.
- f4int Returns a field's value as an 'int'.
- f4long Returns a field's value as a 'long'.
- f4char Returns a field's value as a 'char'.
- */
-
- #include "d4base.h"
- #include "w4.h"
-
- #include <string.h>
-
- void f4ncpy( long, char *, int ) ;
- double f4double( long ) ;
- int f4int( long ) ;
- long f4long( long ) ;
- int f4char( long ) ;
-
- void f4replace_double( long, double ) ;
- void f4replace_long( long, long ) ;
- void f4replace_int( long, int ) ;
- void f4replace_char( long, char ) ;
- void f4replace_str( long, char * ) ;
-
-
- void f4ncpy( f_ref, ptr, n )
- long f_ref ;
- char *ptr ;
- int n ;
- {
- /* 'f4ptr' returns a pointer to the field within the database record buffer. */
- strncpy( ptr, f4ptr(f_ref), n ) ;
- }
-
- double f4double( f_ref )
- long f_ref ;
- {
- /* Convert the field data into a 'double' */
- return( c4atod( f4ptr(f_ref), f4width(f_ref)) ) ;
- }
-
- int f4int( f_ref )
- long f_ref ;
- {
- /* Convert the field data into a 'int' */
- return( c4atoi( f4ptr(f_ref), f4width(f_ref)) ) ;
- }
-
- long f4long( f_ref )
- long f_ref ;
- {
- /* Convert the field data into a 'long' */
- return( c4atol( f4ptr(f_ref), f4width(f_ref)) ) ;
- }
-
- int f4char( f_ref )
- long f_ref ;
- {
- /* Return the first character of the record buffer. */
- return( *f4ptr(f_ref) ) ;
- }
-
- void f4replace_double( f_ref, d_value )
- long f_ref ;
- double d_value ;
- {
- char *ptr ;
-
- /* Convert the 'double' to ASCII and then copy it into the record buffer. */
- ptr = c4dtoa( d_value, f4width(f_ref), f4decimals(f_ref) ) ;
- memcpy( f4ptr(f_ref), ptr, f4width(f_ref) ) ;
- }
-
- void f4replace_long( f_ref, l_value )
- long f_ref ;
- long l_value ;
- {
- f4replace_double( f_ref, (double) l_value ) ;
- }
-
- void f4replace_int( f_ref, i_value )
- long f_ref ;
- int i_value ;
- {
- f4replace_double( f_ref, (double) i_value ) ;
- }
-
- void f4replace_char( f_ref, chr )
- long f_ref ;
- char chr ;
- {
- char *ptr ;
-
- /* Blank the record buffer and then insert the first character. */
- ptr = f4ptr(f_ref) ;
- memset( ptr, (int) ' ', f4width(f_ref) ) ;
- *ptr = chr ;
- }
-
- void f4replace_str( f_ref, str )
- long f_ref ;
- char *str ;
- {
- char *f_ptr ;
- int f_len, copy_bytes ;
-
- f_ptr = f4ptr(f_ref) ;
- f_len = f4width(f_ref) ;
- copy_bytes = strlen(str) ;
-
- if ( copy_bytes > f_len ) copy_bytes = f_len ;
-
- /* Copy the data into the record buffer. */
- memcpy( f_ptr, str, copy_bytes ) ;
-
- /* Make the rest of the field blank. */
- memset( f_ptr+copy_bytes, (int) ' ', f_len-copy_bytes ) ;
- }
-
-
- /* Example */
-
- static FIELD all_fields[] =
- {
- { "NAME", 'C', 20, 0, 0 },
- { "SINGLE", 'L', 1, 0, 0 }, /* Logicals always have a width of 1 */
- { "HEIGHT", 'N', 8, 2, 0 },
- { "BIRTH_DATE", 'D', 8, 0, 0 }, /* Dates always have a width of 8 */
- } ;
-
- static FIELD some_fields[] =
- {
- { "BIRTH_DATE", 'D', 8, 0, 0 }, /* Dates always have a width of 8 */
- { "HEIGHT", 'N', 10, 4, 0 },
- } ;
-
- typedef struct /* Define the data to copy into the database. */
- {
- char name_data[15] ;
- double height_data ;
- char birth_data[8] ;
- char single_data ;
- } DATA ;
-
- static DATA field_data[] =
- {
- { "FRED", 5.8, "19600228", 'T' },
- { "JOHN", 6.3, "19621220", 'F' },
- { "HARRY", 5.2, "19450715", 'T' },
- } ;
-
-
- main()
- {
- int all_ref, some_ref, i, i_birth_ref, i_height_ref, rc ;
-
- long all_name, all_height, all_birth_date, all_single ;
- long some_height, some_birth_date ;
-
- double d ;
-
- /* Create 'd4all.dbf' and assign field reference numbers. */
- all_ref = d4create( "D4ALL", 4, all_fields, 0 ) ;
- all_name = f4ref( "NAME" ) ;
- all_height = f4ref( "HEIGHT" ) ;
- all_birth_date= f4ref( "BIRTH_DATE" ) ;
- all_single = f4ref( "SINGLE" ) ;
-
- /* Create 'd4some.dbf' and assign field reference numbers. */
- some_ref = d4create( "D4SOME", 2, some_fields, 0 ) ;
- some_height = f4ref( "HEIGHT" ) ;
- some_birth_date= f4ref( "BIRTH_DATE" ) ;
-
- d4select( all_ref ) ; /* Select database 'd4some.dbf'. */
-
- /* Add 4 records to 'd4some.dbf'. Get the data from 'field_data'. */
- for ( i=0; i< 3; i++ )
- {
- /* These routines do not care what the type of the destination field
- is. They are only concerned with the type of the source data. */
-
- f4replace_str( all_name, field_data[i].name_data ) ;
- f4replace_double( all_height, field_data[i].height_data ) ;
- f4replace_str( all_birth_date, field_data[i].birth_data ) ;
- f4replace_char( all_single, field_data[i].single_data ) ;
-
- d4write(0L) ; /* Append the record buffer to disk. */
- }
-
- /* List the new records. */
- x4list() ;
- w4(w4row()+1,0, "Press a key." ) ;
- g4char() ;
-
- /* Create two index files for 'd4all.dbf' */
- i_birth_ref = i4index( "I4BIRTH", "BIRTH_DATE", 0, 0 ) ;
- i_height_ref = i4index( "I4HEIGHT", "HEIGHT", 0, 0 ) ;
-
- i4select( i_birth_ref ) ; /* Select the 'i4birth' index file. */
-
- /* Skip through 'd4all.dbf' using birth date order. */
- for ( rc = d4top(); rc != 3; rc = d4skip(1L) )
- {
- /* Copy two of the fields to database 'd4some.dbf' */
- f4replace_double( some_height, f4double(all_height) ) ;
- f4replace_str( some_birth_date, f4str(all_birth_date) ) ;
-
- d4select(some_ref) ;
- d4write(0L) ; /* Append a record to 'd4some.dbf'. */
-
- d4select(all_ref) ; /* Reselect 'd4all.dbf'. */
- }
-
- /* List the new data in 'd4some.dbf' */
- d4select( some_ref ) ;
- x4list() ;
- w4( w4row()+1,0, "Press a key." ) ;
- g4char() ;
-
- /* Select 'd4all.dbf' to do some searching with routine 'd4seek'. */
- d4select( all_ref ) ;
-
- i4select( i_birth_ref ) ;
- d4seek( "19500101" ) ; /* Represent dates as character strings for 'd4seek' */
- w4display( " First birthdate, 1950 or later ", f4str(all_name), (char *) 0 ) ;
-
- i4select( i_height_ref ) ;
-
- /* When searching using numeric keys, 'd4seek' expects a pointer to a 'double'. */
- d = 6.0 ;
- d4seek( &d ) ;
-
- w4display( " First height. Six Feet or Taller ", f4str(all_height), (char *) 0 ) ;
-
- d4close_all() ; /* Close all the database and index files. */
-
- w4exit(0) ;
- }
-