home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Collection - Online Library - January 1996 / CKITOS2196.ISO / diskette / gg244090.dsk / unc.dsk / CHAPTER.10 / DB_MANAG.C < prev    next >
C/C++ Source or Header  |  1993-10-28  |  6KB  |  145 lines

  1. /*****************************************************************************/
  2. /* Module: database_manager.c                                                */
  3. /*                                                                           */
  4. /* Description:                                                              */
  5. /*    This module implements the following RPC operations:                   */
  6. /*                                                                           */
  7. /*             db_open         establishes the connection to a database      */
  8. /*             db_close        detaches the connection                       */
  9. /*             db_read         reads a record from the current position      */
  10. /*             db_setpos       sets the current position                     */
  11. /*             db_getpos       gets the current position                     */
  12. /*                                                                           */
  13. /*    This module also maintains the client's context that is defined by     */
  14. /*    type db_context_t, and therefore implements the rundown procedure      */
  15. /*    "db_context_t_rundown()"                                               */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18. #include "db.h"
  19. #include "errorchk.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. static char database[][ MAX_DB_DATA_SIZE ] = {
  25.    "Almeida, Marcio Cravo De",
  26.    "Gottschalk, Klaus",
  27.    "Miyamoto, Toshiro",
  28.    "Villela, Agostinho De Arruda",
  29.    "Silva, Nelson Alves da ",
  30.    "ITSC, Austin ITSC "
  31. };
  32.  
  33. #define  MAX_DB_RECORDS  sizeof( database ) / MAX_DB_DATA_SIZE
  34. #define  LAST_DB_RECORD  MAX_DB_RECORDS - 1
  35.  
  36. typedef struct _db_client_context_t {
  37.    long  pos;
  38. } db_client_context_t;
  39.  
  40. /*****************************************************************************/
  41. /* Operation: db_open                                                        */
  42. /*****************************************************************************/
  43. void db_open(
  44.       handle_t         binding_h,
  45.       db_context_t    *pcontext_h,
  46.       error_status_t  *pst )
  47. {
  48.    db_client_context_t *pclient_context;
  49.  
  50.    pclient_context = (db_client_context_t *) malloc(
  51.                              sizeof( db_client_context_t ) );
  52.    if ( pclient_context == NULL ) {
  53.       *pcontext_h = NULL;
  54.       *pst = rpc_s_fault_remote_no_memory;
  55.    } else {
  56.       pclient_context->pos = 0;
  57.       *pcontext_h = (db_context_t) pclient_context;
  58.       *pst = rpc_s_ok;
  59.       printf( "db_open:    Context handle = %08X\n", pclient_context );
  60.    }
  61. }
  62.  
  63. /*****************************************************************************/
  64. /* Operation: db_close                                                       */
  65. /*****************************************************************************/
  66. void db_close(
  67.       db_context_t    *pcontext_h,
  68.       error_status_t  *pst )
  69. {
  70.    printf( "db_close:   Context handle = %08X\n", *pcontext_h );
  71.    free( *pcontext_h );
  72.    *pcontext_h = NULL;
  73.    *pst = rpc_s_ok;
  74. }
  75.  
  76. /*****************************************************************************/
  77. /* Operation: db_read                                                        */
  78. /*****************************************************************************/
  79. void db_read(
  80.       db_context_t     context_h,
  81.       db_data_t       *pdata,
  82.       long            *ppos,
  83.       error_status_t  *pst )
  84. {
  85.    db_client_context_t *pclient_context = (db_client_context_t *) context_h;
  86.  
  87.    (*pdata).size = strlen( database[ pclient_context->pos ] ) + 1;
  88.    strcpy( (*pdata).data, database[ pclient_context->pos ] );
  89.    if ( pclient_context->pos < LAST_DB_RECORD )
  90.       pclient_context->pos++;
  91.    else
  92.       pclient_context->pos = LAST_DB_RECORD;
  93.    *ppos = pclient_context->pos;
  94.    printf( "db_read:    New position for context %08X = %ld\n",
  95.             pclient_context, pclient_context->pos );
  96.    *pst = rpc_s_ok;
  97. }
  98.  
  99. /*****************************************************************************/
  100. /* Operation: db_setpos                                                      */
  101. /*****************************************************************************/
  102. void db_setpos(
  103.       db_context_t     context_h,
  104.       long            *ppos,
  105.       error_status_t  *pst )
  106. {
  107.    db_client_context_t *pclient_context = (db_client_context_t *) context_h;
  108.  
  109.    if ( *ppos < MAX_DB_RECORDS )
  110.       pclient_context->pos = *ppos;
  111.    else
  112.       pclient_context->pos = LAST_DB_RECORD ;
  113.  
  114.    printf( "db_setpos:  New position for context %08X = %ld\n",
  115.             pclient_context, pclient_context->pos );
  116.    *pst = rpc_s_ok;
  117. }
  118.  
  119. /*****************************************************************************/
  120. /* Operation: db_getpos                                                      */
  121. /*****************************************************************************/
  122. void db_getpos(
  123.       db_context_t     context_h,
  124.       long            *ppos,
  125.       error_status_t  *pst )
  126. {
  127.    db_client_context_t *pclient_context = (db_client_context_t *) context_h;
  128.  
  129.    *ppos = pclient_context->pos;
  130.    printf( "db_getpos   New position for context %08X = %ld\n",
  131.             pclient_context, pclient_context->pos );
  132.    *pst = rpc_s_ok;
  133. }
  134.  
  135. /*****************************************************************************/
  136. /* Operation: db_context_t_rundown                                            */
  137. /*****************************************************************************/
  138. void db_context_t_rundown(
  139.       db_context_t     context_h )
  140. {
  141.    printf( "db_context_rundown: Context handle = %08X\n", context_h );
  142.    free( context_h );
  143. }
  144.  
  145.