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
/
XTBHistory.cpp
237 lines (182 loc) · 5.55 KB
/
XTBHistory.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
//
// XTBHistory.cpp
// XTBook
//
// Created by 河田 智明 on 8/3/11.
// Copyright 2011 Nexhawks. All rights reserved.
//
#include "XTBHistory.h"
#include "XTBFileStream.h"
#include <tcw/twLock.h>
#include <tcw/twSDLSemaphore.h>
#include <tcw/twStrConv.h>
#include <assert.h>
#include <exception>
#include "TWiki/TWUtils.h"
#include "XTBNotificationCenter.h"
#include <time.h>
XTBHistoryDB::XTBHistoryDB(const XTBSysString& path):
m_dbHandle(NULL),
m_indexHandle(NULL),
m_path(path){
try{
if(XTBDoesPathExist(path+XTBSysText(".historydb"))==false
|| XTBDoesPathExist(path+XTBSysText(".historyindex"))==false){
// create db.
m_dbHandle=XTBFileStream::streamForWritingAtPath(m_path+XTBSysText(".historydb"));
m_indexHandle=XTBFileStream::streamForWritingAtPath(m_path+XTBSysText(".historyindex"));
delete m_dbHandle;
delete m_indexHandle;
}
m_dbHandle=XTBFileStream::streamForUpdatingAtPath(m_path+XTBSysText(".historydb"));
m_indexHandle=XTBFileStream::streamForUpdatingAtPath(m_path+XTBSysText(".historyindex"));
}catch(...){
if(m_dbHandle)
delete m_dbHandle;
if(m_indexHandle)
delete m_indexHandle;
throw;
}
m_indexHandle->seekToEndOfStream();
m_historyCount=m_indexHandle->position()/8;
XTBLog("%d history(ies) loaded", (int)m_historyCount);
m_dbSemaphore=new twSDLSemaphore();
m_indexSemaphore=new twSDLSemaphore();
}
XTBHistoryDB::~XTBHistoryDB(){
m_dbSemaphore->lock();
m_indexSemaphore->lock();
delete m_dbSemaphore;
delete m_indexSemaphore;
delete m_dbHandle;
delete m_indexHandle;
}
XTBHistoryDB::IndexItem XTBHistoryDB::indexItemAtIndex(uint64_t index)
{
twLock lock(m_indexSemaphore);
m_indexHandle->seekToOffset(index*8);
IndexItem item;
uint32_t buf;
buf=m_indexHandle->readBEInt32();
item.length=(uint16_t)(buf>>16);
item.offset=((uint64_t)(buf&0xffff))<<32;
buf=m_indexHandle->readBEInt32();
item.offset|=((uint64_t)buf);
return item;
}
void XTBHistoryDB::writeIndexItemAtEnd(const XTBHistoryDB::IndexItem &item){
twLock lock(m_indexSemaphore);
m_indexHandle->seekToEndOfStream();
uint32_t buf;
buf=((uint32_t)item.length)<<16;
buf|=(uint32_t)(item.offset>>32);
m_indexHandle->writeBEInt32(buf);
buf=(uint32_t)(item.offset);
m_indexHandle->writeBEInt32(buf);
m_indexHandle->synchronize();
}
std::string XTBHistoryDB::entryForItem(const XTBHistoryDBItem & item){
std::string outStr;
outStr=twW2M(item.date);
outStr+='\t';
outStr+=twW2M(item.url);
outStr+='\n';
return outStr;
}
XTBHistoryDBItem XTBHistoryDB::itemForEntry(const std::string & entry){
XTBHistoryDBItem item;
std::string::size_type pos;
pos=entry.find('\t');
if(pos==std::string::npos){
// invalid format... heuristics.
if(entry.empty()){
// empty!? that's strange!
return item;
}
// remove line-end char.
item.url=twM2W(entry.substr(0, entry.size()-1));
}else{
item.date=twM2W(entry.substr(0, pos));
item.url=twM2W(entry.substr(pos+1, entry.size()-2-pos));
}
return item;
}
void XTBHistoryDB::writeEntry(const XTBHistoryDBItem &item){
if(item.url==m_lastUrl){
// duplicating. out.
return;
}
twLock lock(m_dbSemaphore);
std::string entry=entryForItem(item);
IndexItem indItem;
m_dbHandle->seekToEndOfStream();
indItem.offset=m_dbHandle->position();
indItem.length=(uint16_t)entry.size();
m_dbHandle->writeString(entry);
writeIndexItemAtEnd(indItem);
m_dbHandle->synchronize();
m_historyCount++;
m_lastUrl=item.url;
}
XTBHistoryDBItem XTBHistoryDB::entryAtIndex(uint64_t index){
assert(index<entryCount());
twLock lock(m_dbSemaphore);
IndexItem indItem=indexItemAtIndex(index);
std::string entry;
m_dbHandle->seekToOffset(indItem.offset);
entry=m_dbHandle->readToString(indItem.length);
return itemForEntry(entry);
}
static XTBHistoryDB *m_standardHistory=NULL;
XTBHistoryDB *XTBHistoryDB::standardHistory(){
if(!m_standardHistory){
m_standardHistory=new XTBHistoryDB(XTBGetStandardHistoryPath());
}
return m_standardHistory;
}
void XTBHistoryDB::visitPage(const std::wstring &url){
try{
XTBHistoryDB *db=standardHistory();
XTBHistoryDBItem item;
char buf[64];
wchar_t buf2[64];
struct tm t2;
time_t t1=time(NULL);
t2=*(localtime(&t1));
strftime(buf, 64, "%G%m%e%H%M%S", &t2);
mbstowcs(buf2, buf, 64);
item.url=url;
item.date=buf2;
db->writeEntry(item);
}catch(const std::exception& ex){
XTBLog("exception while registering history.: %s", ex.what());
}catch(...){
// ignore error while opening history.
}
XTBNotificationCenter::defaultNotificationCenter()->sendNotification(XTBHistoryModifiedNotification);
}
time_t XTBHistoryDBItem::time() const{
struct tm t;
time_t cur=::time(0);
t=*localtime(&cur);
t.tm_year=TWUtils::intValueFromString(date.substr(0, 4))-1900;
t.tm_mon=TWUtils::intValueFromString(date.substr(4, 2))-1;
t.tm_mday=TWUtils::intValueFromString(date.substr(6, 2));
t.tm_hour=TWUtils::intValueFromString(date.substr(8, 2));
t.tm_min=TWUtils::intValueFromString(date.substr(10, 2));
t.tm_sec=TWUtils::intValueFromString(date.substr(12, 2));
return mktime(&t);
}
void XTBHistoryDB::clearHistory(){
m_historyCount=0;
delete m_dbHandle;
delete m_indexHandle;
// create zero-sized db.
m_dbHandle=XTBFileStream::streamForWritingAtPath(m_path+XTBSysText(".historydb"));
m_indexHandle=XTBFileStream::streamForWritingAtPath(m_path+XTBSysText(".historyindex"));
delete m_dbHandle;
delete m_indexHandle;
m_dbHandle=XTBFileStream::streamForUpdatingAtPath(m_path+XTBSysText(".historydb"));
m_indexHandle=XTBFileStream::streamForUpdatingAtPath(m_path+XTBSysText(".historyindex"));
m_lastUrl.clear();
}