home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / word2x0a.zip / source / reader.h < prev    next >
C/C++ Source or Header  |  1998-04-20  |  5KB  |  274 lines

  1. /* $Id: reader.h,v 1.23 1997/04/17 15:38:19 dps Exp $ */
  2. /* header file for the reader */
  3.  
  4. #ifndef __w6_reader_h__
  5. #define __w6_reader_h__
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <iostream.h>
  9. #include "tblock.h"
  10. #include "interface.h"
  11. #include "fifo.h"
  12. #include "word6.h"
  13.  
  14.  
  15.  
  16. /* Raw reader output */
  17. typedef enum
  18. {
  19.     CH_PAR=0, CH_FIELD, CH_ROW, CH_SPEC, CH_ENDSPEC, CH_OTHER,
  20.     CH_HDRTN, CH_EOF, CH_PAGE, CH_FOOTNOTE,
  21.     CONTROL_FLAG=(1<<17), PART_FLAG=(1<<18)
  22. } chunk_type;
  23.  
  24. #define CH_NONL 255
  25. #define CH_SUSPECT 127
  26.  
  27. /*
  28.  * Anything shorter than in totla this is word abuse an all spec
  29.  * paragraph. This should be kept *very* small.
  30.  */
  31. #define DISPL_TRESHOLD 3
  32. #define PAR_ITEM_SEP_LIMIT 4
  33. #define TEXT_ITEM_SEP_LIMIT 1024
  34.  
  35. #ifndef __EXCLUDE_READER_CLASSES
  36.  
  37. struct chunk_rtn
  38. {
  39.     tblock txt;
  40.     int type;
  41. };
  42.  
  43. /* This class converts raw word 6 into chunks for easier digestion */
  44. class chunk_reader
  45. {
  46.  private:
  47.     tblock text;
  48.     FILE *in;            /* Maybe should use istream here */
  49.     const char *tptr;        /* Points to tblock text */
  50.     int type;            /* Type */
  51.  
  52.     void read_chunk_raw(void);    /* Reads a lot */
  53.  protected:
  54.     struct chunk_rtn read_chunk(void); /* Returns the next bit */
  55.  
  56.     int seek_start(FILE *);    /* Find data start */
  57.     inline chunk_reader(FILE *f)
  58.     {
  59.     tptr=NULL;
  60.     in=f;
  61.     seek_start(f);
  62.     }
  63.     inline ~chunk_reader(void) {}; /* Avoids compiler bug */
  64. };
  65.  
  66.  
  67. /*
  68.  * This class interpolates stuff not in the chunks, for example the
  69.  * the start and statistics of tables.
  70.  */
  71. class tok_seq: private chunk_reader
  72. {
  73.     /* Classes used here */
  74. public:
  75.     /* Public token class */
  76.     class tok
  77.     {
  78.     private:
  79.     enum {TABLE=0, TEXT} dtype;
  80.  
  81.     public:
  82.     enum { TOK_START=0, TOK_END };
  83.     token tok;
  84.     union
  85.     {
  86.         struct
  87.         {
  88.         int rows;
  89.         int cols;
  90.         } table;
  91.         const char *d;
  92.     } data;
  93.     int end;
  94.  
  95.     friend ostream &operator <<(ostream &, const tok *f);
  96.     tok &operator=(const tok &d);
  97.     /* Avoid the need to cast NULL everywhere  for NULL defined as
  98.        (void *) 0 */
  99.     inline tok(token t, void *d, int e)
  100.     {
  101.         tok=t;
  102.         data.d=(const char *) d;
  103.         dtype=TEXT;
  104.         end=e;
  105.     }
  106.     /* Avoid the need to cast NULL everywhere for NULL defined as 0 */
  107.     inline tok(token t, int d, int e)
  108.     {
  109.         d=d;
  110.         tok=t;
  111.         data.d=(const char *) NULL;
  112.         dtype=TEXT;
  113.         end=e;
  114.     }
  115.     /* Avoid the need to cast NULL everywhere for NULL defined as 0L */
  116.     inline tok(token t, long int d, int e)
  117.     {
  118.         d=d;
  119.         tok=t;
  120.         data.d=(const char *) NULL;
  121.         dtype=TEXT;
  122.         end=e;
  123.     }
  124.  
  125.     inline tok(token t, const char *d, int e)
  126.     {
  127.         tok=t;
  128.         if (d!=NULL)
  129.         data.d=strdup(d);
  130.         else
  131.         data.d=NULL;
  132.         dtype=TEXT;
  133.         end=e;
  134.     }
  135.     inline tok(token t, int c, int r, int e)
  136.     {
  137.         tok=t;
  138.         data.table.rows=r;
  139.         data.table.cols=c;
  140.         dtype=TABLE;
  141.         end=e;
  142.     }
  143.     inline ~tok(void)
  144.     {
  145.         if (dtype==TEXT && data.d!=NULL)
  146.         free((void *) data.d);
  147.     }
  148.     };
  149.  
  150.  
  151. private:
  152.     /* Private class for table information */
  153.     class table_info
  154.     {
  155.     private:
  156.     fifo<tok> toks;
  157.  
  158.     public:
  159.     int rows;
  160.     int cols;
  161.     int col;
  162.  
  163.     inline table_info(void)
  164.     {
  165.         cols=0;
  166.         col=0;
  167.         rows=0;
  168.     }
  169.  
  170.     inline void tok_push(token t, tblock *s)
  171.     {
  172.         tok *td;
  173.         td=new(tok)(t, (const char *) (*s), tok::TOK_START);
  174.         toks.enqueue(td);
  175.         td=new(tok)(t, (void *) NULL, tok::TOK_END);
  176.         toks.enqueue(td);
  177.     }
  178.  
  179.     inline void enqueue(const tok *t)
  180.     {
  181.         toks.enqueue(t);
  182.     }
  183.  
  184.     inline void finish(fifo<tok> *out)
  185.     {
  186.         tok *t;
  187.  
  188.         t=new(tok)(T_TABLE, cols, rows, tok::TOK_START);
  189.         out->enqueue(t);
  190.         out->transfer(&toks);
  191.         t=new(tok)(T_TABLE, cols, rows, tok::TOK_END);
  192.         out->enqueue(t);
  193.     }
  194.     inline ~table_info(void) {} // Avoid compiler bug
  195.     };
  196.  
  197. private:
  198.     fifo<tok> output;        /* Output with equation cleaned up */
  199.     const tok *saved_tok;
  200.     int done_end;
  201.     table_info *table;
  202.     int rd_token(void);
  203.     const tok *feed_token(void);
  204.     const tok *math_collect(void);
  205.  
  206.     /* Token pusher */
  207.     inline void tok_push(token t, tblock *s)
  208.     {
  209.     tok *td;
  210.     td=new(tok)(t, (const char *) (*s), tok::TOK_START);
  211.     output.enqueue(td);
  212.     td=new(tok)(t, (void *) NULL, tok::TOK_END);
  213.     output.enqueue(td);
  214.     }
  215.  
  216.     const tok *eqn_rd_token(void);
  217.     
  218.     /* List handling */
  219.     typedef enum { LIST_BULLET, LIST_ENUMERATE, LIST_ENUM_ALPHA } l_type;
  220.     struct list_info
  221.     {
  222.     struct list_info *next_list;
  223.     l_type list_type;
  224.     int obj_cnt;
  225.     int text_cnt;
  226.     int items;
  227.     union
  228.     {
  229.         int item_no;
  230.         int lbullet;
  231.     } ldata;
  232.     fifo<tok_seq::tok> *last_item;
  233.     };
  234.     struct list_info *lp;    /* List pointer */
  235.     fifo<tok> outqueue;        /* Final output (so far...) */
  236.     fifo<tok> *recycled;    /* elements to be processed again */
  237.     struct list_info *list_type(const char *);
  238.     const char *list_check(const char *, struct list_info **);
  239.     const char *l_type_name(const struct list_info *);
  240.  
  241.  public:
  242.  
  243.  
  244.     tok_seq(FILE *in):  chunk_reader(in)
  245.     {
  246.     tok *t=new(tok)(T_DOC, "Converted by word2x", tok::TOK_START);
  247.     table=NULL;        /* No table */
  248.     saved_tok=NULL;        /* No saved token */
  249.     lp=NULL;        /* No list */
  250.     recycled=NULL;        /* Nothing recycled */
  251.     done_end=0;
  252.  
  253.     output.enqueue(t);
  254.     }
  255.  
  256.     inline void return_toks(fifo<tok> *tp)
  257.     {
  258.     if (recycled==NULL)
  259.         recycled=new(fifo<tok>);
  260.     recycled->ins_trans(tp);
  261.     }   
  262.     
  263.     inline ~tok_seq(void) {}    // Avoids compiler bug
  264.  
  265.     const tok *read_token(void);
  266.  
  267. };
  268. #endif /* __EXCLUDE_READER_CLASSES */
  269. #endif /* __w6_reader_h__ */
  270.  
  271.  
  272.  
  273.  
  274.