-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansiparser.cpp
113 lines (99 loc) · 2.04 KB
/
ansiparser.cpp
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
/**
* ANSI text parser.
* vim:ts=8:sw=2
*/
#include "ansiparser.h"
#include "qzfile.h"
#include "unicode.h"
#include <malloc.h>
#define BUFFER_SIZE 1024
static int
makeLineDB(OffsetDB &db, QZFile *fd)
{
char buf[BUFFER_SIZE];
int idx, readed;
unsigned long offset;
bool new_line;
db.beginAdd();
new_line = true;
fd->seek(0,SEEK_SET);
while(1) {
offset = fd->tell();
readed = fd->read(buf, BUFFER_SIZE);
for (idx=0 ; idx<readed ; idx++) {
if (new_line) {
db.addOffset(offset+idx);
new_line = false;
}
if (buf[idx]=='\n')
new_line = true;
}
if (readed<BUFFER_SIZE)
break;
}
db.endAdd(fd->tell());
return 0;
}
AnsiParser::AnsiParser(QZFile *fd, const char *enc)
{
m_fd = fd;
if (enc)
m_enc = strdup(enc);
else
m_enc = NULL;
makeLineDB(m_offdb, fd);
}
bool
AnsiParser::setEncoding(const char *enc)
{
if (m_enc)
free(m_enc);
if (enc)
m_enc = strdup(enc);
else
m_enc = NULL;
return true;
}
Paragraph
AnsiParser::getParagraphI(unsigned long offset, int length)
{
m_fd->seek(offset, SEEK_SET);
char *buffer;
buffer = new char[length+1];
int result = m_fd->read(buffer, length);
if (result<length)
goto fail;
/* remove new line or carrige return */
while (result>0 && (buffer[result-1]=='\n' || buffer[result-1]=='\r'))
result--;
buffer[result] = 0;
if (result<1) goto fail;
{
Paragraph mine(result);
int idx, enc, mb_size, w_idx;
wchar_t wch;
enc = Unicode::getEncoding(m_enc, NEED_MB_TO_WC);
if (enc<0) enc = Unicode::getEncoding(NULL);
w_idx = 0;
for (idx=0 ; idx<result ;) {
mb_size = Unicode::mbtowc(&wch, &buffer[idx], result-idx, enc);
if (mb_size<0) {
idx++;
continue;
}
idx += mb_size;
mine.m_str->append(QChar((unsigned short)wch));
//mine.m_attr[w_idx].attr.col = w_idx%8;
w_idx++;
}
delete buffer;
return mine;
}
fail:
delete buffer;
return Paragraph();
}
AnsiParser::~AnsiParser()
{
delete m_fd;
}