home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / pdsrc / iff / readclip.e next >
Text File  |  1977-12-31  |  4KB  |  167 lines

  1.  
  2. /****
  3.  
  4.     ReadClipboard.e
  5.     © 1995 by Vidar Hokstad <vidarh@rforum.no>
  6.  
  7.  
  8.     COPYRIGHT NOTICE:
  9.  
  10.     This code can be distributed freely, and parts of the code,
  11.     or the whole code, can be used as is, or reused in any
  12.     product - free or commercial - provided the following terms
  13.     are met:
  14.  
  15.     - You accept that I give no guarantee, expressed or implied
  16.         of the usefulness or functionality of this code, and that
  17.         I accept no responsability for damage caused directly or
  18.         indirectly by the use of this program.
  19.  
  20.     - The product in which the code is used can not be used
  21.         for military applications.
  22.  
  23.  
  24.     INFO:
  25.  
  26.     Written as an exersize in using iffparse.library. Reads the
  27.     PRIMARY_CLIP clipboard unit, and prints it to stdout.
  28.  
  29.     Notice: Expects a FTXT clipboard, and will only utilize
  30.     FTXT CHRS chunks (which is what a standard clipboard contains).
  31.  
  32.     The only 2.04+ specific stuff is the use of Vprintf for writing
  33.     debug info. I don't really know if there's a version of
  34.     iffparse.library for older os-versions though. There ought to
  35.     be, but I dont really care :)
  36.  
  37. ***/
  38.  
  39.  
  40. OPT OSVERSION=37,PREPROCESS
  41.  
  42. -> #define DEBUG
  43.  
  44. MODULE 'iffparse','libraries/iffparse','devices/clipboard'
  45.  
  46. RAISE "^C" IF CtrlC() = TRUE
  47.  
  48.  
  49.  
  50. PROC readclip (unit) HANDLE
  51.  
  52.     DEF iff:PTR TO iffhandle,        -> Utility struct. for iffparse
  53.         cliph=NIL,                    -> The clipboard handler
  54.         buf[200]:ARRAY OF CHAR,        -> Buffer for ReadChunkBytes()
  55.         size                        -> Number of read bytes.
  56.  
  57. #ifdef DEBUG
  58.     DEF node:PTR TO contextnode    -> additional info about chunk
  59. #endif
  60.  
  61.  
  62. -> --- INIT
  63.  
  64.     IF (iffparsebase:= OpenLibrary('iffparse.library',0))=NIL
  65.         Raise ("iffp")
  66.     ENDIF
  67.  
  68.     -> You *MUST* use AllocIFF() to allocate an iffhandle structure
  69.     IF (iff:=AllocIFF())=NIL THEN Raise ("iffh")
  70.  
  71.     -> Prepare the iffhandle to use the clipboard
  72.     InitIFFasClip(iff)
  73.  
  74.     -> Open a clipboard stream and fill inn the iffhandle
  75.     IF (cliph:=OpenClipboard (unit) )=0 THEN Raise ("open")
  76.     iff.stream:=cliph
  77.  
  78.     -> Start a new IO session
  79.     IF OpenIFF (iff,IFFF_READ) THEN Raise ("oiff")
  80.  
  81.     -> Tell iffparse.library you want to examine chunks of type FTXT
  82.     -> and id CHRS which is pure ASCII text.
  83.  
  84. #ifndef DEBUG
  85.     IF StopChunk (iff,"FTXT","CHRS") THEN Raise ("schn")
  86. #endif
  87.  
  88.  
  89. -> --- MAIN LOOP
  90.  
  91. ->         While theres something left to read, parse the IFF stream.
  92. ->         For a clipboard, there's usually only one chunk of interest,
  93. ->         but this way the code will work if theres more chunks too.
  94.  
  95. ->        IF "DEBUG" is defined, the IFF file is stepped through one
  96. ->        context change at a time, and additional info is dumped to
  97. ->        Output()
  98.  
  99.  
  100. #ifndef DEBUG
  101.     WHILE ParseIFF(iff,IFFPARSE_SCAN)<>IFFERR_EOF
  102. #endif
  103.  
  104. #ifdef DEBUG
  105.     WHILE ParseIFF(iff,IFFPARSE_RAWSTEP)<>IFFERR_EOF
  106. #endif
  107.         CtrlC()
  108.  
  109. #ifdef DEBUG
  110.         node:= CurrentChunk(iff)
  111.         Vprintf ('id = "%s", type = "%s", size = %ld\n',
  112.             [[node.id,0],[node.type,0],node.size])
  113.         Flush(stdout)
  114.  
  115.         -> Ensure that we only dump "CHRS" chunks
  116.         IF node.id="CHRS"
  117. #endif
  118.  
  119.         -> Copy the chunk to the output file
  120.         WHILE (size:=ReadChunkBytes(iff,buf,200))>0
  121.             Write(stdout,buf,size)
  122.         ENDWHILE
  123.  
  124. #ifdef DEBUG
  125.         ENDIF
  126. #endif
  127.  
  128.     ENDWHILE
  129.  
  130. EXCEPT DO
  131.  
  132. ->--- CLEANUP:
  133.  
  134.     -> Was iffparse.library opened?
  135.     IF iffparsebase
  136.  
  137.         -> Was the iffhandle structure allocated?
  138.         IF iff
  139.  
  140.             -> Did OpenIFF() fail?
  141.             IF exception<>"oiff" THEN CloseIFF(iff)
  142.  
  143.             -> Was the Clipboard structure allocated?
  144.             IF cliph THEN CloseClipboard(cliph)
  145.  
  146.             -> Free the iffhandle. *MUST* be done with FreeIFF()
  147.             FreeIFF(iff)
  148.         ENDIF
  149.         CloseLibrary (iffparsebase)
  150.     ENDIF
  151.  
  152.     -> IF an exception occured, let the next exception handler deal
  153.     -> with it too...
  154.  
  155.     ReThrow()
  156. ENDPROC
  157.  
  158.  
  159. -> Read the PRIMARY_CLIP clipboard, and print it's contents to stdout
  160. -> Display any exception that occurs.
  161.  
  162. PROC main() HANDLE
  163.     readclip (PRIMARY_CLIP)
  164. EXCEPT
  165.     Vprintf ('exception = %ld ("%s")\n',[exception,[exception,0]])
  166. ENDPROC
  167.