This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
XTBDicDBReader.cpp
187 lines (147 loc) · 3.85 KB
/
XTBDicDBReader.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
//
// XTBDicDBReader.cpp
// XTBook
//
// Created by Kawada Tomoaki on 7/6/11.
// Copyright 2011 Nexhawks. All rights reserved.
//
#include "XTBDicDBReader.h"
#include <tcw/twLock.h>
#include <tcw/twSDLSemaphore.h>
#include "XTBFileStream.h"
#include <assert.h>
#include "XTBException.h"
XTBDicDBReader::XTBDicDBReader(const XTBSysString& path):
m_contentsStream(NULL),
m_keysStream(NULL),
m_keyIndexStream(NULL),
m_path(path){
try{
m_contentsStream=XTBFileStream::streamForReadingAtPath(pathForContents(), true);
m_keysStream=XTBFileStream::streamForReadingAtPath(pathForKeys());
m_keyIndexStream=XTBFileStream::streamForReadingAtPath(pathForKeyIndex());
}catch(...){
if(m_contentsStream)
delete m_contentsStream;
if(m_keysStream)
delete m_keysStream;
if(m_keyIndexStream)
delete m_keyIndexStream;
throw;
}
m_contentsMutex=new twSDLSemaphore();
m_keysMutex=new twSDLSemaphore();
m_keyIndexMutex=new twSDLSemaphore();
m_keyCount=m_keyIndexStream->readBEInt32();
if(m_keyCount==0){
XTBThrowException(L"XTBInvalidFormatException", NULL);
}
}
XTBSysString XTBDicDBReader::pathForContents() const{
return m_path+XTBSysText(".db");
}
XTBSysString XTBDicDBReader::pathForKeys() const{
return m_path+XTBSysText(".keys");
}
XTBSysString XTBDicDBReader::pathForKeyIndex() const{
return m_path+XTBSysText(".indices");
}
XTBDicDBReader::~XTBDicDBReader(){
m_contentsMutex->wait();
m_keysMutex->wait();
m_keyIndexMutex->wait();
delete m_contentsStream;
delete m_keysStream;
delete m_keyIndexStream;
delete m_contentsMutex;
delete m_keysMutex;
delete m_keyIndexMutex;
}
std::string XTBDicDBReader::contentsAtPointer(XTBDicDBPointer ptr,
uint32_t maxLength) const{
twLock lock(m_contentsMutex);
m_contentsStream->seekToOffset((XTBOffset)ptr);
uint32_t length;
length=m_contentsStream->readBEInt32();
if(length>maxLength)
length=maxLength;
return m_contentsStream->readToString(length);
}
XTBDicDBReaderKey XTBDicDBReader::keyAtIndex(unsigned int index) const{
assert(index<m_keyCount);
twLock lock1(m_keysMutex);
twLock lock2(m_keyIndexMutex);
m_keyIndexStream->seekToOffset(index*4+4);
// read address to key.
uint32_t addr=m_keyIndexStream->readBEInt32();
XTBDicDBReaderKey key;
// read lower address.
m_keysStream->seekToOffset(addr);
assert(m_keysStream->position()==addr);
key.pointer=m_keysStream->readBEInt32();
// ???: is this really needed?
if(index>0){
assert(addr!=0);
}else{
assert(addr==0);
}
// read upper address, and length.
uint32_t composite;
size_t length;
composite=m_keysStream->readBEInt32();
key.pointer|=((uint64_t)(composite&0xffff))<<32;
length=composite>>16;
// read key.
key.key=m_keysStream->readToString(length);
return key;
}
XTBDicDBPointer XTBDicDBReader::pointerForKey(const std::string &query) const{
// special case
if(m_keyCount==1){
XTBDicDBReaderKey k=keyAtIndex(0);
if(k.key==query)
return k.pointer;
return XTBDicDBInvalidPointer;
}
// do binary search
XTBDicDBReaderKey k1, k2, k3;
unsigned i1, i2, i3;
i1=0; i2=m_keyCount-1;
k1=keyAtIndex(i1);
k2=keyAtIndex(i2);
if(query<k1.key) // outta range?
return XTBDicDBInvalidPointer;
else if(query==k1.key)
return k1.pointer;
if(query>k2.key) // outta range?
return XTBDicDBInvalidPointer;
else if(query==k2.key)
return k2.pointer;
while(i1<=i2){
i3=(i1+i2)/2;
if(i3==i1)
k3=k1;
else if(i3==i2)
k3=k2;
else
k3=keyAtIndex(i3);
if(query==k3.key){
return k3.pointer;
}else if(query>k3.key){
i1=i3+1;
k1=keyAtIndex(i1);
if(query<k1.key) // outta range?
return XTBDicDBInvalidPointer;
else if(query==k1.key)
return k1.pointer;
}else{
i2=i3-1;
k2=keyAtIndex(i2);
if(query>k2.key) // outta range?
return XTBDicDBInvalidPointer;
else if(query==k2.key)
return k2.pointer;
}
}
return XTBDicDBInvalidPointer;
}