home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / clipboard / cbio.e next >
Text File  |  1977-12-31  |  8KB  |  310 lines

  1. -> Cbio.e
  2. ->
  3. -> Provide standard clipboard device interface routines
  4. ->            such as Open, Close, Post, Read, Write, etc.
  5. ->
  6. -> NOTES: These functions are useful for writing and reading simple FTXT. 
  7. -> Writing and reading complex FTXT, ILBM, etc., requires more work.  You
  8. -> should use the iffparse.library to write and read FTXT, ILBM and other IFF
  9. -> file types.  When this code is used with older versions of the Amiga OS
  10. -> (i.e., before V36) a memory loss of 536 bytes will occur due to bugs in the
  11. -> clipboard device.
  12.  
  13. ->>> Header (globals)
  14. OPT MODULE
  15.  
  16. MODULE 'devices/clipboard',
  17.        'exec/io',
  18.        'exec/memory',
  19.        'exec/ports',
  20.        'amigalib/ports',
  21.        'amigalib/io'
  22.  
  23. ENUM ERR_NONE, ERR_DERR, ERR_DEV, ERR_DLEN, ERR_DOIO, ERR_IO, ERR_PORT
  24.  
  25. RAISE ERR_DEV  IF OpenDevice()<>0,
  26.       ERR_DOIO IF DoIO()<>0
  27.  
  28. -> E-Note: don't need size field since using NewM()/Dispose()
  29. EXPORT OBJECT cbbuf
  30.   count  -> Number of characters after stripping
  31.   mem
  32. ENDOBJECT
  33.  
  34. EXPORT CONST ID_FORM="FORM", ID_FTXT="FTXT", ID_CHRS="CHRS"
  35. ->>>
  36.  
  37. ->>> EXPORT PROC cbOpen(unit)
  38. ->
  39. ->  FUNCTION
  40. ->      Opens the clipboard.device.  A clipboard unit number must be passed in
  41. ->      as an argument.  By default, the unit number should be 0 (currently
  42. ->      valid unit numbers are 0-255).
  43. ->
  44. ->  RESULTS
  45. ->      A pointer to an initialised IOClipReq structure.  An exception is
  46. ->      raised if the function fails ("CBOP")
  47. EXPORT PROC cbOpen(unit) HANDLE
  48.   DEF mp=NIL, ior=NIL
  49.   IF NIL=(mp:=createPort(0,0)) THEN Raise(ERR_PORT)
  50.   IF NIL=(ior:=createExtIO(mp, SIZEOF ioclipreq)) THEN Raise(ERR_IO)
  51.   OpenDevice('clipboard.device', unit, ior, 0)
  52. EXCEPT
  53.   IF ior THEN deleteExtIO(ior)
  54.   IF mp THEN deletePort(mp)
  55.   Raise("CBOP")
  56. ENDPROC ior
  57. ->>>
  58.  
  59. ->>> EXPORT PROC cbClose(ior:PTR TO ioclipreq)
  60. ->
  61. ->  FUNCTION
  62. ->      Close the clipboard.device unit which was opened via cbOpen().
  63. ->
  64. EXPORT PROC cbClose(ior:PTR TO ioclipreq)
  65.   DEF mp
  66.   mp:=ior.message.replyport
  67.   CloseDevice(ior)
  68.   deleteExtIO(ior)
  69.   deletePort(mp)
  70. ENDPROC
  71. ->>>
  72.  
  73. ->>> EXPORT PROC cbWriteFTXT(ior:PTR TO ioclipreq, string)
  74. ->
  75. ->  FUNCTION
  76. ->      Write a NIL terminated string of text to the clipboard.  The string
  77. ->      will be written in simple FTXT format.
  78. ->
  79. ->      Note that this function pads odd length strings automatically to
  80. ->      conform to the IFF standard.
  81. ->
  82. ->  RESULTS
  83. ->      If the write did not succeed an exception is raised ("CBWR")
  84. ->
  85. EXPORT PROC cbWriteFTXT(ior:PTR TO ioclipreq, string) HANDLE
  86.   DEF length, slen, odd
  87.   slen:=StrLen(string)
  88.   odd:=Odd(slen)  -> Pad byte flag
  89.   length:=IF odd THEN slen+1 ELSE slen
  90.  
  91.   -> Initial set-up for offset, error, and clipid
  92.   ior.offset:=0
  93.   ior.error:=0
  94.   ior.clipid:=0
  95.  
  96.   -> Create the IFF header information
  97.   writeLong(ior, 'FORM')    -> 'FORM'
  98.   length:=length+12         -> + length '[size]FTXTCHRS'
  99.   writeLong(ior, {length})  -> Total length
  100.   writeLong(ior, 'FTXT')    -> 'FTXT'
  101.   writeLong(ior, 'CHRS')    -> 'CHRS'
  102.   writeLong(ior, {slen})    -> String length
  103.  
  104.   -> Write string
  105.   ior.data:=string
  106.   ior.length:=slen
  107.   ior.command:=CMD_WRITE
  108.   DoIO(ior)
  109.  
  110.   -> Pad if needed
  111.   IF odd
  112.     ior.data:=''
  113.     ior.length:=1
  114.     DoIO(ior)
  115.   ENDIF
  116.  
  117.   -> Tell the clipboard we are done writing
  118.   ior.command:=CMD_UPDATE
  119.   DoIO(ior)
  120.   -> Check if error was set by any of the preceding IO requests
  121.   IF ior.error THEN Raise(ERR_DERR)
  122. EXCEPT
  123.   Raise("CBWR")
  124. ENDPROC
  125. ->>>
  126.  
  127. ->>> PROC writeLong(ior:PTR TO ioclipreq, ldata)
  128. PROC writeLong(ior:PTR TO ioclipreq, ldata)
  129.   ior.data:=ldata
  130.   ior.length:=4
  131.   ior.command:=CMD_WRITE
  132.   DoIO(ior)
  133.   IF ior.actual<>4 THEN Raise(ERR_DLEN)
  134. ENDPROC
  135. ->>>
  136.  
  137. ->>> EXPORT PROC cbQueryFTXT(ior:PTR TO ioclipreq)
  138. ->
  139. ->  FUNCTION
  140. ->      Check to see if the clipboard contains FTXT.  If so, call cbReadCHRS()
  141. ->      one or more times until all CHRS chunks have been read.
  142. ->
  143. ->  RESULTS
  144. ->      TRUE if the clipboard contains an FTXT chunk, else FALSE.
  145. ->
  146. ->  NOTES
  147. ->      If this function returns TRUE, you must either call cbReadCHRS() until
  148. ->      cbReadCHRS() returns FALSE, or call cbReadDone() to tell the
  149. ->      clipboard.device that you are done reading.
  150. ->
  151. EXPORT PROC cbQueryFTXT(ior:PTR TO ioclipreq) HANDLE
  152.   DEF cbuff[4]:ARRAY OF LONG
  153.  
  154.   -> Initial set-up for offset, error, and clipid
  155.   ior.offset:=0
  156.   ior.error:=0
  157.   ior.clipid:=0
  158.  
  159.   -> Look for 'FORM[size]FTXT'
  160.   ior.command:=CMD_READ
  161.   ior.data:=cbuff
  162.   ior.length:=12
  163.  
  164.   DoIO(ior)
  165.  
  166.   -> Check to see if we have at least 12 bytes
  167.   IF ior.actual<>12 THEN Raise(ERR_DERR)
  168.   -> Check to see if it starts with 'FORM'
  169.   IF cbuff[]<>ID_FORM THEN Raise(ERR_DERR)
  170.   -> Check to see if it is 'FTXT'
  171.   IF cbuff[2]<>ID_FTXT THEN Raise(ERR_DERR)
  172.   -> E-Note: all checks passed...
  173.   RETURN TRUE
  174. EXCEPT
  175.   -> It's not 'FORM[size]FTXT', so tell clipboard we are done
  176.   cbReadDone(ior)
  177. ENDPROC FALSE
  178. ->>>
  179.  
  180. ->>> EXPORT PROC cbReadCHRS(ior:PTR TO ioclipreq)
  181. ->
  182. ->  FUNCTION
  183. ->      Reads and returns the text in the next CHRS chunk (if any) from the
  184. ->      clipboard.
  185. ->
  186. ->      Allocates memory to hold data in next CHRS chunk.
  187. ->
  188. ->  RESULTS
  189. ->      Pointer to a cbbuf object, or NIL if no more CHRS chunks.  An
  190. ->      exception ("CBRD") is raised if failure (e.g., not enough memory).
  191. ->
  192. ->      ***Important***
  193. ->
  194. ->      The caller must free the returned buffer when done with the
  195. ->      data by calling cbFreeBuf().
  196. ->
  197. ->  NOTES
  198. ->      This function strips NIL bytes, however, a full reader may wish to
  199. ->      perform more complete checking to verify that the text conforms to the
  200. ->      IFF standard (stripping data as required).
  201. ->
  202. EXPORT PROC cbReadCHRS(ior:PTR TO ioclipreq) HANDLE
  203.   DEF chunk, size, gotchunk
  204.   -> Find next CHRS chunk
  205.   -> E-Note: loop until exception from reading or found non-empty CHRS chunk
  206.   LOOP
  207.     gotchunk:=FALSE
  208.     readLong(ior, {chunk})
  209.     gotchunk:=TRUE
  210.     -> Is CHRS chunk?
  211.     IF chunk=ID_CHRS
  212.       -> Get size of chunk, and copy data
  213.       readLong(ior, {size})
  214.       -> E-Note: C version is wrong, should keep looping if empty CHRS chunk
  215.       IF size THEN RETURN fillCBData(ior, size)
  216.     ELSE
  217.     -> If not, skip to next chunk
  218.       readLong(ior, {size})
  219.       IF Odd(size) THEN INC size  -> If odd size, add pad byte
  220.       ior.offset:=ior.offset+size
  221.     ENDIF
  222.   ENDLOOP
  223. EXCEPT
  224.   cbReadDone(ior)  -> Tell clipboard we are done
  225.   -> E-Note: pass on exception if there are chunks left
  226.   IF gotchunk THEN Raise("CBRD")
  227. ENDPROC NIL
  228. ->>>
  229.  
  230. ->>> PROC readLong(ior:PTR TO ioclipreq, ldata)
  231. PROC readLong(ior:PTR TO ioclipreq, ldata)
  232.   ior.command:=CMD_READ
  233.   ior.data:=ldata
  234.   ior.length:=4
  235.   DoIO(ior)
  236.   IF ior.actual<>4 THEN Raise(ERR_DLEN)
  237.   IF ior.error THEN Raise(ERR_DERR)
  238. ENDPROC
  239. ->>>
  240.  
  241. ->>> PROC fillCBData(ior:PTR TO ioclipreq, size)
  242. PROC fillCBData(ior:PTR TO ioclipreq, size) HANDLE
  243.   DEF to, from, x, count, length, buf=NIL:PTR TO cbbuf
  244.   -> E-Note: clear mem to make sure buf.mem is NIL if NewM() succeeds
  245.   buf:=NewM(SIZEOF cbbuf, MEMF_PUBLIC OR MEMF_CLEAR)
  246.   length:=size
  247.   IF Odd(size) THEN INC length  -> If odd size, read 1 more
  248.   buf.mem:=NewM(length+1, MEMF_PUBLIC)
  249.  
  250.   ior.command:=CMD_READ
  251.   ior.data:=buf.mem
  252.   ior.length:=length
  253.  
  254.   to:=buf.mem
  255.   count:=0
  256.  
  257.   DoIO(ior)
  258.   IF ior.actual<>length THEN Raise(ERR_DLEN)
  259.  
  260.   -> Strip NIL bytes
  261.   from:=buf.mem
  262.   FOR x:=0 TO size-1
  263.     IF from[]
  264.       to[]:=from[]
  265.       to++
  266.       INC count
  267.     ENDIF
  268.     from++
  269.   ENDFOR
  270.   to[]:=NIL  -> NIL terminate buffer
  271.   buf.count:=count  -> Cache count of chars in buf
  272. EXCEPT
  273.   IF buf
  274.     IF buf.mem THEN Dispose(buf.mem)
  275.     Dispose(buf)
  276.   ENDIF
  277.   ReThrow()
  278. ENDPROC buf
  279. ->>>
  280.  
  281. ->>> EXPORT PROC cbReadDone(ior:PTR TO ioclipreq)
  282. ->
  283. ->  FUNCTION
  284. ->      Reads past end of clipboard file until actual is equal to 0.
  285. ->      This is tells the clipboard that we are done reading.
  286. ->
  287. EXPORT PROC cbReadDone(ior:PTR TO ioclipreq) HANDLE
  288.   DEF buffer[256]:ARRAY
  289.   ior.command:=CMD_READ
  290.   ior.data:=buffer
  291.   ior.length:=254
  292.   -> Falls through immediately if actual=0
  293.   WHILE ior.actual DO DoIO(ior)
  294. EXCEPT
  295.   -> E-Note: ignore exceptions from DoIO()
  296. ENDPROC
  297. ->>>
  298.  
  299. ->>> EXPORT PROC cbFreeBuf(buf:PTR TO cbbuf)
  300. ->
  301. ->  FUNCTION
  302. ->      Frees a buffer allocated by cbReadCHRS().
  303. ->
  304. EXPORT PROC cbFreeBuf(buf:PTR TO cbbuf)
  305.   Dispose(buf.mem)
  306.   Dispose(buf)
  307. ENDPROC
  308. ->>>
  309.  
  310.