summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/arrierego.cpp
blob: be2a3fa0f0ecd0a3c298e7b354313ee602ed76ec (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
#ifdef USENEF
#include <qimage.h>
#include "arrierego.h"

struct CArriere_dataRecord
{
    UInt16 uid;
    UInt16 nParagraphs;
    UInt32 size;
    UInt8 type;
    UInt8 reserved;
};

int CArriere::HeaderSize()
{
    return 10;
}

void CArriere::GetHeader(UInt16& uid, UInt16& nParagraphs, UInt32& size, UInt8& type, UInt8& reserved)
{
    CArriere_dataRecord thishdr;
    unsuspend();
    fread(&thishdr, 1, HeaderSize(), fin);
    uid = ntohs(thishdr.uid);
    nParagraphs = ntohs(thishdr.nParagraphs);
    size = ntohl(thishdr.size);
    type = thishdr.type;
    reserved = thishdr.reserved;
    //    qDebug("UID:%u Paras:%u Size:%u Type:%u Reserved:%u", uid, nParagraphs, size, (unsigned int)type, (unsigned int)reserved);
//    for (int i = 0; i < sizeof(thishdr); i++)
//	printf("%d %x\n", i, (int)(((char *)(&thishdr))[i]));
}

bool CArriere::CorrectDecoder()
{
  char  * type = (char*)(&(head.type));
  for (int i = 0; i < 8; ++i) qDebug("%c", type[i]);
    return (memcmp(&head.type, "ArriereG", 8) == 0);
}

int CArriere::bgetch()
{
    int ch = EOF;
    if (bufferpos >= buffercontent)
    {
	if (bufferrec >= ntohs(head.recordList.numRecords) - 1)
	{
		return EOF;
	}
	if (isEndOfSection(bufferrec))
	{
		return EOF;
	}
	if (!expand(bufferrec+1))
	{
		return EOF;
	}
	mystyle.unset();
	if (m_ParaOffsets[m_nextParaIndex] == 0)
	{
	    while (m_ParaOffsets[m_nextParaIndex+1] == 0)
	    {
		m_nextParaIndex++;
	    }
	}
	mystyle.setExtraSpace((m_ParaAttrs[m_nextParaIndex]&7)*2);
    }
    if (bufferpos == m_nextPara)
    {
	while (bufferpos == m_nextPara)
	{
	    m_nextParaIndex++;
	    if (m_nextParaIndex == m_nParas)
	    {
		m_nextPara = -1;
	    }
	    else
	    {
		m_nextPara += m_ParaOffsets[m_nextParaIndex];
	    }
	}
	mystyle.unset();
	mystyle.setExtraSpace((m_ParaAttrs[m_nextParaIndex]&7)*2);
	if (m_lastBreak == locate())
	{
	    currentpos++;
	    ch = expandedtextbuffer[bufferpos++];
	}
	else
	{
	    ch = 10;
	}
    }
    else
    {
	currentpos++;
	ch = expandedtextbuffer[bufferpos++];
    }
    return ch;
}

tchar CArriere::getch(bool fast)
{
    mystyle.clearPicture();
    return getch_base(fast);
}

/*
void CArriere::setlink(QString& fn, const QString& wrd)
{
    fn = wrd;
}
*/

QImage* CArriere::imagefromdata(UInt8* imgbuffer, UInt32 imgsize)
{
    QByteArray arr;
    arr.assign((const char*)imgbuffer, imgsize);

    QImage* qimage = new QImage(arr);

    return qimage;
}

void CArriere::start2endSection()
{
    m_currentstart = NEFstartSection();
    m_currentend = NEFendSection();
}

unsigned long CArriere::NEFstartSection()
{
//inefficient - Should calc start/end of section on entry to section?
    UInt16 thishdr_uid, thishdr_nParagraphs;
    UInt32 thishdr_size;
    UInt8 thishdr_type, thishdr_reserved;
    unsigned long textlength = currentpos-bufferpos;
    for (UInt16 recptr = bufferrec-1; recptr >= 1; recptr--)
    {
	gotorecordnumber(recptr);
	//qDebug("recptr:%u", recptr);
	GetHeader(thishdr_uid, thishdr_nParagraphs, thishdr_size, thishdr_type, thishdr_reserved);
	if (thishdr_type < 2)
	{
	    if ((thishdr_reserved && continuation_bit) == 0) break;
	    textlength -= thishdr_size;
	    //qDebug("Textlength:%u, reserved:%u, recptr:%u", textlength, thishdr_reserved, recptr);
	}
    }
    return textlength;
}

unsigned long CArriere::NEFendSection()
{
//inefficient - Should calc start/end of section on entry to section?
    unsigned long textlength = currentpos-bufferpos+buffercontent;
    gotorecordnumber(bufferrec);
    UInt16 thishdr_uid, thishdr_nParagraphs;
    UInt32 thishdr_size;
    UInt8 thishdr_type, thishdr_reserved;
    GetHeader(thishdr_uid, thishdr_nParagraphs, thishdr_size, thishdr_type, thishdr_reserved);
    if ((thishdr_reserved && continuation_bit) != 0)
      {
	for (UInt16 recptr = bufferrec+1; recptr < ntohs(head.recordList.numRecords); recptr++)
	  {
	    gotorecordnumber(recptr);
	    UInt16 thishdr_uid, thishdr_nParagraphs;
	    UInt32 thishdr_size;
	    UInt8 thishdr_type, thishdr_reserved;
	    GetHeader(thishdr_uid, thishdr_nParagraphs, thishdr_size, thishdr_type, thishdr_reserved);
	    //	qDebug("recptr %u bufferrec %u type %u m_reserved %u", recptr, bufferrec, typ
	    if (thishdr_type < 2)
	      {
		textlength += thishdr_size;
		if ((thishdr_reserved && continuation_bit) == 0) break;
	      }
	  }
      }
    return textlength;
}

#ifndef __STATIC
extern "C"
{
  CExpander* newcodec() { return new CArriere; }
}
#endif
#endif