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

  1. /*
  2.  *
  3.  * hl2text.c - Output a HotLink'ed text file to an ASCII 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.  
  16. /* hotlink library base pointer */
  17. struct HotLinksBase *HotLinksBase = 0;
  18.  
  19. /* hotlinks publication block pointer */
  20. struct PubBlock *pb = 0;
  21.  
  22. /* hotlinks handle */
  23. int hlh = 0;
  24.  
  25. /* version string */
  26. char     VERSTAG[]="\0$VER: hl2text B5 (12.20.91)";
  27.  
  28.  
  29. /* forward declarations */
  30. int __asm filterproc(register __a0 struct PubBlock *);
  31. int getcompnum();
  32. void shutdown(), processtext(), processcommand();
  33.  
  34.  
  35. int main(argc, argv)
  36. int argc;
  37. char *argv[];
  38. {
  39.         int stilldata, chunksize, error, fo;
  40.         unsigned int chunktype;
  41.         
  42.         if(argc!=2)
  43.         {
  44.                 printf("USAGE: hl2text <filename>\n");
  45.                 exit(0);
  46.         }
  47.         
  48.         /* try to open the hotlink.library.
  49.          * The library will not open unless hotlinks is running.
  50.          */
  51.         if((HotLinksBase=(struct HotLinksBase *)OpenLibrary("hotlinks.library", 0))==0)
  52.         {
  53.                 printf("ERROR - could not open the hotlinks.library\n");
  54.                 exit(20);
  55.         }
  56.  
  57.         /* register this program with the hotlinks system */
  58.         hlh = HLRegister(1,0,0);
  59.         
  60.         /* get a PubBlock pointer */
  61.         pb=AllocPBlock(hlh);
  62.         
  63.         /* check for errors */
  64.         if((pb==(struct PubBlock *)NOMEMORY)||(pb==(struct PubBlock *)NOPRIV))
  65.         {
  66.                 printf("ERROR - AllocPBlock call failed: error=%d\n", pb);
  67.                 shutdown();
  68.                 exit(0);
  69.         }
  70.                 
  71.         /* get a publication using the publication requester provided by the
  72.          * hotlink.library.
  73.          */
  74.         error = GetPub(pb, &filterproc);
  75.         
  76.         /* check for errors */
  77.         if(error!=NOERROR)
  78.         {
  79.                 /* check for errors */
  80.                 switch(error)
  81.                 {
  82.                         case NOPRIV: printf("ERROR: privalge violation\n");
  83.                                      break;
  84.                                      
  85.                         case INVPARAM: printf("ERROR: invaild parameters\n");
  86.                                        break;
  87.                                        
  88.                         case IOERROR: /* user canceled requester */
  89.                                       break;
  90.                 }
  91.                 shutdown();
  92.                 exit(0);
  93.         }
  94.         
  95.         /* open the publication */
  96.         if((error=OpenPub(pb, OPEN_READ))!=NOERROR)
  97.         {
  98.                 printf("ERROR - could not open the publication for reading\n");
  99.                 shutdown();                
  100.                 exit(0);
  101.         }
  102.         
  103.         /* open the output file */
  104.         if((fo=Open(argv[1], MODE_NEWFILE))==0)
  105.         {
  106.                 printf("ERROR - could not open output file for writing\n");
  107.                 ClosePub(pb);
  108.                 shutdown();
  109.                 exit(0);
  110.         }
  111.         
  112.         ReadPub(pb, (char *)&chunktype, 4); /* get the FORM tag */
  113.         ReadPub(pb, (char *)&stilldata, 4); /* get the FORM size */
  114.         ReadPub(pb, (char *)&chunktype, 4); /* get the FORM type (DTXT) */
  115.         
  116.         /* process the chunks */
  117.         stilldata -= 4;
  118.         while(stilldata>0)
  119.         {
  120.                 ReadPub(pb, (char *)&chunktype, 4);
  121.                 ReadPub(pb, (char *)&chunksize, 4);
  122.                 switch(chunktype)
  123.                 {
  124.                         case DTXT: /* process the DTXT chunk data */
  125.                                    processtext(pb, fo, chunksize);
  126.                                    
  127.                                    /* adjust the chunklength for an odd length */
  128.                                    if(chunksize&0x00000001)
  129.                                    {
  130.                                         chunksize++;
  131.                                    }
  132.                                    break;
  133.                                    
  134.                         case DTAG: /* process the DTAG chunk */
  135.                                      
  136.                         default: /* adjust the length if it is an odd length */
  137.                                  if(chunksize&0x00000001)
  138.                                  {
  139.                                         chunksize++;
  140.                                  }
  141.                                    
  142.                                  /* does the chunk have any data in it? */
  143.                                  if(chunksize)
  144.                                  {
  145.                                         /* skip the chunk data */
  146.                                         SeekPub(pb, chunksize, SEEK_CURRENT);
  147.                                  }
  148.                                  break;
  149.                 }
  150.                 stilldata -= (chunksize+8);
  151.         }
  152.  
  153.         /* close the output file */
  154.         Close(fo);
  155.         
  156.         /* close the publication */
  157.         ClosePub(pb);
  158.         
  159.         shutdown();
  160. }
  161.  
  162.  
  163. void shutdown()
  164. {
  165.         if(pb)
  166.         {
  167.                 /* free the publication block pointer */
  168.                 FreePBlock(pb);
  169.         }
  170.         if(hlh)
  171.         {
  172.                 /* unregister this program from hotlinks */
  173.                 UnRegister(hlh);
  174.         }
  175.         if(HotLinksBase)
  176.         {
  177.                 /* close the library */
  178.                 CloseLibrary((struct Library *)HotLinksBase);
  179.         }
  180. }
  181.  
  182. /* this is the filter procedure that gets called by GetPub.
  183.  * the PubBlock pointer is passed in register a0,
  184.  * the return value is passed back in d0 and must be either ACCEPT or NOACCEPT.
  185.  */
  186. int __asm filterproc(register __a0 struct PubBlock *pb)
  187. {
  188.         if(pb->PRec.Type==DTXT)
  189.         {
  190.                 return(ACCEPT);
  191.         }
  192.         else
  193.         {
  194.                 return(NOACCEPT);
  195.         }
  196. }
  197.  
  198. /* process the DTXT chunk from the HotLink'ed file */
  199. void processtext(pb, fo, size)
  200. struct PubBlock *pb;
  201. int fo, size;
  202. {
  203.         int clen, cflag, command;
  204.         unsigned char input;
  205.         
  206.         while(size>0)
  207.         {
  208.                 ReadPub(pb, &input, 1);
  209.                 size--;
  210.                 
  211.                 /* is it a command (signaled by a leading 0) */
  212.                 if(input==0)
  213.                 {
  214.                         size -= getcompnum(pb, &command);
  215.                         size -= getcompnum(pb, &cflag);
  216.                         size -= getcompnum(pb, &clen);
  217.                         processcommand(command, cflag, clen, pb, fo);
  218.                         size -= clen;
  219.                 }
  220.                 else
  221.                 {
  222.                         /* else it must be text so write it out */
  223.                         Write(fo, &input, 1);
  224.                 }
  225.         }
  226. }
  227.  
  228. int getcompnum(pb, clen)
  229. struct PubBlock *pb;
  230. int *clen;
  231. {
  232.         int clen2, count;
  233.         unsigned char len;
  234.         
  235.         /* init clen to 0 */
  236.         clen2 = 0;
  237.         count = 0;
  238.         
  239.         /* read in the first command length byte */
  240.         ReadPub(pb, &len, 1);
  241.         count++;
  242.         
  243.         /* check for "compressed" numeric form (indicated by the high bit being set) */
  244.         while(len&0x80)
  245.         {
  246.                 /* mask off the upper bit (indicator bit) */
  247.                 len&=0x7f;
  248.                 
  249.                 /* shift the current command length up by seven bits */
  250.                 clen2<<=7;
  251.                 
  252.                 /* add in the new length byte */
  253.                 clen2+=len;
  254.                 
  255.                 /* get the next len byte */
  256.                 ReadPub(pb, &len, 1);
  257.                 count++;
  258.         }
  259.         clen2+=len;
  260.         *clen=clen2;
  261.         return(count);
  262. }
  263.  
  264. void processcommand(com, flag, len, pb, fo)
  265. int com, flag, len;
  266. struct PubBlock *pb;
  267. int fo;
  268. {
  269.         /*
  270.         int x;
  271.         char input;
  272.         */
  273.         char output;
  274.         
  275.         switch(com)
  276.         {
  277.                 case TEXT_TAB: output = 9;
  278.                                Write(fo, &output, 1);
  279.                                break;
  280.                 
  281.                 case TEXT_NEWLINE: output = 10;
  282.                                    Write(fo, &output, 1);
  283.                                    break;
  284.                 
  285.                 case TEXT_TAG: /*
  286.                                output = 10;
  287.                                Write(fo, &output, 1);
  288.                                Write(fo, "TEXT_TAG: ", 10);
  289.                                for(x=0; x<len; x++)
  290.                                {
  291.                                    ReadPub(pb, &input, 1);
  292.                                    Write(fo, &input, 1);
  293.                                }
  294.                                output = 10;
  295.                                Write(fo, &output, 1);
  296.                                break;
  297.                                */
  298.                                
  299.                 case TEXT_FONT: /*
  300.                                 output = 10;
  301.                                 Write(fo, &output, 1);
  302.                                 Write(fo, "TEXT_FONT: ", 11);
  303.                                 for(x=0; x<len; x++)
  304.                                 {
  305.                                     ReadPub(pb, &input, 1);
  306.                                     Write(fo, &input, 1);
  307.                                 }
  308.                                 output = 10;
  309.                                 Write(fo, &output, 1);
  310.                                 break;
  311.                                 */
  312.                                 
  313.                 case TEXT_ATTRB: /*
  314.                                  output = 10;
  315.                                  Write(fo, &output, 1);
  316.                                  Write(fo, "TEXT_ATTRB: ", 12);
  317.                                  for(x=0; x<len; x++)
  318.                                  {
  319.                                      ReadPub(pb, &input, 1);
  320.                                      switch(input)
  321.                                      {
  322.                                         case ATTRB_NORMAL: Write(fo, "Normal ", 7);
  323.                                                            break;
  324.                                                            
  325.                                         case ATTRB_BOLD: Write(fo, "Bold ", 5);
  326.                                                          break;
  327.                                                          
  328.                                         case ATTRB_LIGHT: Write(fo, "Light ", 6);
  329.                                                           break;
  330.                                                           
  331.                                         case ATTRB_ITALIC: Write(fo, "Italic ", 7);
  332.                                                            break;
  333.                                                            
  334.                                         case ATTRB_SHADOW: Write(fo, "Shadow ", 7);
  335.                                                            break;
  336.                                                            
  337.                                         case ATTRB_OUTLINE: Write(fo, "OutLine ", 8);
  338.                                                             break;
  339.                                                             
  340.                                         case ATTRB_UNDERLINE: Write(fo, "Underline ", 10);
  341.                                                               break;
  342.                                                               
  343.                                         case ATTRB_WEIGHT: Write(fo, "Weight=", 7);
  344.                                                            ReadPub(pb, &input, 1);
  345.                                                            Write(fo, &input, 1);
  346.                                                            output=' ';
  347.                                                            Write(fo, &output, 1);
  348.                                                            break;
  349.                                      }
  350.                                  }
  351.                                  output = 10;
  352.                                  Write(fo, &output, 1);
  353.                                  break;
  354.                                  */
  355.                 
  356.                 case TEXT_JUSTIFY: /*
  357.                                    output = 10;
  358.                                    Write(fo, &output, 1);
  359.                                    Write(fo, "TEXT_JUSTIFY: ", 14);
  360.                                    for(x=0; x<len; x++)
  361.                                    {
  362.                                        ReadPub(pb, &input, 1);
  363.                                        switch(input)
  364.                                        {
  365.                                            case JUSTIFY_LEFT: Write(fo, "Left ", 5);
  366.                                                               break;
  367.                                                               
  368.                                            case JUSTIFY_CENTER: Write(fo, "Center ", 7);
  369.                                                                 break;
  370.                                                                 
  371.                                            case JUSTIFY_RIGHT: Write(fo, "Right ", 6);
  372.                                                                break;
  373.                                                                
  374.                                            case JUSTIFY_CHAR: Write(fo, "Char ", 5);
  375.                                                               break;
  376.                                                               
  377.                                            case JUSTIFY_WORD: Write(fo, "Word ", 5);
  378.                                                               break;
  379.                                                               
  380.                                            case JUSTIFY_AUTO: Write(fo, "Auto ", 5);
  381.                                                               break;
  382.                                        }
  383.                                    }
  384.                                    output = 10;
  385.                                    Write(fo, &output, 1);
  386.                                    break;
  387.                                    */
  388.                 case TEXT_EOC:
  389.                 case TEXT_EOP:
  390.                 case TEXT_BCCB:
  391.                 case TEXT_ECCB:
  392.                 case TEXT_BCPB:
  393.                 case TEXT_ECPB:
  394.                 case TEXT_PAGENUM:
  395.                 case TEXT_MARK:
  396.                 case TEXT_BRANGE:
  397.                 case TEXT_ERANGE:
  398.                 case TEXT_FOOTNOTE:
  399.                 case TEXT_RULER:
  400.                 case TEXT_BAKERN:
  401.                 case TEXT_EAKERN:
  402.                 case TEXT_BAHYPHEN:
  403.                 case TEXT_EAHYPHEN:
  404.                 case TEXT_TRACKRANGE:
  405.                 case TEXT_DROPCAP:
  406.                 case TEXT_POINT:
  407.                 case TEXT_PARAGRAPH:
  408.                 case TEXT_INDENT:
  409.                 case TEXT_LEADING:
  410.                 case TEXT_PARALEAD:
  411.                 case TEXT_TRACKING:
  412.                 case TEXT_BASELINE:
  413.                 case TEXT_MKERN:
  414.                 case TEXT_AKERN:
  415.                 case TEXT_MHYPHEN:
  416.                 case TEXT_AHYPHEN:
  417.                 
  418.                 default: SeekPub(pb, len, SEEK_CURRENT);
  419.                          break;
  420.         }
  421. }
  422.