iffparse.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. {
  2. This file is part of the Free Pascal run time library.
  3. iffparse.library interface unit, MorphOS version
  4. Copyright (c) 1998-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. MorphOS adaptation
  7. Copyright (c) 2014 by Karoly Balogh
  8. member of the Free Pascal development team
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. {$PACKRECORDS 2}
  16. unit iffparse;
  17. INTERFACE
  18. uses exec, clipboard, utility;
  19. const
  20. IFFPARSENAME : PChar = 'iffparse.library';
  21. {
  22. * Struct associated with an active IFF stream.
  23. * "iff_Stream" is a value used by the client's read/write/seek functions -
  24. * it will not be accessed by the library itself and can have any value
  25. * (could even be a pointer or a BPTR).
  26. }
  27. Type
  28. pIFFHandle = ^tIFFHandle;
  29. tIFFHandle = record
  30. iff_Stream,
  31. iff_Flags : DWord;
  32. iff_Depth : LONGINT; { Depth of context stack. }
  33. { There are private fields hiding here. }
  34. END;
  35. {
  36. * Bit masks for "iff_Flags" field.
  37. }
  38. CONST
  39. IFFF_READ = 0; { read mode - default }
  40. IFFF_WRITE = 1; { write mode }
  41. IFFF_RWBITS = (IFFF_READ + IFFF_WRITE); { read/write bits }
  42. IFFF_FSEEK = 2; { forward seek only }
  43. IFFF_RSEEK = 4; { random seek }
  44. IFFF_RESERVED = $FFFF0000; { Don't touch these bits. }
  45. {
  46. * When the library calls your stream handler, you'll be passed a pointer
  47. * to this structure as the "message packet".
  48. }
  49. Type
  50. pIFFStreamCmd = ^tIFFStreamCmd;
  51. tIFFStreamCmd = record
  52. sc_Command : Longint; { Operation to be performed (IFFCMD_) }
  53. sc_Buf : Pointer; { Pointer to data buffer }
  54. sc_NBytes : Longint; { Number of bytes to be affected }
  55. END;
  56. {
  57. * A node associated with a context on the iff_Stack. Each node
  58. * represents a chunk, the stack representing the current nesting
  59. * of chunks in the open IFF file. Each context node has associated
  60. * local context items in the (private) LocalItems list. The ID, type,
  61. * size and scan values describe the chunk associated with this node.
  62. }
  63. pContextNode = ^tContextNode;
  64. tContextNode = record
  65. cn_Node : tMinNode;
  66. cn_ID,
  67. cn_Type,
  68. cn_Size, { Size of this chunk }
  69. cn_Scan : Longint; { # of bytes read/written so far }
  70. { There are private fields hiding here. }
  71. END;
  72. {
  73. * Local context items live in the ContextNode's. Each class is identified
  74. * by its lci_Ident code and has a (private) purge vector for when the
  75. * parent context node is popped.
  76. }
  77. pLocalContextItem = ^tLocalContextItem;
  78. tLocalContextItem = record
  79. lci_Node : tMinNode;
  80. lci_ID,
  81. lci_Type,
  82. lci_Ident : DWord;
  83. { There are private fields hiding here. }
  84. END;
  85. {
  86. * StoredProperty: a local context item containing the data stored
  87. * from a previously encountered property chunk.
  88. }
  89. pStoredProperty = ^tStoredProperty;
  90. tStoredProperty = Record
  91. sp_Size : Longint;
  92. sp_Data : Pointer;
  93. END;
  94. {
  95. * Collection Item: the actual node in the collection list at which
  96. * client will look. The next pointers cross context boundaries so
  97. * that the complete list is accessable.
  98. }
  99. pCollectionItem = ^tCollectionItem;
  100. tCollectionItem = record
  101. ci_Next : pCollectionItem;
  102. ci_Size : Longint;
  103. ci_Data : Pointer;
  104. END;
  105. {
  106. * Structure returned by OpenClipboard(). You may do CMD_POSTs and such
  107. * using this structure. However, once you call OpenIFF(), you may not
  108. * do any more of your own I/O to the clipboard until you call CloseIFF().
  109. }
  110. pClipboardHandle = ^tClipBoardHandle;
  111. tClipboardHandle = record
  112. cbh_Req : tIOClipReq;
  113. cbh_CBport,
  114. cbh_SatisfyPort : tMsgPort;
  115. END;
  116. {
  117. * IFF return codes. Most functions return either zero for success or
  118. * one of these codes. The exceptions are the read/write functions which
  119. * return positive values for number of bytes or records read or written,
  120. * or a negative error code. Some of these codes are not errors per sae,
  121. * but valid conditions such as EOF or EOC (End of Chunk).
  122. }
  123. CONST
  124. IFFERR_EOF = -1 ; { Reached logical END of file }
  125. IFFERR_EOC = -2 ; { About to leave context }
  126. IFFERR_NOSCOPE = -3 ; { No valid scope for property }
  127. IFFERR_NOMEM = -4 ; { Internal memory alloc failed}
  128. IFFERR_READ = -5 ; { Stream read error }
  129. IFFERR_WRITE = -6 ; { Stream write error }
  130. IFFERR_SEEK = -7 ; { Stream seek error }
  131. IFFERR_MANGLED = -8 ; { Data in file is corrupt }
  132. IFFERR_SYNTAX = -9 ; { IFF syntax error }
  133. IFFERR_NOTIFF = -10; { Not an IFF file }
  134. IFFERR_NOHOOK = -11; { No call-back hook provided }
  135. IFF_RETURN2CLIENT = -12; { Client handler normal return}
  136. {
  137. MAKE_ID(a,b,c,d) \
  138. ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  139. }
  140. {
  141. * Universal IFF identifiers.
  142. }
  143. ID_FORM = 1179603533;
  144. ID_LIST = 1279873876;
  145. ID_CAT = 1128354848;
  146. ID_PROP = 1347571536;
  147. ID_NULL = 538976288;
  148. {
  149. * Ident codes for universally recognized local context items.
  150. }
  151. IFFLCI_PROP = 1886547824;
  152. IFFLCI_COLLECTION = 1668246636;
  153. IFFLCI_ENTRYHANDLER = 1701734500;
  154. IFFLCI_EXITHANDLER = 1702389860;
  155. {
  156. * Control modes for ParseIFF() function.
  157. }
  158. IFFPARSE_SCAN = 0;
  159. IFFPARSE_STEP = 1;
  160. IFFPARSE_RAWSTEP = 2;
  161. {
  162. * Control modes for StoreLocalItem().
  163. }
  164. IFFSLI_ROOT = 1; { Store in default context }
  165. IFFSLI_TOP = 2; { Store in current context }
  166. IFFSLI_PROP = 3; { Store in topmost FORM OR LIST }
  167. {
  168. * "Flag" for writing functions. If you pass this value in as a size
  169. * to PushChunk() when writing a file, the parser will figure out the
  170. * size of the chunk for you. (Chunk sizes >= 2**31 are forbidden by the
  171. * IFF specification, so this works.)
  172. }
  173. IFFSIZE_UNKNOWN = -1;
  174. {
  175. * Possible call-back command values. (Using 0 as the value for IFFCMD_INIT
  176. * was, in retrospect, probably a bad idea.)
  177. }
  178. IFFCMD_INIT = 0; { Prepare the stream for a session }
  179. IFFCMD_CLEANUP = 1; { Terminate stream session }
  180. IFFCMD_READ = 2; { Read bytes from stream }
  181. IFFCMD_WRITE = 3; { Write bytes to stream }
  182. IFFCMD_SEEK = 4; { Seek on stream }
  183. IFFCMD_ENTRY = 5; { You just entered a new context }
  184. IFFCMD_EXIT = 6; { You're about to leave a context }
  185. IFFCMD_PURGELCI= 7; { Purge a LocalContextItem }
  186. { Backward compatibility. Don't use these in new code. }
  187. IFFSCC_INIT = IFFCMD_INIT;
  188. IFFSCC_CLEANUP = IFFCMD_CLEANUP;
  189. IFFSCC_READ = IFFCMD_READ;
  190. IFFSCC_WRITE = IFFCMD_WRITE;
  191. IFFSCC_SEEK = IFFCMD_SEEK;
  192. { Seek modes for SeekChunkBytes and SeekChunkRecords }
  193. IFFOFFSET_BEGINNING = 0;
  194. IFFOFFSET_END = 1;
  195. IFFOFFSET_CURRENT = 2;
  196. VAR IFFParseBase : pLibrary = nil;
  197. FUNCTION AllocIFF : pIFFHandle; syscall IFFParseBase 030;
  198. FUNCTION AllocLocalItem(typ : LONGINT location 'd0'; id : LONGINT location 'd1'; ident : LONGINT location 'd2'; dataSize : LONGINT location 'd3') : pLocalContextItem; syscall IFFParseBase 186;
  199. PROCEDURE CloseClipboard(clipHandle : pClipboardHandle location 'a0'); syscall IFFParseBase 252;
  200. PROCEDURE CloseIFF(iff : pIFFHandle location 'a0'); syscall IFFParseBase 048;
  201. FUNCTION CollectionChunk(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 138;
  202. FUNCTION CollectionChunks(iff : pIFFHandle location 'a0'; const propArray : pLONGINT location 'a1'; numPairs : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 144;
  203. FUNCTION CurrentChunk(const iff : pIFFHandle location 'a0') : pContextNode; syscall IFFParseBase 174;
  204. FUNCTION EntryHandler(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1'; position : LONGINT location 'd2'; handler : pHook location 'a1'; obj : POINTER location 'a2') : LONGINT; syscall IFFParseBase 102;
  205. FUNCTION ExitHandler(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1'; position : LONGINT location 'd2'; handler : pHook location 'a1'; obj : POINTER location 'a2') : LONGINT; syscall IFFParseBase 108;
  206. FUNCTION FindCollection(const iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1') : pCollectionItem; syscall IFFParseBase 162;
  207. FUNCTION FindLocalItem(const iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1'; ident : LONGINT location 'd2') : pLocalContextItem; syscall IFFParseBase 210;
  208. FUNCTION FindProp(const iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1') : pStoredProperty; syscall IFFParseBase 156;
  209. FUNCTION FindPropContext(const iff : pIFFHandle location 'a0') : pContextNode; syscall IFFParseBase 168;
  210. PROCEDURE FreeIFF(iff : pIFFHandle location 'a0'); syscall IFFParseBase 054;
  211. PROCEDURE FreeLocalItem(localItem : pLocalContextItem location 'a0'); syscall IFFParseBase 204;
  212. FUNCTION GoodID(id : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 258;
  213. FUNCTION GoodType(typ : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 264;
  214. FUNCTION IDtoStr(id : LONGINT location 'd0'; buf : pCHAR location 'a0') : pCHAR; syscall IFFParseBase 270;
  215. PROCEDURE InitIFF(iff : pIFFHandle location 'a0'; flags : LONGINT location 'd0'; const streamHook : pHook location 'a1'); syscall IFFParseBase 228;
  216. PROCEDURE InitIFFasClip(iff : pIFFHandle location 'a0'); syscall IFFParseBase 240;
  217. PROCEDURE InitIFFasDOS(iff : pIFFHandle location 'a0'); syscall IFFParseBase 234;
  218. FUNCTION LocalItemData(const localItem : pLocalContextItem location 'a0') : POINTER; syscall IFFParseBase 192;
  219. FUNCTION OpenClipboard(unitNumber : LONGINT location 'd0') : pClipboardHandle; syscall IFFParseBase 246;
  220. FUNCTION OpenIFF(iff : pIFFHandle location 'a0'; rwMode : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 036;
  221. FUNCTION ParentChunk(const contextNode : pContextNode location 'a0') : pContextNode; syscall IFFParseBase 180;
  222. FUNCTION ParseIFF(iff : pIFFHandle location 'a0'; control : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 042;
  223. FUNCTION PopChunk(iff : pIFFHandle location 'a0') : LONGINT; syscall IFFParseBase 090;
  224. FUNCTION PropChunk(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 114;
  225. FUNCTION PropChunks(iff : pIFFHandle location 'a0'; const propArray : pLONGINT location 'a1'; numPairs : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 120;
  226. FUNCTION PushChunk(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1'; size : LONGINT location 'd2') : LONGINT; syscall IFFParseBase 084;
  227. FUNCTION ReadChunkBytes(iff : pIFFHandle location 'a0'; buf : POINTER location 'a1'; numBytes : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 060;
  228. FUNCTION ReadChunkRecords(iff : pIFFHandle location 'a0'; buf : POINTER location 'a1'; bytesPerRecord : LONGINT location 'd0'; numRecords : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 072;
  229. PROCEDURE SetLocalItemPurge(localItem : pLocalContextItem location 'a0'; const purgeHook : pHook location 'a1'); syscall IFFParseBase 198;
  230. FUNCTION StopChunk(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 126;
  231. FUNCTION StopChunks(iff : pIFFHandle location 'a0'; const propArray : pLONGINT location 'a1'; numPairs : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 132;
  232. FUNCTION StopOnExit(iff : pIFFHandle location 'a0'; typ : LONGINT location 'd0'; id : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 150;
  233. PROCEDURE StoreItemInContext(iff : pIFFHandle location 'a0'; localItem : pLocalContextItem location 'a1'; contextNode : pContextNode location 'a2'); syscall IFFParseBase 222;
  234. FUNCTION StoreLocalItem(iff : pIFFHandle location 'a0'; localItem : pLocalContextItem location 'a1'; position : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 216;
  235. FUNCTION WriteChunkBytes(iff : pIFFHandle location 'a0'; const buf : POINTER location 'a1'; numBytes : LONGINT location 'd0') : LONGINT; syscall IFFParseBase 066;
  236. FUNCTION WriteChunkRecords(iff : pIFFHandle location 'a0'; const buf : POINTER location 'a1'; bytesPerRecord : LONGINT location 'd0'; numRecords : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 078;
  237. { MorphOS specific }
  238. FUNCTION SeekChunkBytes(iff : pIFFHandle location 'a0'; numBytes : LONGINT location 'd0'; mode : LONGINT location 'd1') : LONGINT; syscall IFFParseBase 276;
  239. FUNCTION SeekChunkRecords(iff : pIFFHandle location 'a0'; bytesPerRecord : LONGINT location 'd0'; numRecords : LONGINT location 'd1'; mode : LONGINT location 'd2') : LONGINT; syscall IFFParseBase 282;
  240. Function Make_ID(const str : String) : LONGINT;
  241. function InitIFFPARSELibrary: boolean;
  242. IMPLEMENTATION
  243. function Make_ID(const str : String) : LONGINT;
  244. begin
  245. Make_ID := (LONGINT(Ord(Str[1])) shl 24) or
  246. (LONGINT(Ord(Str[2])) shl 16 ) or
  247. (LONGINT(Ord(Str[3])) shl 8 ) or
  248. (LONGINT(Ord(Str[4])));
  249. end;
  250. const
  251. { Change VERSION and LIBVERSION to proper values }
  252. VERSION : string[2] = '0';
  253. LIBVERSION : longword = 0;
  254. function InitIFFParseLibrary: boolean;
  255. begin
  256. InitIFFParseLibrary := Assigned(IFFParseBase);
  257. end;
  258. initialization
  259. IFFParseBase := OpenLibrary(IFFPARSENAME,LIBVERSION);
  260. finalization
  261. if Assigned(IFFParseBase) then
  262. CloseLibrary(IFFParseBase);
  263. END. (* UNIT IFFPARSE *)