disable debug.
[AROS.git] / workbench / demos / sift.c
blob8a0d819a79502e2480a5281ea481925f6faa7551
1 ;/* sift.c - Execute me to compile me with SAS C 5.10
2 LC -b1 -cfistq -v -j73 sift.c
3 Blink FROM LIB:c.o,sift.o TO sift LIBRARY LIB:LC.lib,LIB:Amiga.lib
4 quit
5 */
7 /*
8 Copyright (c) 1992 Commodore-Amiga, Inc.
10 This example is provided in electronic form by Commodore-Amiga, Inc. for
11 use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
12 published by Addison-Wesley (ISBN 0-201-56774-1).
14 The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
15 information on the correct usage of the techniques and operating system
16 functions presented in these examples. The source and executable code
17 of these examples may only be distributed in free electronic form, via
18 bulletin board or as part of a fully non-commercial and freely
19 redistributable diskette. Both the source and executable code (including
20 comments) must be included, without modification, in any copy. This
21 example may not be published in printed form or distributed with any
22 commercial product. However, the programming techniques and support
23 routines set forth in these examples may be used in the development
24 of original executable software products for Commodore Amiga computers.
26 All other rights reserved.
28 This example is provided "as-is" and is subject to change; no
29 warranties are made. All use is at your own risk. No liability or
30 responsibility is assumed.
34 AROS NOTE
36 This example had to be changed, because it
38 a) uses obsolete includes, which are not included in the AROS distribution and
39 b) is not fully compatible with gcc.
41 All changes are indicated by AROS NOTE.
46 * sift.c: Takes any IFF file and tells you what's in it. Verifies syntax and all that cool stuff.
48 * Usage: sift -c ; For clipboard scanning
49 * or sift <file> ; For DOS file scanning
51 * Reads the specified stream and prints an IFFCheck-like listing of the contents of the IFF file, if any.
52 * Stream is a DOS file for <file> argument, or is the clipboard's primary clip for -c.
53 * This program must be run from a CLI.
57 #include <exec/types.h>
58 #include <exec/memory.h>
60 AROS NOTE: In the Commodore version of sift the following include was
61 <libraries/dos.h>. But because <libraries/dos.h> is considered obsolete
62 and AROS doesn't include any obsolete includes or definitions, it was
63 changed to <dos/dos.h>.
65 #include <dos/dos.h>
66 #include <libraries/iffparse.h>
67 #include <proto/exec.h>
68 #include <proto/dos.h>
69 #include <proto/iffparse.h>
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <string.h>
74 #ifdef LATTICE
75 int CXBRK(void) { return(0); } /* Disable Lattice CTRL/C handling */
76 int chkabort(void) { return(0); } /* really */
77 #endif
79 #define MINARGS 2
82 AROS NOTE: The version was changed from 37.1 to 37.2 and the current date
83 was included. The date was missing in the Commodore version.
84 The original string was: "\0$VER: sift 37.1"
86 UBYTE vers[] = "\0$VER: sift 37.2 (6.3.97)"; /* 2.0 Version string for c:Version to find */
87 UBYTE usage[] = "Usage: sift IFFfilename (or -c for clipboard)";
89 void PrintTopChunk (struct IFFHandle *); /* proto for our function */
92 * Text error messages for possible IFFERR_#? returns from various IFF routines. To get the index into
93 * this array, take your IFFERR code, negate it, and subtract one.
94 * idx = -error - 1;
96 char *errormsgs[] = {
97 "End of file (not an error).", "End of context (not an error).", "No lexical scope.",
98 "Insufficient memory.", "Stream read error.", "Stream write error.",
99 "Stream seek error.", "File is corrupt.", "IFF syntax error.",
100 "Not an IFF file.", "Required call-back hook missing.", "Return to client. You should never see this."
103 struct Library *IFFParseBase;
105 int main(int argc, char **argv)
107 struct IFFHandle *iff = NULL;
108 long error;
109 short cbio = 0;
111 /* if not enough args or '?', print usage */
112 if(((argc)&&(argc<MINARGS))||(argv[argc-1][0]=='?'))
114 printf("%s\n", usage);
115 goto bye;
118 /* Check to see if we are doing I/O to the Clipboard. */
119 cbio = (argv[1][0] == '-' && argv[1][1] == 'c');
121 if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L)))
123 puts("Can't open iff parsing library.");
124 goto bye;
127 /* Allocate IFF_File structure. */
128 if (!(iff = AllocIFF ()))
130 puts ("AllocIFF() failed.");
131 goto bye;
135 * Internal support is provided for both AmigaDOS files, and the clipboard.device. This bizarre
136 * 'if' statement performs the appropriate machinations for each case.
138 if (cbio)
141 * Set up IFF_File for Clipboard I/O.
143 if (!(iff->iff_Stream =
144 (IPTR) OpenClipboard (PRIMARY_CLIP)))
146 puts ("Clipboard open failed.");
147 goto bye;
149 InitIFFasClip (iff);
151 else
153 /* Set up IFF_File for AmigaDOS I/O. */
155 AROS NOTE: Added cast. This is necessary, because Open returns a BPTR,
156 but iff_Stream is declared as IPTR, which is in fact an unsigned integer.
157 IPTR is used in AROS instead of ULONG, if the corresponding field stores
158 a value, which may be used as pointer.
160 if (!(iff->iff_Stream = (IPTR)Open (argv[1], MODE_OLDFILE)))
163 AROS NOTE: Show what error it was */
164 PrintFault (IoErr (), "File open failed");
165 goto bye;
167 InitIFFasDOS (iff);
170 /* Start the IFF transaction. */
172 AROS NOTE: Added extra parentheses around "error = OpenIFF()". Gcc wants
173 this for some reason.
175 if ((error = OpenIFF (iff, IFFF_READ)))
177 puts ("OpenIFF failed.");
178 goto bye;
181 while (1)
184 * The interesting bit. IFFPARSE_RAWSTEP permits us to have precision monitoring of the
185 * parsing process, which is necessary if we wish to print the structure of an IFF file.
186 * ParseIFF() with _RAWSTEP will return the following things for the following reasons:
188 * Return code: Reason:
189 * 0 Entered new context.
190 * IFFERR_EOC About to leave a context.
191 * IFFERR_EOF Encountered end-of-file.
192 * <anything else> A parsing error.
194 error = ParseIFF (iff, IFFPARSE_RAWSTEP);
197 * Since we're only interested in when we enter a context, we "discard" end-of-context
198 * (_EOC) events.
200 if (error == IFFERR_EOC)
201 continue;
202 else if (error)
204 * Leave the loop if there is any other error.
206 break;
209 /* If we get here, error was zero. Print out the current state of affairs. */
210 PrintTopChunk (iff);
214 * If error was IFFERR_EOF, then the parser encountered the end of
215 * the file without problems. Otherwise, we print a diagnostic.
217 if (error == IFFERR_EOF)
218 puts ("File scan complete.");
219 else
220 printf ("File scan aborted, error %ld: %s\n",
221 error, errormsgs[-error - 1]);
223 bye:
224 if (iff) {
225 /* Terminate the IFF transaction with the stream. Free all associated structures. */
226 CloseIFF (iff);
229 * Close the stream itself.
231 if (iff->iff_Stream)
233 if (cbio)
234 CloseClipboard ((struct ClipboardHandle *)
235 iff->iff_Stream);
236 else
238 AROS NOTE: Added cast. See above for reasons.
240 Close ((BPTR)iff->iff_Stream);
243 /* Free the IFF_File structure itself. */
244 FreeIFF (iff);
246 if (IFFParseBase) CloseLibrary (IFFParseBase);
248 exit (RETURN_OK);
251 void
252 PrintTopChunk (iff)
253 struct IFFHandle *iff;
255 struct ContextNode *top;
256 short i;
257 char idbuf[5];
259 /* Get a pointer to the context node describing the current context. */
260 if (!(top = CurrentChunk (iff)))
261 return;
264 * Print a series of dots equivalent to the current nesting depth of chunks processed so far.
265 * This will cause nested chunks to be printed out indented.
267 for (i = iff->iff_Depth; i--; )
268 printf (". ");
270 /* Print out the current chunk's ID and size. */
272 /* Print the current chunk's type, with a newline. */
273 puts (IDtoStr (top->cn_Type, idbuf));