File: httext.cc

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (330 lines) | stat: -rw-r--r-- 7,002 bytes parent folder | download | duplicates (2)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* 

 *	HT Editor
 *	httext.cc
 *
 *	Copyright (C) 1999-2002 Stefan Weyergraf
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "htsearch.h"
#include "httext.h"
#include "stream.h"

#include <string.h>

ht_view *httext_init(Bounds *b, File *file, ht_format_group *group)
{
	/* no httext for file > 5 MiB */
	if (file->getSize() > 5*1024*1024) {
		return NULL;
	}

	ht_text_viewer2 *v=new ht_text_viewer2();
	v->init(b, TEXT_DESC, 0/*VC_EDIT | VC_GOTO | VC_SEARCH | VC_BLOCKOP | VC_TRUNCATE*/, file, group);

	v->search_caps|=SEARCHMODE_BIN | SEARCHMODE_EVALSTR;

	ht_text_sub *t=new ht_text_sub();
	t->init(file, 0x0, file->getSize());
	v->insertsub(t);
	return v;
}

format_viewer_if httext_if = {
	httext_init,
	0
};

/*
 *	CLASS ht_text_viewer2
 */

/*bool ht_text_viewer2::offset_to_pos(FileOfs ofs, viewer_pos *pos)
{
	pos->u.sub = first_sub;
	pos->u.line_id.id1 = ofs;
	pos->u.line_id.id2 = 0;
	pos->u.tag_idx = 0;
	pos->u.tag_group = 0;
	return true;
}

bool ht_text_viewer2::pos_to_offset(viewer_pos pos, FileOfs *ofs)
{
	*ofs = pos.u.line_id.id1;
	return true;
}*/

void ht_text_viewer2::handlemsg(htmsg *msg)
{
	switch (msg->msg) {
	case msg_keypressed:
		switch (msg->data1.integer) {
		case K_Left: {
			// FIXME: send cmd_bla when available
			htmsg m;
			m.msg = msg_keypressed;
			m.type = mt_empty;
			m.data1.integer = K_Control_Left;
			sendmsg(&m);
			clearmsg(msg);
			return;
		}
		case K_Right: {
			// FIXME: send cmd_bla when available
			htmsg m;
			m.msg = msg_keypressed;
			m.type = mt_empty;
			m.data1.integer = K_Control_Right;
			sendmsg(&m);
			clearmsg(msg);
			return;
		}
		}
		break;
	}
	return ht_uformat_viewer::handlemsg(msg);
}

/*
 *	CLASS ht_text_sub
 */

/* FIXME: put it somewhere else..., why ain't this a POSIX function ? */
const void *ht_memrchr(const void *string, int ch, size_t num)
{
	while (num--) {
		if (((const char*)string)[num]==ch) return ((const char*)string)+num;
	}
	return NULL;
}
 

#define TEXT_SUB_READSIZE		256
#define TEXT_SUB_MAX_LINELEN		512

#define TEXT_SUB_MAX_LINEENDLEN	2

#define TEXT_SUB_TABSIZE			5

byte ht_text_sub_line[TEXT_SUB_MAX_LINELEN];

void ht_text_sub::init(File *file, FileOfs offset, int size)
{
	ht_linear_sub::init(file, offset, size);
}

void ht_text_sub::done()
{
	ht_linear_sub::done();
}

bool ht_text_sub::convert_ofs_to_id(const FileOfs offset, LINE_ID *line_id)
{
	clear_line_id(line_id);
	line_id->id1 = offset;
	prev_line_id(line_id, 1);
	return true;
}

bool ht_text_sub::convert_id_to_ofs(const LINE_ID line_id, FileOfs *offset)
{
	return false;
}

uint ht_text_sub::find_linelen_backwd(byte *buf, uint maxbuflen, FileOfs ofs, int *le_len)
{
	uint readlen=(maxbuflen>TEXT_SUB_READSIZE) ? TEXT_SUB_READSIZE : maxbuflen;
	uint oreadlen=readlen;
	FileOfs oofs=ofs;
	byte *bufp;
	uint s;
	uint len=0;
	uint lineends=0;

	if (le_len) *le_len=0;
	do {
		if (ofs==fofs) break;
		if (readlen>ofs) readlen=ofs;
		if (ofs-readlen<fofs) readlen=ofs-fofs;
		ofs-=readlen;
		file->seek(ofs);
/* make sure current and next read overlap
   to guarantee proper lineend-matching */
		if (readlen==oreadlen) ofs+=TEXT_SUB_MAX_LINEENDLEN-1; else
			if (ofs+readlen+TEXT_SUB_MAX_LINEENDLEN-1<=oofs)
				readlen+=TEXT_SUB_MAX_LINEENDLEN-1;
		s=file->read(buf, readlen);
		int l;
		bufp=match_lineend_backwd(buf, s, &l);
		if (bufp) {
			lineends++;
			if (lineends==1) {
				bufp=match_lineend_backwd(buf, bufp-buf, &l);
				if (bufp) lineends++;
			}
			if (lineends==2) {
				len+=buf+s-bufp-1;
				if (len>TEXT_SUB_MAX_LINELEN) {
					len=TEXT_SUB_MAX_LINELEN;
					break;
				}
				if (le_len) *le_len=l;
				break;
			}
		}
		len+=s;
		if (len>TEXT_SUB_MAX_LINELEN) {
			len=TEXT_SUB_MAX_LINELEN;
			break;
		}
	} while (s);
	return len;
}

