home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / itcapr91.arj / DOGGIE.C < prev    next >
Text File  |  1991-09-07  |  4KB  |  101 lines

  1. /************************************************************************
  2. *    DOGGIE.C - The access functions for the DOGGIE database.            *
  3. *    To compile, see DOGDATA.C                                            *
  4. ************************************************************************/
  5.  
  6. #include <string.h>
  7. #include "doggie.h"
  8.  
  9. #define MAX_REC        10
  10. #define MAX_NAME    16
  11. #define NUM_AWARDS    0x000F
  12. #define CUST_NUM    0x0FFF
  13.  
  14. /************************************************************************
  15. *    Internal definition of the database                                    *
  16. ************************************************************************/
  17. struct DumbDogDefinition
  18.     {
  19.     unsigned CustNum;            /* 0..11 = Customer #, 12-15 = # dogs    */
  20.     char LName[MAX_NAME];        /* Customers' last name, reversed        */
  21.     unsigned char OtherInfo;    /* bits 0-3 hold the # of awards earned    */
  22.     } DumbDogData[MAX_REC] =    /* Array holding doggie records            */
  23.     {
  24.         { 0xF12D, "nosirraH",  11 },    { 0x4326, "nonneL",         9 },
  25.         { 0x0CE3, "rratS",        0 },    { 0xC810, "yentraCcM",    12 },
  26.         { 0x24FF, "eromliG",    1 },    { 0x82E4, "notpalC",     7 },
  27.         { 0xA769, "iaV",        4 },    { 0x0129, "doowniW",     0 },
  28.         { 0x1169, "appaZ",        3 },    { 0x0989, "snilloC",     0 }
  29.     };
  30. static int RecNum;                /* Current record number in the array    */
  31.  
  32. /************************************************************************
  33. *    Access functions                                                    *
  34. ************************************************************************/
  35.  
  36. /*----------
  37.  * InitializeDoggieData - initializes the doggie database
  38.  * OUT:        0 if successful, -1 on failure
  39.  */
  40. int InitializeDoggieData(void)
  41.     { return RecNum=0; }                /* Set last record read            */
  42.  
  43. /*----------
  44.  * CleanupAfterDoggies - tells information cluster that the
  45.  * application has no use for any more doggies.
  46.  * OUT:        0 if successful, -1 on failure
  47.  */
  48. int CleanupAfterDoggies(void)
  49.     { return 0; }                        /* We just can't fail...        */
  50.  
  51. /*----------
  52.  * StartDoggieSearch - tells the database that we're preparing
  53.  * to start a search.
  54.  * OUT:        0 on success, -1 on failure
  55.  */
  56. int StartDoggieSearch(void)
  57.     { return RecNum=0; }                /* Point to the first record    */
  58.  
  59. /*----------
  60.  * GetNextRecord - return the next record in the doggie database
  61.  * OUT:        -1 if no more records, record number otherwise
  62.  */
  63. int GetNextRecord(void)
  64.     { return (++RecNum < MAX_REC) ? RecNum : -1; }
  65.  
  66. /*----------
  67.  * GetNextDogLover - scan the database for the next person who
  68.  * owns the specified number of dogs (or more).
  69.  * OUT:        Record number if successful, -1 on failure
  70.  */
  71. int GetNextDogLover(int minDogs)
  72.     {
  73.     int numDoggies;
  74.     for (RecNum+=1; RecNum < MAX_REC; RecNum++)
  75.         {
  76.         numDoggies = DumbDogData[RecNum].CustNum>>12;
  77.         if ( numDoggies >= minDogs)
  78.             return RecNum;
  79.         }
  80.     return -1;                                /* No more doggie lovers    */
  81.     }
  82.  
  83. /*----------
  84.  * GetDoggieData - return information from specified record
  85.  * INP:        recordNum - the record to retrieve data from
  86.  *            userRec - buffer to put data into
  87.  * OUT:        0 on success, -1 on failure
  88.  */
  89. int GetDoggieData(int recNo, struct DoggieDef *usrRec)
  90.     {
  91.     /* get customer name & remove security jumbling from it        */
  92.     strncpy(usrRec->CustName,DumbDogData[recNo].LName,MAX_NAME-1);
  93.     usrRec->CustName[MAX_NAME-1]=0;            /* Ensure that string ends    */
  94.     strrev(usrRec->CustName);                /* Reverse string in place    */
  95.  
  96.     usrRec->CustNum   =DumbDogData[recNo].CustNum & CUST_NUM;
  97.     usrRec->NumAwards =DumbDogData[recNo].OtherInfo&NUM_AWARDS;
  98.     usrRec->NumDoggies=DumbDogData[recNo].CustNum>>12;
  99.     return 0;
  100.     }
  101.