-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
242 lines (208 loc) · 4.14 KB
/
parser.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
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
/**
* Text Parser.
* vim:sw=2:sts=2:et
*/
#include "parser.h"
#include "para.h"
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include "qzfile.h"
OffsetDB::OffsetDB()
{
m_ldb = NULL;
m_list = m_last = NULL;
m_lines = 0;
}
OffsetDB::~OffsetDB()
{
LineInfo *to_del;
while (m_list) {
to_del = m_list;
m_list = m_list->next;
free(to_del);
}
if (m_ldb)
free(m_ldb);
}
void
OffsetDB::beginAdd()
{
LineInfo *to_del;
while (m_list) {
to_del = m_list;
m_list = m_list->next;
free(to_del);
}
if (m_ldb)
free(m_ldb);
m_ldb = NULL;
m_list = NULL;
m_last = NULL;
m_lines = 0;
}
#define MAX_OFFSET_COUNT 100
void
OffsetDB::addOffset(unsigned long offset)
{
if (m_last==NULL || m_last->cnt==MAX_OFFSET_COUNT) {
LineInfo *n_line;
n_line = (LineInfo*)malloc(sizeof(LineInfo)+
sizeof(unsigned long)*(MAX_OFFSET_COUNT-1));
n_line->cnt = 0;
n_line->next = NULL;
if (m_last) {
m_last->next = n_line;
} else {
m_list = n_line;
}
m_last = n_line;
}
m_last->offsets[m_last->cnt++] = offset;
m_lines++;
}
void
OffsetDB::endAdd(unsigned long offset)
{
addOffset(offset);
int db_count;
db_count = (m_lines+MAX_OFFSET_COUNT-1)/MAX_OFFSET_COUNT;
m_ldb = (unsigned long **)malloc(sizeof(unsigned long*)*db_count);
LineInfo *ptr = m_list;
db_count = 0;
while (ptr) {
m_ldb[db_count++] = ptr->offsets;
ptr = ptr->next;
}
m_lines--;
}
unsigned long
OffsetDB::operator[] (int para)
{
if (m_lines>para && para>=0) {
return m_ldb[para/MAX_OFFSET_COUNT][para%MAX_OFFSET_COUNT];
}
if (m_lines>0)
return m_ldb[0][0];
return 0;
}
int
OffsetDB::count()
{
return m_lines;
}
int
OffsetDB::length(int para)
{
if (m_lines>para && para>=0) {
return (int)(m_ldb[(para+1)/MAX_OFFSET_COUNT][(para+1)%MAX_OFFSET_COUNT]
- m_ldb[para/MAX_OFFSET_COUNT][para%MAX_OFFSET_COUNT]);
}
if (m_lines>0)
return (int)(m_ldb[0][1]-m_ldb[0][0]);
return 0;
}
int
OffsetDB::indexOf(unsigned long offset)
{
LineInfo *ptr = m_list;
int idx = 0, idx_base = 0;
while (ptr) {
if (ptr->offsets[0]<=offset &&
ptr->offsets[ptr->cnt-1]>=offset) {
for (idx=0 ; idx<ptr->cnt && ptr->offsets[idx]<offset ;idx++);
if (idx>0 && (idx>=ptr->cnt || ptr->offsets[idx]!=offset)) idx--;
return idx_base+idx;
} else {
idx_base += ptr->cnt;
ptr = ptr->next;
}
}
return m_lines-1;
}
SimpleParser::SimpleParser()
{
}
Paragraph
SimpleParser::getParagraph(int idx)
{
//printf("GetParagraphI(%d,%d)\n", m_offdb[idx], m_offdb.length(idx));
return getParagraphI(m_offdb[idx], m_offdb.length(idx));
}
unsigned long
SimpleParser::getOffset(int idx)
{
return m_offdb[idx];
}
unsigned long
SimpleParser::getLength(int idx)
{
return m_offdb.length(idx);
}
int
SimpleParser::getParaID(unsigned long offset)
{
return m_offdb.indexOf(offset);
}
int
SimpleParser::getParaCount()
{
return m_offdb.count();
}
/**
* search keyword in paragraph.
*/
SearchResult
SimpleParser::search(const QString &str, int flags, int para, short offset)
{
return SearchResult();
}
#include "hcode.h"
#include "ansiparser.h"
/**
* Detecting format of file.
*/
TParser *
TParser::getParser(QZFile *fd)
{
QString f_name;
/* file name check */
/*
f_name = fd->name();
if (f_name.right(3)==".dz") {
return new DictParser(fd);
}
*/
return getANSIParser(fd, 0);
}
TParser *
TParser::getANSIParser(QZFile *fd, int encoding_id)
{
const char *encoding = "euc-kr";
switch (encoding_id) {
default:
case 0:
{
int ks_count, kssm_count, eng_count;
char buffer[2048];
int readed;
readed = fd->read(buffer, sizeof(buffer));
isKS(buffer, readed, &ks_count, &kssm_count, &eng_count);
if ((ks_count+kssm_count)>=eng_count) {
if (kssm_count>ks_count)
encoding = "johab";
else
encoding = "euc-kr";
}
}
break;
case 1:
encoding = "euc-kr";
break;
case 2:
encoding = "johab";
break;
}
//printf("ENCODING:%s\n", encoding);
return new AnsiParser(fd, encoding);
}