uint ht_text_sub::find_linelen_forwd(byte *buf, uint maxbuflen, FileOfs ofs, int *le_len)
{
	uint readlen=(maxbuflen>TEXT_SUB_READSIZE) ? TEXT_SUB_READSIZE : maxbuflen;
	byte *bufp;
	uint s;
	uint len = 0;

	if (le_len) *le_len = 0;
	do {
		file->seek(ofs);
		s = file->read(buf, readlen);
		int l;
		bufp = match_lineend_forwd(buf, s, &l);
		if (bufp) {
			len += bufp-buf+l;
			if (le_len) *le_len = l;
			break;
		}
		if (s != readlen) {
			len += s;
			break;
		}
		/* make sure current and next read overlap
		   to guarantee proper lineend-matching */
		if (s > (TEXT_SUB_MAX_LINEENDLEN-1)) {
			len += s-(TEXT_SUB_MAX_LINEENDLEN-1);
		}
		ofs += s-(TEXT_SUB_MAX_LINEENDLEN-1);
	} while (s == readlen);
	if (len > TEXT_SUB_MAX_LINELEN) {
		len = TEXT_SUB_MAX_LINELEN;
		if (le_len) *le_len = 0;
	}
	return len;
}

void ht_text_sub::first_line_id(LINE_ID *line_id)
{
	clear_line_id(line_id);
	line_id->id1 = 0;
}

bool ht_text_sub::getline(char *line, int maxlen, const LINE_ID line_id)
{
	byte *bufp = (byte*)line;
	FileOfs ofs = line_id.id1;
	int ll;
	uint l = find_linelen_forwd(ht_text_sub_line, sizeof ht_text_sub_line, ofs, &ll);
	if (l) {
		l -= ll;
		if (l > 255) l = 255;
		file->seek(ofs);
		l = file->read(line, l);
		while (l--) {
			if (*bufp=='\e' || *bufp==0) *bufp = '.';
			bufp++;
		}
		*bufp = 0;
		return true;
	}
	return false;
}

void ht_text_sub::last_line_id(LINE_ID *line_id)
{
	clear_line_id(line_id);
	FileOfs ofs = fofs+fsize;
	uint l = find_linelen_backwd(ht_text_sub_line, sizeof ht_text_sub_line, ofs, NULL);
	line_id->id1 = ofs-l;
}

byte *ht_text_sub::match_lineend_forwd(byte *buf, uint buflen, int *le_len)
{
	byte *result=NULL;
	
	byte *n=(byte*)memchr(buf, '\n', buflen);
	if (n) {
		if ((n>buf) && (n[-1] == '\r')) {
			*le_len=2;
			result=n-1;
		} else {
			*le_len=1;
			result=n;
		}
	}
	return result;
}

byte *ht_text_sub::match_lineend_backwd(byte *buf, uint buflen, int *le_len)
{
	byte *result=NULL;
	
	byte *n=(byte*)ht_memrchr(buf, '\n', buflen);
	if (n) {
		if ((n>buf) && (n[-1] == '\r')) {
			*le_len=2;
			result=n-1;
		} else {
			*le_len=1;
			result=n;
		}
	}
	return result;
}

int ht_text_sub::next_line_id(LINE_ID *line_id, int n)
{
	FileOfs ofs = line_id->id1;
	int r=0;
	while (n--) {
		uint l=find_linelen_forwd(ht_text_sub_line, sizeof ht_text_sub_line, ofs, NULL);
		ofs+=l;
		if (!l) break;
		r++;
	}
	line_id->id1 = ofs;
	return r;
}

int ht_text_sub::prev_line_id(LINE_ID *line_id, int n)
{
	FileOfs ofs = line_id->id1;
	int r=0;
	while (n--) {
		uint l=find_linelen_backwd(ht_text_sub_line, sizeof ht_text_sub_line, ofs, NULL);
		ofs-=l;
		if (!l) break;
		r++;
	}
	line_id->id1 = ofs;
	return r;
}