home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / h / hldevkit.zip / HLIFF.C < prev    next >
C/C++ Source or Header  |  1992-04-28  |  6KB  |  230 lines

  1. /*
  2.  *
  3.  * hliff.c - Check the IFF format of a HotLink'ed file (dump the chunk types)
  4.  *
  5.  */
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/hotlinks.h>
  10. #include <exec/memory.h>
  11. #include <dos/dos.h>
  12. #include <lattice/stdio.h>
  13. #include <hotlinks/hotlinks.h>
  14. #include <lattice/stdio.h>
  15.  
  16. #define FORM (('F'<<24)+('O'<<16)+('R'<<8)+('M')) 
  17.  
  18. /* hotlink library base pointer */
  19. struct HotLinksBase *HotLinksBase = 0;
  20.  
  21. /* hotlinks publication block pointer */
  22. struct PubBlock *pb = 0;
  23.  
  24. /* hotlinks handle */
  25. int hlh = 0;
  26.  
  27. /* level counter */
  28. int level;
  29.  
  30. /* error flag: 0=OK, 1=error occured */
  31. int errorflag;
  32.  
  33. /* version string */
  34. char     VERSTAG[]="\0$VER: hliff B3 (10.2.91)";
  35.  
  36.  
  37. /* forward declarations */
  38. int __asm filterproc(register __a0 struct PubBlock *);
  39. void shutdown(), processform(), printchunktype(), showlevel();
  40.  
  41.  
  42. int main()
  43. {
  44.         int chunksize, error;
  45.         unsigned int chunktype;
  46.         
  47.         /* try to open the hotlink.library.
  48.          * The library will not open unless hotlinks is running.
  49.          */
  50.         if((HotLinksBase=(struct HotLinksBase *)OpenLibrary("hotlinks.library", 0))==0)
  51.         {
  52.                 printf("ERROR - could not open the hotlinks.library\n");
  53.                 exit(20);
  54.         }
  55.  
  56.         /* register this program with the hotlinks system */
  57.         hlh = HLRegister(1,0,0);
  58.         
  59.         /* get a PubBlock pointer */
  60.         pb=AllocPBlock(hlh);
  61.         
  62.         /* check for errors */
  63.         if((pb==(struct PubBlock *)NOMEMORY)||(pb==(struct PubBlock *)NOPRIV))
  64.         {
  65.                 printf("ERROR - AllocPBlock call failed: error=%d\n", pb);
  66.                 shutdown();
  67.                 exit(0);
  68.         }
  69.                 
  70.         /* get a publication using the publication requester provided by the
  71.          * hotlink.library.
  72.          */
  73.         error = GetPub(pb, &filterproc);
  74.         
  75.         /* check for errors */
  76.         if(error!=NOERROR)
  77.         {
  78.                 /* check for errors */
  79.                 switch(error)
  80.                 {
  81.                         case NOPRIV: printf("ERROR: privalge violation\n");
  82.                                      break;
  83.                                      
  84.                         case INVPARAM: printf("ERROR: invaild parameters\n");
  85.                                        break;
  86.                                        
  87.                         case IOERROR: /* user canceled requester */
  88.                                       break;
  89.                 }
  90.                 shutdown();
  91.                 exit(0);
  92.         }
  93.         
  94.         /* open the publication */
  95.         if((error=OpenPub(pb, OPEN_READ))!=NOERROR)
  96.         {
  97.                 printf("ERROR - could not open the publication for reading\n");
  98.                 shutdown();                
  99.                 exit(0);
  100.         }
  101.         
  102.         level = 0;
  103.         errorflag = 0;
  104.         ReadPub(pb, (char *)&chunktype, 4); /* get the FORM tag */
  105.         ReadPub(pb, (char *)&chunksize, 4); /* get the FORM size */
  106.         processform(pb, chunktype, chunksize);
  107.         
  108.         /* close the publication */
  109.         ClosePub(pb);
  110.         
  111.         shutdown();
  112. }
  113.  
  114.  
  115. void shutdown()
  116. {
  117.         if(pb)
  118.         {
  119.                 /* free the publication block pointer */
  120.                 FreePBlock(pb);
  121.         }
  122.         if(hlh)
  123.         {
  124.                 /* unregister this program from hotlinks */
  125.                 UnRegister(hlh);
  126.         }
  127.         if(HotLinksBase)
  128.         {
  129.                 /* close the library */
  130.                 CloseLibrary((struct Library *)HotLinksBase);
  131.         }
  132. }
  133.  
  134. /* this is the filter procedure that gets called by GetPub.
  135.  * the PubBlock pointer is passed in register a0,
  136.  * the return value is passed back in d0 and must be either ACCEPT or NOACCEPT.
  137.  */
  138. int __asm filterproc(register __a0 struct PubBlock *pb)
  139. {
  140.         /* accept all file types */
  141.         return(ACCEPT);
  142. }
  143.  
  144. void processform(pb, form, stilldata)
  145. struct PubBlock *pb;
  146. int form, stilldata;
  147. {        
  148.         int chunktype, chunksize;
  149.         
  150.         showlevel();
  151.         printchunktype(form);
  152.         printf(" %d ", stilldata);
  153.         
  154.         ReadPub(pb, (char *)&chunktype, 4); /* get the FORM type (ILBM) */
  155.         printchunktype(chunktype);
  156.         printf("\n");
  157.         
  158.         /* increment the level counter */
  159.         level++;
  160.         
  161.         /* process the chunks */
  162.         stilldata -= 4;
  163.         while(stilldata>0)
  164.         {
  165.                 showlevel();
  166.                 ReadPub(pb, (char *)&chunktype, 4);
  167.                 printchunktype(chunktype);
  168.                 
  169.                 ReadPub(pb, (char *)&chunksize, 4);
  170.                 if(chunksize>stilldata)
  171.                 {
  172.                         printf("\nERROR in HL IFF chunk size\n");
  173.                         errorflag = 1;
  174.                         return;
  175.                 }
  176.                 else if(chunktype==FORM)
  177.                 {
  178.                         processform(pb, chunktype, chunksize);
  179.                         if(errorflag)
  180.                         {
  181.                                 return;
  182.                         }
  183.                 }
  184.                 else
  185.                 {
  186.                         printf(" %d\n", chunksize);
  187.                 }
  188.                                    
  189.                 /* adjust the length if it is an odd length */
  190.                 if(chunksize&0x00000001)
  191.                 {
  192.                         chunksize++;
  193.                 }
  194.                 
  195.                 /* seek past the chunk data */
  196.                 SeekPub(pb, chunksize, SEEK_CURRENT);
  197.                 
  198.                 /* adjust data counter and continue */
  199.                 stilldata -= (chunksize+8);
  200.         }
  201.         
  202.         /* decrement level counter and return */
  203.         level--;
  204. }
  205.  
  206. void showlevel()
  207. {
  208.         int x;
  209.         
  210.         for(x=0; x<level; x++)
  211.         {
  212.                 printf(".");
  213.         }
  214. }
  215.  
  216. void printchunktype(type)
  217. int type;
  218. {
  219.         char c1,c2,c3,c4;
  220.         
  221.         c4 = type&0x0000007F;
  222.         type>>=8;
  223.         c3 = type&0x0000007F;
  224.         type>>=8;
  225.         c2 = type&0x0000007F;
  226.         type>>=8;
  227.         c1 = type&0x0000007F;
  228.         
  229.         printf("%c%c%c%c", c1, c2, c3, c4);
  230. }