home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / NOTEPAD2.ZIP / MBUF.C < prev    next >
C/C++ Source or Header  |  1989-02-08  |  6KB  |  236 lines

  1. /*
  2.  * mbuf.c -- Buffer Manager
  3.  *
  4.  * Created by Microsoft Corporation, 1989
  5.  */
  6.  
  7. #define INCL_WIN
  8. #include <os2.h>
  9. #include "mle.h"
  10. #include "mtypes.h"
  11. #include "mfuncs.h"
  12.  
  13. /***********************************************************************
  14. * Forward declarations
  15. ***********************************************************************/
  16.  
  17.  
  18. /*
  19.  * VOID BufInit(PED ped)
  20.  *
  21.  * Initializes the buffer manager
  22.  */
  23. public VOID BufInit(PED ped)
  24. {
  25.         ped->pchBlockBuf = NULL;
  26.         ped->usBufLength = 0;
  27.         ped->usBlockType = MLE_CFTEXT;
  28. }
  29.  
  30.  
  31. /*
  32.  * BOOL BufSetBuf(PED ped,PCHAR pch,USHORT usLen)
  33.  *
  34.  * Sets the transfer buffer to a particular address
  35.  */
  36. public BOOL BufSetBuf(PED ped,PCHAR pch,USHORT usLen)
  37. {
  38.         ped->pchBlockBuf = pch;
  39.         ped->usBufLength = usLen;
  40.         return(TRUE);
  41. }
  42.  
  43.  
  44. /*
  45.  * PCHAR BufQueryBuf(PED ped)
  46.  *
  47.  * Returns the address of the current transfer buffer
  48.  */
  49. public PCHAR BufQueryBuf(PED ped)
  50. {
  51.         return(ped->pchBlockBuf);
  52. }
  53.  
  54.  
  55. /*
  56.  * USHORT BufQueryBufSize(PED ped)
  57.  *
  58.  * Returns the size of the current transfer buffer
  59.  */
  60. public USHORT BufQueryBufSize(PED ped)
  61. {
  62.         return(ped->usBufLength);
  63. }
  64.  
  65.  
  66. /*
  67.  * BOOL BufSetFormat(PED ped,USHORT usFmt)
  68.  *
  69.  * Sets the transfer format
  70.  */
  71. public BOOL BufSetFormat(PED ped,USHORT usFmt)
  72. {
  73.         ped->usBlockType = usFmt;
  74.         return(TRUE);
  75. }
  76.  
  77.  
  78. /*
  79.  * USHORT BufQueryFormat(PED ped)
  80.  *
  81.  * Returns the current format
  82.  */
  83. public USHORT BufQueryFormat(PED ped)
  84. {
  85.         return(ped->usBlockType);
  86. }
  87.  
  88.  
  89. /*
  90.  * IPT BufFormattedSize(PED ped, IPT iptFrom, IPT iptTo, USHORT usFormat)
  91.  *
  92.  * Determines the size, in bytes, of a range of ipts in a particular format
  93.  * Returns a value < 0 if there is an error.
  94.  */
  95. public IPT BufFormattedSize(PED ped, IPT iptFrom, IPT iptTo, USHORT usFormat)
  96. {
  97.         PPR ppr;
  98.         IPT ipt;
  99.         OFFSET off;
  100.         ULONG cBytes;
  101.         IPT cch = iptTo - iptFrom;
  102.  
  103.         cBytes = 0;
  104.         ipt = iptFrom;
  105.  
  106.         ppr = TxtPPROfIpt(ped, &ipt);
  107.         off = (USHORT)ipt;
  108.         while (cch > 0) {
  109.                 if (fIsMarker(ppr)) {
  110.                         if (((PMR)ppr)->fFlags & MARKER_LINE) {
  111.                                 switch (usFormat) {
  112.                                 case MLE_CFTEXT:
  113.                                         cBytes++;
  114.                                         break;
  115.                                 case MLE_CSTD:
  116.                                         break;
  117.                                 case MLE_WINFMT:
  118.                                         cBytes++;
  119.                                         if (((PMR)ppr)->fFlags&
  120.                                             (MARKER_SLB||MARKER_SPILL)) {
  121.                                                 cBytes++;
  122.                                         }
  123.                                         break;
  124.                                 default:
  125.                                         return(-1);
  126.                                         break;
  127.                                 }
  128.                         }
  129.                 } else {
  130.                         cBytes++;
  131.                 }
  132.                 AdvancePointer(&ppr,&off);
  133.                 cch--;
  134.         }
  135.         return(cBytes);
  136. }
  137.  
  138. /*
  139.  * OFFSET BufCopyOut(PED ped, IPT iptFrom, USHORT usFormat,
  140.  *                 PSZ pszBuff, OFFSET cch)
  141.  *
  142.  * Copies from the MLE text to a buffer.  If the buffer is null, returns
  143.  * zero and does nothing.  The number of bytes copied into the buffer is
  144.  * limited by cch.  Note that NULL's ('\0') are not given special treatment
  145.  * unless so defined by the copy format.  Function returns the number of
  146.  * bytes copied into the buffer.
  147.  */
  148. public OFFSET BufCopyOut(PED ped, IPT iptFrom, USHORT usFormat,
  149.                       PSZ pszBuff, OFFSET cch)
  150. {
  151.     PPR ppr;
  152.     IPT ipt;
  153.     OFFSET off;
  154.     OFFSET cBytes;
  155.     CHAR ch;
  156.  
  157.     if (pszBuff == NULL)
  158.             return(0);
  159.  
  160.     cch = min(cch, ped->usBufLength);
  161.     cBytes = 0;
  162.     ipt = iptFrom;
  163.  
  164.     ppr = TxtPPROfIpt(ped, &ipt);
  165.     off = (USHORT)ipt;
  166.     ch = CharOfPointer(ppr, off);
  167.     while (cch > cBytes) {
  168.         if (fIsMarker(ppr)) {
  169.             if (((PMR)ppr)->fFlags & MARKER_LINE) {
  170.                 switch (usFormat) {
  171.                 case MLE_CFTEXT:
  172.                 case MLE_CSTD:
  173.                     break;
  174.                 case MLE_WINFMT:
  175.                     if (fIsMarkerType(ppr, MARKER_SOFTLINE)) {
  176.                         if (cBytes+3 <= cch) {
  177.                             cBytes += 3;
  178.                             *(pszBuff++) = '\r';
  179.                             *(pszBuff++) = '\r';
  180.                             *(pszBuff++) = '\n';
  181.                         }
  182.                     }
  183.                     break;
  184.                 default:
  185.                     return(-1);
  186.                     break;
  187.                 }
  188.             }
  189.         } else {
  190.             if (ch == '\n') {
  191.                 switch (usFormat) {
  192.                 case MLE_CFTEXT:
  193.                 case MLE_WINFMT:
  194.                     if (cBytes+2 >= cch) {
  195.                         return(cBytes);
  196.                     } else {
  197.                         *(pszBuff++) = '\r';
  198.                         *(pszBuff++) = '\n';
  199.                         cBytes += 2;
  200.                     }
  201.                     break;
  202.                 case MLE_CSTD:
  203.                     cBytes ++;
  204.                     *(pszBuff++) = '\n';
  205.                     break;
  206.                 default:
  207.                     return(-1);
  208.                     break;
  209.                 }
  210.             } else {
  211.                 *(pszBuff++) = ch;
  212.                 cBytes++;
  213.             }
  214.         }
  215.         ch = AdvancePointer(&ppr,&off);
  216.     }
  217.     return(cBytes);
  218. }
  219.  
  220.  
  221. /*
  222.  * BOOL BufCopyIn(PED ped, IPT iptFrom, IPT iptTo, 
  223.  *                PSZ pszBuff, OFFSET cch)
  224.  *
  225.  * Copies from an external buffer to the MLE text.  Replaces the text in the
  226.  * range iptFrom..iptTo with the contents of the buffer.  Returns TRUE iff
  227.  * all the text was properly inserted into the MLE.
  228.  */
  229. public BOOL BufCopyIn(PED ped, IPT iptFrom, IPT iptTo,
  230.                         PSZ pszBuff, OFFSET cch)
  231. {
  232.     if (pszBuff == NULL)
  233.         return(FALSE);
  234.     return(TxtChange(ped, iptFrom, iptTo, pszBuff, cch));
  235. }
  236.