home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TEMPLATE / WMF3.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  4KB  |  169 lines

  1. /*
  2.  * WMF Import Filter - UTILITY FUNCTIONS
  3.  *
  4.  * LANGUAGE      : Microsoft C6.0
  5.  * MODEL         : medium
  6.  * ENVIRONMENT   : Microsoft Windows 3.0 SDK
  7.  * STATUS        : operational
  8.  *
  9.  * This module contains utility functions used throughout the
  10.  * WMF import filter.
  11.  *
  12.  *    Eikon Systems, Inc.
  13.  *    989 East Hillsdale Blvd, Suite 260
  14.  *    Foster City, California 94404
  15.  *    415-349-4664
  16.  *
  17.  * 08/01/91 1.00 - David E. West - initial creation.
  18.  * 09/01/91 1.01 - Kevin P. Welch - minor revisions.
  19.  *
  20.  */
  21.  
  22. #define NOCOMM
  23.  
  24. #include <windows.h>
  25. #include <limits.h>
  26. #include <dos.h>
  27. #include "wmf.h"
  28.  
  29. /* local definitions */
  30. #define MAX_READ        0x10000L
  31.  
  32. /* local function definitions */
  33. static WORD NEAR PASCAL        MyRead( INT, LPSTR, DWORD, LPDWORD );
  34.  
  35. /* */
  36.  
  37. /*
  38.  * hread( hfSrc, hpb, cbToRead ) : DWORD;
  39.  *
  40.  *    hfSrc          handle to file to read from
  41.  *    hpb            handle to buffer to read into
  42.  *    cbToRead       number of bytes to read
  43.  *
  44.  * This function reads cbRead bytes from hfSrc into the memory block hpb.
  45.  * The buffer can start anywhere in memory and cross 64K boundaries.  The
  46.  * amount read can be more than 64K.  This function will do multiple reads
  47.  * if necessary.
  48.  *
  49.  * The return value is the number of bytes read.
  50.  *
  51.  */
  52.  
  53. DWORD FAR PASCAL hread(
  54.     INT            hfSrc,
  55.     HPSTR          hpb,
  56.     DWORD          cbToRead )
  57. {
  58.     BOOL                fContinue;
  59.     WORD                idErr;
  60.     DWORD                cbRead;
  61.     DWORD                cbSize;
  62.     DWORD                cbTotal;
  63.  
  64.     /* initialize */
  65.     cbTotal = 0;
  66.     cbSize = MAX_READ - FP_OFF(hpb);        /* up to segment boundary */
  67.  
  68.     /* read bytes */
  69.     fContinue = TRUE;
  70.     while ((cbToRead >= cbSize) && fContinue)
  71.     {
  72.         idErr = MyRead( hfSrc, hpb, cbSize, &cbRead );
  73.         if (idErr == 0)
  74.         {
  75.             cbToRead -= cbRead;
  76.             hpb += cbRead;
  77.             cbTotal += cbRead;
  78.             cbSize = MAX_READ - FP_OFF(hpb);        /* up to segment boundary */
  79.         }
  80.         else
  81.             fContinue = FALSE;
  82.     }
  83.  
  84.     /* read last segment */
  85.     if (fContinue && (cbToRead > 0))
  86.     {
  87.         idErr = MyRead( hfSrc, hpb, cbToRead, &cbRead );
  88.         if (idErr == 0)
  89.             cbTotal += cbRead;
  90.     }
  91.  
  92.     return cbTotal;
  93. }
  94.  
  95. /* */
  96.  
  97. /*
  98.  * MyRead( hf, lpb, cbToRead, lpcbRead ) : WORD;
  99.  *
  100.  *    hf             handle to file to read
  101.  *    lpb            pointer to buffer to read into
  102.  *    cbToRead       number of bytes to read (<=64K)
  103.  *    lpcbRead       number of bytes actually read
  104.  *
  105.  * This function reads up to 64K bytes from the file hf into the buffer
  106.  * lpb.  The offset of lpb must be <= the number of bytes to read, since
  107.  * the DOS read function will not cross segment boundaries in the buffer
  108.  * in which it places data.  This error condition is not checked for!!!
  109.  *
  110.  * The number of bytes actually read is returned in *lpcbRead.
  111.  *
  112.  * The return value is zero if the function is successful, and a nonzero
  113.  * error code otherwise.
  114.  *
  115.  */
  116.  
  117. static WORD NEAR PASCAL MyRead(
  118.     INT            hf,
  119.     LPSTR          lpb,
  120.     DWORD          cbToRead,
  121.     LPDWORD            lpcbRead )
  122. {
  123.     WORD                idErr;
  124.     DWORD                cbTotal;
  125.     DWORD                cbMaxRead;
  126.  
  127.     /* initialize */
  128.     idErr = 0;
  129.     cbTotal = 0;
  130.  
  131.     cbMaxRead = 64L * 1024L;
  132.     if ((cbToRead > 0) && (cbToRead <= cbMaxRead))
  133.     {
  134.         WORD            cbRead;
  135.         WORD            cbFirst;
  136.         WORD            cbSecond;
  137.  
  138.         cbFirst = (cbToRead < (0xffff-1)) ? (WORD)cbToRead : (WORD)(cbToRead/2);    
  139.         cbSecond = (WORD)(cbToRead - cbFirst);
  140.  
  141.         /* read first block */
  142.         cbRead = _lread( hf, lpb, cbFirst );
  143.         if (cbRead != -1)
  144.         {
  145.             /* read second block */
  146.             cbTotal += cbRead;
  147.             lpb += cbRead;
  148.             if (cbSecond > 0)
  149.             {
  150.                 cbRead = _lread( hf, lpb, cbSecond );
  151.                 if (cbRead != -1)
  152.                     cbTotal += cbRead;
  153.                 else
  154.                     idErr = 1;
  155.             }
  156.         }
  157.         else
  158.             idErr = 1;
  159.     }
  160.     else
  161.         idErr = 1;
  162.  
  163.     /* assign number of bytes read */
  164.     if (idErr == 0)
  165.         *lpcbRead = cbTotal;
  166.  
  167.     return idErr;
  168. }
  169.