home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 61.dms / 61.adf / source.files / iffcheck.c < prev    next >
C/C++ Source or Header  |  1986-03-21  |  7KB  |  216 lines

  1. /*--------------------------------------------------------------------- */
  2. /* IFFCheck.C  Print out the structure of an IFF-85 file,      1/23/86  */
  3. /* checking for structural errors.                    */
  4. /*                                    */
  5. /* DO NOT USE THIS AS A SKELETAL PROGRAM FOR AN IFF READER!        */
  6. /* See ShowILBM.C for a skeletal example.                */
  7. /*                                                                      */ 
  8. /* By Jerry Morrison and Steve Shaw, Electronic Arts.                   */ 
  9. /* This software is in the public domain.                               */ 
  10. /*                                                                      */ 
  11. /* This version for the Commodore-Amiga computer.                       */
  12. /*                                                                      */ 
  13. /* ---------------------------------------------------------------------*/
  14. #include "iff/iff.h"
  15.  
  16.  
  17. /* ---------- IFFCheck -------------------------------------------------*/
  18. /* [TBD] More extensive checking could be done on the IDs encountered in the
  19.  * file. Check that the reserved IDs "FOR1".."FOR9", "LIS1".."LIS9", and
  20.  * "CAT1".."CAT9" aren't used. Check that reserved IDs aren't used as Form
  21.  * types. Check that all IDs are made of 4 printable characters (trailing
  22.  * spaces ok). */
  23.  
  24. typedef struct {
  25.     ClientFrame clientFrame;
  26.     int levels;        /* # groups currently nested within.*/
  27.     } Frame;
  28.  
  29. char MsgOkay[] = { "----- (IFF_OKAY) A good IFF file." };
  30. char MsgEndMark[] = {"----- (END_MARK) How did you get this message??" };
  31. char MsgDone[] = { "----- (IFF_DONE) How did you get this message??" };
  32. char MsgDos[] = { "----- (DOS_ERROR) The DOS gave back an error." };
  33. char MsgNot[] = { "----- (NOT_IFF) not an IFF file." };
  34. char MsgNoFile[] = { "----- (NO_FILE) no such file found." };
  35. char MsgClientError[] = {"----- (CLIENT_ERROR) IFF Checker bug."};
  36. char MsgForm[] = { "----- (BAD_FORM) How did you get this message??" };
  37. char MsgShort[] = { "----- (SHORT_CHUNK) How did you get this message??" };
  38. char MsgBad[] = { "----- (BAD_IFF) a mangled IFF file." };
  39.  
  40. /* MUST GET THESE IN RIGHT ORDER!!*/
  41. char *IFFPMessages[-(int)LAST_ERROR+1] = {
  42.     /*IFF_OKAY*/  MsgOkay,
  43.     /*END_MARK*/  MsgEndMark,
  44.     /*IFF_DONE*/  MsgDone,
  45.     /*DOS_ERROR*/ MsgDos,
  46.     /*NOT_IFF*/   MsgNot,
  47.     /*NO_FILE*/   MsgNoFile,
  48.     /*CLIENT_ERROR*/ MsgClientError,
  49.     /*BAD_FORM*/  MsgForm,
  50.     /*SHORT_CHUNK*/  MsgShort,
  51.     /*BAD_IFF*/   MsgBad
  52.     };
  53.  
  54. /* FORWARD REFERENCES */
  55. extern IFFP GetList(GroupContext *);
  56. extern IFFP GetForm(GroupContext *);
  57. extern IFFP GetProp(GroupContext *);
  58. extern IFFP GetCat (GroupContext *);
  59.  
  60. void IFFCheck(name)  char *name; {
  61.     IFFP iffp;
  62.     BPTR file = Open(name, MODE_OLDFILE);
  63.     Frame frame;
  64.  
  65.     frame.levels = 0;
  66.     frame.clientFrame.getList = GetList;
  67.     frame.clientFrame.getForm = GetForm;
  68.     frame.clientFrame.getProp = GetProp;
  69.     frame.clientFrame.getCat  = GetCat ;
  70.  
  71.     printf("----- Checking file '%s' -----\n", name);
  72.     if (file == 0)
  73.     iffp = NO_FILE;
  74.     else
  75.     iffp = ReadIFF(file, (ClientFrame *)&frame);
  76.  
  77.     Close(file);
  78.     printf("%s\n", IFFPMessages[-iffp]);
  79.     }
  80.  
  81. main(argc, argv)   int argc;  char **argv; {
  82.     if (argc != 1+1) {
  83.     printf("Usage: 'iffcheck filename'\n");
  84.     exit(0);
  85.     }
  86.     IFFCheck(argv[1]);
  87.     }
  88.  
  89. /* ---------- Put... ---------------------------------------------------*/
  90.  
  91. PutLevels(count)   int count; {
  92.     for ( ;  count > 0;  --count) {
  93.     printf(".");
  94.     }
  95.     }
  96.  
  97. PutID(id)  ID id; {
  98.     printf("%c%c%c%c",
  99.        (char)((id>>24L) & 0x7f),
  100.        (char)((id>>16L) & 0x7f),
  101.        (char)((id>>8)   & 0x7f),
  102.        (char)(id        & 0x7f) );
  103.     }
  104.  
  105. PutN(n)   int n; {
  106.     printf(" %d ", n);
  107.     }
  108.  
  109. /* Put something like "...BMHD 14" or "...LIST 14 PLBM". */
  110. PutHdr(context)  GroupContext *context; {
  111.     PutLevels( ((Frame *)context->clientFrame)->levels );
  112.     PutID(context->ckHdr.ckID);
  113.     PutN(context->ckHdr.ckSize);
  114.  
  115.     if (context->subtype != NULL_CHUNK)
  116.     PutID(context->subtype);
  117.  
  118.     printf("\n");
  119.     }
  120.  
  121. /* ---------- AtLeaf ---------------------------------------------------*/
  122.  
  123. /* At Leaf chunk.  That is, a chunk which does NOT contain other chunks.
  124.  * Print "ID size".*/
  125. IFFP AtLeaf(context)  GroupContext *context; {
  126.  
  127.     PutHdr(context);
  128.     /* A typical reader would read the chunk's contents, using the "Frame"
  129.      * for local data, esp. shared property settings (PROP).*/
  130.     /* IFFReadBytes(context, ...buffer, context->ckHdr->ckSize); */
  131.     return(IFF_OKAY);
  132.     }
  133.  
  134. /* ---------- GetList --------------------------------------------------*/
  135. /* Handle a LIST chunk.  Print "LIST size subTypeID".
  136.  * Then dive into it.*/
  137. IFFP GetList(parent)  GroupContext *parent; {
  138.     Frame newFrame;
  139.  
  140.     newFrame = *(Frame *)parent->clientFrame;  /* copy parent's frame*/
  141.     newFrame.levels++;
  142.  
  143.     PutHdr(parent);
  144.  
  145.     return( ReadIList(parent, (ClientFrame *)&newFrame) );
  146.     }
  147.  
  148. /* ---------- GetForm --------------------------------------------------*/
  149. /* Handle a FORM chunk.  Print "FORM size subTypeID".
  150.  * Then dive into it.*/
  151. IFFP GetForm(parent)   GroupContext *parent; {
  152.     /*CompilerBug register*/ IFFP iffp;
  153.     GroupContext new;
  154.     Frame newFrame;
  155.  
  156.     newFrame = *(Frame *)parent->clientFrame;  /* copy parent's frame*/
  157.     newFrame.levels++;
  158.  
  159.     PutHdr(parent);
  160.  
  161.     iffp = OpenRGroup(parent, &new);
  162.     CheckIFFP();
  163.     new.clientFrame = (ClientFrame *)&newFrame;
  164.  
  165.     /* FORM reader for Checker. */
  166.     /* LIST, FORM, PROP, CAT already handled by GetF1ChunkHdr. */
  167.     do {if ( (iffp = GetF1ChunkHdr(&new)) > 0 )
  168.         iffp = AtLeaf(&new);
  169.     } while (iffp >= IFF_OKAY);
  170.  
  171.     CloseRGroup(&new);
  172.     return(iffp == END_MARK ? IFF_OKAY : iffp);
  173.     }
  174.  
  175. /* ---------- GetProp --------------------------------------------------*/
  176. /* Handle a PROP chunk.  Print "PROP size subTypeID".
  177.  * Then dive into it.*/
  178. IFFP GetProp(listContext)  GroupContext *listContext; {
  179.     /*CompilerBug register*/ IFFP iffp;
  180.     GroupContext new;
  181.  
  182.     PutHdr(listContext);
  183.  
  184.     iffp = OpenRGroup(listContext, &new);
  185.     CheckIFFP();
  186.  
  187.     /* PROP reader for Checker. */
  188.     ((Frame *)listContext->clientFrame)->levels++;
  189.  
  190.     do {if ( (iffp = GetPChunkHdr(&new)) > 0 )
  191.         iffp = AtLeaf(&new);
  192.     } while (iffp >= IFF_OKAY);
  193.  
  194.     ((Frame *)listContext->clientFrame)->levels--;
  195.  
  196.     CloseRGroup(&new);
  197.     return(iffp == END_MARK ? IFF_OKAY : iffp);
  198.     }
  199.  
  200. /* ---------- GetCat ---------------------------------------------------*/
  201. /* Handle a CAT chunk.  Print "CAT size subTypeID".
  202.  * Then dive into it.*/
  203. IFFP GetCat(parent)  GroupContext *parent;  {
  204.     IFFP iffp;
  205.  
  206.     ((Frame *)parent->clientFrame)->levels++;
  207.  
  208.     PutHdr(parent);
  209.  
  210.     iffp = ReadICat(parent);
  211.  
  212.     ((Frame *)parent->clientFrame)->levels--;
  213.     return(iffp);
  214.     }
  215.  
  216.