/*********************************************************************
  d4exmemo.c   (c)Copyright Sequiter Software Inc., 1991-1993.
  All rights reserved.

  This example program creates and places data into a database.     
  The contents are then displayed using the selected tags.

  *********************************************************************/

#include "d4all.h"

#ifdef __TURBOC__
#pragma hdrstop                     /* use pre-defined headers */
#endif

#ifdef __TURBOC__                      /* if Borland C is the compiler */
extern unsigned _stklen = 10000 ;   /* set stack length to 10000 */
#endif

CODE4   cb ;                          /* initialize global variables */
DATA4  *data ;
FIELD4 *f_name, *l_name, *grade, *student_id, *birthdate, *will_pass, *notes ;
TAG4 *name, *class_list ;

static FIELD4INFO field_info[] =     /* define database field structure */
{
  /* field name,   type, width, decimals */
{ "F_NAME",      'C',   17,     0 },
{ "L_NAME",      'C',   17,     0 },
{ "GRADE",       'N',    5,     2 },
{ "STUDENT_ID",  'N',    6,     0 },
{ "BIRTHDT",     'D',    8,     0 },
{ "WILL_PASS",   'L',    1,     0 },
{ "NOTES",       'M',   10,     0 },
{ 0, 0, 0, 0 },
} ;

static TAG4INFO tag_info[] =         /* define multiple tags */
{
  /* name,        expression,      filter, unique,           descending */
{ "NAME",       "L_NAME+F_NAME", "",    r4unique_continue,      0       },
{ "CLASS_LIST", "GRADE",         "",      0,               r4descending },
{ 0,  0,  0, 0, 0 },
};


void main()
{
  FIELD4 *field_ptr ;
  int  j ;
  double d ;
  char buffer[13] ;

  d4init( &cb ) ;
  cb.safety = 0 ;        /* will create new database, regardless of */
  /* it's current existence  */
  printf("Creating Database. . .\n") ;

  data = d4create( &cb, "EXAMPLE", field_info, tag_info ) ;

  f_name   =  d4field( data, "F_NAME" ) ;     /* assign pointers to fields */
  l_name   =  d4field( data, "L_NAME" ) ;
  grade    =  d4field( data, "GRADE" ) ;
  student_id  =  d4field( data, "STUDENT_ID" ) ;
  birthdate   =  d4field( data, "BIRTHDT" ) ;
  will_pass   =  d4field( data, "WILL_PASS" ) ;
  notes    =  d4field( data, "NOTES" ) ;

  d4append_start( data, 0 ) ;              /* append records */  
  f4assign( f_name , "Fred" ) ;
  f4assign( l_name , "Jones" ) ;
  d = 76.8 ;
  f4assign_double( grade , d ) ;
  f4assign_long( student_id, 164534L ) ;
  f4assign( birthdate, "19651012" ) ;
  f4assign_char( will_pass , 'N' ) ;
  f4memo_assign( notes , "Fred must study more, and be more attentive. \
            \n        His marks have been dropping, and his attendance \
            \n        is poor.  I recommend that you send a note home \
            \n        to his parents." ) ;
  d4append( data ) ;

  d4append_start( data, 0 ) ;
  f4assign( f_name , "Mary" ) ;
  f4assign( l_name , "Borgerson" ) ;
  d = 89.2 ;
  f4assign_double( grade , d ) ;
  f4assign_long( student_id, 145464L ) ;
  f4assign( birthdate, "19640821" ) ;
  f4assign_char( will_pass , 'Y' ) ;
  f4memo_assign( notes , "Mary is doing well." ) ;
  d4append( data ) ;

  d4append_start( data, 0 ) ;
  f4assign( f_name , "Larry" ) ;
  f4assign( l_name , "Smith" ) ;
  d = 45.4 ;
  f4assign_double( grade , d ) ;
  f4assign_long( student_id, 134578L ) ;
  f4assign( birthdate, "19650430" ) ;
  f4assign_char( will_pass , 'Y' ) ;
  f4memo_assign( notes , "Larry is going to be moving away.  Please \
            \n        forward his transcripts to the new school" ) ;
  d4append( data ) ;

  d4append_start( data, 0 ) ;
  f4assign( f_name , "Sara" ) ;
  f4assign( l_name , "Abbott" ) ;
  d = 54.0 ;
  f4assign_double( grade , d ) ;
  f4assign_long( student_id, 124344L ) ;
  f4assign( birthdate, "19641102" ) ;
  f4assign_char( will_pass , 'Y' ) ;
  f4memo_assign( notes , "Sara's parents have requested some further \
            \n        information on the special school programs. \
            \n        Please send that information out." ) ;
  d4append( data ) ;

  /* print database ordered by first tag */
  printf("\n\nThe listing of the Database ordered using last name,first name:\n");
  printf("\n  FIRST NAME         LAST NAME          GRADE   I.D.    BIRTHDATE     PASS?\n");
  printf(" -------------------------------------------------------------------------\n");

  d4tag_select( data , d4tag_default( data )) ;
  /* Loop through the entire data file. */
  for ( d4top( data ); ! d4eof( data ); d4skip( data, 1L ) )
  {
    printf( "\n" ) ;  /* Display the record on a new line. */

    /* Loop through every field */
    for ( j = 1; j < d4num_fields( data ); j++ )
    {
      switch( f4type( field_ptr = d4field_j( data, j ) ) )
      {

      case 'L':  /* if the logical field */
        if ( f4true( field_ptr ) )
          printf( "   YES" ) ; 
        else
          printf( "   NO" ) ; 
        break ;

      case 'D':  /* if the date field */
        date4format( f4memo_str( field_ptr), buffer, "MMM DD/CCYY" ) ;
        printf( "  %s", buffer ) ; /* Display the field. */
        break ;

      case 'N':  /* if a numeric field */
        printf( "  %s", f4memo_str( field_ptr ) ) ; /* Display the field. */
        if ( !memcmp( f4name( field_ptr ), "GRADE", 5 ) )
          printf( "\%" ) ; 
        break ;

      default:
        printf( "  %s", f4memo_str( field_ptr ) ) ; /* Display the field. */
        break ;
      }
    }
    field_ptr =  d4field_j( data, d4num_fields(data) ) ;
    printf( "\nNOTES:" ) ; 
    printf( "  %s\n", f4memo_str( field_ptr ) ) ; /* Display the memo field. */

  }

  d4close_all( &cb ) ;     /* close all database, index and/or memo files */
  d4init_undo( &cb ) ;     /* free up memory */
  mem4reset() ;            /* free up memory */

  printf("\nFINISHED\n") ;
}