home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / parse.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  10KB  |  447 lines

  1. //    $Author: M_J_Paddock $
  2. //    $Date: 1993/06/06 23:17:33 $
  3. //    $Revision: 1.2 $
  4.  
  5. /*  
  6.  * parse.c - iffparse file IO support module
  7.  *   based on some of looki.c by Leo Schwab
  8.  *
  9.  * The filename for clipboard is -c or -cUnit as in -c0 -c1 etc. (default 0)
  10.  */
  11.  
  12. // Minor changes to error handlin MJP
  13.  
  14. #include <exec/types.h>
  15.  
  16. #include "iffp/iff.h"
  17.  
  18. /* local function prototypes */
  19.  
  20. static LONG stdio_stream(struct Hook *, struct IFFHandle *, struct IFFStreamCmd *);
  21.  
  22. UBYTE *omodes[2] = {"r","w"};
  23.  
  24.  
  25. /* openifile
  26.  *
  27.  * Passed a ParseInfo structure with a not-in-use IFFHandle, filename
  28.  *   ("-c" or -cUnit like "-c1" for clipboard), and IFF open mode
  29.  *   (IFFF_READ or IFFF_WRITE) opens file or clipboard for use with
  30.  *   iffparse.library support modules.
  31.  *
  32.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  33.  */
  34.  
  35. LONG openifile(struct ParseInfo *pi, UBYTE *filename, ULONG iffopenmode)
  36. {
  37.     struct IFFHandle    *iff;
  38.     BOOL    cboard;
  39.     ULONG     unit = PRIMARY_CLIP;
  40.     LONG     error;
  41.  
  42.     if(!pi)            return(CLIENT_ERROR);
  43.         if(!(iff=pi->iff))    return(CLIENT_ERROR);
  44.  
  45.     cboard = (*filename == '-'  &&  filename[1] == 'c');
  46.      if(cboard && filename[2])    unit = atoi(&filename[2]);
  47.  
  48.     if (cboard)
  49.         {
  50.         /*
  51.          * Set up IFFHandle for Clipboard I/O.
  52.          */
  53.         pi->clipboard = TRUE;
  54.         if (!(iff->iff_Stream =
  55.                 (ULONG)OpenClipboard(unit)))
  56.             {
  57. //            message(SI(MSG_IFFP_NOCLIP_D),unit);    // MJP
  58.             message(SI(MSG_IFFP_NOCLIP_D),NULL,5);    // MJP 5 = HE_IFFCLIP
  59.             return(NOFILE);
  60.             }
  61.         InitIFFasClip(iff);
  62.         }
  63.     else
  64.         {
  65.         pi->clipboard = FALSE;
  66.         /*
  67.          * Set up IFFHandle for buffered stdio I/O.
  68.          */
  69.         if (!(iff->iff_Stream = (ULONG)
  70.            fopen(filename, omodes[iffopenmode & 1])))
  71.             {
  72.             message(SI(MSG_IFFP_NOFILE_S),filename,7);    // MJP 7 = HE_IFFFileS
  73.             return(NOFILE);
  74.             }
  75.         else initiffasstdio(iff);
  76.         }
  77.  
  78.     D(bug("%s file opened: \n", cboard ? "[Clipboard]" : filename));
  79.  
  80.     pi->filename = filename;
  81.  
  82.     error=OpenIFF(iff, iffopenmode);
  83.  
  84.     pi->opened = error ? FALSE : TRUE;    /* currently open handle */
  85.  
  86.     D(bug("OpenIFF error = %ld\n",error));
  87.     return(error);
  88. }
  89.  
  90.  
  91. /* closeifile
  92.  *
  93.  * closes file or clipboard opened with openifile, and frees all
  94.  *   iffparse context parsed by parseifile.
  95.  *
  96.  * Note - You should closeifile as soon as possible if using clipboard
  97.  *   ("-c[n]").  You also need to closeifile if, for example, you wish to
  98.  *   reopen the file to write changes back out.  See the copychunks.c
  99.  *   module for routines which allow you clone the chunks iffparse has
  100.  *   gathered so that you can closeifile and still be able to modify and
  101.  *   write back out gathered chunks.
  102.  *   
  103.  */
  104.  
  105. void closeifile(struct ParseInfo *pi)
  106. {
  107. struct IFFHandle *iff;
  108.  
  109.     D(bug("closeifile:\n"));
  110.  
  111.     if(!pi)            return;
  112.         if(!(iff=pi->iff))    return;
  113.  
  114.     DD(bug("closeifile: About to CloseIFF if open, iff=$%lx, opened=%ld\n",
  115.             iff, pi->opened));
  116.  
  117.     if(pi->opened)    CloseIFF(iff);
  118.  
  119.     DD(bug("closeifile: About to close %s, stream=$%lx\n",
  120.             pi->clipboard ? "clipboard" : "file", iff->iff_Stream));
  121.     if(iff->iff_Stream)
  122.         {
  123.         if (pi->clipboard)
  124.            CloseClipboard((struct ClipHandle *)(iff->iff_Stream));
  125.         else
  126.            fclose ((FILE *)(iff->iff_Stream));
  127.         }
  128.  
  129.     iff->iff_Stream = NULL;
  130.     pi->clipboard = NULL;
  131.     pi->opened = NULL;
  132. }
  133.  
  134.  
  135. /* parseifile
  136.  *
  137.  * Passed a ParseInfo with an initialized and open IFFHandle,
  138.  *  grouptype (like ID_FORM), groupid (like ID_ILBM),
  139.  *  and TAG_DONE terminated longword arrays of type,id
  140.  *  for chunks to be grabbed, gathered, and stopped on
  141.  *  (like { ID_ILBM, ID_BMHD, ID_ILBM, ID_CAMG, TAG_DONE })
  142.  *  will parse an IFF file, grabbing/gathering and stopping
  143.  *  on specified chunk.
  144.  *
  145.  * Note - you can call getcontext() (to continue after a stop chunk) or
  146.  *  nextcontext() (after IFFERR_EOC, to parse next form in the same file)
  147.  *  if you wish to continue parsing the same IFF file.  If parseifile()
  148.  *  has to delve into a complex format to find your desired FORM, the
  149.  *  pi->hunt flag will be set.  This should be a signal to you that
  150.  *  you may not have the capability to simply modify and rewrite
  151.  *  the data you have gathered.
  152.  *
  153.  * Returns 0 for success else and IFFERR (libraries/iffparse.h)
  154.  */ 
  155.  
  156. LONG parseifile(pi,groupid,grouptype,propchks,collectchks,stopchks)
  157. struct    ParseInfo *pi;
  158. LONG groupid, grouptype;
  159. LONG *propchks, *collectchks, *stopchks;
  160. {
  161. struct IFFHandle *iff;    
  162. register struct ContextNode    *cn;
  163. LONG            error = 0L;
  164.  
  165.  
  166.         if(!(iff=pi->iff))    return(CLIENT_ERROR);
  167.  
  168.     if(!iff->iff_Stream)    return(IFFERR_READ);
  169.  
  170.     pi->hunt = FALSE;
  171.  
  172.     /*
  173.      * Declare property, collection and stop chunks.
  174.      */
  175.     if (propchks)
  176.       if (error = PropChunks (iff, propchks, chkcnt(propchks)))
  177.         return (error);
  178.     if (collectchks)
  179.       if (error =
  180.           CollectionChunks(iff, collectchks, chkcnt(collectchks)))
  181.         return (error);
  182.     if (stopchks)
  183.       if (error = StopChunks (iff, stopchks, chkcnt(stopchks)))
  184.         return (error);
  185.  
  186.     /*
  187.      * We want to stop at the end of an ILBM context.
  188.      */
  189.     if (grouptype)
  190.       if (error = StopOnExit (iff, grouptype, groupid))
  191.         return(error);
  192.  
  193.     /*
  194.      * Take first parse step to enter main chunk.
  195.      */
  196.     if (error = ParseIFF (iff, IFFPARSE_STEP))
  197.         return(error);
  198.  
  199.     /*
  200.      * Test the chunk info to see if simple form of type we want (ILBM).
  201.      */
  202.     if (!(cn = CurrentChunk (iff)))
  203.         {
  204.         /*
  205.          * This really should never happen.  If it does, it means
  206.          * our parser is broken.
  207.          */
  208.         message(SI(MSG_IFFP_NOTOP),NULL,8);        // MJP 8 = HE_IFFTOP
  209.         return(NOFILE);
  210.         }
  211.  
  212.     if (cn->cn_ID != groupid  ||  cn->cn_Type != grouptype)
  213.         {
  214.         
  215.         D(bug("This is a(n) %.4s.%.4s.  Looking for embedded %.4s's...\n",
  216.           &cn->cn_Type, &cn->cn_ID, &grouptype));
  217.  
  218.         pi->hunt = TRUE;    /* Warning - this is a complex file */
  219.         }
  220.  
  221.     if(!error)    error = getcontext(iff);
  222.     return(error);
  223. }
  224.  
  225. /* chkcnt
  226.  *
  227.  * simply counts the number of chunk pairs (type,id) in array
  228.  */
  229. LONG chkcnt(LONG *taggedarray)
  230. {
  231. LONG k = 0;
  232.  
  233.     while(taggedarray[k] != TAG_DONE) k++;
  234.     return(k>>1);
  235. }
  236.  
  237.  
  238. /* currentchunkis
  239.  *
  240.  * returns the ID of the current chunk (like ID_CAMG)
  241.  */
  242. LONG currentchunkis(struct IFFHandle *iff, LONG type, LONG id)
  243. {
  244. register struct ContextNode    *cn;
  245. LONG result = 0;
  246.  
  247.     if (cn = CurrentChunk (iff))
  248.         {
  249.         if((cn->cn_Type == type)&&(cn->cn_ID == id)) result = 1;
  250.         }
  251.     return(result);
  252. }
  253.  
  254.  
  255. /* contextis
  256.  *
  257.  * returns the enclosing context of the current chunk (like ID_ILBM)
  258.  */
  259. LONG contextis(struct IFFHandle *iff, LONG type, LONG id)
  260. {
  261. register struct ContextNode    *cn;
  262. LONG result = 0;
  263.  
  264.        if (cn = (CurrentChunk (iff)))
  265.            {
  266.            if (cn = (ParentChunk(cn)))
  267.                {
  268.                if((cn->cn_Type == type)&&(cn->cn_ID == id)) result = 1;
  269.                }
  270.        }
  271.  
  272.     D(bug("This is a %.4s %.4s\n",&cn->cn_Type,&cn->cn_ID));
  273.  
  274.     return(result);
  275. }
  276.  
  277.  
  278. /* getcontext()
  279.  *
  280.  * Continues to gather the context which was specified to parseifile(),
  281.  *  stopping at specified stop chunk, or end of context, or EOF
  282.  *
  283.  * Returns 0 (stopped on a stop chunk)
  284.  *      or IFFERR_EOC (end of context, not an error)
  285.  *      or IFFER_EOF (end of file)
  286.  */
  287. LONG getcontext(iff)
  288. struct    IFFHandle *iff;
  289. {
  290.     LONG error = 0L;
  291.  
  292.     /* Based on our parse initialization,
  293.      * ParseIFF() will return on a stop chunk (error = 0)
  294.      * or end of context for an ILBM FORM (error = IFFERR_EOC)
  295.      * or end of file (error = IFFERR_EOF)
  296.      */
  297.     return(error = ParseIFF(iff, IFFPARSE_SCAN));
  298. }
  299.  
  300.  
  301. /* nextcontext
  302.  *
  303.  * If you have finished parsing and reading your context (IFFERR_EOC),
  304.  *   nextcontext will enter the next context contained in the file
  305.  *   and parse it.
  306.  *
  307.  * Returns 0 or an IFFERR such as IFFERR_EOF (end of file)
  308.  */
  309.  
  310. LONG nextcontext(iff)
  311. struct    IFFHandle *iff;
  312. {
  313.     LONG error = 0L;
  314.  
  315.     error = ParseIFF(iff, IFFPARSE_STEP);
  316.  
  317.     D(bug("nextcontext: Got through next step\n"));
  318.  
  319.     return(error);
  320. }
  321.  
  322.  
  323. /* findpropdata
  324.  *
  325.  * finds specified chunk parsed from IFF file, and
  326.  *   returns pointer to its sp_Data (or 0 for not found)
  327.  */
  328. UBYTE *findpropdata(iff, type, id)
  329. struct IFFHandle    *iff;
  330. LONG type, id;
  331.     {
  332.     register struct StoredProperty    *sp;
  333.  
  334.     if(sp = FindProp (iff, type, id)) return(sp->sp_Data);
  335.     return(0);
  336.     }
  337.  
  338.  
  339. /*
  340.  * File I/O hook functions which the IFF library will call.
  341.  * A return of 0 indicates success (no error).
  342.  *
  343.  * Iffparse.library calls this code via struct Hook and Hook.asm
  344.  *