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

  1. /*
  2.  *
  3.  * hl2ilbm.c - Output a HotLink'ed bitmap file to a standard IFF ILBM file
  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.  
  15. #define BMHD (('B'<<24)+('M'<<16)+('H'<<8)+('D')) 
  16. #define CMAP (('C'<<24)+('M'<<16)+('A'<<8)+('P'))
  17. #define BODY (('B'<<24)+('O'<<16)+('D'<<8)+('Y'))
  18.  
  19.  
  20. /* hotlink library base pointer */
  21. struct HotLinksBase *HotLinksBase = 0;
  22.  
  23. /* hotlinks publication block pointer */
  24. struct PubBlock *pb = 0;
  25.  
  26. /* hotlinks handle */
  27. int hlh = 0;
  28.  
  29. /* version string */
  30. char     VERSTAG[]="\0$VER: hl2ilbm B3 (10.2.91)";
  31.  
  32.  
  33.  
  34. /* forward declarations */
  35. int __asm filterproc(register __a0 struct PubBlock *);
  36. void shutdown();
  37.  
  38.  
  39. int main(argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.         char *buff;
  44.         int stilldata, chunksize, error, fo;
  45.         unsigned int chunktype;
  46.         
  47.         if(argc!=2)
  48.         {
  49.                 printf("USAGE: hl2ilbm <filename>\n");
  50.                 exit(0);
  51.         }
  52.         
  53.         /* try to open the hotlink.library.
  54.          * The library will not open unless hotlinks is running.
  55.          */
  56.         if((HotLinksBase=(struct HotLinksBase *)OpenLibrary("hotlinks.library", 0))==0)
  57.         {
  58.                 printf("ERROR - could not open the hotlinks.library\n");
  59.                 exit(20);
  60.         }
  61.  
  62.         /* register this program with the hotlinks system */
  63.         hlh = HLRegister(1,0,0);
  64.         
  65.         /* get a PubBlock pointer */
  66.         pb=AllocPBlock(hlh);
  67.         
  68.         /* check for errors */
  69.         if((pb==(struct PubBlock *)NOMEMORY)||(pb==(struct PubBlock *)NOPRIV))
  70.         {
  71.                 printf("ERROR - AllocPBlock call failed: error=%d\n", pb);
  72.                 shutdown();
  73.                 exit(0);
  74.         }
  75.                 
  76.         /* get a publication using the publication requester provided by the
  77.          * hotlink.library.
  78.          */
  79.         error = GetPub(pb, &filterproc);
  80.         
  81.         /* check for errors */
  82.         if(error!=NOERROR)
  83.         {
  84.                 /* check for errors */
  85.                 switch(error)
  86.                 {
  87.                         case NOPRIV: printf("ERROR: privalge violation\n");
  88.                                      break;
  89.                                      
  90.                         case INVPARAM: printf("ERROR: invaild parameters\n");
  91.                                        break;
  92.                                        
  93.                         case IOERROR: /* user canceled requester */
  94.                                       break;
  95.                 }
  96.                 shutdown();
  97.                 exit(0);
  98.         }
  99.         
  100.         /* open the publication */
  101.         if((error=OpenPub(pb, OPEN_READ))!=NOERROR)
  102.         {
  103.                 printf("ERROR - could not open the publication for reading\n");
  104.                 shutdown();                
  105.                 exit(0);
  106.         }
  107.         
  108.         /* open the output file */
  109.         if((fo=Open(argv[1], MODE_NEWFILE))==0)
  110.         {
  111.                 printf("ERROR - could not open output file for writing\n");
  112.                 ClosePub(pb);
  113.                 shutdown();
  114.                 exit(0);
  115.         }
  116.         
  117.         ReadPub(pb, (char *)&chunktype, 4); /* get the FORM tag */
  118.         Write(fo, (char *)&chunktype, 4);   /* write the FORM tag */
  119.         
  120.         ReadPub(pb, (char *)&stilldata, 4); /* get the FORM size */
  121.         Write(fo, (char *)&stilldata, 4);   /* write the FORM size */
  122.         
  123.         ReadPub(pb, (char *)&chunktype, 4); /* get the FORM type (ILBM) */
  124.         Write(fo, (char *)&chunktype, 4);   /* write the FORM type (ILBM) */
  125.         
  126.         /* process the chunks */
  127.         stilldata -= 4;
  128.         while(stilldata)
  129.         {
  130.                 ReadPub(pb, (char *)&chunktype, 4);
  131.                 ReadPub(pb, (char *)&chunksize, 4);
  132.                 switch(chunktype)
  133.                 {
  134.                         case BMHD:
  135.                         case CMAP:
  136.                         case BODY:
  137.                         default:
  138.                                    /* write out the chunk header */
  139.                                    Write(fo, (char *)&chunktype, 4);
  140.                                    Write(fo, (char *)&chunksize, 4);
  141.                                    
  142.                                    /* adjust the length if it is an odd length */
  143.                                    if(chunksize&0x00000001)
  144.                                    {
  145.                                         chunksize++;
  146.                                    }
  147.                                    
  148.                                    /* does the chunk have any data in it? */
  149.                                    if(chunksize)
  150.                                    {
  151.                                         /* allocate memory to hold the chunk */
  152.                                         if((buff=AllocMem(chunksize, 0))==0)
  153.                                         {
  154.                                                 printf("ERROR - out of memory\n");
  155.                                                 ClosePub(pb);
  156.                                                 Close(fo);
  157.                                                 DeleteFile(argv[1]);
  158.                                                 shutdown();
  159.                                                 exit(0);
  160.                                         }
  161.                                         
  162.                                         /* read the chunk data in */
  163.                                         ReadPub(pb, buff, chunksize);
  164.                                         
  165.                                         /* and write it back out */
  166.                                         Write(fo, buff, chunksize);
  167.                                         
  168.                                         /* free the memory */
  169.                                         FreeMem(buff, chunksize);
  170.                                    }
  171.                                    break;
  172.                 }
  173.                 stilldata -= (chunksize+8);
  174.         }
  175.  
  176.         /* close the output file */
  177.         Close(fo);
  178.         
  179.         /* close the publication */
  180.         ClosePub(pb);
  181.         
  182.         shutdown();
  183. }
  184.  
  185.  
  186. void shutdown()
  187. {
  188.         if(pb)
  189.         {
  190.                 /* free the publication block pointer */
  191.                 FreePBlock(pb);
  192.         }
  193.         if(hlh)
  194.         {
  195.                 /* unregister this program from hotlinks */
  196.                 UnRegister(hlh);
  197.         }
  198.         if(HotLinksBase)
  199.         {
  200.                 /* close the library */
  201.                 CloseLibrary((struct Library *)HotLinksBase);
  202.         }
  203. }
  204.  
  205. /* this is the filter procedure that gets called by GetPub.
  206.  * the PubBlock pointer is passed in register a0,
  207.  * the return value is passed back in d0 and must be either ACCEPT or NOACCEPT.
  208.  */
  209. int __asm filterproc(register __a0 struct PubBlock *pb)
  210. {
  211.         if(pb->PRec.Type==ILBM)
  212.         {
  213.                 return(ACCEPT);
  214.         }
  215.         else
  216.         {
  217.                 return(NOACCEPT);
  218.         }
  219. }
  220.