summaryrefslogtreecommitdiff
path: root/support/word2x/reader.h
blob: 4bcc4700dd39bf693b8bca9c716528dd032e533c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* $Id: reader.h,v 1.23 1997/04/17 15:38:19 dps Exp $ */
/* header file for the reader */

#ifndef __w6_reader_h__
#define __w6_reader_h__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <iostream.h>
#include "tblock.h"
#include "interface.h"
#include "fifo.h"
#include "word6.h"

/* Raw reader output */
typedef enum
{
    CH_PAR=0, CH_FIELD, CH_ROW, CH_SPEC, CH_ENDSPEC, CH_OTHER,
    CH_HDRTN, CH_EOF, CH_PAGE, CH_FOOTNOTE,
    CONTROL_FLAG=(1<<17), PART_FLAG=(1<<18)
} chunk_type;

#define CH_NONL 255
#define CH_SUSPECT 127

/*
 * Anything shorter than in totla this is word abuse an all spec
 * paragraph. This should be kept *very* small.
 */
#define DISPL_TRESHOLD 3
#define PAR_ITEM_SEP_LIMIT 4
#define TEXT_ITEM_SEP_LIMIT 1024

#ifndef __EXCLUDE_READER_CLASSES

struct chunk_rtn
{
    tblock txt;
    int type;
};

/* This class converts raw word 6 into chunks for easier digestion */
class chunk_reader
{
 private:
    tblock text;
    istream *in;		/* Maybe should use istream here */
    const char *tptr;		/* Points to tblock text */
    int type;			/* Type */

    void read_chunk_raw(void);	/* Reads a lot */
 protected:
    struct chunk_rtn read_chunk(void); /* Returns the next bit */

    inline chunk_reader(istream *f)
    {
	tptr=NULL;
	in=f;
    }
    inline ~chunk_reader(void) {}; /* Avoids compiler bug */
};


/*
 * This class interpolates stuff not in the chunks, for example the
 * the start and statistics of tables.
 */
class tok_seq: private chunk_reader
{
    /* Classes used here */
public:
    /* Public token class */
    class tok
    {
    private:
	enum {TABLE=0, TEXT} dtype;

    public:
	enum { TOK_START=0, TOK_END };
	token tokval;
	union
	{
	    struct
	    {
		int rows;
		int cols;
	    } table;
	    const char *d;
	} data;
	int end;

	friend ostream &operator <<(ostream &, const tok *f);
	tok &operator=(const tok &d);

	/* Avoid the need to cast NULL everywhere  for NULL defined as
	   (void *) 0 */
	inline tok(token t, void *d, int e)
	{
	    tokval=t;
	    data.d=(const char *) d;
	    dtype=TEXT;
	    end=e;
	}
	/* Avoid the need to cast NULL everywhere for NULL defined as 0 */
	inline tok(token t, int d, int e)
	{
	    d=d;
	    tokval=t;
	    data.d=(const char *) NULL;
	    dtype=TEXT;
	    end=e;
	}
	/* Avoid the need to cast NULL everywhere for NULL defined as 0L */
	inline tok(token t, long int d, int e)
	{
	    d=d;
	    tokval=t;
	    data.d=(const char *) NULL;
	    dtype=TEXT;
	    end=e;
	}

	inline tok(token t, const char *d, int e)
	{
	    tokval=t;
	    if (d!=NULL)
		data.d=strdup(d);
	    else
		data.d=NULL;
	    dtype=TEXT;
	    end=e;
	}
	inline tok(token t, int c, int r, int e)
	{
	    tokval=t;
	    data.table.rows=r;
	    data.table.cols=c;
	    dtype=TABLE;
	    end=e;
	}
	inline ~tok(void)
	{
	    if (dtype==TEXT && data.d!=NULL)
		free((void *) data.d);
	}
    };


private:
    /* Private class for table information */
    class table_info
    {
    private:
	fifo<tok> toks;

    public:
	int rows;
	int cols;
	int col;

	inline table_info(void)
	{
	    cols=0;
	    col=0;
	    rows=0;
	}

	inline void tok_push(token t, tblock *s)
	{
	    tok *td;
	    td=new(tok)(t, (const char *) (*s), tok::TOK_START);
	    toks.enqueue(td);
	    td=new(tok)(t, (void *) NULL, tok::TOK_END);
	    toks.enqueue(td);
	}

	inline void enqueue(const tok *t)
	{
	    toks.enqueue(t);
	}

	inline void finish(fifo<tok> *out)
	{
	    tok *t;

	    t=new(tok)(T_TABLE, cols, rows, tok::TOK_START);
	    out->enqueue(t);
	    out->transfer(&toks);
	    t=new(tok)(T_TABLE, cols, rows, tok::TOK_END);
	    out->enqueue(t);
	}
	inline ~table_info(void) {} // Avoid compiler bug
    };

private:
    fifo<tok> output;		/* Output with equation cleaned up */
    const tok *saved_tok;
    int done_end;
    table_info *table;
    int rd_token(void);
    const tok *feed_token(void);
    const tok *math_collect(void);

    /* Token pusher */
    inline void tok_push(token t, tblock *s)
    {
	tok *td;
	td=new(tok)(t, (const char *) (*s), tok::TOK_START);
	output.enqueue(td);
	td=new(tok)(t, (void *) NULL, tok::TOK_END);
	output.enqueue(td);
    }

    const tok *eqn_rd_token(void);
	
    /* List handling */
    typedef enum { LIST_BULLET, LIST_ENUMERATE, LIST_ENUM_ALPHA } l_type;
    struct list_info
    {
	struct list_info *next_list;
	l_type list_type;
	int obj_cnt;
	int text_cnt;
	int items;
	union
	{
	    int item_no;
	    int lbullet;
	} ldata;
	fifo<tok_seq::tok> *last_item;
    };
    struct list_info *lp;	/* List pointer */
    fifo<tok> outqueue;		/* Final output (so far...) */
    fifo<tok> *recycled;	/* elements to be processed again */
    struct list_info *list_type(const char *);
    const char *list_check(const char *, struct list_info **);
    const char *l_type_name(const struct list_info *);

 public:


    tok_seq(istream *in):  chunk_reader(in)
    {
	tok *t=new(tok)(T_DOC, "Converted by word2x", tok::TOK_START);
	table=NULL;		/* No table */
	saved_tok=NULL;		/* No saved token */
	lp=NULL;		/* No list */
	recycled=NULL;		/* Nothing recycled */
	done_end=0;

	output.enqueue(t);
    }

    inline void return_toks(fifo<tok> *tp)
    {
	if (recycled==NULL)
	    recycled=new(fifo<tok>);
	recycled->ins_trans(tp);
    }   
    
    inline ~tok_seq(void) {}	// Avoids compiler bug

    const tok *read_token(void);

};
#endif /* __EXCLUDE_READER_CLASSES */
#endif /* __w6_reader_h__ */