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

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