]>
Commit | Line | Data |
---|---|---|
8ec2b484 HH |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpdata.cpp | |
3 | // Purpose: wxHtmlHelpData | |
f42b1601 | 4 | // Notes: Based on htmlhelp.cpp, implementing a monolithic |
8ec2b484 HH |
5 | // HTML Help controller class, by Vaclav Slavik |
6 | // Author: Harm van der Heijden and Vaclav Slavik | |
69941f05 | 7 | // RCS-ID: $Id$ |
8ec2b484 HH |
8 | // Copyright: (c) Harm van der Heijden and Vaclav Slavik |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
69941f05 | 13 | #pragma implementation |
8ec2b484 HH |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #include "wx/defs.h" | |
24 | ||
25 | #if wxUSE_HTML | |
26 | ||
27 | #ifndef WXPRECOMP | |
28 | #include "wx/wx.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/html/helpdata.h" | |
32 | #include "wx/tokenzr.h" | |
33 | #include "wx/wfstream.h" | |
34 | #include "wx/busyinfo.h" | |
f890e2d4 VS |
35 | #include "wx/encconv.h" |
36 | #include "wx/fontmap.h" | |
69941f05 | 37 | #include "wx/html/htmlpars.h" |
8ec2b484 HH |
38 | #include "wx/html/htmldefs.h" |
39 | ||
40 | #include "wx/arrimpl.cpp" | |
41 | WX_DEFINE_OBJARRAY(wxHtmlBookRecArray) | |
42 | ||
43 | //----------------------------------------------------------------------------- | |
44 | // static helper functions | |
45 | //----------------------------------------------------------------------------- | |
46 | ||
47 | // Reads one line, stores it into buf and returns pointer to new line or NULL. | |
48 | static char* ReadLine(char *line, char *buf) | |
49 | { | |
50 | char *writeptr = buf, *readptr = line; | |
51 | ||
52 | while (*readptr != 0 && *readptr != '\r' && *readptr != '\n') *(writeptr++) = *(readptr++); | |
53 | *writeptr = 0; | |
54 | while (*readptr == '\r' || *readptr == '\n') readptr++; | |
55 | if (*readptr == 0) return NULL; | |
56 | else return readptr; | |
57 | } | |
58 | ||
59 | ||
8ec2b484 | 60 | |
16a12a3d | 61 | static int LINKAGEMODE IndexCompareFunc(const void *a, const void *b) |
8ec2b484 | 62 | { |
66a77a74 | 63 | return wxStrcmp(((wxHtmlContentsItem*)a) -> m_Name, ((wxHtmlContentsItem*)b) -> m_Name); |
8ec2b484 HH |
64 | } |
65 | ||
66 | ||
67 | //----------------------------------------------------------------------------- | |
68 | // HP_Parser | |
69 | //----------------------------------------------------------------------------- | |
70 | ||
71 | class HP_Parser : public wxHtmlParser | |
72 | { | |
73 | public: | |
0c5d3e1c VZ |
74 | void AddText(const char* WXUNUSED(text)) { } |
75 | wxObject* GetProduct() { return NULL; } | |
8ec2b484 HH |
76 | }; |
77 | ||
78 | ||
79 | //----------------------------------------------------------------------------- | |
80 | // HP_TagHandler | |
81 | //----------------------------------------------------------------------------- | |
82 | ||
83 | class HP_TagHandler : public wxHtmlTagHandler | |
84 | { | |
85 | private: | |
86 | wxString m_Name, m_Page; | |
87 | int m_Level; | |
d5bb85a0 | 88 | int m_ID; |
8ec2b484 HH |
89 | int m_Index; |
90 | wxHtmlContentsItem *m_Items; | |
91 | int m_ItemsCnt; | |
92 | wxHtmlBookRecord *m_Book; | |
93 | ||
94 | public: | |
d5bb85a0 | 95 | HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler() {m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; m_Level = 0; } |
66a77a74 | 96 | wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); } |
8ec2b484 HH |
97 | bool HandleTag(const wxHtmlTag& tag); |
98 | void WriteOut(wxHtmlContentsItem*& array, int& size); | |
99 | void ReadIn(wxHtmlContentsItem* array, int size); | |
100 | }; | |
101 | ||
102 | ||
103 | bool HP_TagHandler::HandleTag(const wxHtmlTag& tag) | |
104 | { | |
0413cec5 | 105 | if (tag.GetName() == wxT("UL")) { |
8ec2b484 HH |
106 | m_Level++; |
107 | ParseInner(tag); | |
108 | m_Level--; | |
109 | return TRUE; | |
110 | } | |
66a77a74 | 111 | else if (tag.GetName() == wxT("OBJECT")) { |
8ec2b484 HH |
112 | m_Name = m_Page = wxEmptyString; |
113 | ParseInner(tag); | |
50494a55 | 114 | |
4157f43f VS |
115 | if (!m_Page.IsEmpty()) |
116 | /* should be 'if (tag.GetParam("TYPE") == "text/sitemap")' | |
117 | but this works fine. Valid HHW's file may contain only two | |
118 | object tags: | |
119 | ||
120 | <OBJECT type="text/site properties"> | |
121 | <param name="ImageType" value="Folder"> | |
122 | </OBJECT> | |
123 | ||
124 | or | |
125 | ||
126 | <OBJECT type="text/sitemap"> | |
127 | <param name="Name" value="main page"> | |
128 | <param name="Local" value="another.htm"> | |
129 | </OBJECT> | |
130 | ||
131 | We're interested in the latter. !m_Page.IsEmpty() is valid | |
132 | condition because text/site properties does not contain Local param | |
133 | */ | |
134 | { | |
135 | if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0) | |
136 | m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem)); | |
137 | m_Items[m_ItemsCnt].m_Level = m_Level; | |
138 | m_Items[m_ItemsCnt].m_ID = m_ID; | |
139 | m_Items[m_ItemsCnt].m_Page = new wxChar[m_Page.Length() + 1]; | |
140 | wxStrcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str()); | |
141 | m_Items[m_ItemsCnt].m_Name = new wxChar [m_Name.Length() + 1]; | |
142 | wxStrcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str()); | |
143 | m_Items[m_ItemsCnt].m_Book = m_Book; | |
144 | m_ItemsCnt++; | |
145 | } | |
50494a55 | 146 | |
8ec2b484 HH |
147 | return TRUE; |
148 | } | |
8ec2b484 | 149 | else { // "PARAM" |
66a77a74 OK |
150 | if (m_Name == wxEmptyString && tag.GetParam(wxT("NAME")) == wxT("Name")) m_Name = tag.GetParam(wxT("VALUE")); |
151 | if (tag.GetParam(wxT("NAME")) == wxT("Local")) m_Page = tag.GetParam(wxT("VALUE")); | |
152 | if (tag.GetParam(wxT("NAME")) == wxT("ID")) tag.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID); | |
8ec2b484 HH |
153 | return FALSE; |
154 | } | |
155 | } | |
156 | ||
157 | ||
158 | ||
159 | void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size) | |
160 | { | |
161 | array = m_Items; | |
162 | size = m_ItemsCnt; | |
163 | m_Items = NULL; | |
164 | m_ItemsCnt = 0; | |
165 | } | |
166 | ||
167 | void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size) | |
168 | { | |
169 | m_Items = array; | |
170 | m_ItemsCnt = size; | |
171 | } | |
172 | ||
d5bb85a0 VS |
173 | |
174 | ||
175 | ||
8ec2b484 HH |
176 | //----------------------------------------------------------------------------- |
177 | // wxHtmlHelpData | |
178 | //----------------------------------------------------------------------------- | |
179 | ||
180 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject) | |
181 | ||
f42b1601 | 182 | wxHtmlHelpData::wxHtmlHelpData() |
8ec2b484 HH |
183 | { |
184 | m_TempPath = wxEmptyString; | |
185 | ||
186 | m_Contents = NULL; | |
187 | m_ContentsCnt = 0; | |
188 | m_Index = NULL; | |
189 | m_IndexCnt = 0; | |
190 | } | |
191 | ||
192 | wxHtmlHelpData::~wxHtmlHelpData() | |
193 | { | |
194 | int i; | |
195 | ||
196 | m_BookRecords.Empty(); | |
197 | if (m_Contents) { | |
198 | for (i = 0; i < m_ContentsCnt; i++) { | |
199 | delete[] m_Contents[i].m_Page; | |
200 | delete[] m_Contents[i].m_Name; | |
201 | } | |
202 | free(m_Contents); | |
203 | } | |
204 | if (m_Index) { | |
205 | for (i = 0; i < m_IndexCnt; i++) { | |
206 | delete[] m_Index[i].m_Page; | |
207 | delete[] m_Index[i].m_Name; | |
208 | } | |
209 | free(m_Index); | |
210 | } | |
211 | } | |
212 | ||
213 | bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile) | |
214 | { | |
215 | wxFSFile *f; | |
216 | char *buf; | |
217 | int sz; | |
218 | wxString string; | |
f42b1601 | 219 | |
8ec2b484 HH |
220 | HP_Parser parser; |
221 | HP_TagHandler *handler = new HP_TagHandler(book); | |
222 | parser.AddTagHandler(handler); | |
223 | ||
af111fc3 | 224 | f = ( contentsfile.IsEmpty() ? 0 : fsys.OpenFile(contentsfile) ); |
8ec2b484 HH |
225 | if (f) { |
226 | sz = f -> GetStream() -> GetSize(); | |
d5bb85a0 | 227 | buf = new char[sz + 1]; |
8ec2b484 HH |
228 | buf[sz] = 0; |
229 | f -> GetStream() -> Read(buf, sz); | |
230 | delete f; | |
231 | handler -> ReadIn(m_Contents, m_ContentsCnt); | |
232 | parser.Parse(buf); | |
233 | handler -> WriteOut(m_Contents, m_ContentsCnt); | |
234 | delete[] buf; | |
235 | } | |
236 | ||
af111fc3 | 237 | f = ( indexfile.IsEmpty() ? 0 : fsys.OpenFile(indexfile) ); |
8ec2b484 HH |
238 | if (f) { |
239 | sz = f -> GetStream() -> GetSize(); | |
d5bb85a0 | 240 | buf = new char[sz + 1]; |
8ec2b484 HH |
241 | buf[sz] = 0; |
242 | f -> GetStream() -> Read(buf, sz); | |
243 | delete f; | |
244 | handler -> ReadIn(m_Index, m_IndexCnt); | |
245 | parser.Parse(buf); | |
246 | handler -> WriteOut(m_Index, m_IndexCnt); | |
247 | delete[] buf; | |
248 | } | |
249 | return TRUE; | |
250 | } | |
251 | ||
252 | ||
f35822af VS |
253 | |
254 | ||
255 | #if wxUSE_UNICODE | |
256 | ||
257 | #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f -> Read(&tmpc, 1); s[i] = (wxChar)tmpc;} } | |
258 | #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f -> Write(&tmpc, 1);} } | |
259 | ||
260 | #else | |
261 | ||
262 | #define READ_STRING(f, s, lng) f -> Read(s, lng * sizeof(char)); | |
263 | #define WRITE_STRING(f, s, lng) f -> Write(s, lng * sizeof(char)); | |
264 | ||
265 | #endif | |
266 | ||
267 | ||
268 | #define CURRENT_CACHED_BOOK_VERSION 1 | |
269 | ||
8ec2b484 HH |
270 | bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f) |
271 | { | |
272 | int i, st; | |
f35822af VS |
273 | wxInt32 x; |
274 | wxInt32 version; | |
275 | ||
276 | /* load header - version info : */ | |
277 | ||
278 | f -> Read(&x, sizeof(x)); | |
279 | version = wxINT32_SWAP_ON_BE(x); | |
280 | ||
281 | if (version != CURRENT_CACHED_BOOK_VERSION) return FALSE; | |
282 | // NOTE: when adding new version, please ensure backward compatibility! | |
8ec2b484 HH |
283 | |
284 | /* load contents : */ | |
285 | ||
286 | f -> Read(&x, sizeof(x)); | |
287 | st = m_ContentsCnt; | |
f35822af VS |
288 | m_ContentsCnt += wxINT32_SWAP_ON_BE(x); |
289 | m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, | |
290 | (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) * | |
291 | wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem)); | |
8ec2b484 HH |
292 | for (i = st; i < m_ContentsCnt; i++) { |
293 | f -> Read(&x, sizeof(x)); | |
f35822af | 294 | m_Contents[i].m_Level = wxINT32_SWAP_ON_BE(x); |
8ec2b484 | 295 | f -> Read(&x, sizeof(x)); |
f35822af VS |
296 | m_Contents[i].m_ID = wxINT32_SWAP_ON_BE(x); |
297 | f -> Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x); | |
66a77a74 | 298 | m_Contents[i].m_Name = new wxChar[x]; |
f35822af VS |
299 | READ_STRING(f, m_Contents[i].m_Name, x); |
300 | f -> Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x); | |
66a77a74 | 301 | m_Contents[i].m_Page = new wxChar[x]; |
f35822af | 302 | READ_STRING(f, m_Contents[i].m_Page, x); |
8ec2b484 HH |
303 | m_Contents[i].m_Book = book; |
304 | } | |
305 | ||
306 | /* load index : */ | |
307 | ||
308 | f -> Read(&x, sizeof(x)); | |
309 | st = m_IndexCnt; | |
f35822af VS |
310 | m_IndexCnt += wxINT32_SWAP_ON_BE(x); |
311 | m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) * | |
312 | wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem)); | |
8ec2b484 | 313 | for (i = st; i < m_IndexCnt; i++) { |
f35822af | 314 | f -> Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x); |
66a77a74 | 315 | m_Index[i].m_Name = new wxChar[x]; |
f35822af VS |
316 | READ_STRING(f, m_Index[i].m_Name, x); |
317 | f -> Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x); | |
66a77a74 | 318 | m_Index[i].m_Page = new wxChar[x]; |
f35822af | 319 | READ_STRING(f, m_Index[i].m_Page, x); |
8ec2b484 HH |
320 | m_Index[i].m_Book = book; |
321 | } | |
322 | return TRUE; | |
323 | } | |
324 | ||
325 | ||
326 | bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f) | |
327 | { | |
328 | int i; | |
f35822af VS |
329 | wxInt32 x; |
330 | ||
331 | /* save header - version info : */ | |
332 | ||
333 | x = wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION); | |
334 | f -> Write(&x, sizeof(x)); | |
8ec2b484 HH |
335 | |
336 | /* save contents : */ | |
337 | ||
338 | x = 0; | |
339 | for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++; | |
f35822af | 340 | x = wxINT32_SWAP_ON_BE(x); |
8ec2b484 HH |
341 | f -> Write(&x, sizeof(x)); |
342 | for (i = 0; i < m_ContentsCnt; i++) { | |
343 | if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue; | |
f35822af | 344 | x = wxINT32_SWAP_ON_BE(m_Contents[i].m_Level); |
8ec2b484 | 345 | f -> Write(&x, sizeof(x)); |
f35822af | 346 | x = wxINT32_SWAP_ON_BE(m_Contents[i].m_ID); |
8ec2b484 | 347 | f -> Write(&x, sizeof(x)); |
f35822af | 348 | x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Name) + 1); |
8ec2b484 | 349 | f -> Write(&x, sizeof(x)); |
f35822af VS |
350 | WRITE_STRING(f, m_Contents[i].m_Name, x); |
351 | x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Page) + 1); | |
8ec2b484 | 352 | f -> Write(&x, sizeof(x)); |
f35822af | 353 | WRITE_STRING(f, m_Contents[i].m_Page, x); |
8ec2b484 HH |
354 | } |
355 | ||
356 | /* save index : */ | |
357 | ||
358 | x = 0; | |
359 | for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++; | |
f35822af | 360 | x = wxINT32_SWAP_ON_BE(x); |
8ec2b484 HH |
361 | f -> Write(&x, sizeof(x)); |
362 | for (i = 0; i < m_IndexCnt; i++) { | |
363 | if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue; | |
f35822af | 364 | x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Name) + 1); |
8ec2b484 | 365 | f -> Write(&x, sizeof(x)); |
f35822af VS |
366 | WRITE_STRING(f, m_Index[i].m_Name, x); |
367 | x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Page) + 1); | |
8ec2b484 | 368 | f -> Write(&x, sizeof(x)); |
f35822af | 369 | WRITE_STRING(f, m_Index[i].m_Page, x); |
8ec2b484 HH |
370 | } |
371 | return TRUE; | |
372 | } | |
373 | ||
374 | ||
375 | void wxHtmlHelpData::SetTempDir(const wxString& path) | |
376 | { | |
377 | if (path == wxEmptyString) m_TempPath = path; | |
378 | else { | |
d5bb85a0 | 379 | if (wxIsAbsolutePath(path)) m_TempPath = path; |
f35822af | 380 | else m_TempPath = wxGetCwd() + _T("/") + path; |
8ec2b484 | 381 | |
f35822af VS |
382 | if (m_TempPath[m_TempPath.Length() - 1] != _T('/')) |
383 | m_TempPath << _T('/'); | |
8ec2b484 HH |
384 | } |
385 | } | |
386 | ||
387 | ||
f35822af | 388 | bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile, |
f890e2d4 | 389 | wxFontEncoding encoding, |
f35822af | 390 | const wxString& title, const wxString& contfile, |
d5bb85a0 VS |
391 | const wxString& indexfile, const wxString& deftopic, |
392 | const wxString& path) | |
8ec2b484 HH |
393 | { |
394 | wxFileSystem fsys; | |
395 | wxFSFile *fi; | |
396 | wxHtmlBookRecord *bookr; | |
f890e2d4 VS |
397 | |
398 | int IndexOld = m_IndexCnt, | |
399 | ContentsOld = m_ContentsCnt; | |
f42b1601 | 400 | |
8ec2b484 | 401 | if (! path.IsEmpty()) |
d5bb85a0 | 402 | fsys.ChangePathTo(path, TRUE); |
8ec2b484 | 403 | |
68364659 | 404 | bookr = new wxHtmlBookRecord(fsys.GetPath(), title, deftopic); |
8ec2b484 | 405 | |
efba2b89 VS |
406 | if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0) |
407 | m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem)); | |
8ec2b484 HH |
408 | m_Contents[m_ContentsCnt].m_Level = 0; |
409 | m_Contents[m_ContentsCnt].m_ID = 0; | |
66a77a74 OK |
410 | m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1]; |
411 | wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str()); | |
412 | m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1]; | |
413 | wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str()); | |
8ec2b484 HH |
414 | m_Contents[m_ContentsCnt].m_Book = bookr; |
415 | ||
416 | // store the contents index for later | |
417 | int cont_start = m_ContentsCnt++; | |
418 | ||
419 | // Try to find cached binary versions: | |
f35822af VS |
420 | // 1. save file as book, but with .hhp.cached extension |
421 | // 2. same as 1. but in temp path | |
422 | // 3. otherwise or if cache load failed, load it from MS. | |
423 | ||
424 | fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached")); | |
425 | ||
426 | if (fi == NULL || | |
427 | fi -> GetModificationTime() < bookfile.GetModificationTime() || | |
428 | !LoadCachedBook(bookr, fi -> GetStream())) | |
429 | { | |
430 | if (fi != NULL) delete fi; | |
431 | fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached")); | |
432 | if (m_TempPath == wxEmptyString || fi == NULL || | |
433 | fi -> GetModificationTime() < bookfile.GetModificationTime() || | |
434 | !LoadCachedBook(bookr, fi -> GetStream())) | |
435 | { | |
436 | LoadMSProject(bookr, fsys, indexfile, contfile); | |
437 | if (m_TempPath != wxEmptyString) | |
438 | { | |
439 | wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath + | |
440 | wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached")); | |
441 | SaveCachedBook(bookr, outs); | |
442 | delete outs; | |
443 | } | |
d5bb85a0 | 444 | } |
8ec2b484 | 445 | } |
f35822af VS |
446 | |
447 | if (fi != NULL) delete fi; | |
8ec2b484 HH |
448 | |
449 | // Now store the contents range | |
450 | bookr->SetContentsRange(cont_start, m_ContentsCnt); | |
f890e2d4 VS |
451 | |
452 | // Convert encoding, if neccessary: | |
453 | if (encoding != wxFONTENCODING_SYSTEM) | |
454 | { | |
455 | wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(encoding); | |
456 | if (a.GetCount() != 0 && a[0] != encoding) | |
457 | { | |
458 | int i; | |
459 | wxEncodingConverter conv; | |
460 | conv.Init(encoding, a[0]); | |
461 | ||
462 | for (i = IndexOld; i < m_IndexCnt; i++) | |
463 | conv.Convert(m_Index[i].m_Name); | |
464 | for (i = ContentsOld; i < m_ContentsCnt; i++) | |
465 | conv.Convert(m_Contents[i].m_Name); | |
466 | } | |
467 | } | |
8ec2b484 HH |
468 | |
469 | m_BookRecords.Add(bookr); | |
470 | if (m_IndexCnt > 0) | |
471 | qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), IndexCompareFunc); | |
f42b1601 | 472 | |
8ec2b484 HH |
473 | return TRUE; |
474 | } | |
475 | ||
476 | ||
477 | bool wxHtmlHelpData::AddBook(const wxString& book) | |
478 | { | |
68364659 VS |
479 | if (book.Right(4).Lower() == wxT(".zip") || |
480 | book.Right(4).Lower() == wxT(".htb") /*html book*/) | |
481 | ||
482 | { | |
483 | wxFileSystem fsys; | |
484 | wxString s; | |
485 | bool rt = FALSE; | |
486 | ||
487 | s = fsys.FindFirst(book + wxT("#zip:") + wxT("*.hhp"), wxFILE); | |
488 | while (!s.IsEmpty()) | |
489 | { | |
490 | if (AddBook(s)) rt = TRUE; | |
491 | s = fsys.FindNext(); | |
492 | } | |
493 | ||
494 | return rt; | |
495 | } | |
8ec2b484 | 496 | |
68364659 VS |
497 | |
498 | else | |
499 | { | |
500 | wxFSFile *fi; | |
501 | wxFileSystem fsys; | |
502 | wxInputStream *s; | |
503 | wxString bookFull; | |
504 | ||
505 | int sz; | |
506 | char *buff, *lineptr; | |
507 | char linebuf[300]; | |
508 | ||
509 | wxString title = _("noname"), | |
f890e2d4 VS |
510 | safetitle, |
511 | start = wxEmptyString, | |
512 | contents = wxEmptyString, | |
513 | index = wxEmptyString, | |
514 | charset = wxEmptyString; | |
68364659 VS |
515 | |
516 | if (wxIsAbsolutePath(book)) bookFull = book; | |
517 | else bookFull = wxGetCwd() + "/" + book; | |
518 | ||
519 | fi = fsys.OpenFile(bookFull); | |
520 | if (fi == NULL) return FALSE; | |
521 | fsys.ChangePathTo(bookFull); | |
522 | s = fi -> GetStream(); | |
523 | sz = s -> GetSize(); | |
524 | buff = new char[sz + 1]; | |
525 | buff[sz] = 0; | |
526 | s -> Read(buff, sz); | |
527 | lineptr = buff; | |
68364659 VS |
528 | |
529 | do { | |
530 | lineptr = ReadLine(lineptr, linebuf); | |
531 | ||
532 | if (strstr(linebuf, "Title=") == linebuf) | |
533 | title = linebuf + strlen("Title="); | |
534 | if (strstr(linebuf, "Default topic=") == linebuf) | |
535 | start = linebuf + strlen("Default topic="); | |
536 | if (strstr(linebuf, "Index file=") == linebuf) | |
537 | index = linebuf + strlen("Index file="); | |
538 | if (strstr(linebuf, "Contents file=") == linebuf) | |
539 | contents = linebuf + strlen("Contents file="); | |
f890e2d4 VS |
540 | if (strstr(linebuf, "Charset=") == linebuf) |
541 | charset = linebuf + strlen("Charset="); | |
68364659 VS |
542 | } while (lineptr != NULL); |
543 | delete[] buff; | |
f890e2d4 VS |
544 | |
545 | wxFontEncoding enc; | |
546 | if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM; | |
547 | else enc = wxTheFontMapper -> CharsetToEncoding(charset); | |
548 | bool rtval = AddBookParam(*fi, enc, | |
549 | title, contents, index, start, fsys.GetPath()); | |
f35822af VS |
550 | delete fi; |
551 | return rtval; | |
68364659 | 552 | } |
8ec2b484 HH |
553 | } |
554 | ||
555 | wxString wxHtmlHelpData::FindPageByName(const wxString& x) | |
556 | { | |
557 | int cnt; | |
558 | int i; | |
559 | wxFileSystem fsys; | |
560 | wxFSFile *f; | |
561 | wxString url(wxEmptyString); | |
562 | ||
563 | /* 1. try to open given file: */ | |
564 | ||
565 | cnt = m_BookRecords.GetCount(); | |
566 | for (i = 0; i < cnt; i++) { | |
567 | f = fsys.OpenFile(m_BookRecords[i].GetBasePath() + x); | |
568 | if (f) { | |
569 | url = m_BookRecords[i].GetBasePath() + x; | |
570 | delete f; | |
571 | return url; | |
572 | } | |
573 | } | |
574 | ||
575 | ||
576 | /* 2. try to find a book: */ | |
577 | ||
578 | for (i = 0; i < cnt; i++) { | |
579 | if (m_BookRecords[i].GetTitle() == x) { | |
580 | url = m_BookRecords[i].GetBasePath() + m_BookRecords[i].GetStart(); | |
581 | return url; | |
582 | } | |
583 | } | |
584 | ||
585 | /* 3. try to find in contents: */ | |
586 | ||
587 | cnt = m_ContentsCnt; | |
588 | for (i = 0; i < cnt; i++) { | |
66a77a74 | 589 | if (wxStrcmp(m_Contents[i].m_Name, x) == 0) { |
8ec2b484 HH |
590 | url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page; |
591 | return url; | |
592 | } | |
593 | } | |
594 | ||
595 | ||
596 | /* 4. try to find in index: */ | |
597 | ||
598 | cnt = m_IndexCnt; | |
599 | for (i = 0; i < cnt; i++) { | |
66a77a74 | 600 | if (wxStrcmp(m_Index[i].m_Name, x) == 0) { |
8ec2b484 HH |
601 | url = m_Index[i].m_Book -> GetBasePath() + m_Index[i].m_Page; |
602 | return url; | |
603 | } | |
604 | } | |
605 | ||
606 | return url; | |
607 | } | |
608 | ||
609 | wxString wxHtmlHelpData::FindPageById(int id) | |
f42b1601 | 610 | { |
8ec2b484 HH |
611 | int i; |
612 | wxString url(wxEmptyString); | |
613 | ||
614 | for (i = 0; i < m_ContentsCnt; i++) { | |
615 | if (m_Contents[i].m_ID == id) { | |
616 | url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page; | |
617 | return url; | |
618 | } | |
619 | } | |
620 | ||
621 | return url; | |
622 | } | |
623 | ||
624 | //---------------------------------------------------------------------------------- | |
625 | // wxHtmlSearchStatus functions | |
626 | //---------------------------------------------------------------------------------- | |
627 | ||
628 | wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword, | |
c4971147 | 629 | bool case_sensitive, bool whole_words_only, |
d5bb85a0 | 630 | const wxString& book) |
8ec2b484 HH |
631 | { |
632 | m_Data = data; | |
633 | m_Keyword = keyword; | |
634 | wxHtmlBookRecord* bookr = NULL; | |
635 | if (book != wxEmptyString) { | |
d5bb85a0 VS |
636 | // we have to search in a specific book. Find it first |
637 | int i, cnt = data->m_BookRecords.GetCount(); | |
638 | for (i = 0; i < cnt; i++) | |
639 | if (data->m_BookRecords[i].GetTitle() == book) { | |
640 | bookr = &(data->m_BookRecords[i]); | |
641 | m_CurIndex = bookr->GetContentsStart(); | |
642 | m_MaxIndex = bookr->GetContentsEnd(); | |
643 | break; | |
644 | } | |
645 | // check; we won't crash if the book doesn't exist, but it's Bad Anyway. | |
646 | wxASSERT(bookr); | |
8ec2b484 HH |
647 | } |
648 | if (! bookr) { | |
d5bb85a0 VS |
649 | // no book specified; search all books |
650 | m_CurIndex = 0; | |
651 | m_MaxIndex = m_Data->m_ContentsCnt; | |
8ec2b484 | 652 | } |
c4971147 | 653 | m_Engine.LookFor(keyword, case_sensitive, whole_words_only); |
8ec2b484 | 654 | m_Active = (m_CurIndex < m_MaxIndex); |
b5a7b000 | 655 | m_LastPage = NULL; |
8ec2b484 HH |
656 | } |
657 | ||
658 | bool wxHtmlSearchStatus::Search() | |
659 | { | |
8ec2b484 | 660 | wxFSFile *file; |
d5bb85a0 | 661 | int i = m_CurIndex; // shortcut |
8ec2b484 | 662 | bool found = FALSE; |
b5a7b000 | 663 | wxChar *thepage; |
8ec2b484 | 664 | |
50494a55 | 665 | if (!m_Active) { |
f35822af | 666 | // sanity check. Illegal use, but we'll try to prevent a crash anyway |
50494a55 | 667 | wxASSERT(m_Active); |
50494a55 | 668 | return FALSE; |
8ec2b484 HH |
669 | } |
670 | ||
8ec2b484 | 671 | m_Name = wxEmptyString; |
b5a7b000 VS |
672 | m_ContentsItem = NULL; |
673 | thepage = m_Data->m_Contents[i].m_Page; | |
8ec2b484 | 674 | |
b5a7b000 VS |
675 | m_Active = (++m_CurIndex < m_MaxIndex); |
676 | // check if it is same page with different anchor: | |
677 | if (m_LastPage != NULL) | |
678 | { | |
679 | wxChar *p1, *p2; | |
680 | for (p1 = thepage, p2 = m_LastPage; | |
681 | *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {} | |
682 | ||
683 | m_LastPage = thepage; | |
684 | ||
685 | if (*p1 == 0 || *p1 == _T('#')) | |
686 | return FALSE; | |
687 | } | |
688 | else m_LastPage = thepage; | |
689 | ||
f35822af | 690 | wxFileSystem fsys; |
b5a7b000 VS |
691 | file = fsys.OpenFile(m_Data->m_Contents[i].m_Book -> GetBasePath() + thepage); |
692 | if (file) | |
693 | { | |
694 | if (m_Engine.Scan(file -> GetStream())) { | |
695 | m_Name = m_Data->m_Contents[i].m_Name; | |
696 | m_ContentsItem = m_Data->m_Contents + i; | |
697 | found = TRUE; | |
d5bb85a0 VS |
698 | } |
699 | delete file; | |
8ec2b484 | 700 | } |
8ec2b484 HH |
701 | return found; |
702 | } | |
703 | ||
d5bb85a0 VS |
704 | |
705 | ||
706 | ||
707 | ||
708 | ||
709 | ||
710 | ||
711 | //-------------------------------------------------------------------------------- | |
712 | // wxSearchEngine | |
713 | //-------------------------------------------------------------------------------- | |
714 | ||
c4971147 | 715 | void wxSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only) |
d5bb85a0 | 716 | { |
c4971147 VS |
717 | m_CaseSensitive = case_sensitive; |
718 | m_WholeWords = whole_words_only; | |
d5bb85a0 | 719 | if (m_Keyword) delete[] m_Keyword; |
66a77a74 OK |
720 | m_Keyword = new wxChar[keyword.Length() + 1]; |
721 | wxStrcpy(m_Keyword, keyword.c_str()); | |
c4971147 VS |
722 | |
723 | if (!m_CaseSensitive) | |
724 | for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--) | |
725 | if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z'))) | |
726 | m_Keyword[i] += wxT('a') - wxT('A'); | |
d5bb85a0 VS |
727 | } |
728 | ||
729 | ||
730 | ||
c4971147 VS |
731 | #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t') |
732 | ||
d5bb85a0 VS |
733 | bool wxSearchEngine::Scan(wxInputStream *stream) |
734 | { | |
50494a55 | 735 | wxASSERT_MSG(m_Keyword != NULL, wxT("wxSearchEngine::LookFor must be called before scanning!")); |
d5bb85a0 VS |
736 | |
737 | int i, j; | |
738 | int lng = stream ->GetSize(); | |
66a77a74 | 739 | int wrd = wxStrlen(m_Keyword); |
d5bb85a0 VS |
740 | bool found = FALSE; |
741 | char *buf = new char[lng + 1]; | |
742 | stream -> Read(buf, lng); | |
743 | buf[lng] = 0; | |
744 | ||
c4971147 VS |
745 | if (!m_CaseSensitive) |
746 | for (i = 0; i < lng; i++) | |
747 | if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A'; | |
d5bb85a0 | 748 | |
c4971147 VS |
749 | if (m_WholeWords) |
750 | { | |
751 | for (i = 0; i < lng - wrd; i++) { | |
752 | if (WHITESPACE(buf[i])) continue; | |
753 | j = 0; | |
754 | while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++; | |
755 | if (j == wrd && WHITESPACE(buf[i + j])) {found = TRUE; break; } | |
756 | } | |
757 | } | |
758 | ||
759 | else | |
760 | { | |
761 | for (i = 0; i < lng - wrd; i++) { | |
762 | j = 0; | |
763 | while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++; | |
764 | if (j == wrd) {found = TRUE; break; } | |
765 | } | |
d5bb85a0 VS |
766 | } |
767 | ||
768 | delete[] buf; | |
769 | return found; | |
770 | } | |
771 | ||
772 | ||
773 | ||
774 | ||
8ec2b484 | 775 | #endif |