home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / e / epmmac2.zip / BUFF.E < prev    next >
Text File  |  1992-12-18  |  6KB  |  186 lines

  1. ;
  2. ;  Test macros for the buffer() opcode.      Bryan Lewis 8/1/88.
  3. ;
  4. ;Changes 8/29 -- look for change marker '|'.
  5. ;* Can specify a buffer to be private, non-shared.  This allows a process
  6. ;  to create any number of buffers for internal use, without running into the
  7. ;  OS/2 limit of 30 shared buffers.
  8. ;* New format option FINAL_NULL to append a null at end of buffer, as needed
  9. ;  for clipboard format.
  10.  
  11. compile if not defined(SMALL)
  12.  include 'stdconst.e'
  13.  define INCLUDING_FILE = 'BUFF.E'
  14.  tryinclude 'MYCNF.E'
  15.  
  16.  compile if not defined(SITE_CONFIG)
  17.     const SITE_CONFIG = 'SITECNF.E'
  18.  compile endif
  19.  compile if SITE_CONFIG
  20.     tryinclude SITE_CONFIG
  21.  compile endif
  22.  
  23.  compile if not defined(NLS_LANGUAGE)
  24.    const NLS_LANGUAGE = 'ENGLISH'
  25.  compile endif
  26.  include NLS_LANGUAGE'.e'
  27. compile endif
  28.  
  29. definit
  30.    universal format, bufname, bufhndl
  31.    format = 0     -- Initialize.  Zero will give standard CR-LF format.
  32.    bufname = 'EBUF'
  33.    bufhndl = 0    -- Always set bufhndl to zero if no buffer is active.
  34.                   -- E will safely reject a zero bufhndl, but has no way of
  35.                   -- knowing whether other arbitrary bufhndl's are valid.
  36.  
  37. define
  38. compile if EVERSION < '5.21'
  39.    MSGC = '.messagecolor'
  40. compile else
  41.    MSGC = 'vMESSAGECOLOR'
  42. compile endif
  43.  
  44. defc buffhelp =
  45.    sayat leftstr(CREATEBUF_HELP__MSG ,72), 1, 4, $MSGC
  46.    sayat leftstr(PUTBUF_HELP__MSG    ,72), 2, 4, $MSGC
  47.    sayat leftstr(GETBUF_HELP__MSG    ,72), 3, 4, $MSGC
  48.    sayat leftstr(FREEBUF_HELP__MSG   ,72), 4, 4, $MSGC
  49.  
  50.  
  51. --| New optional argument:  GETBUF 1 means a private buffer.
  52. --| Means don't try to open and close a named buffer; just use current bufhndl.
  53. defc getbuf    -- Gets entire buffer into current file.
  54.                -- Buffer must have been precreated.
  55.                -- Opens the buffer before the get, closes it afterward.
  56.                -- Note:  if we were only doing this from the same E session
  57.                --        as where we created the buffer, we wouldn't need the
  58.                --        open.  But we might get from another session!
  59.    universal format, bufname
  60.    universal bufhndl --| We use the previous bufhndl if private.
  61.  
  62.    private = 0                   --| new option to create non-shared buffer
  63.    if arg(1) then
  64.       private = 1
  65.    endif
  66.    if not private then
  67.       bufhndl = buffer(OPENBUF, bufname)
  68.       if not bufhndl then sayerror 'OPENBUF' ERROR_NUMBER__MSG RC; stop; endif
  69.    endif
  70.  
  71.    noflines = buffer(GETBUF,bufhndl)
  72.    if not noflines then
  73.       if rc then
  74.          sayerror 'GETBUF' ERROR_NUMBER__MSG RC
  75.       else
  76.          sayerror EMPTYBUF_ERROR__MSG
  77.       endif
  78.       stop
  79.    endif
  80.    usedsize = buffer(USEDSIZEBUF,bufhndl)   -- Get these for info.
  81.    maxsize  = buffer(MAXSIZEBUF,bufhndl)
  82.  
  83.    if not private then  --|
  84.       success  = buffer(FREEBUF,  bufhndl)
  85.       if not success then sayerror 'FREEBUF' ERROR_NUMBER__MSG RC; stop; endif
  86.    endif
  87.  
  88.    sayerror GOT__MSG usedsize BYTES_FROM_A__MSG maxsize || BYTE_BUFFER__MSG'.  'noflines LINES__MSG
  89.  
  90. defc putbuf    -- Puts current file from current line on.
  91.                -- Buffer must be created first.
  92.    universal format, bufhndl
  93.  
  94.    -- From current line to end of file.
  95.    noflines = buffer(PUTBUF,   bufhndl, .line, 0, format)
  96.    if not noflines then sayerror 'PUTBUF' ERROR_NUMBER__MSG RC; stop; endif
  97.    usedsize = buffer(USEDSIZEBUF,bufhndl)   -- Get these for info.
  98.    maxsize  = buffer(MAXSIZEBUF,bufhndl)
  99.    sayerror PUT__MSG usedsize BYTES_TO_A__MSG maxsize || BYTE_BUFFER__MSG'.  'noflines LINES__MSG
  100.  
  101. --| New optional argument:  CREATEBUF 1 means a private buffer.
  102. defc createbuf -- Creates the buffer of default size.
  103.    universal bufname, bufhndl
  104.    private = 0                   --| new option to create non-shared buffer
  105.    if arg(1) then
  106.       private = 1
  107.    endif
  108.    bufhndl = buffer(CREATEBUF, bufname, MAXBUFSIZE, private )  --|
  109.    if not bufhndl then sayerror 'CREATEBUF' ERROR_NUMBER__MSG RC; stop; endif
  110.    sayerror CREATED__MSG
  111.  
  112. defc freebuf
  113.    universal bufhndl
  114.    success = buffer(FREEBUF, bufhndl)
  115.    if not success then sayerror 'FREEBUF' ERROR_NUMBER__MSG RC; stop; endif
  116.    sayerror FREED__MSG
  117.    bufhndl = 0             -- Reset to no-buffer value.
  118.  
  119. defproc put_shared_text(buffer_name, firstline, lastline)
  120.    if buffer_name='' then
  121.       sayerror MISSING_BUFFER__MSG
  122.       stop
  123.    endif
  124.    -- Try to open the buffer.  If it doesn't exist, create it.
  125.    bufhndl = buffer(OPENBUF, buffer_name)
  126.    if bufhndl then
  127.       opened = 1
  128.    else
  129.       -- Make a 64K buffer... memory's plentiful.  Easily changed.
  130.       bufsize = MAXBUFSIZE
  131.       bufhndl = buffer(CREATEBUF, buffer_name, bufsize)
  132.       opened = 0
  133.    endif
  134.    if not bufhndl then
  135.       sayerror CAN_NOT_OPEN__MSG buffer_name '-' ERROR_NUMBER__MSG RC
  136.       stop
  137.    endif
  138.    noflines = buffer(PUTBUF, bufhndl, firstline, lastline)
  139.    if opened then
  140.       call buffer(FREEBUF, bufhndl)
  141.    endif
  142.    if noflines < lastline-firstline+1 then
  143.       sayerror ONLY_ACCEPTED__MSG noflines LINES__MSG
  144.       stop
  145.    endif
  146.  
  147. defproc get_shared_text(buffer_name)
  148.    if buffer_name='' then
  149.       sayerror MISSING_BUFFER__MSG
  150.       stop
  151.    endif
  152.    -- Try to open the buffer.  If it doesn't exist, create it.
  153.    bufhndl = buffer(OPENBUF, buffer_name)
  154.    if bufhndl then
  155.       opened = 1
  156.    else
  157.       -- Make a 64K buffer... memory's plentiful.  Easily changed.
  158.       bufsize = MAXBUFSIZE
  159.       bufhndl = buffer(CREATEBUF, buffer_name, bufsize)
  160.       opened = 0
  161.    endif
  162.    if not bufhndl then
  163.       sayerror CAN_NOT_OPEN__MSG buffer_name '-' ERROR_NUMBER__MSG RC
  164.       stop
  165.    endif
  166.    noflines = buffer(GETBUF, bufhndl)
  167.    if opened then
  168.       call buffer(FREEBUF, bufhndl)
  169.    endif
  170.  
  171. defc pt= -- Put text to buffer.  Argument = noflines.
  172.    noflines = arg(1)
  173.    firstline = .line
  174.    if not noflines then          -- no argument means current line only
  175.       lastline = firstline
  176.    endif
  177.    if noflines='*' then          -- '*' means to end of file
  178.       lastline = .last
  179.    endif
  180.    buffer_name='EXSESS'          -- buffer name:  E across sessions
  181.    call put_shared_text(buffer_name, firstline, lastline)
  182.  
  183. defc gt= -- Get text from buffer.  No argument.
  184.    buffer_name='EXSESS'          -- buffer name:  E across sessions
  185.    call get_shared_text(buffer_name)
  186.