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

  1. /*
  2.  *
  3.  * text2hl.c - Publish a standard ASCII 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.  
  17. /* hotlink library base pointer */
  18. struct HotLinksBase *HotLinksBase = 0;
  19.  
  20. /* hotlinks publication block pointer */
  21. struct PubBlock *pb = 0;
  22.  
  23. /* hotlinks handle */
  24. int hlh = 0;
  25.  
  26. /* version string */
  27. char     VERSTAG[]="\0$VER: text2hl B6 (12.20.91)";
  28.  
  29.  
  30.  
  31. /* forward declarations */
  32. int writetext();
  33. void shutdown();
  34.  
  35.  
  36. int main(argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40.         struct FileInfoBlock fib;
  41.         char *buff;
  42.         int chunksize, error, fi, filesize, lock, newlen;
  43.         unsigned int chunktype;
  44.         
  45.         if(argc!=2)
  46.         {
  47.                 printf("USAGE: text2hl <filename>\n");
  48.                 exit(0);
  49.         }
  50.         
  51.         /* try to open the hotlink.library.
  52.          * The library will not open unless hotlinks is running.
  53.          */
  54.         if((HotLinksBase=(struct HotLinksBase *)OpenLibrary("hotlinks.library", 0))==0)
  55.         {
  56.                 printf("ERROR - could not open the hotlinks.library\n");
  57.                 exit(20);
  58.         }
  59.  
  60.         /* register this program with the hotlinks system */
  61.         hlh = HLRegister(1,0,0);
  62.         
  63.         /* get a PubBlock pointer */
  64.         pb=AllocPBlock(hlh);
  65.         
  66.         /* check for errors */
  67.         if((pb==(struct PubBlock *)NOMEMORY)||(pb==(struct PubBlock *)NOPRIV))
  68.         {
  69.                 printf("ERROR - AllocPBlock call failed: error=%d\n", pb);
  70.                 shutdown();
  71.                 exit(0);
  72.         }
  73.  
  74.         /* get the file size */
  75.         lock = Lock(argv[1], ACCESS_READ);
  76.         if(lock==0)
  77.         {
  78.                 printf("ERROR - could not obtain lock on input file\n");
  79.                 shutdown();
  80.                 exit(0);
  81.         }
  82.         if((Examine(lock, &fib))==0)
  83.         {
  84.                 printf("ERROR - could not examine input file\n");
  85.                 UnLock(lock);
  86.                 shutdown();
  87.                 exit(0);
  88.         }
  89.         filesize = fib.fib_Size;
  90.         UnLock(lock);
  91.         
  92.         /* open the input file */
  93.         if((fi=Open(argv[1], MODE_OLDFILE))==0)
  94.         {
  95.                 printf("ERROR - could not open input file for reading\n");
  96.                 shutdown();
  97.                 exit(0);
  98.         }
  99.         
  100.         /* set up some defaults */
  101.         pb->PRec.Type = DTXT;
  102.         pb->PRec.Access = ACC_DEFAULT;
  103.         stcgfn(pb->PRec.Name, argv[1]);
  104.         
  105.         /* get a publication using the publication requester provided by the
  106.          * hotlink.library.
  107.          */
  108.         error = PutPub(pb, 0);
  109.         
  110.         /* if the user selected a file and pressed ok then delete the file*/
  111.         if(error!=NOERROR)
  112.         {
  113.                 /* check for errors */
  114.                 switch(error)
  115.                 {
  116.                         case NOPRIV: printf("ERROR: privalge violation\n");
  117.                                      break;
  118.                                      
  119.                         case INVPARAM: printf("ERROR: invaild parameters\n");
  120.                                        break;
  121.                 }
  122.                 Close(fi);
  123.                 shutdown();                
  124.                 exit(0);
  125.         }
  126.         
  127.         
  128.         /* open the publication file */
  129.         if((error=OpenPub(pb, OPEN_WRITE))!=NOERROR)
  130.         {
  131.                 printf("ERROR - could not open the publication for writing\n");
  132.                 Close(fi);
  133.                 shutdown();
  134.                 exit(0);
  135.         }
  136.         
  137.         
  138.         chunktype = FORM;
  139.         WritePub(pb, (char *)&chunktype, 4); /* write the FORM tag */
  140.         chunksize = filesize+12;
  141.         WritePub(pb, (char *)&chunksize, 4); /* write the FORM size */
  142.         chunktype = DTXT;
  143.         WritePub(pb, (char *)&chunktype, 4); /* write the FORM type (DTXT) */
  144.  
  145.         WritePub(pb, (char *)&chunktype, 4); /* write out the chunk header (DTXT) */
  146.         chunksize = filesize;
  147.         WritePub(pb, (char *)&chunksize, 4);
  148.  
  149.         /* allocate memory to hold the chunk */
  150.         if((buff=AllocMem(filesize, 0))==0)
  151.         {
  152.                 printf("ERROR - out of memory\n");
  153.                 ClosePub(pb);
  154.                 Close(fi);
  155.                 RemovePub(pb);
  156.                 shutdown();
  157.                 exit(0);
  158.         }
  159.  
  160.         /* read the chunk data in */
  161.         Read(fi, buff, filesize);
  162.         
  163.         newlen = writetext(pb, buff, filesize);
  164.         
  165.         /* pad the file out to an even byte */
  166.         if(filesize&0x00000001)
  167.         {
  168.                 *buff = 0;
  169.                 WritePub(pb, buff, 1);
  170.         }
  171.         
  172.         /* free the memory used */
  173.         FreeMem(buff, filesize);
  174.                 
  175.         /* close the input file */
  176.         Close(fi);
  177.         
  178.         /* adjust the chunk lengths for the commands written out */
  179.         SeekPub(pb, 4, SEEK_BEGINNING);
  180.         chunksize = newlen+12;
  181.         WritePub(pb, (char *)&chunksize, 4);
  182.         SeekPub(pb, 8, SEEK_CURRENT);
  183.         chunksize = newlen;
  184.         WritePub(pb, (char *)&chunksize, 4);
  185.         SeekPub(pb, 0, SEEK_END);
  186.         
  187.         /* close the publication */
  188.         ClosePub(pb);
  189.         
  190.         shutdown();
  191. }
  192.  
  193.  
  194. void shutdown()
  195. {
  196.         if(pb)
  197.         {
  198.                 /* free the publication block pointer */
  199.                 FreePBlock(pb);
  200.         }
  201.         if(hlh)
  202.         {
  203.                 /* unregister this program from hotlinks */
  204.                 UnRegister(hlh);
  205.         }
  206.         if(HotLinksBase)
  207.         {
  208.                 /* close the library */
  209.                 CloseLibrary((struct Library *)HotLinksBase);
  210.         }
  211. }
  212.  
  213. int writetext(pb, buff, len)
  214. struct PubBLock *pb;
  215. char *buff;
  216. int len;
  217. {
  218.         int flen;
  219.         char command, cflag, clen;
  220.         
  221.         flen = 0;
  222.         while(len)
  223.         {
  224.                 switch(*buff)
  225.                 {
  226.                         /* write out the TEXT_TAB command */
  227.                         case 0x09: command = TEXT_TAB;
  228.                                    cflag = TEXT_FLAGS_TAB;
  229.                                    clen = 0;
  230.                                    WritePub(pb, &clen, 1);
  231.                                    WritePub(pb, &command, 1);
  232.                                    WritePub(pb, &cflag, 1);
  233.                                    WritePub(pb, &clen, 1);
  234.                                    flen+=4;
  235.                                    break;
  236.                                    
  237.                         /* write out the TEXT_NEWLINE command */
  238.                         case 0x0a: command = TEXT_NEWLINE;
  239.                                    cflag = TEXT_FLAGS_NEWLINE;
  240.                                    clen = 0;
  241.                                    WritePub(pb, &clen, 1);
  242.                                    WritePub(pb, &command, 1);
  243.                                    WritePub(pb, &cflag, 1);
  244.                                    WritePub(pb, &clen, 1);
  245.                                    flen+=4;
  246.                                    break;
  247.                                    
  248.                         /* write out the text itself */
  249.                         default: WritePub(pb, buff, 1);
  250.                                  flen++;
  251.                                  break;
  252.                 }
  253.                 len--;
  254.                 buff++;
  255.         }
  256.         return(flen);
  257. }
  258.