]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: helpdata.cpp | |
3 | // Purpose: wxHtmlHelpData | |
4 | // Notes: Based on htmlhelp.cpp, implementing a monolithic | |
5 | // HTML Help controller class, by Vaclav Slavik | |
6 | // Author: Harm van der Heijden and Vaclav Slavik | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Harm van der Heijden and Vaclav Slavik | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "helpdata.h" | |
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 && wxUSE_STREAMS | |
26 | ||
27 | #ifndef WXPRECOMP | |
28 | #include "wx/intl.h" | |
29 | #include "wx/log.h" | |
30 | #endif | |
31 | ||
32 | #include <ctype.h> | |
33 | ||
34 | #include "wx/html/helpdata.h" | |
35 | #include "wx/tokenzr.h" | |
36 | #include "wx/wfstream.h" | |
37 | #include "wx/busyinfo.h" | |
38 | #include "wx/encconv.h" | |
39 | #include "wx/fontmap.h" | |
40 | #include "wx/log.h" | |
41 | #include "wx/html/htmlpars.h" | |
42 | #include "wx/html/htmldefs.h" | |
43 | #include "wx/html/htmlfilt.h" | |
44 | #include "wx/filename.h" | |
45 | ||
46 | #include "wx/arrimpl.cpp" | |
47 | WX_DEFINE_OBJARRAY(wxHtmlBookRecArray) | |
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // static helper functions | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | // Reads one line, stores it into buf and returns pointer to new line or NULL. | |
54 | static const wxChar* ReadLine(const wxChar *line, wxChar *buf, size_t bufsize) | |
55 | { | |
56 | wxChar *writeptr = buf; | |
57 | wxChar *endptr = buf + bufsize - 1; | |
58 | const wxChar *readptr = line; | |
59 | ||
60 | while (*readptr != 0 && *readptr != _T('\r') && *readptr != _T('\n') && | |
61 | writeptr != endptr) | |
62 | *(writeptr++) = *(readptr++); | |
63 | *writeptr = 0; | |
64 | while (*readptr == _T('\r') || *readptr == _T('\n')) | |
65 | readptr++; | |
66 | if (*readptr == 0) | |
67 | return NULL; | |
68 | else | |
69 | return readptr; | |
70 | } | |
71 | ||
72 | ||
73 | ||
74 | extern "C" int LINKAGEMODE | |
75 | wxHtmlHelpIndexCompareFunc(const void *a, const void *b) | |
76 | { | |
77 | return wxStricmp(((wxHtmlContentsItem*)a)->m_Name, ((wxHtmlContentsItem*)b)->m_Name); | |
78 | } | |
79 | ||
80 | ||
81 | //----------------------------------------------------------------------------- | |
82 | // HP_Parser | |
83 | //----------------------------------------------------------------------------- | |
84 | ||
85 | class HP_Parser : public wxHtmlParser | |
86 | { | |
87 | public: | |
88 | HP_Parser() { } | |
89 | ||
90 | wxObject* GetProduct() { return NULL; } | |
91 | ||
92 | protected: | |
93 | virtual void AddText(const wxChar* WXUNUSED(txt)) {} | |
94 | ||
95 | DECLARE_NO_COPY_CLASS(HP_Parser) | |
96 | }; | |
97 | ||
98 | ||
99 | //----------------------------------------------------------------------------- | |
100 | // HP_TagHandler | |
101 | //----------------------------------------------------------------------------- | |
102 | ||
103 | class HP_TagHandler : public wxHtmlTagHandler | |
104 | { | |
105 | private: | |
106 | wxString m_Name, m_Page; | |
107 | int m_Level; | |
108 | int m_ID; | |
109 | int m_Index; | |
110 | wxHtmlContentsItem *m_Items; | |
111 | int m_ItemsCnt; | |
112 | wxHtmlBookRecord *m_Book; | |
113 | ||
114 | public: | |
115 | HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler() | |
116 | { m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; | |
117 | m_Level = 0; m_ID = -1; } | |
118 | wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); } | |
119 | bool HandleTag(const wxHtmlTag& tag); | |
120 | void WriteOut(wxHtmlContentsItem*& array, int& size); | |
121 | void ReadIn(wxHtmlContentsItem* array, int size); | |
122 | ||
123 | DECLARE_NO_COPY_CLASS(HP_TagHandler) | |
124 | }; | |
125 | ||
126 | ||
127 | bool HP_TagHandler::HandleTag(const wxHtmlTag& tag) | |
128 | { | |
129 | if (tag.GetName() == wxT("UL")) | |
130 | { | |
131 | m_Level++; | |
132 | ParseInner(tag); | |
133 | m_Level--; | |
134 | return TRUE; | |
135 | } | |
136 | else if (tag.GetName() == wxT("OBJECT")) | |
137 | { | |
138 | m_Name = m_Page = wxEmptyString; | |
139 | ParseInner(tag); | |
140 | ||
141 | #if 0 | |
142 | if (!m_Page.IsEmpty()) | |
143 | /* Valid HHW's file may contain only two object tags: | |
144 | ||
145 | <OBJECT type="text/site properties"> | |
146 | <param name="ImageType" value="Folder"> | |
147 | </OBJECT> | |
148 | ||
149 | or | |
150 | ||
151 | <OBJECT type="text/sitemap"> | |
152 | <param name="Name" value="main page"> | |
153 | <param name="Local" value="another.htm"> | |
154 | </OBJECT> | |
155 | ||
156 | We're interested in the latter. !m_Page.IsEmpty() is valid | |
157 | condition because text/site properties does not contain Local param | |
158 | */ | |
159 | #endif | |
160 | if (tag.GetParam(wxT("TYPE")) == wxT("text/sitemap")) | |
161 | { | |
162 | if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0) | |
163 | m_Items = (wxHtmlContentsItem*) realloc(m_Items, | |
164 | (m_ItemsCnt + wxHTML_REALLOC_STEP) * | |
165 | sizeof(wxHtmlContentsItem)); | |
166 | ||
167 | m_Items[m_ItemsCnt].m_Level = m_Level; | |
168 | m_Items[m_ItemsCnt].m_ID = m_ID; | |
169 | m_Items[m_ItemsCnt].m_Page = new wxChar[m_Page.Length() + 1]; | |
170 | wxStrcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str()); | |
171 | m_Items[m_ItemsCnt].m_Name = new wxChar [m_Name.Length() + 1]; | |
172 | wxStrcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str()); | |
173 | m_Items[m_ItemsCnt].m_Book = m_Book; | |
174 | m_ItemsCnt++; | |
175 | } | |
176 | ||
177 | return TRUE; | |
178 | } | |
179 | else | |
180 | { // "PARAM" | |
181 | if (m_Name == wxEmptyString && tag.GetParam(wxT("NAME")) == wxT("Name")) | |
182 | m_Name = tag.GetParam(wxT("VALUE")); | |
183 | if (tag.GetParam(wxT("NAME")) == wxT("Local")) | |
184 | m_Page = tag.GetParam(wxT("VALUE")); | |
185 | if (tag.GetParam(wxT("NAME")) == wxT("ID")) | |
186 | tag.GetParamAsInt(wxT("VALUE"), &m_ID); | |
187 | return FALSE; | |
188 | } | |
189 | } | |
190 | ||
191 | ||
192 | ||
193 | void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size) | |
194 | { | |
195 | array = m_Items; | |
196 | size = m_ItemsCnt; | |
197 | m_Items = NULL; | |
198 | m_ItemsCnt = 0; | |
199 | } | |
200 | ||
201 | void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size) | |
202 | { | |
203 | m_Items = array; | |
204 | m_ItemsCnt = size; | |
205 | } | |
206 | ||
207 | ||
208 | ||
209 | ||
210 | //----------------------------------------------------------------------------- | |
211 | // wxHtmlHelpData | |
212 | //----------------------------------------------------------------------------- | |
213 | ||
214 | wxString wxHtmlBookRecord::GetFullPath(const wxString &page) const | |
215 | { | |
216 | if (wxIsAbsolutePath(page)) | |
217 | return page; | |
218 | else | |
219 | return m_BasePath + page; | |
220 | } | |
221 | ||
222 | ||
223 | ||
224 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject) | |
225 | ||
226 | wxHtmlHelpData::wxHtmlHelpData() | |
227 | { | |
228 | m_TempPath = wxEmptyString; | |
229 | ||
230 | m_Contents = NULL; | |
231 | m_ContentsCnt = 0; | |
232 | m_Index = NULL; | |
233 | m_IndexCnt = 0; | |
234 | } | |
235 | ||
236 | wxHtmlHelpData::~wxHtmlHelpData() | |
237 | { | |
238 | int i; | |
239 | ||
240 | m_BookRecords.Empty(); | |
241 | if (m_Contents) | |
242 | { | |
243 | for (i = 0; i < m_ContentsCnt; i++) | |
244 | { | |
245 | delete[] m_Contents[i].m_Page; | |
246 | delete[] m_Contents[i].m_Name; | |
247 | } | |
248 | free(m_Contents); | |
249 | } | |
250 | if (m_Index) | |
251 | { | |
252 | for (i = 0; i < m_IndexCnt; i++) | |
253 | { | |
254 | delete[] m_Index[i].m_Page; | |
255 | delete[] m_Index[i].m_Name; | |
256 | } | |
257 | free(m_Index); | |
258 | } | |
259 | } | |
260 | ||
261 | bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile) | |
262 | { | |
263 | wxFSFile *f; | |
264 | wxHtmlFilterHTML filter; | |
265 | wxString buf; | |
266 | wxString string; | |
267 | ||
268 | HP_Parser parser; | |
269 | HP_TagHandler *handler = new HP_TagHandler(book); | |
270 | parser.AddTagHandler(handler); | |
271 | ||
272 | f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) ); | |
273 | if (f) | |
274 | { | |
275 | buf.clear(); | |
276 | buf = filter.ReadFile(*f); | |
277 | delete f; | |
278 | handler->ReadIn(m_Contents, m_ContentsCnt); | |
279 | parser.Parse(buf); | |
280 | handler->WriteOut(m_Contents, m_ContentsCnt); | |
281 | } | |
282 | else | |
283 | { | |
284 | wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str()); | |
285 | } | |
286 | ||
287 | f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) ); | |
288 | if (f) | |
289 | { | |
290 | buf.clear(); | |
291 | buf = filter.ReadFile(*f); | |
292 | delete f; | |
293 | handler->ReadIn(m_Index, m_IndexCnt); | |
294 | parser.Parse(buf); | |
295 | handler->WriteOut(m_Index, m_IndexCnt); | |
296 | } | |
297 | else if (!indexfile.IsEmpty()) | |
298 | { | |
299 | wxLogError(_("Cannot open index file: %s"), indexfile.c_str()); | |
300 | } | |
301 | return TRUE; | |
302 | } | |
303 | ||
304 | ||
305 | ||
306 | inline static void CacheWriteInt32(wxOutputStream *f, wxInt32 value) | |
307 | { | |
308 | wxInt32 x = wxINT32_SWAP_ON_BE(value); | |
309 | f->Write(&x, sizeof(x)); | |
310 | } | |
311 | ||
312 | inline static wxInt32 CacheReadInt32(wxInputStream *f) | |
313 | { | |
314 | wxInt32 x; | |
315 | f->Read(&x, sizeof(x)); | |
316 | return wxINT32_SWAP_ON_BE(x); | |
317 | } | |
318 | ||
319 | inline static void CacheWriteString(wxOutputStream *f, const wxChar *str) | |
320 | { | |
321 | #if wxUSE_UNICODE | |
322 | wxWX2MBbuf mbstr(wxConvUTF8.cWX2MB(str)); | |
323 | #else | |
324 | const wxChar *mbstr = str; | |
325 | #endif | |
326 | size_t len = strlen(mbstr)+1; | |
327 | CacheWriteInt32(f, len); | |
328 | f->Write(mbstr, len); | |
329 | } | |
330 | ||
331 | inline static wxChar* CacheReadString(wxInputStream *f) | |
332 | { | |
333 | char *str; | |
334 | size_t len = (size_t)CacheReadInt32(f); | |
335 | str = new char[len]; | |
336 | f->Read(str, len); | |
337 | #if !wxUSE_UNICODE | |
338 | return str; | |
339 | #else | |
340 | wxMB2WXbuf wxstr(wxConvUTF8.cMB2WX(str)); | |
341 | wxChar *outstr = new wxChar[wxStrlen(wxstr)+1]; | |
342 | wxStrcpy(outstr, wxstr); | |
343 | return outstr; | |
344 | #endif | |
345 | } | |
346 | ||
347 | #define CURRENT_CACHED_BOOK_VERSION 4 | |
348 | ||
349 | // Additional flags to detect incompatibilities of the runtime environment: | |
350 | #define CACHED_BOOK_FORMAT_FLAGS \ | |
351 | (wxUSE_UNICODE << 0) | |
352 | ||
353 | ||
354 | bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f) | |
355 | { | |
356 | int i, st; | |
357 | wxInt32 version; | |
358 | ||
359 | /* load header - version info : */ | |
360 | version = CacheReadInt32(f); | |
361 | ||
362 | if (version != CURRENT_CACHED_BOOK_VERSION) | |
363 | { | |
364 | // NB: We can just silently return FALSE here and don't worry about | |
365 | // it anymore, because AddBookParam will load the MS project in | |
366 | // absence of (properly versioned) .cached file and automatically | |
367 | // create new .cached file immediately afterward. | |
368 | return FALSE; | |
369 | } | |
370 | ||
371 | if (CacheReadInt32(f) != CACHED_BOOK_FORMAT_FLAGS) | |
372 | return FALSE; | |
373 | ||
374 | /* load contents : */ | |
375 | st = m_ContentsCnt; | |
376 | m_ContentsCnt += CacheReadInt32(f); | |
377 | m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, | |
378 | (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) * | |
379 | wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem)); | |
380 | for (i = st; i < m_ContentsCnt; i++) | |
381 | { | |
382 | m_Contents[i].m_Level = CacheReadInt32(f); | |
383 | m_Contents[i].m_ID = CacheReadInt32(f); | |
384 | m_Contents[i].m_Name = CacheReadString(f); | |
385 | m_Contents[i].m_Page = CacheReadString(f); | |
386 | m_Contents[i].m_Book = book; | |
387 | } | |
388 | ||
389 | /* load index : */ | |
390 | st = m_IndexCnt; | |
391 | m_IndexCnt += CacheReadInt32(f); | |
392 | m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) * | |
393 | wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem)); | |
394 | for (i = st; i < m_IndexCnt; i++) | |
395 | { | |
396 | m_Index[i].m_Name = CacheReadString(f); | |
397 | m_Index[i].m_Page = CacheReadString(f); | |
398 | m_Index[i].m_Book = book; | |
399 | } | |
400 | return TRUE; | |
401 | } | |
402 | ||
403 | ||
404 | bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f) | |
405 | { | |
406 | int i; | |
407 | wxInt32 cnt; | |
408 | ||
409 | /* save header - version info : */ | |
410 | CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION); | |
411 | CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS); | |
412 | ||
413 | /* save contents : */ | |
414 | for (cnt = 0, i = 0; i < m_ContentsCnt; i++) | |
415 | if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) | |
416 | cnt++; | |
417 | CacheWriteInt32(f, cnt); | |
418 | ||
419 | for (i = 0; i < m_ContentsCnt; i++) | |
420 | { | |
421 | if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) | |
422 | continue; | |
423 | CacheWriteInt32(f, m_Contents[i].m_Level); | |
424 | CacheWriteInt32(f, m_Contents[i].m_ID); | |
425 | CacheWriteString(f, m_Contents[i].m_Name); | |
426 | CacheWriteString(f, m_Contents[i].m_Page); | |
427 | } | |
428 | ||
429 | /* save index : */ | |
430 | for (cnt = 0, i = 0; i < m_IndexCnt; i++) | |
431 | if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) | |
432 | cnt++; | |
433 | CacheWriteInt32(f, cnt); | |
434 | ||
435 | for (i = 0; i < m_IndexCnt; i++) | |
436 | { | |
437 | if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) | |
438 | continue; | |
439 | CacheWriteString(f, m_Index[i].m_Name); | |
440 | CacheWriteString(f, m_Index[i].m_Page); | |
441 | } | |
442 | return TRUE; | |
443 | } | |
444 | ||
445 | ||
446 | void wxHtmlHelpData::SetTempDir(const wxString& path) | |
447 | { | |
448 | if (path == wxEmptyString) m_TempPath = path; | |
449 | else | |
450 | { | |
451 | if (wxIsAbsolutePath(path)) m_TempPath = path; | |
452 | else m_TempPath = wxGetCwd() + _T("/") + path; | |
453 | ||
454 | if (m_TempPath[m_TempPath.Length() - 1] != _T('/')) | |
455 | m_TempPath << _T('/'); | |
456 | } | |
457 | } | |
458 | ||
459 | ||
460 | ||
461 | static wxString SafeFileName(const wxString& s) | |
462 | { | |
463 | wxString res(s); | |
464 | res.Replace(wxT("#"), wxT("_")); | |
465 | res.Replace(wxT(":"), wxT("_")); | |
466 | res.Replace(wxT("\\"), wxT("_")); | |
467 | res.Replace(wxT("/"), wxT("_")); | |
468 | return res; | |
469 | } | |
470 | ||
471 | bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile, | |
472 | wxFontEncoding encoding, | |
473 | const wxString& title, const wxString& contfile, | |
474 | const wxString& indexfile, const wxString& deftopic, | |
475 | const wxString& path) | |
476 | { | |
477 | wxFileSystem fsys; | |
478 | wxFSFile *fi; | |
479 | wxHtmlBookRecord *bookr; | |
480 | ||
481 | int IndexOld = m_IndexCnt, | |
482 | ContentsOld = m_ContentsCnt; | |
483 | ||
484 | if (!path.IsEmpty()) | |
485 | fsys.ChangePathTo(path, TRUE); | |
486 | ||
487 | size_t booksCnt = m_BookRecords.GetCount(); | |
488 | for (size_t i = 0; i < booksCnt; i++) | |
489 | { | |
490 | if ( m_BookRecords[i].GetBookFile() == bookfile.GetLocation() ) | |
491 | return TRUE; // book is (was) loaded | |
492 | } | |
493 | ||
494 | bookr = new wxHtmlBookRecord(bookfile.GetLocation(), fsys.GetPath(), title, deftopic); | |
495 | ||
496 | if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0) | |
497 | m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem)); | |
498 | m_Contents[m_ContentsCnt].m_Level = 0; | |
499 | m_Contents[m_ContentsCnt].m_ID = 0; | |
500 | m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1]; | |
501 | wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str()); | |
502 | m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1]; | |
503 | wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str()); | |
504 | m_Contents[m_ContentsCnt].m_Book = bookr; | |
505 | ||
506 | // store the contents index for later | |
507 | int cont_start = m_ContentsCnt++; | |
508 | ||
509 | // Try to find cached binary versions: | |
510 | // 1. save file as book, but with .hhp.cached extension | |
511 | // 2. same as 1. but in temp path | |
512 | // 3. otherwise or if cache load failed, load it from MS. | |
513 | ||
514 | fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached")); | |
515 | ||
516 | if (fi == NULL || | |
517 | #if wxUSE_DATETIME | |
518 | fi->GetModificationTime() < bookfile.GetModificationTime() || | |
519 | #endif // wxUSE_DATETIME | |
520 | !LoadCachedBook(bookr, fi->GetStream())) | |
521 | { | |
522 | if (fi != NULL) delete fi; | |
523 | fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached")); | |
524 | if (m_TempPath == wxEmptyString || fi == NULL || | |
525 | #if wxUSE_DATETIME | |
526 | fi->GetModificationTime() < bookfile.GetModificationTime() || | |
527 | #endif // wxUSE_DATETIME | |
528 | !LoadCachedBook(bookr, fi->GetStream())) | |
529 | { | |
530 | LoadMSProject(bookr, fsys, indexfile, contfile); | |
531 | if (m_TempPath != wxEmptyString) | |
532 | { | |
533 | wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath + | |
534 | SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached")); | |
535 | SaveCachedBook(bookr, outs); | |
536 | delete outs; | |
537 | } | |
538 | } | |
539 | } | |
540 | ||
541 | if (fi != NULL) delete fi; | |
542 | ||
543 | // Now store the contents range | |
544 | bookr->SetContentsRange(cont_start, m_ContentsCnt); | |
545 | ||
546 | // Convert encoding, if neccessary: | |
547 | if (encoding != wxFONTENCODING_SYSTEM) | |
548 | { | |
549 | wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(encoding); | |
550 | if (a.GetCount() != 0 && a[0] != encoding) | |
551 | { | |
552 | int i; | |
553 | wxEncodingConverter conv; | |
554 | conv.Init(encoding, a[0]); | |
555 | ||
556 | for (i = IndexOld; i < m_IndexCnt; i++) | |
557 | conv.Convert(m_Index[i].m_Name); | |
558 | for (i = ContentsOld; i < m_ContentsCnt; i++) | |
559 | conv.Convert(m_Contents[i].m_Name); | |
560 | } | |
561 | } | |
562 | ||
563 | m_BookRecords.Add(bookr); | |
564 | if (m_IndexCnt > 0) | |
565 | qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), wxHtmlHelpIndexCompareFunc); | |
566 | ||
567 | return TRUE; | |
568 | } | |
569 | ||
570 | ||
571 | bool wxHtmlHelpData::AddBook(const wxString& book) | |
572 | { | |
573 | wxString extension(book.Right(4).Lower()); | |
574 | if (extension == wxT(".zip") || | |
575 | #if wxUSE_LIBMSPACK | |
576 | extension == wxT(".chm") /*compressed html help book*/ || | |
577 | #endif | |
578 | extension == wxT(".htb") /*html book*/) | |
579 | { | |
580 | wxFileSystem fsys; | |
581 | wxString s; | |
582 | bool rt = FALSE; | |
583 | ||
584 | #if wxUSE_LIBMSPACK | |
585 | if (extension == wxT(".chm")) | |
586 | s = fsys.FindFirst(book + wxT("#chm:*.hhp"), wxFILE); | |
587 | else | |
588 | #endif | |
589 | s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE); | |
590 | ||
591 | while (!s.IsEmpty()) | |
592 | { | |
593 | if (AddBook(s)) rt = TRUE; | |
594 | s = fsys.FindNext(); | |
595 | } | |
596 | ||
597 | return rt; | |
598 | } | |
599 | ||
600 | wxFSFile *fi; | |
601 | wxFileSystem fsys; | |
602 | ||
603 | wxString title = _("noname"), | |
604 | safetitle, | |
605 | start = wxEmptyString, | |
606 | contents = wxEmptyString, | |
607 | index = wxEmptyString, | |
608 | charset = wxEmptyString; | |
609 | ||
610 | fi = fsys.OpenFile(book); | |
611 | if (fi == NULL) | |
612 | { | |
613 | wxLogError(_("Cannot open HTML help book: %s"), book.c_str()); | |
614 | return FALSE; | |
615 | } | |
616 | fsys.ChangePathTo(book); | |
617 | ||
618 | const wxChar *lineptr; | |
619 | wxChar linebuf[300]; | |
620 | wxString tmp; | |
621 | wxHtmlFilterPlainText filter; | |
622 | tmp = filter.ReadFile(*fi); | |
623 | lineptr = tmp.c_str(); | |
624 | ||
625 | do | |
626 | { | |
627 | lineptr = ReadLine(lineptr, linebuf, 300); | |
628 | ||
629 | for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++) | |
630 | *ch = tolower(*ch); | |
631 | ||
632 | if (wxStrstr(linebuf, _T("title=")) == linebuf) | |
633 | title = linebuf + wxStrlen(_T("title=")); | |
634 | if (wxStrstr(linebuf, _T("default topic=")) == linebuf) | |
635 | start = linebuf + wxStrlen(_T("default topic=")); | |
636 | if (wxStrstr(linebuf, _T("index file=")) == linebuf) | |
637 | index = linebuf + wxStrlen(_T("index file=")); | |
638 | if (wxStrstr(linebuf, _T("contents file=")) == linebuf) | |
639 | contents = linebuf + wxStrlen(_T("contents file=")); | |
640 | if (wxStrstr(linebuf, _T("charset=")) == linebuf) | |
641 | charset = linebuf + wxStrlen(_T("charset=")); | |
642 | } while (lineptr != NULL); | |
643 | ||
644 | wxFontEncoding enc; | |
645 | if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM; | |
646 | else enc = wxFontMapper::Get()->CharsetToEncoding(charset); | |
647 | bool rtval = AddBookParam(*fi, enc, | |
648 | title, contents, index, start, fsys.GetPath()); | |
649 | delete fi; | |
650 | return rtval; | |
651 | } | |
652 | ||
653 | wxString wxHtmlHelpData::FindPageByName(const wxString& x) | |
654 | { | |
655 | int cnt; | |
656 | int i; | |
657 | wxFileSystem fsys; | |
658 | wxFSFile *f; | |
659 | wxString url(wxEmptyString); | |
660 | ||
661 | /* 1. try to open given file: */ | |
662 | ||
663 | cnt = m_BookRecords.GetCount(); | |
664 | for (i = 0; i < cnt; i++) | |
665 | { | |
666 | f = fsys.OpenFile(m_BookRecords[i].GetFullPath(x)); | |
667 | if (f) | |
668 | { | |
669 | url = m_BookRecords[i].GetFullPath(x); | |
670 | delete f; | |
671 | return url; | |
672 | } | |
673 | } | |
674 | ||
675 | ||
676 | /* 2. try to find a book: */ | |
677 | ||
678 | for (i = 0; i < cnt; i++) | |
679 | { | |
680 | if (m_BookRecords[i].GetTitle() == x) | |
681 | { | |
682 | url = m_BookRecords[i].GetFullPath(m_BookRecords[i].GetStart()); | |
683 | return url; | |
684 | } | |
685 | } | |
686 | ||
687 | /* 3. try to find in contents: */ | |
688 | ||
689 | cnt = m_ContentsCnt; | |
690 | for (i = 0; i < cnt; i++) | |
691 | { | |
692 | if (wxStrcmp(m_Contents[i].m_Name, x) == 0) | |
693 | { | |
694 | url = m_Contents[i].GetFullPath(); | |
695 | return url; | |
696 | } | |
697 | } | |
698 | ||
699 | ||
700 | /* 4. try to find in index: */ | |
701 | ||
702 | cnt = m_IndexCnt; | |
703 | for (i = 0; i < cnt; i++) | |
704 | { | |
705 | if (wxStrcmp(m_Index[i].m_Name, x) == 0) | |
706 | { | |
707 | url = m_Index[i].GetFullPath(); | |
708 | return url; | |
709 | } | |
710 | } | |
711 | ||
712 | return url; | |
713 | } | |
714 | ||
715 | wxString wxHtmlHelpData::FindPageById(int id) | |
716 | { | |
717 | int i; | |
718 | wxString url(wxEmptyString); | |
719 | ||
720 | for (i = 0; i < m_ContentsCnt; i++) | |
721 | { | |
722 | if (m_Contents[i].m_ID == id) | |
723 | { | |
724 | url = m_Contents[i].GetFullPath(); | |
725 | return url; | |
726 | } | |
727 | } | |
728 | ||
729 | return url; | |
730 | } | |
731 | ||
732 | //---------------------------------------------------------------------------------- | |
733 | // wxHtmlSearchStatus functions | |
734 | //---------------------------------------------------------------------------------- | |
735 | ||
736 | wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword, | |
737 | bool case_sensitive, bool whole_words_only, | |
738 | const wxString& book) | |
739 | { | |
740 | m_Data = data; | |
741 | m_Keyword = keyword; | |
742 | wxHtmlBookRecord* bookr = NULL; | |
743 | if (book != wxEmptyString) | |
744 | { | |
745 | // we have to search in a specific book. Find it first | |
746 | int i, cnt = data->m_BookRecords.GetCount(); | |
747 | for (i = 0; i < cnt; i++) | |
748 | if (data->m_BookRecords[i].GetTitle() == book) | |
749 | { | |
750 | bookr = &(data->m_BookRecords[i]); | |
751 | m_CurIndex = bookr->GetContentsStart(); | |
752 | m_MaxIndex = bookr->GetContentsEnd(); | |
753 | break; | |
754 | } | |
755 | // check; we won't crash if the book doesn't exist, but it's Bad Anyway. | |
756 | wxASSERT(bookr); | |
757 | } | |
758 | if (! bookr) | |
759 | { | |
760 | // no book specified; search all books | |
761 | m_CurIndex = 0; | |
762 | m_MaxIndex = m_Data->m_ContentsCnt; | |
763 | } | |
764 | m_Engine.LookFor(keyword, case_sensitive, whole_words_only); | |
765 | m_Active = (m_CurIndex < m_MaxIndex); | |
766 | m_LastPage = NULL; | |
767 | } | |
768 | ||
769 | bool wxHtmlSearchStatus::Search() | |
770 | { | |
771 | wxFSFile *file; | |
772 | int i = m_CurIndex; // shortcut | |
773 | bool found = FALSE; | |
774 | wxChar *thepage; | |
775 | ||
776 | if (!m_Active) | |
777 | { | |
778 | // sanity check. Illegal use, but we'll try to prevent a crash anyway | |
779 | wxASSERT(m_Active); | |
780 | return FALSE; | |
781 | } | |
782 | ||
783 | m_Name = wxEmptyString; | |
784 | m_ContentsItem = NULL; | |
785 | thepage = m_Data->m_Contents[i].m_Page; | |
786 | ||
787 | m_Active = (++m_CurIndex < m_MaxIndex); | |
788 | // check if it is same page with different anchor: | |
789 | if (m_LastPage != NULL) | |
790 | { | |
791 | wxChar *p1, *p2; | |
792 | for (p1 = thepage, p2 = m_LastPage; | |
793 | *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {} | |
794 | ||
795 | m_LastPage = thepage; | |
796 | ||
797 | if (*p1 == 0 || *p1 == _T('#')) | |
798 | return FALSE; | |
799 | } | |
800 | else m_LastPage = thepage; | |
801 | ||
802 | wxFileSystem fsys; | |
803 | file = fsys.OpenFile(m_Data->m_Contents[i].m_Book->GetFullPath(thepage)); | |
804 | if (file) | |
805 | { | |
806 | if (m_Engine.Scan(*file)) | |
807 | { | |
808 | m_Name = m_Data->m_Contents[i].m_Name; | |
809 | m_ContentsItem = m_Data->m_Contents + i; | |
810 | found = TRUE; | |
811 | } | |
812 | delete file; | |
813 | } | |
814 | return found; | |
815 | } | |
816 | ||
817 | ||
818 | ||
819 | ||
820 | ||
821 | ||
822 | ||
823 | ||
824 | //-------------------------------------------------------------------------------- | |
825 | // wxHtmlSearchEngine | |
826 | //-------------------------------------------------------------------------------- | |
827 | ||
828 | void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only) | |
829 | { | |
830 | m_CaseSensitive = case_sensitive; | |
831 | m_WholeWords = whole_words_only; | |
832 | if (m_Keyword) delete[] m_Keyword; | |
833 | m_Keyword = new wxChar[keyword.Length() + 1]; | |
834 | wxStrcpy(m_Keyword, keyword.c_str()); | |
835 | ||
836 | if (!m_CaseSensitive) | |
837 | { | |
838 | for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--) | |
839 | { | |
840 | if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z'))) | |
841 | m_Keyword[i] += wxT('a') - wxT('A'); | |
842 | } | |
843 | } | |
844 | } | |
845 | ||
846 | ||
847 | static inline bool WHITESPACE(wxChar c) | |
848 | { | |
849 | return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t'); | |
850 | } | |
851 | ||
852 | bool wxHtmlSearchEngine::Scan(const wxFSFile& file) | |
853 | { | |
854 | wxASSERT_MSG(m_Keyword != NULL, wxT("wxHtmlSearchEngine::LookFor must be called before scanning!")); | |
855 | ||
856 | int i, j; | |
857 | int wrd = wxStrlen(m_Keyword); | |
858 | bool found = FALSE; | |
859 | wxHtmlFilterHTML filter; | |
860 | wxString tmp = filter.ReadFile(file); | |
861 | int lng = tmp.length(); | |
862 | const wxChar *buf = tmp.c_str(); | |
863 | ||
864 | if (!m_CaseSensitive) | |
865 | for (i = 0; i < lng; i++) | |
866 | tmp[(size_t)i] = (wxChar)wxTolower(tmp[(size_t)i]); | |
867 | ||
868 | if (m_WholeWords) | |
869 | { | |
870 | for (i = 0; i < lng - wrd; i++) | |
871 | { | |
872 | if (WHITESPACE(buf[i])) continue; | |
873 | j = 0; | |
874 | while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++; | |
875 | if (j == wrd && WHITESPACE(buf[i + j])) { found = TRUE; break; } | |
876 | } | |
877 | } | |
878 | ||
879 | else | |
880 | { | |
881 | for (i = 0; i < lng - wrd; i++) | |
882 | { | |
883 | j = 0; | |
884 | while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++; | |
885 | if (j == wrd) { found = TRUE; break; } | |
886 | } | |
887 | } | |
888 | ||
889 | return found; | |
890 | } | |
891 | ||
892 | ||
893 | ||
894 | #endif |