summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/BuffDoc.h
blob: 7c5ef9a420a422e890b612b2f43074af53863ca8 (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
#ifndef __BuffDoc_h
#define __BuffDoc_h

#include "CBuffer.h"
#include "ZText.h"
#include "Aportis.h"
#include "ztxt.h"
#include "ppm_expander.h"
#include "CFilter.h"
#include <qfontmetrics.h>
#include <qmessagebox.h>

class BuffDoc
{
    CBuffer lastword;
    CSizeBuffer lastsizes, allsizes;
    size_t laststartline;
    bool lastispara;
    CExpander* exp;
    CFilterChain* filt;
    QFontMetrics* m_fm;
 public:
    ~BuffDoc()
	{
	    delete filt;
	    delete exp;
	}
    BuffDoc() : m_fm(NULL)
	{
	    exp = NULL;
	    filt = NULL;
	    //    qDebug("Buffdoc created");
	}
    bool empty() { return (exp == NULL); }
    void setfm(QFontMetrics* fm)
	{
	    m_fm = fm;
	    //    qDebug("Buffdoc:setfm");
	}
    void setfilter(CFilterChain* _f)
	{
	    if (filt != NULL) delete filt;
	    filt = _f;
	    filt->setsource(exp);
	}
    CList<Bkmk>* getbkmklist() { return exp->getbkmklist(); }
    bool hasrandomaccess() { return (exp == NULL) ? false : exp->hasrandomaccess(); }
    bool iseol() { return (lastword[0] == '\0'); }
    int openfile(QWidget* _parent, const char *src)
	{
	    //    qDebug("BuffDoc:Openfile:%s", src);
	    //    qDebug("Trying aportis %x",exp);
	    if (exp != NULL) delete exp;
	    lastword[0] = '\0';
	    lastsizes[0] = laststartline = 0;
	    lastispara = false;
	    /*
	      exp = new Text;
	      int ret = exp->openfile(src);
	    */

	    exp = new Aportis;
	    //    qDebug("Calling openfile");
	    int ret = exp->openfile(src);
	    //    qDebug("Called openfile");
	    if (ret == -1)
	    {
		//		qDebug("problem opening source file:%s",src);
		delete exp;
		exp = NULL;
		return ret;
	    }
	    if (ret == -2)
	    {

		delete exp;
//		qDebug("Trying ztxt");
		exp = new ztxt;
		ret = exp->openfile(src);
	    }
#ifndef SMALL
	    if (ret != 0)
	    {
		delete exp;
//		qDebug("Trying ppms");
		exp = new ppm_expander;
		ret = exp->openfile(src);
	    }
	    
	    if (ret != 0)
	    {
		delete exp;
		exp = new Text;
//		qDebug("Trying text");
		ret = exp->openfile(src);
	    }
#else
	    if (ret != 0)
	    {
		delete exp;
	        exp = new Text;
		ret = exp->openfile(src);
	    }
#endif
	    if (ret != 0)
	    {
		delete exp;
		QMessageBox::information(_parent, "QTReader", "Unknown file compression type","Try another file");
		return ret;
	    }
	    //        qDebug("Doing final open:%x:%x",exp,filt);

	    lastword[0] = '\0';
	    lastsizes[0] = laststartline = 0;
	    lastispara = false;
	    exp->locate(0);
	    filt->setsource(exp);
	    //        qDebug("BuffDoc:file opened");
	    return 0;
	}
    int getch() { return (exp == NULL) ? UEOF : filt->getch(); }
    unsigned int locate() { return (exp == NULL) ? 0 : laststartline; }
    void locate(unsigned int n);
    bool getline(CBuffer* buff, int w);
    bool getline(CBuffer* buff, int w, int cw);
    void sizes(unsigned long& fs, unsigned long& ts) { exp->sizes(fs,ts); }
    int getpara(CBuffer& buff)
	{
	    int ch, i = 0;
	    while ((ch = getch()) != 10 && ch != UEOF) buff[i++] = ch;
	    buff[i] = '\0';
	    if (i == 0 && ch == UEOF) i = -1;
	    laststartline = exp->locate();
	    return i;
	}
};

#endif