home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MSJV5-1.ZIP / ISAM.ARC / FIG16.C < prev    next >
Text File  |  1989-12-11  |  3KB  |  107 lines

  1.  
  2.  
  3. /* Handle to isrequest struct, previously allocated from the
  4. nonbanked portion  of the  global heap. An appropriate place
  5. to allocate the request block is in isconnect. */
  6.  
  7. GLOBALHANDLE hRequestBlock;
  8.  
  9. /* Table  maintained by  the C  API that  holds  the  record
  10. length of all open ISAM files. Indexed by isfd of open file.
  11. The table  record length  is filled in the table when a file
  12. is opened or created. */
  13.  
  14. int iRecLenTbl[MAX_FILE_DESCRIPTORS];
  15.  
  16. /* Function to read a record from an ISAM file. */
  17.  
  18. int isread(
  19.     int    isfd,         /* fd  of  previously  opened  ISAM
  20. file */
  21.     char   *record,      /* buf to copy read record into */
  22.     int    mode)         /* which record, next, prev, equal,
  23. etc */
  24. {
  25.     struct isquery far *lpRB;
  26.     LPSTR              lpRec;
  27.     int                iRecLen;
  28.     int                iRetVal;
  29.  
  30.     lpRB = (struct isquery far *)GlobalLock(hRequestBlock);
  31.     if (lpRB == NULL) {
  32.         /* Error, not able to lock query block. */
  33.         iserrno = ISNULLPTR;
  34.         return -1;
  35.     }
  36.     lpRB->iOpcode = OPREAD;
  37.     lpRB->iIsfd = isfd;
  38.     lpRB->iMode = mode;
  39.  
  40. /* WinTrieve allows the user to search for a record based on
  41. index key fields. It is expected the  values of interest are
  42. filled in  the record  bufferto be passed to the server. For
  43. the sake  of  simplicity assume that the global memory block
  44. specified by hRecord is large enough to hold the record. */
  45.  
  46.     lpRec = GlobalLock(lpRB->hRecord);
  47.     if (lpRec == NULL) {
  48.         /* Error, not able to lock the record block. */
  49.         GlobalUnLock(hRequestBlock);
  50.         iserrno = ISNULLPTR;
  51.         return -1;
  52.     }
  53.     /* Copy the record. */
  54.     iRecLen = iRecLenTbl[isfd];
  55.     lmemcpy(lpRec, (LPSTR)record, iRecLen);
  56.  
  57.       /* All fields filled, now unlock and make the request.
  58. */
  59.     GlobalUnlock(lpRB->hRecord);
  60.     GlobalUnlock(hRequestBlock);
  61.           iRetVal  =   SendMessage(hWndServer,   wmSendISAM,
  62. hWndClient, MAKELONG(hRequestBlock, 0));
  63.     if (iRetVal != ISOK) {
  64.         /* Protocol error. */
  65.         if (iRetVal == 0L)
  66.             iserrno = ISNOSERVER;
  67.         else
  68.             iserrno = iRetVal;
  69.         return -1;
  70.     }
  71.  
  72.     /* Lock the request block to get the response values. */
  73.     lpRB = (struct isquery far *)GlobalLock(hRequestBlock);
  74.     if (lpRB == NULL) {
  75.         /* Error, not able to lock query block. */
  76.         iserrno = ISNULLPTR;
  77.         return -1;
  78.     }
  79.     /* This introduces the second level of error handling.
  80.      * These are errors related to the processing of the
  81.      * request which are differentiated from the processing
  82.      * of the protocol.
  83.      */
  84.     iRetVal = lpRB->iRetVal;
  85.     if (iRetVal == -1) {
  86.         iserrno = lpRB->iserrno;
  87.         return iRetVal;
  88.     }
  89.  
  90.     lpRec = GlobalLock(lpRB->hRecord);
  91.     if (lpRec == NULL) {
  92.         /* Error, not able to lock the record block. */
  93.         GlobalUnLock(hRequestBlock);
  94.         iserrno = ISNULLPTR;
  95.         return -1;
  96.     }
  97.     /* Copy the record that was read. Assume lmemcpy is
  98.      * long pointer version of C Runtime memcpy.
  99.      */
  100.     lmemcpy((void far *)record, lpRec, iRecLen);
  101.     GlobalUnlock(lpRB->hRecord);
  102.     GlobalUnlock(hRequestBlock);
  103.  
  104.     return iRetVal;
  105. }
  106.  
  107.