home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / SQDEV200.ZIP / SRC / SQ_READ.C < prev    next >
C/C++ Source or Header  |  1994-05-23  |  7KB  |  219 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Squish Developers Kit Source, Version 2.00                             *
  4.  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
  5.  *                                                                         *
  6.  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
  7.  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
  8.  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
  9.  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
  10.  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
  11.  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
  12.  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
  13.  *  ABLE TO REACH WITH THE AUTHOR.                                         *
  14.  *                                                                         *
  15.  *  You can contact the author at one of the address listed below:         *
  16.  *                                                                         *
  17.  *  Scott Dudley       FidoNet     1:249/106                               *
  18.  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
  19.  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
  20.  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
  21.  *                                                                         *
  22.  ***************************************************************************/
  23.  
  24. #pragma off(unreferenced)
  25. static char rcs_id[]="$Id: sq_read.c 1.4 1993/12/30 00:49:38 sjd Exp sjd $";
  26. #pragma on(unreferenced)
  27.  
  28. #define MSGAPI_HANDLERS
  29. #define MSGAPI_NO_OLD_TYPES
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <io.h>
  34. #include <fcntl.h>
  35. #include "prog.h"
  36. #include "msgapi.h"
  37. #include "api_sq.h"
  38. #include "apidebug.h"
  39.  
  40.  
  41.  
  42. /* Read in the binary message header from the data file */
  43.  
  44. static unsigned near _SquishReadXmsg(HMSG hmsg, PXMSG pxm, dword *pdwOfs)
  45. {
  46.   long ofs=hmsg->foRead + HSqd->cbSqhdr;
  47.  
  48.   if (*pdwOfs != (dword)ofs)
  49.     if (lseek(HSqd->sfd, ofs, SEEK_SET) != ofs)
  50.     {
  51.       msgapierr=MERR_BADF;
  52.       return FALSE;
  53.     }
  54.  
  55.   if (read(HSqd->sfd, (char *)pxm, sizeof *pxm) != (int)sizeof *pxm)
  56.   {
  57.     msgapierr=MERR_BADF;
  58.     return FALSE;
  59.   }
  60.  
  61.   /* Update our position */
  62.  
  63.   *pdwOfs=(dword)ofs + (dword)sizeof *pxm;
  64.  
  65.   /* If there is a UMSGID associated with this message, store it in         *
  66.    * memory in case we have to write the message later.  Blank it           *
  67.    * out so that the application cannot access it.                          */
  68.  
  69.   if (pxm->attr & MSGUID)
  70.   {
  71.     hmsg->uidUs=pxm->umsgid;
  72.     pxm->attr &= ~MSGUID;
  73.     pxm->umsgid=0L;
  74.   }
  75.  
  76.   return TRUE;
  77. }
  78.  
  79.  
  80. /* Read in the control information for the current message */
  81.  
  82. static unsigned near _SquishReadCtrl(HMSG hmsg, byte OS2FAR *szCtrl,
  83.                                      dword dwCtrlLen, dword *pdwOfs)
  84. {
  85.   long ofs=hmsg->foRead + HSqd->cbSqhdr + sizeof(XMSG);
  86.   unsigned uMaxLen=(unsigned)min(dwCtrlLen, hmsg->sqhRead.clen);
  87.  
  88.   /* Read the specified amount of text, but no more than specified in       *
  89.    * the frame header.                                                      */
  90.  
  91.  
  92.   if (*pdwOfs != (dword)ofs)
  93.     if (lseek(HSqd->sfd, ofs, SEEK_SET) != ofs)
  94.     {
  95.       msgapierr=MERR_BADF;
  96.       return FALSE;
  97.     }
  98.  
  99.   if (read(HSqd->sfd, (char *)szCtrl, uMaxLen) != (int)uMaxLen)
  100.   {
  101.     msgapierr=MERR_BADF;
  102.     return FALSE;
  103.   }
  104.  
  105.   *pdwOfs=(dword)ofs + (dword)uMaxLen;
  106.  
  107.   return TRUE;
  108. }
  109.  
  110.  
  111. /* Read in the text body for the current message */
  112.  
  113. static dword near _SquishReadTxt(HMSG hmsg, byte OS2FAR *szTxt, dword dwTxtLen,
  114.                                  dword *pdwOfs)
  115. {
  116.   /* Start reading from the cur_pos offset */
  117.  
  118.   long ofs=hmsg->foRead + (long)HSqd->cbSqhdr + (long)sizeof(XMSG)
  119.                         + (long)hmsg->sqhRead.clen;
  120.  
  121.   /* Max length is the size of the msg text inside the frame */
  122.  
  123.   unsigned uMaxLen=(unsigned)(hmsg->sqhRead.msg_length -
  124.                               hmsg->sqhRead.clen - sizeof(XMSG));
  125.  
  126.   /* Make sure that we don't try to read beyond the end of the msg */
  127.  
  128.   if (hmsg->cur_pos > uMaxLen)
  129.     hmsg->cur_pos=uMaxLen;
  130.  
  131.   /* Now seek to the position that we are supposed to read from */
  132.  
  133.   ofs += (long)hmsg->cur_pos;
  134.  
  135.   /* Decrement the max length by the seek posn, but don't read more than    *
  136.    * the user asked for.                                                    */
  137.  
  138.   uMaxLen -= (unsigned)hmsg->cur_pos;
  139.   uMaxLen=min(uMaxLen, (unsigned)dwTxtLen);
  140.  
  141.   /* Now try to read that information from the file */
  142.  
  143.   if (ofs != (long)*pdwOfs)
  144.     if (lseek(HSqd->sfd, ofs, SEEK_SET) != ofs)
  145.     {
  146.       msgapierr=MERR_BADF;
  147.       return (dword)-1L;
  148.     }
  149.  
  150.  
  151.   if (read(HSqd->sfd, (char *)szTxt, uMaxLen) != (int)uMaxLen)
  152.   {
  153.     msgapierr=MERR_BADF;
  154.     return (dword)-1L;
  155.   }
  156.  
  157.   *pdwOfs = (dword)ofs + (dword)uMaxLen;
  158.  
  159.  
  160.   /* Increment the current position by the number of bytes that we read */
  161.  
  162.   hmsg->cur_pos += (dword)uMaxLen;
  163.  
  164.   return (dword)uMaxLen;
  165. }
  166.  
  167.  
  168. /* Read a message from the Squish base */
  169.  
  170. dword MAPIENTRY SquishReadMsg(HMSG hmsg, PXMSG pxm, dword dwOfs,
  171.                               dword dwTxtLen, byte OS2FAR *szTxt,
  172.                               dword dwCtrlLen, byte OS2FAR *szCtrl)
  173. {
  174.   dword dwSeekOfs=(dword)-1L; /* Current offset */
  175.   unsigned fOkay=TRUE;        /* Any errors so far? */
  176.   dword dwGot=0;              /* Bytes read from file */
  177.  
  178.   /* Make sure that we have a valid handle (and that it's in read mode) */
  179.  
  180.   if (MsgInvalidHmsg(hmsg) || !_SquishReadMode(hmsg))
  181.     return (dword)-1L;
  182.  
  183.  
  184.   /* Make sure that we can use szTxt and szCtrl as flags controlling what   *
  185.    * to read.                                                               */
  186.  
  187.   if (!dwTxtLen)
  188.     szTxt=NULL;
  189.  
  190.   if (!dwCtrlLen)
  191.     szCtrl=NULL;
  192.  
  193.  
  194.   /* Now read in the message header, the control information, and the       *
  195.    * message text.                                                          */
  196.  
  197.  if (pxm)
  198.     fOkay=_SquishReadXmsg(hmsg, pxm, &dwSeekOfs);
  199.  
  200.   if (fOkay && szCtrl)
  201.     fOkay=_SquishReadCtrl(hmsg, szCtrl, dwCtrlLen, &dwSeekOfs);
  202.  
  203.   if (fOkay && szTxt)
  204.   {
  205.     hmsg->cur_pos=dwOfs;
  206.  
  207.     if ((dwGot=_SquishReadTxt(hmsg, szTxt, dwTxtLen, &dwSeekOfs))==(dword)-1L)
  208.       fOkay=FALSE;
  209.   }
  210.  
  211.   /* If everything worked okay, return the number bytes that we read        *
  212.    * from the message body.                                                 */
  213.  
  214.   return fOkay ? dwGot : (dword)-1L;
  215. }
  216.  
  217.  
  218.  
  219.