home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / OB3.2D3.DMS / in.adf / Interfaces / IFFParse.mod < prev    next >
Encoding:
Text File  |  1993-05-23  |  15.9 KB  |  353 lines

  1. (*-------------------------------------------------------------------------*)
  2. (*                                                                         *)
  3. (*  Amiga Oberon Interface Module:                    Date: 02-Nov-92      *)
  4. (*                                                                         *)
  5. (*   © 1992 by Fridtjof Siebert                                            *)
  6. (*   updated for V39 by hartmut Goebel                                     *)
  7. (*                                                                         *)
  8. (*-------------------------------------------------------------------------*)
  9.  
  10. MODULE IFFParse;
  11.  
  12. IMPORT e  * := Exec,
  13.        cb * := ClipBoard,
  14.        u  * := Utility,
  15.        sys* := SYSTEM;
  16.  
  17. CONST
  18.   iffparseName * = "iffparse.library";
  19.  
  20. (* Struct associated with an active IFF stream.
  21.  * "IFFHandle.stream" is a value used by the client's read/write/seek functions -
  22.  * it will not be accessed by the library itself and can have any value
  23.  * (could even be a pointer or a BPTR).
  24.  *
  25.  * This structure can only be allocated by iffparse.library
  26.  *)
  27.  
  28. TYPE
  29.  
  30.   IFFHandlePtr * = UNTRACED POINTER TO IFFHandle;
  31.   IFFHandle * = STRUCT
  32.     stream * : LONGINT;
  33.     flags * : LONGSET;
  34.     depth * : LONGINT;      (* Depth of context stack *)
  35.   END;
  36.  
  37. CONST
  38.  
  39. (* bit masks for "IFFHandle.flags" field *)
  40.   read      * = LONGSET{};           (* read mode - default *)
  41.   write     * = LONGSET{0};          (* write mode *)
  42.   rwBits    * = read + write;        (* read/write bits *)
  43.   fSeek     * = LONGSET{1};          (* forward seek only *)
  44.   rSeek     * = LONGSET{2};          (* random seek *)
  45.   reserved  * = LONGSET{16..31};     (* Don't touch these bits *)
  46.  
  47.  
  48. TYPE
  49. (*****************************************************************************)
  50. (* When the library calls your stream handler, you'll be passed a pointer
  51.  * to this structure as the "message packet".
  52.  *)
  53.   IFFStreamCmdPtr * = UNTRACED POINTER TO IFFStreamCmd;
  54.   IFFStreamCmd * = STRUCT
  55.     command * : LONGINT;     (*  Operation to be performed (IFFCMD_) *)
  56.     buf * : e.APTR;          (*  Pointer to data buffer              *)
  57.     nBytes * : LONGINT;      (*  Number of bytes to be affected      *)
  58.   END;
  59.  
  60.  
  61. (*****************************************************************************)
  62.  
  63. (* A node associated with a context on the iff_Stack.  Each node
  64.  * represents a chunk, the stack representing the current nesting
  65.  * of chunks in the open IFF file.  Each context node has associated
  66.  * local context items in the (private) LocalItems list.  The ID, type,
  67.  * size and scan values describe the chunk associated with this node.
  68.  *
  69.  * This structure can only be allocated by iffparse.library
  70.  *)
  71.   ContextNodePtr * = UNTRACED POINTER TO ContextNode;
  72.   ContextNode * = STRUCT (node * : e.MinNode)
  73.     id * : LONGINT;
  74.     type * : LONGINT;
  75.     size * : LONGINT;        (*  Size of this chunk             *)
  76.     scan * : LONGINT;        (*  # of bytes read/written so far *)
  77.   END;
  78.  
  79.  
  80. (*****************************************************************************)
  81.  
  82. (* Local context items live in the ContextNode's.  Each class is identified
  83.  * by its lci_Ident code and has a (private) purge vector for when the
  84.  * parent context node is popped.
  85.  *
  86.  * This structure can only be allocated by iffparse.library
  87.  *)
  88.   LocalContextItemPtr * = UNTRACED POINTER TO LocalContextItem;
  89.   LocalContextItem * = STRUCT (node * : e.MinNode)
  90.     id * : LONGINT;
  91.     type * : LONGINT;
  92.     ident * : LONGINT;
  93.   END;
  94.  
  95.  
  96. (*****************************************************************************)
  97.  
  98. (* StoredProperty: a local context item containing the data stored
  99.  * from a previously encountered property chunk.
  100.  *)
  101.   StoredPropertyPtr * = UNTRACED POINTER TO StoredProperty;
  102.   StoredProperty * = STRUCT
  103.     size * : LONGINT;
  104.     data * : e.APTR;
  105.   END;
  106.  
  107. (*****************************************************************************)
  108.  
  109. (* Collection Item: the actual node in the collection list at which
  110.  * client will look.  The next pointers cross context boundaries so
  111.  * that the complete list is accessable.
  112.  *)
  113.   CollectionItemPtr * = UNTRACED POINTER TO CollectionItem;
  114.   CollectionItem * = STRUCT
  115.     next * : CollectionItemPtr;
  116.     size * : LONGINT;
  117.     data * : e.APTR;
  118.   END;
  119.  
  120. (*****************************************************************************)
  121.  
  122. (* Structure returned by OpenClipboard().  You may do CMD_POSTs and such
  123.  * using this structure.  However, once you call OpenIFF(), you may not
  124.  * do any more of your own I/O to the clipboard until you call CloseIFF().
  125.  *)
  126.   ClipboardHandlePtr * = UNTRACED POINTER TO ClipboardHandle;
  127.   ClipboardHandle * = STRUCT (req * : cb.IOClipReq)
  128.     cbport * : e.MsgPort;
  129.     satisfyPort * : e.MsgPort;
  130.   END;
  131.  
  132. CONST
  133. (*****************************************************************************)
  134.  
  135. (* IFF return codes.  Most functions return either zero for success or
  136.  * one of these codes.  The exceptions are the read/write functions which
  137.  * return positive values for number of bytes or records read or written,
  138.  * or a negative error code.  Some of these codes are not errors per sae,
  139.  * but valid conditions such as EOF or EOC (End of Chunk).
  140.  *)
  141.   errEOF          * = -1;     (*  Reached logical end of file *)
  142.   errEOC          * = -2;     (*  About to leave context      *)
  143.   errNoScope      * = -3;     (*  No valid scope for property *)
  144.   errNoMem        * = -4;     (*  Internal memory alloc failed*)
  145.   errRead         * = -5;     (*  Stream read error           *)
  146.   errWrite        * = -6;     (*  Stream write error          *)
  147.   errSeek         * = -7;     (*  Stream seek error           *)
  148.   errMamgled      * = -8;     (*  Data in file is corrupt     *)
  149.   errSyntax       * = -9;     (*  IFF syntax error            *)
  150.   errNotIFF       * = -10;    (*  Not an IFF file             *)
  151.   errNoHook       * = -11;    (*  No call-back hook provided  *)
  152.   return2Client   * = -12;    (*  Client handler normal return*)
  153.  
  154. (*****************************************************************************)
  155.  
  156. (* Universal IFF identifiers *)
  157.   idFORM   * = sys.VAL(LONGINT,"FORM");
  158.   idLIST   * = sys.VAL(LONGINT,"LIST");
  159.   idCAT    * = sys.VAL(LONGINT,"CAT ");
  160.   idPROP   * = sys.VAL(LONGINT,"PROP");
  161.   idNULL   * = sys.VAL(LONGINT,"    ");
  162.  
  163. (* Identifier codes for universally recognized local context items *)
  164.   lciPROP         * = sys.VAL(LONGINT,"prop");
  165.   lciCOLLECTION   * = sys.VAL(LONGINT,"coll");
  166.   lciENTRYHANDLER * = sys.VAL(LONGINT,"enhd");
  167.   lciEXITHANDLER  * = sys.VAL(LONGINT,"exhd");
  168.  
  169.  
  170. (*****************************************************************************)
  171.  
  172. (* Control modes for ParseIFF() function *)
  173.   parseScan       * = 0;
  174.   parseStep       * = 1;
  175.   parseRawStep    * = 2;
  176.  
  177.  
  178. (*****************************************************************************)
  179.  
  180. (* Control modes for StoreLocalItem() function *)
  181.   sliRoot         * = 1;      (*  Store in default context       *)
  182.   sliTop          * = 2;      (*  Store in current context       *)
  183.   sliProp         * = 3;      (*  Store in topmost FORM or LIST  *)
  184.  
  185.  
  186. (*****************************************************************************)
  187.  
  188. (* Magic value for writing functions. If you pass this value in as a size
  189.  * to PushChunk() when writing a file, the parser will figure out the
  190.  * size of the chunk for you. If you know the size, is it better to
  191.  * provide as it makes things faster.
  192.  *)
  193.  
  194.   sizeUnknown     * = -1;
  195.  
  196.  
  197. (*****************************************************************************)
  198.  
  199. (* Possible call-back command values *)
  200.   cmdInit     * = 0;       (*  Prepare the stream for a session    *)
  201.   cmdCleanup  * = 1;       (*  Terminate stream session            *)
  202.   cmdRead     * = 2;       (*  Read bytes from stream              *)
  203.   cmdWrite    * = 3;       (*  Write bytes to stream               *)
  204.   cmdSeek     * = 4;       (*  Seek on stream                      *)
  205.   cmdEntry    * = 5;       (*  You just entered a new context      *)
  206.   cmdExit     * = 6;       (*  You're about to leave a context     *)
  207.   cmdPurgeLCI * = 7;       (*  Purge a LocalContextItem            *)
  208.  
  209. VAR
  210.   base * : e.LibraryPtr;
  211.  
  212. (*--- functions in V36 or higher (distributed as Release 2.0) ---*)
  213.  
  214. (*------ Basic functions ------*)
  215.  
  216. PROCEDURE AllocIFF          *{base,- 30}(): IFFHandlePtr;
  217. PROCEDURE OpenIFF           *{base,- 36}(iff{8}            : IFFHandlePtr;
  218.                                          rwMode{0}         : LONGSET): LONGINT;
  219. PROCEDURE ParseIFF          *{base,- 42}(iff{8}            : IFFHandlePtr;
  220.                                          control{0}        : LONGINT): LONGINT;
  221. PROCEDURE CloseIFF          *{base,- 48}(iff{8}            : IFFHandlePtr);
  222. PROCEDURE FreeIFF           *{base,- 54}(ifff{8}           : IFFHandlePtr);
  223.  
  224. (*------ Read/Write functions ------*)
  225.  
  226. PROCEDURE ReadChunkBytes    *{base,- 60}(iff{8}            : IFFHandlePtr;
  227.                                          VAR buf{9}        : ARRAY OF e.BYTE;
  228.                                          numBytes{0}       : LONGINT): LONGINT;
  229. PROCEDURE WriteChunkBytes   *{base,- 66}(iff{8}            : IFFHandlePtr;
  230.                                          buf{9}            : ARRAY OF e.BYTE;
  231.                                          numBytes{0}       : LONGINT): LONGINT;
  232. PROCEDURE ReadChunkRecords  *{base,- 72}(iff{8}            : IFFHandlePtr;
  233.                                          VAR buf{9}        : ARRAY OF e.BYTE;
  234.                                          bytesPerRecord{0} : LONGINT;
  235.                                          numRecords{1}     : LONGINT): LONGINT;
  236. PROCEDURE WriteChunkRecords *{base,- 78}(iff{8}            : IFFHandlePtr;
  237.                                          buf{9}            : ARRAY OF e.BYTE;
  238.                                          bytesPerRecord{0} : LONGINT;
  239.                                          numRecords{1}     : LONGINT): LONGINT;
  240.  
  241. (*------ Context entry/exit ------*)
  242.  
  243. PROCEDURE PushChunk         *{base,- 84}(iff{8}            : IFFHandlePtr;
  244.                                          type{0}           : LONGINT;
  245.                                          id{1}             : LONGINT;
  246.                                          size{2}           : LONGINT): LONGINT;
  247. PROCEDURE PopChunk          *{base,- 90}(iff{8}            : IFFHandlePtr): LONGINT;
  248.  
  249. (*------ Low-level handler installation ------*)
  250.  
  251. PROCEDURE EntryHandler      *{base,-102}(iff{8}            : IFFHandlePtr;
  252.                                          type{0}           : LONGINT;
  253.                                          id{1}             : LONGINT;
  254.                                          position{2}       : LONGINT;
  255.                                          handler{9}        : u.HookPtr;
  256.                                          object{10}        : e.APTR): LONGINT;
  257. PROCEDURE ExitHandler       *{base,-108}(iff{8}            : IFFHandlePtr;
  258.                                          type{0}           : LONGINT;
  259.                                          id{1}             : LONGINT;
  260.                                          position{2}       : LONGINT;
  261.                                          handler{9}        : u.HookPtr;
  262.                                          object{10}        : e.APTR): LONGINT;
  263.  
  264. (*------ Built-in chunk/property handlers ------*)
  265.  
  266. PROCEDURE PropChunk         *{base,-114}(iff{8}            : IFFHandlePtr;
  267.                                          type{0}           : LONGINT;
  268.                                          id{1}             : LONGINT): LONGINT;
  269. PROCEDURE PropChunks        *{base,-120}(iff{8}            : IFFHandlePtr;
  270.                                          propArray{9}      : ARRAY OF LONGINT;
  271.                                          numPairs{0}       : LONGINT): LONGINT;
  272. PROCEDURE StopChunk         *{base,-126}(iff{8}            : IFFHandlePtr;
  273.                                          type{0}           : LONGINT;
  274.                                          id{1}             : LONGINT): LONGINT;
  275. PROCEDURE StopChunks        *{base,-132}(iff{8}            : IFFHandlePtr;
  276.                                          propArray{9}      : ARRAY OF LONGINT;
  277.                                          numPairs{0}       : LONGINT): LONGINT;
  278. PROCEDURE CollectionChunk   *{base,-138}(iff{8}            : IFFHandlePtr;
  279.                                          type{0}           : LONGINT;
  280.                                          id{1}             : LONGINT): LONGINT;
  281. PROCEDURE CollectionChunks  *{base,-144}(iff{8}            : IFFHandlePtr;
  282.                                          propArray{9}      : ARRAY OF LONGINT;
  283.                                          numPairs{0}       : LONGINT): LONGINT;
  284. PROCEDURE StopOnExit        *{base,-150}(iff{8}            : IFFHandlePtr;
  285.                                          type{0}           : LONGINT;
  286.                                          id{1}             : LONGINT): LONGINT;
  287.  
  288. (*------ Context utilities ------*)
  289.  
  290. PROCEDURE FindProp          *{base,-156}(iff{8}            : IFFHandlePtr;
  291.                                          type{0}           : LONGINT;
  292.                                          id{1}             : LONGINT): StoredPropertyPtr;
  293. PROCEDURE FindCollection    *{base,-162}(iff{8}            : IFFHandlePtr;
  294.                                          type{0}           : LONGINT;
  295.                                          id{0}             : LONGINT): CollectionItemPtr;
  296. PROCEDURE FindPropContext   *{base,-168}(iff{8}            : IFFHandlePtr): ContextNodePtr;
  297. PROCEDURE CurrentChunk      *{base,-174}(iff{8}            : IFFHandlePtr): ContextNodePtr;
  298. PROCEDURE ParentChunk       *{base,-180}(contextNode{8}    : ContextNodePtr): ContextNodePtr;
  299.  
  300. (*------ LocalContextItem support functions ------*)
  301.  
  302. PROCEDURE AllocLocalItem    *{base,-186}(type{0}           : LONGINT;
  303.                                          id{1}             : LONGINT;
  304.                                          ident{2}          : LONGINT;
  305.                                          dataSize{3}       : LONGINT): LocalContextItemPtr;
  306. PROCEDURE LocalItemData     *{base,-192}(localItem{8}      : LocalContextItemPtr): e.APTR;
  307. PROCEDURE SetLocalItemPurge *{base,-198}(localItem{8}      : LocalContextItemPtr;
  308.                                          purgeHook{9}      : u.HookPtr);
  309. PROCEDURE FreeLocalItem     *{base,-204}(localItem{8}      : LocalContextItemPtr);
  310. PROCEDURE FindLocalItem     *{base,-210}(iff{8}            : IFFHandlePtr;
  311.                                          type{0}           : LONGINT;
  312.                                          id{1}             : LONGINT;
  313.                                          ident{2}          : LONGINT ): LocalContextItemPtr;
  314. PROCEDURE StoreLocalItem    *{base,-216}(iff{8}            : IFFHandlePtr;
  315.                                          localItem{9}      : LocalContextItemPtr;
  316.                                          position{0}       : LONGINT): LONGINT;
  317. PROCEDURE StoreItemInContext*{base,-222}(iff{8}            : IFFHandlePtr;
  318.                                          localItem{8}      : LocalContextItemPtr;
  319.                                          contextNode{10}   : ContextNodePtr);
  320.  
  321. (*------ IFFHandle initialization ------*)
  322.  
  323. PROCEDURE InitIFF           *{base,-228}(iff{8}            : IFFHandlePtr;
  324.                                          flags{0}          : LONGSET;
  325.                                          streamHook{9}     : u.HookPtr);
  326. PROCEDURE InitIFFasDOS      *{base,-234}(iff{8}            : IFFHandlePtr);
  327. PROCEDURE InitIFFasClip     *{base,-240}(iff{8}            : IFFHandlePtr);
  328.  
  329. (*------ Internal clipboard support ------*)
  330.  
  331. PROCEDURE OpenClipboard     *{base,-246}(unitNumber{0}     : LONGINT): ClipboardHandlePtr;
  332. PROCEDURE CloseClipboard    *{base,-252}(clipHandle{8}     : ClipboardHandlePtr);
  333.  
  334. (*------ Miscellaneous ------*)
  335.  
  336. PROCEDURE GoodID            *{base,-258}(id{0}             : LONGINT): LONGINT;
  337. PROCEDURE GoodType          *{base,-264}(type{0}           : LONGINT): LONGINT;
  338. PROCEDURE IDtoStr           *{base,-270}(id{0}             : LONGINT;
  339.                                          VAR buf{8}        : ARRAY OF CHAR);
  340.  
  341.  
  342. (* $OvflChk- $RangeChk- $StackChk- $NilChk- $ReturnChk- $CaseChk- *)
  343.  
  344.  
  345. BEGIN
  346.   base :=  e.OpenLibrary(iffparseName,37);
  347.  
  348. CLOSE
  349.   IF base#NIL THEN e.CloseLibrary(base) END;
  350.  
  351. END IFFParse.
  352.  
  353.