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