]>
Commit | Line | Data |
---|---|---|
8ec2b484 | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/html/helpdata.cpp |
8ec2b484 | 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 | |
8ec2b484 | 7 | // Copyright: (c) Harm van der Heijden and Vaclav Slavik |
65571936 | 8 | // Licence: wxWindows licence |
8ec2b484 HH |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
8ec2b484 HH |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
93763ad5 | 15 | #pragma hdrstop |
8ec2b484 HH |
16 | #endif |
17 | ||
f6bcfd97 | 18 | #if wxUSE_HTML && wxUSE_STREAMS |
8ec2b484 | 19 | |
b4f4d3dd | 20 | #ifndef WX_PRECOMP |
04dbb646 VZ |
21 | #include "wx/intl.h" |
22 | #include "wx/log.h" | |
8ec2b484 HH |
23 | #endif |
24 | ||
401eb3de RR |
25 | #include <ctype.h> |
26 | ||
8ec2b484 HH |
27 | #include "wx/html/helpdata.h" |
28 | #include "wx/tokenzr.h" | |
29 | #include "wx/wfstream.h" | |
30 | #include "wx/busyinfo.h" | |
f890e2d4 VS |
31 | #include "wx/encconv.h" |
32 | #include "wx/fontmap.h" | |
69941f05 | 33 | #include "wx/html/htmlpars.h" |
8ec2b484 | 34 | #include "wx/html/htmldefs.h" |
2b5f62a0 | 35 | #include "wx/html/htmlfilt.h" |
057b55b0 | 36 | #include "wx/filename.h" |
8ec2b484 HH |
37 | |
38 | #include "wx/arrimpl.cpp" | |
39 | WX_DEFINE_OBJARRAY(wxHtmlBookRecArray) | |
91fa114d | 40 | WX_DEFINE_OBJARRAY(wxHtmlHelpDataItems) |
8ec2b484 HH |
41 | |
42 | //----------------------------------------------------------------------------- | |
43 | // static helper functions | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | // Reads one line, stores it into buf and returns pointer to new line or NULL. | |
1a1dac18 | 47 | static const wxChar* ReadLine(const wxChar *line, wxChar *buf, size_t bufsize) |
8ec2b484 | 48 | { |
1a1dac18 MB |
49 | wxChar *writeptr = buf; |
50 | wxChar *endptr = buf + bufsize - 1; | |
51 | const wxChar *readptr = line; | |
8ec2b484 | 52 | |
9a83f860 | 53 | while (*readptr != 0 && *readptr != wxT('\r') && *readptr != wxT('\n') && |
d1da8872 | 54 | writeptr != endptr) |
3dde6c72 | 55 | *(writeptr++) = *(readptr++); |
8ec2b484 | 56 | *writeptr = 0; |
9a83f860 | 57 | while (*readptr == wxT('\r') || *readptr == wxT('\n')) |
3dde6c72 VS |
58 | readptr++; |
59 | if (*readptr == 0) | |
60 | return NULL; | |
d1da8872 | 61 | else |
3dde6c72 | 62 | return readptr; |
8ec2b484 HH |
63 | } |
64 | ||
65 | ||
8ec2b484 | 66 | |
91fa114d VS |
67 | static int |
68 | wxHtmlHelpIndexCompareFunc(wxHtmlHelpDataItem **a, wxHtmlHelpDataItem **b) | |
8ec2b484 | 69 | { |
91fa114d VS |
70 | wxHtmlHelpDataItem *ia = *a; |
71 | wxHtmlHelpDataItem *ib = *b; | |
3912b3f8 | 72 | |
7fbe5489 VS |
73 | if (ia == NULL) |
74 | return -1; | |
75 | if (ib == NULL) | |
76 | return 1; | |
42841dfc | 77 | |
91fa114d VS |
78 | if (ia->parent == ib->parent) |
79 | { | |
80 | return ia->name.CmpNoCase(ib->name); | |
81 | } | |
82 | else if (ia->level == ib->level) | |
83 | { | |
84 | return wxHtmlHelpIndexCompareFunc(&ia->parent, &ib->parent); | |
85 | } | |
86 | else | |
87 | { | |
88 | wxHtmlHelpDataItem *ia2 = ia; | |
89 | wxHtmlHelpDataItem *ib2 = ib; | |
d1da8872 | 90 | |
91fa114d VS |
91 | while (ia2->level > ib2->level) |
92 | { | |
93 | ia2 = ia2->parent; | |
94 | } | |
95 | while (ib2->level > ia2->level) | |
96 | { | |
97 | ib2 = ib2->parent; | |
98 | } | |
d1da8872 | 99 | |
91fa114d VS |
100 | wxASSERT(ia2); |
101 | wxASSERT(ib2); | |
102 | int res = wxHtmlHelpIndexCompareFunc(&ia2, &ib2); | |
103 | if (res != 0) | |
104 | return res; | |
105 | else if (ia->level > ib->level) | |
106 | return 1; | |
107 | else | |
108 | return -1; | |
109 | } | |
8ec2b484 HH |
110 | } |
111 | ||
8ec2b484 HH |
112 | //----------------------------------------------------------------------------- |
113 | // HP_Parser | |
114 | //----------------------------------------------------------------------------- | |
115 | ||
116 | class HP_Parser : public wxHtmlParser | |
117 | { | |
211dfedd | 118 | public: |
67c276bd VS |
119 | HP_Parser() |
120 | { | |
121 | GetEntitiesParser()->SetEncoding(wxFONTENCODING_ISO8859_1); | |
122 | } | |
fc7a2a60 | 123 | |
211dfedd | 124 | wxObject* GetProduct() { return NULL; } |
fc7a2a60 | 125 | |
211dfedd | 126 | protected: |
5bce3e6f | 127 | virtual void AddText(const wxString& WXUNUSED(txt)) {} |
fc7a2a60 | 128 | |
c0c133e1 | 129 | wxDECLARE_NO_COPY_CLASS(HP_Parser); |
8ec2b484 HH |
130 | }; |
131 | ||
132 | ||
133 | //----------------------------------------------------------------------------- | |
134 | // HP_TagHandler | |
135 | //----------------------------------------------------------------------------- | |
136 | ||
137 | class HP_TagHandler : public wxHtmlTagHandler | |
138 | { | |
139 | private: | |
91fa114d VS |
140 | wxString m_name, m_page; |
141 | int m_level; | |
142 | int m_id; | |
143 | int m_index; | |
144 | int m_count; | |
145 | wxHtmlHelpDataItem *m_parentItem; | |
146 | wxHtmlBookRecord *m_book; | |
d1da8872 | 147 | |
91fa114d | 148 | wxHtmlHelpDataItems *m_data; |
8ec2b484 HH |
149 | |
150 | public: | |
04dbb646 | 151 | HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler() |
91fa114d VS |
152 | { |
153 | m_data = NULL; | |
154 | m_book = b; | |
155 | m_name = m_page = wxEmptyString; | |
156 | m_level = 0; | |
d1da8872 | 157 | m_id = wxID_ANY; |
91fa114d VS |
158 | m_count = 0; |
159 | m_parentItem = NULL; | |
160 | } | |
66a77a74 | 161 | wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); } |
8ec2b484 | 162 | bool HandleTag(const wxHtmlTag& tag); |
91fa114d VS |
163 | |
164 | void Reset(wxHtmlHelpDataItems& data) | |
165 | { | |
166 | m_data = &data; | |
167 | m_count = 0; | |
168 | m_level = 0; | |
169 | m_parentItem = NULL; | |
170 | } | |
22f3361e | 171 | |
c0c133e1 | 172 | wxDECLARE_NO_COPY_CLASS(HP_TagHandler); |
8ec2b484 HH |
173 | }; |
174 | ||
175 | ||
176 | bool HP_TagHandler::HandleTag(const wxHtmlTag& tag) | |
177 | { | |
04dbb646 | 178 | if (tag.GetName() == wxT("UL")) |
4f9297b0 | 179 | { |
91fa114d VS |
180 | wxHtmlHelpDataItem *oldparent = m_parentItem; |
181 | m_level++; | |
182 | m_parentItem = (m_count > 0) ? &(*m_data)[m_data->size()-1] : NULL; | |
8ec2b484 | 183 | ParseInner(tag); |
91fa114d VS |
184 | m_level--; |
185 | m_parentItem = oldparent; | |
186 | return true; | |
8ec2b484 | 187 | } |
04dbb646 | 188 | else if (tag.GetName() == wxT("OBJECT")) |
4f9297b0 | 189 | { |
91fa114d | 190 | m_name = m_page = wxEmptyString; |
8ec2b484 | 191 | ParseInner(tag); |
50494a55 | 192 | |
daa084c2 | 193 | #if 0 |
91fa114d | 194 | if (!page.IsEmpty()) |
7df9fbc3 | 195 | /* Valid HHW's file may contain only two object tags: |
04dbb646 | 196 | |
4157f43f VS |
197 | <OBJECT type="text/site properties"> |
198 | <param name="ImageType" value="Folder"> | |
199 | </OBJECT> | |
04dbb646 | 200 | |
4157f43f | 201 | or |
04dbb646 VZ |
202 | |
203 | <OBJECT type="text/sitemap"> | |
204 | <param name="Name" value="main page"> | |
205 | <param name="Local" value="another.htm"> | |
206 | </OBJECT> | |
207 | ||
91fa114d | 208 | We're interested in the latter. !page.IsEmpty() is valid |
4157f43f VS |
209 | condition because text/site properties does not contain Local param |
210 | */ | |
daa084c2 VS |
211 | #endif |
212 | if (tag.GetParam(wxT("TYPE")) == wxT("text/sitemap")) | |
4157f43f | 213 | { |
91fa114d VS |
214 | wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem(); |
215 | item->parent = m_parentItem; | |
216 | item->level = m_level; | |
217 | item->id = m_id; | |
218 | item->page = m_page; | |
219 | item->name = m_name; | |
d1da8872 | 220 | |
91fa114d VS |
221 | item->book = m_book; |
222 | m_data->Add(item); | |
223 | m_count++; | |
4157f43f | 224 | } |
50494a55 | 225 | |
91fa114d | 226 | return true; |
8ec2b484 | 227 | } |
04dbb646 | 228 | else |
4f9297b0 | 229 | { // "PARAM" |
91fa114d VS |
230 | if (m_name.empty() && tag.GetParam(wxT("NAME")) == wxT("Name")) |
231 | m_name = tag.GetParam(wxT("VALUE")); | |
04dbb646 | 232 | if (tag.GetParam(wxT("NAME")) == wxT("Local")) |
91fa114d | 233 | m_page = tag.GetParam(wxT("VALUE")); |
04dbb646 | 234 | if (tag.GetParam(wxT("NAME")) == wxT("ID")) |
91fa114d VS |
235 | tag.GetParamAsInt(wxT("VALUE"), &m_id); |
236 | return false; | |
8ec2b484 HH |
237 | } |
238 | } | |
239 | ||
240 | ||
8ec2b484 HH |
241 | //----------------------------------------------------------------------------- |
242 | // wxHtmlHelpData | |
243 | //----------------------------------------------------------------------------- | |
244 | ||
468ae730 VS |
245 | wxString wxHtmlBookRecord::GetFullPath(const wxString &page) const |
246 | { | |
d5bfe584 | 247 | if (wxIsAbsolutePath(page) || page.Find(wxT("file:")) == 0) |
468ae730 VS |
248 | return page; |
249 | else | |
250 | return m_BasePath + page; | |
251 | } | |
252 | ||
91fa114d VS |
253 | wxString wxHtmlHelpDataItem::GetIndentedName() const |
254 | { | |
255 | wxString s; | |
256 | for (int i = 1; i < level; i++) | |
9a83f860 | 257 | s << wxT(" "); |
91fa114d VS |
258 | s << name; |
259 | return s; | |
260 | } | |
468ae730 VS |
261 | |
262 | ||
8ec2b484 HH |
263 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject) |
264 | ||
f42b1601 | 265 | wxHtmlHelpData::wxHtmlHelpData() |
8ec2b484 | 266 | { |
8ec2b484 HH |
267 | } |
268 | ||
269 | wxHtmlHelpData::~wxHtmlHelpData() | |
270 | { | |
8ec2b484 HH |
271 | } |
272 | ||
67c276bd VS |
273 | bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, |
274 | const wxString& indexfile, | |
275 | const wxString& contentsfile) | |
8ec2b484 HH |
276 | { |
277 | wxFSFile *f; | |
2b5f62a0 | 278 | wxHtmlFilterHTML filter; |
eb37e1d2 | 279 | wxString buf; |
8ec2b484 | 280 | wxString string; |
f42b1601 | 281 | |
8ec2b484 HH |
282 | HP_Parser parser; |
283 | HP_TagHandler *handler = new HP_TagHandler(book); | |
284 | parser.AddTagHandler(handler); | |
285 | ||
d3b9f782 | 286 | f = ( contentsfile.empty() ? NULL : fsys.OpenFile(contentsfile) ); |
04dbb646 | 287 | if (f) |
4f9297b0 | 288 | { |
eb37e1d2 | 289 | buf.clear(); |
2b5f62a0 | 290 | buf = filter.ReadFile(*f); |
8ec2b484 | 291 | delete f; |
91fa114d | 292 | handler->Reset(m_contents); |
8ec2b484 | 293 | parser.Parse(buf); |
8ec2b484 | 294 | } |
f3c82859 | 295 | else |
2b5f62a0 | 296 | { |
f6bcfd97 | 297 | wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str()); |
2b5f62a0 | 298 | } |
8ec2b484 | 299 | |
d3b9f782 | 300 | f = ( indexfile.empty() ? NULL : fsys.OpenFile(indexfile) ); |
04dbb646 | 301 | if (f) |
4f9297b0 | 302 | { |
eb37e1d2 | 303 | buf.clear(); |
2b5f62a0 | 304 | buf = filter.ReadFile(*f); |
8ec2b484 | 305 | delete f; |
91fa114d | 306 | handler->Reset(m_index); |
8ec2b484 | 307 | parser.Parse(buf); |
8ec2b484 | 308 | } |
93763ad5 | 309 | else if (!indexfile.empty()) |
2b5f62a0 | 310 | { |
f6bcfd97 | 311 | wxLogError(_("Cannot open index file: %s"), indexfile.c_str()); |
2b5f62a0 | 312 | } |
d1da8872 | 313 | return true; |
8ec2b484 HH |
314 | } |
315 | ||
4fd5055c VS |
316 | inline static void CacheWriteInt32(wxOutputStream *f, wxInt32 value) |
317 | { | |
318 | wxInt32 x = wxINT32_SWAP_ON_BE(value); | |
319 | f->Write(&x, sizeof(x)); | |
320 | } | |
f35822af | 321 | |
4fd5055c VS |
322 | inline static wxInt32 CacheReadInt32(wxInputStream *f) |
323 | { | |
324 | wxInt32 x; | |
325 | f->Read(&x, sizeof(x)); | |
326 | return wxINT32_SWAP_ON_BE(x); | |
327 | } | |
f35822af | 328 | |
3912b3f8 | 329 | inline static void CacheWriteString(wxOutputStream *f, const wxString& str) |
d1da8872 | 330 | { |
3912b3f8 VS |
331 | const wxWX2MBbuf mbstr = str.mb_str(wxConvUTF8); |
332 | size_t len = strlen((const char*)mbstr)+1; | |
4fd5055c | 333 | CacheWriteInt32(f, len); |
3912b3f8 | 334 | f->Write((const char*)mbstr, len); |
4fd5055c | 335 | } |
f35822af | 336 | |
3912b3f8 | 337 | inline static wxString CacheReadString(wxInputStream *f) |
4fd5055c | 338 | { |
4fd5055c | 339 | size_t len = (size_t)CacheReadInt32(f); |
3912b3f8 VS |
340 | wxCharBuffer str(len-1); |
341 | f->Read(str.data(), len); | |
342 | return wxString(str, wxConvUTF8); | |
4fd5055c | 343 | } |
f35822af | 344 | |
91fa114d | 345 | #define CURRENT_CACHED_BOOK_VERSION 5 |
d1a9c82b VS |
346 | |
347 | // Additional flags to detect incompatibilities of the runtime environment: | |
348 | #define CACHED_BOOK_FORMAT_FLAGS \ | |
349 | (wxUSE_UNICODE << 0) | |
350 | ||
f35822af | 351 | |
8ec2b484 HH |
352 | bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f) |
353 | { | |
91fa114d | 354 | int i, st, newsize; |
f35822af VS |
355 | wxInt32 version; |
356 | ||
357 | /* load header - version info : */ | |
4fd5055c | 358 | version = CacheReadInt32(f); |
d1da8872 | 359 | |
04dbb646 | 360 | if (version != CURRENT_CACHED_BOOK_VERSION) |
f3c82859 | 361 | { |
d1da8872 | 362 | // NB: We can just silently return false here and don't worry about |
4fd5055c | 363 | // it anymore, because AddBookParam will load the MS project in |
d1da8872 | 364 | // absence of (properly versioned) .cached file and automatically |
4fd5055c | 365 | // create new .cached file immediately afterward. |
91fa114d | 366 | return false; |
f3c82859 | 367 | } |
04dbb646 | 368 | |
d1a9c82b | 369 | if (CacheReadInt32(f) != CACHED_BOOK_FORMAT_FLAGS) |
91fa114d | 370 | return false; |
d1a9c82b | 371 | |
8ec2b484 | 372 | /* load contents : */ |
91fa114d VS |
373 | st = m_contents.size(); |
374 | newsize = st + CacheReadInt32(f); | |
375 | m_contents.Alloc(newsize); | |
376 | for (i = st; i < newsize; i++) | |
4f9297b0 | 377 | { |
91fa114d VS |
378 | wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem; |
379 | item->level = CacheReadInt32(f); | |
380 | item->id = CacheReadInt32(f); | |
381 | item->name = CacheReadString(f); | |
382 | item->page = CacheReadString(f); | |
383 | item->book = book; | |
384 | m_contents.Add(item); | |
8ec2b484 HH |
385 | } |
386 | ||
387 | /* load index : */ | |
91fa114d VS |
388 | st = m_index.size(); |
389 | newsize = st + CacheReadInt32(f); | |
390 | m_index.Alloc(newsize); | |
391 | for (i = st; i < newsize; i++) | |
4f9297b0 | 392 | { |
91fa114d VS |
393 | wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem; |
394 | item->name = CacheReadString(f); | |
395 | item->page = CacheReadString(f); | |
396 | item->level = CacheReadInt32(f); | |
397 | item->book = book; | |
398 | int parentShift = CacheReadInt32(f); | |
399 | if (parentShift != 0) | |
400 | item->parent = &m_index[m_index.size() - parentShift]; | |
401 | m_index.Add(item); | |
8ec2b484 | 402 | } |
91fa114d | 403 | return true; |
8ec2b484 HH |
404 | } |
405 | ||
406 | ||
407 | bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f) | |
408 | { | |
409 | int i; | |
4fd5055c | 410 | wxInt32 cnt; |
f35822af VS |
411 | |
412 | /* save header - version info : */ | |
4fd5055c | 413 | CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION); |
d1a9c82b | 414 | CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS); |
8ec2b484 HH |
415 | |
416 | /* save contents : */ | |
91fa114d | 417 | int len = m_contents.size(); |
d1da8872 | 418 | for (cnt = 0, i = 0; i < len; i++) |
91fa114d | 419 | if (m_contents[i].book == book && m_contents[i].level > 0) |
4fd5055c VS |
420 | cnt++; |
421 | CacheWriteInt32(f, cnt); | |
8ec2b484 | 422 | |
91fa114d | 423 | for (i = 0; i < len; i++) |
4f9297b0 | 424 | { |
d1da8872 | 425 | if (m_contents[i].book != book || m_contents[i].level == 0) |
4fd5055c | 426 | continue; |
91fa114d VS |
427 | CacheWriteInt32(f, m_contents[i].level); |
428 | CacheWriteInt32(f, m_contents[i].id); | |
429 | CacheWriteString(f, m_contents[i].name); | |
430 | CacheWriteString(f, m_contents[i].page); | |
8ec2b484 HH |
431 | } |
432 | ||
433 | /* save index : */ | |
91fa114d | 434 | len = m_index.size(); |
d1da8872 WS |
435 | for (cnt = 0, i = 0; i < len; i++) |
436 | if (m_index[i].book == book && m_index[i].level > 0) | |
4fd5055c VS |
437 | cnt++; |
438 | CacheWriteInt32(f, cnt); | |
8ec2b484 | 439 | |
91fa114d | 440 | for (i = 0; i < len; i++) |
4f9297b0 | 441 | { |
d1da8872 | 442 | if (m_index[i].book != book || m_index[i].level == 0) |
4fd5055c | 443 | continue; |
91fa114d VS |
444 | CacheWriteString(f, m_index[i].name); |
445 | CacheWriteString(f, m_index[i].page); | |
446 | CacheWriteInt32(f, m_index[i].level); | |
447 | // save distance to parent item, if any: | |
448 | if (m_index[i].parent == NULL) | |
449 | { | |
450 | CacheWriteInt32(f, 0); | |
451 | } | |
452 | else | |
453 | { | |
17a1ebd1 | 454 | int cnt2 = 0; |
91fa114d VS |
455 | wxHtmlHelpDataItem *parent = m_index[i].parent; |
456 | for (int j = i-1; j >= 0; j--) | |
457 | { | |
458 | if (m_index[j].book == book && m_index[j].level > 0) | |
17a1ebd1 | 459 | cnt2++; |
91fa114d VS |
460 | if (&m_index[j] == parent) |
461 | break; | |
462 | } | |
17a1ebd1 VZ |
463 | wxASSERT(cnt2 > 0); |
464 | CacheWriteInt32(f, cnt2); | |
91fa114d | 465 | } |
8ec2b484 | 466 | } |
91fa114d | 467 | return true; |
8ec2b484 HH |
468 | } |
469 | ||
470 | ||
471 | void wxHtmlHelpData::SetTempDir(const wxString& path) | |
472 | { | |
91fa114d VS |
473 | if (path.empty()) |
474 | m_tempPath = path; | |
04dbb646 | 475 | else |
4f9297b0 | 476 | { |
ec2c85bf VZ |
477 | wxFileName fn(path); |
478 | fn.MakeAbsolute(); | |
8ec2b484 | 479 | |
ec2c85bf | 480 | m_tempPath = fn.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR); |
8ec2b484 HH |
481 | } |
482 | } | |
483 | ||
484 | ||
29e60597 VS |
485 | |
486 | static wxString SafeFileName(const wxString& s) | |
487 | { | |
488 | wxString res(s); | |
489 | res.Replace(wxT("#"), wxT("_")); | |
490 | res.Replace(wxT(":"), wxT("_")); | |
491 | res.Replace(wxT("\\"), wxT("_")); | |
492 | res.Replace(wxT("/"), wxT("_")); | |
493 | return res; | |
494 | } | |
495 | ||
f35822af | 496 | bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile, |
f890e2d4 | 497 | wxFontEncoding encoding, |
f35822af | 498 | const wxString& title, const wxString& contfile, |
d5bb85a0 VS |
499 | const wxString& indexfile, const wxString& deftopic, |
500 | const wxString& path) | |
8ec2b484 | 501 | { |
6a63bdc8 JS |
502 | #if wxUSE_WCHAR_T |
503 | #if wxUSE_UNICODE | |
504 | #define CORRECT_STR(str, conv) \ | |
505 | str = wxString((str).mb_str(wxConvISO8859_1), conv) | |
506 | #else | |
507 | #define CORRECT_STR(str, conv) \ | |
508 | str = wxString((str).wc_str(conv), wxConvLocal) | |
509 | #endif | |
510 | #else | |
511 | #define CORRECT_STR(str, conv) | |
512 | #endif | |
513 | ||
8ec2b484 HH |
514 | wxFileSystem fsys; |
515 | wxFSFile *fi; | |
516 | wxHtmlBookRecord *bookr; | |
04dbb646 | 517 | |
91fa114d VS |
518 | int IndexOld = m_index.size(), |
519 | ContentsOld = m_contents.size(); | |
f42b1601 | 520 | |
93763ad5 | 521 | if (!path.empty()) |
d1da8872 | 522 | fsys.ChangePathTo(path, true); |
8ec2b484 | 523 | |
91fa114d | 524 | size_t booksCnt = m_bookRecords.GetCount(); |
5ecdcaa7 VS |
525 | for (size_t i = 0; i < booksCnt; i++) |
526 | { | |
91fa114d VS |
527 | if ( m_bookRecords[i].GetBookFile() == bookfile.GetLocation() ) |
528 | return true; // book is (was) loaded | |
5ecdcaa7 VS |
529 | } |
530 | ||
6a63bdc8 JS |
531 | wxString title1 = title; |
532 | if (encoding != wxFONTENCODING_SYSTEM) | |
533 | { | |
534 | wxCSConv conv(encoding); | |
535 | CORRECT_STR(title1, conv); | |
536 | if (title1.IsEmpty() && !title.IsEmpty()) | |
537 | title1 = title; | |
538 | } | |
539 | ||
540 | bookr = new wxHtmlBookRecord(bookfile.GetLocation(), fsys.GetPath(), title1, deftopic); | |
d1da8872 | 541 | |
91fa114d VS |
542 | wxHtmlHelpDataItem *bookitem = new wxHtmlHelpDataItem; |
543 | bookitem->level = 0; | |
544 | bookitem->id = 0; | |
545 | bookitem->page = deftopic; | |
546 | bookitem->name = title; | |
547 | bookitem->book = bookr; | |
8ec2b484 HH |
548 | |
549 | // store the contents index for later | |
91fa114d VS |
550 | int cont_start = m_contents.size(); |
551 | ||
552 | m_contents.Add(bookitem); | |
8ec2b484 HH |
553 | |
554 | // Try to find cached binary versions: | |
f35822af VS |
555 | // 1. save file as book, but with .hhp.cached extension |
556 | // 2. same as 1. but in temp path | |
557 | // 3. otherwise or if cache load failed, load it from MS. | |
04dbb646 | 558 | |
f35822af | 559 | fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached")); |
04dbb646 VZ |
560 | |
561 | if (fi == NULL || | |
e2b87f38 | 562 | #if wxUSE_DATETIME |
04dbb646 | 563 | fi->GetModificationTime() < bookfile.GetModificationTime() || |
e2b87f38 | 564 | #endif // wxUSE_DATETIME |
4f9297b0 | 565 | !LoadCachedBook(bookr, fi->GetStream())) |
f35822af VS |
566 | { |
567 | if (fi != NULL) delete fi; | |
91fa114d VS |
568 | fi = fsys.OpenFile(m_tempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached")); |
569 | if (m_tempPath.empty() || fi == NULL || | |
e2b87f38 | 570 | #if wxUSE_DATETIME |
04dbb646 | 571 | fi->GetModificationTime() < bookfile.GetModificationTime() || |
e2b87f38 | 572 | #endif // wxUSE_DATETIME |
4f9297b0 | 573 | !LoadCachedBook(bookr, fi->GetStream())) |
f35822af VS |
574 | { |
575 | LoadMSProject(bookr, fsys, indexfile, contfile); | |
91fa114d | 576 | if (!m_tempPath.empty()) |
f35822af | 577 | { |
91fa114d | 578 | wxFileOutputStream *outs = new wxFileOutputStream(m_tempPath + |
29e60597 | 579 | SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached")); |
f35822af VS |
580 | SaveCachedBook(bookr, outs); |
581 | delete outs; | |
582 | } | |
d5bb85a0 | 583 | } |
8ec2b484 | 584 | } |
04dbb646 | 585 | |
f35822af | 586 | if (fi != NULL) delete fi; |
8ec2b484 HH |
587 | |
588 | // Now store the contents range | |
91fa114d | 589 | bookr->SetContentsRange(cont_start, m_contents.size()); |
04dbb646 | 590 | |
3912b3f8 VS |
591 | // MS HTML Help files [written by MS HTML Help Workshop] are broken |
592 | // in that the data are iso-8859-1 (including HTML entities), but must | |
593 | // be interpreted as being in language's windows charset. Correct the | |
594 | // differences here and also convert to wxConvLocal in ANSI build | |
67c276bd | 595 | if (encoding != wxFONTENCODING_SYSTEM) |
3912b3f8 | 596 | { |
67c276bd | 597 | wxCSConv conv(encoding); |
91fa114d VS |
598 | size_t IndexCnt = m_index.size(); |
599 | size_t ContentsCnt = m_contents.size(); | |
600 | size_t i; | |
601 | for (i = IndexOld; i < IndexCnt; i++) | |
f890e2d4 | 602 | { |
91fa114d | 603 | CORRECT_STR(m_index[i].name, conv); |
f890e2d4 | 604 | } |
91fa114d | 605 | for (i = ContentsOld; i < ContentsCnt; i++) |
3912b3f8 | 606 | { |
91fa114d | 607 | CORRECT_STR(m_contents[i].name, conv); |
3912b3f8 VS |
608 | } |
609 | #undef CORRECT_STR | |
f890e2d4 | 610 | } |
8ec2b484 | 611 | |
91fa114d VS |
612 | m_bookRecords.Add(bookr); |
613 | if (!m_index.empty()) | |
614 | { | |
615 | m_index.Sort(wxHtmlHelpIndexCompareFunc); | |
616 | } | |
f42b1601 | 617 | |
91fa114d | 618 | return true; |
8ec2b484 HH |
619 | } |
620 | ||
621 | ||
622 | bool wxHtmlHelpData::AddBook(const wxString& book) | |
623 | { | |
3527f29c VS |
624 | wxString extension(book.Right(4).Lower()); |
625 | if (extension == wxT(".zip") || | |
626 | #if wxUSE_LIBMSPACK | |
627 | extension == wxT(".chm") /*compressed html help book*/ || | |
628 | #endif | |
629 | extension == wxT(".htb") /*html book*/) | |
68364659 VS |
630 | { |
631 | wxFileSystem fsys; | |
632 | wxString s; | |
d1da8872 | 633 | bool rt = false; |
68364659 | 634 | |
3527f29c VS |
635 | #if wxUSE_LIBMSPACK |
636 | if (extension == wxT(".chm")) | |
637 | s = fsys.FindFirst(book + wxT("#chm:*.hhp"), wxFILE); | |
638 | else | |
639 | #endif | |
640 | s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE); | |
641 | ||
93763ad5 | 642 | while (!s.empty()) |
68364659 | 643 | { |
d1da8872 | 644 | if (AddBook(s)) rt = true; |
68364659 VS |
645 | s = fsys.FindNext(); |
646 | } | |
04dbb646 | 647 | |
68364659 VS |
648 | return rt; |
649 | } | |
68364659 | 650 | |
a509f830 VZ |
651 | wxFSFile *fi; |
652 | wxFileSystem fsys; | |
3dde6c72 | 653 | |
a509f830 VZ |
654 | wxString title = _("noname"), |
655 | safetitle, | |
656 | start = wxEmptyString, | |
657 | contents = wxEmptyString, | |
658 | index = wxEmptyString, | |
659 | charset = wxEmptyString; | |
68364659 | 660 | |
a509f830 VZ |
661 | fi = fsys.OpenFile(book); |
662 | if (fi == NULL) | |
663 | { | |
664 | wxLogError(_("Cannot open HTML help book: %s"), book.c_str()); | |
d1da8872 | 665 | return false; |
68364659 | 666 | } |
a509f830 VZ |
667 | fsys.ChangePathTo(book); |
668 | ||
669 | const wxChar *lineptr; | |
670 | wxChar linebuf[300]; | |
671 | wxString tmp; | |
672 | wxHtmlFilterPlainText filter; | |
673 | tmp = filter.ReadFile(*fi); | |
674 | lineptr = tmp.c_str(); | |
675 | ||
d1da8872 | 676 | do |
a509f830 VZ |
677 | { |
678 | lineptr = ReadLine(lineptr, linebuf, 300); | |
d1da8872 | 679 | |
a509f830 | 680 | for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++) |
42841dfc | 681 | *ch = (wxChar)wxTolower(*ch); |
a509f830 | 682 | |
9a83f860 VZ |
683 | if (wxStrstr(linebuf, wxT("title=")) == linebuf) |
684 | title = linebuf + wxStrlen(wxT("title=")); | |
685 | if (wxStrstr(linebuf, wxT("default topic=")) == linebuf) | |
686 | start = linebuf + wxStrlen(wxT("default topic=")); | |
687 | if (wxStrstr(linebuf, wxT("index file=")) == linebuf) | |
688 | index = linebuf + wxStrlen(wxT("index file=")); | |
689 | if (wxStrstr(linebuf, wxT("contents file=")) == linebuf) | |
690 | contents = linebuf + wxStrlen(wxT("contents file=")); | |
691 | if (wxStrstr(linebuf, wxT("charset=")) == linebuf) | |
692 | charset = linebuf + wxStrlen(wxT("charset=")); | |
a509f830 | 693 | } while (lineptr != NULL); |
d1da8872 | 694 | |
5a969262 RR |
695 | wxFontEncoding enc = wxFONTENCODING_SYSTEM; |
696 | #if wxUSE_FONTMAP | |
697 | if (charset != wxEmptyString) | |
698 | enc = wxFontMapper::Get()->CharsetToEncoding(charset); | |
699 | #endif | |
a509f830 | 700 | |
67c276bd | 701 | bool rtval = AddBookParam(*fi, enc, |
a509f830 VZ |
702 | title, contents, index, start, fsys.GetPath()); |
703 | delete fi; | |
91fa114d | 704 | |
a509f830 | 705 | return rtval; |
8ec2b484 HH |
706 | } |
707 | ||
708 | wxString wxHtmlHelpData::FindPageByName(const wxString& x) | |
709 | { | |
8ec2b484 | 710 | int i; |
8ec2b484 | 711 | |
8680ee06 RR |
712 | bool has_non_ascii = false; |
713 | wxString::const_iterator it; | |
714 | for (it = x.begin(); it != x.end(); ++it) | |
715 | { | |
716 | wxUniChar ch = *it; | |
717 | if (!ch.IsAscii()) | |
718 | { | |
719 | has_non_ascii = true; | |
720 | break; | |
721 | } | |
722 | } | |
03647350 | 723 | |
8680ee06 | 724 | int cnt = m_bookRecords.GetCount(); |
03647350 | 725 | |
8680ee06 | 726 | if (!has_non_ascii) |
4f9297b0 | 727 | { |
8680ee06 RR |
728 | wxFileSystem fsys; |
729 | wxFSFile *f; | |
730 | // 1. try to open given file: | |
731 | for (i = 0; i < cnt; i++) | |
732 | { | |
91fa114d | 733 | f = fsys.OpenFile(m_bookRecords[i].GetFullPath(x)); |
04dbb646 VZ |
734 | if (f) |
735 | { | |
91fa114d | 736 | wxString url = m_bookRecords[i].GetFullPath(x); |
8ec2b484 HH |
737 | delete f; |
738 | return url; | |
739 | } | |
8680ee06 | 740 | } |
8ec2b484 HH |
741 | } |
742 | ||
743 | ||
735a74df | 744 | // 2. try to find a book: |
04dbb646 | 745 | for (i = 0; i < cnt; i++) |
4f9297b0 | 746 | { |
91fa114d VS |
747 | if (m_bookRecords[i].GetTitle() == x) |
748 | return m_bookRecords[i].GetFullPath(m_bookRecords[i].GetStart()); | |
8ec2b484 HH |
749 | } |
750 | ||
735a74df | 751 | // 3. try to find in contents: |
91fa114d | 752 | cnt = m_contents.size(); |
04dbb646 | 753 | for (i = 0; i < cnt; i++) |
4f9297b0 | 754 | { |
91fa114d VS |
755 | if (m_contents[i].name == x) |
756 | return m_contents[i].GetFullPath(); | |
8ec2b484 HH |
757 | } |
758 | ||
759 | ||
735a74df | 760 | // 4. try to find in index: |
91fa114d | 761 | cnt = m_index.size(); |
04dbb646 | 762 | for (i = 0; i < cnt; i++) |
4f9297b0 | 763 | { |
91fa114d VS |
764 | if (m_index[i].name == x) |
765 | return m_index[i].GetFullPath(); | |
8ec2b484 HH |
766 | } |
767 | ||
735a74df VZ |
768 | // 4b. if still not found, try case-insensitive comparison |
769 | for (i = 0; i < cnt; i++) | |
770 | { | |
771 | if (m_index[i].name.CmpNoCase(x) == 0) | |
772 | return m_index[i].GetFullPath(); | |
773 | } | |
774 | ||
91fa114d | 775 | return wxEmptyString; |
8ec2b484 HH |
776 | } |
777 | ||
778 | wxString wxHtmlHelpData::FindPageById(int id) | |
f42b1601 | 779 | { |
91fa114d VS |
780 | size_t cnt = m_contents.size(); |
781 | for (size_t i = 0; i < cnt; i++) | |
4f9297b0 | 782 | { |
91fa114d | 783 | if (m_contents[i].id == id) |
04dbb646 | 784 | { |
91fa114d | 785 | return m_contents[i].GetFullPath(); |
8ec2b484 HH |
786 | } |
787 | } | |
788 | ||
91fa114d VS |
789 | return wxEmptyString; |
790 | } | |
791 | ||
91fa114d | 792 | |
8ec2b484 HH |
793 | //---------------------------------------------------------------------------------- |
794 | // wxHtmlSearchStatus functions | |
795 | //---------------------------------------------------------------------------------- | |
796 | ||
797 | wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword, | |
c4971147 | 798 | bool case_sensitive, bool whole_words_only, |
d5bb85a0 | 799 | const wxString& book) |
8ec2b484 HH |
800 | { |
801 | m_Data = data; | |
802 | m_Keyword = keyword; | |
803 | wxHtmlBookRecord* bookr = NULL; | |
04dbb646 | 804 | if (book != wxEmptyString) |
4f9297b0 | 805 | { |
d5bb85a0 | 806 | // we have to search in a specific book. Find it first |
91fa114d | 807 | int i, cnt = data->m_bookRecords.GetCount(); |
d5bb85a0 | 808 | for (i = 0; i < cnt; i++) |
91fa114d | 809 | if (data->m_bookRecords[i].GetTitle() == book) |
04dbb646 | 810 | { |
91fa114d | 811 | bookr = &(data->m_bookRecords[i]); |
d5bb85a0 VS |
812 | m_CurIndex = bookr->GetContentsStart(); |
813 | m_MaxIndex = bookr->GetContentsEnd(); | |
814 | break; | |
815 | } | |
816 | // check; we won't crash if the book doesn't exist, but it's Bad Anyway. | |
817 | wxASSERT(bookr); | |
8ec2b484 | 818 | } |
04dbb646 | 819 | if (! bookr) |
4f9297b0 | 820 | { |
d5bb85a0 VS |
821 | // no book specified; search all books |
822 | m_CurIndex = 0; | |
91fa114d | 823 | m_MaxIndex = m_Data->m_contents.size(); |
8ec2b484 | 824 | } |
c4971147 | 825 | m_Engine.LookFor(keyword, case_sensitive, whole_words_only); |
8ec2b484 | 826 | m_Active = (m_CurIndex < m_MaxIndex); |
8ec2b484 HH |
827 | } |
828 | ||
829 | bool wxHtmlSearchStatus::Search() | |
830 | { | |
8ec2b484 | 831 | wxFSFile *file; |
d5bb85a0 | 832 | int i = m_CurIndex; // shortcut |
91fa114d | 833 | bool found = false; |
3912b3f8 | 834 | wxString thepage; |
8ec2b484 | 835 | |
04dbb646 | 836 | if (!m_Active) |
4f9297b0 | 837 | { |
f35822af | 838 | // sanity check. Illegal use, but we'll try to prevent a crash anyway |
50494a55 | 839 | wxASSERT(m_Active); |
91fa114d | 840 | return false; |
8ec2b484 HH |
841 | } |
842 | ||
8ec2b484 | 843 | m_Name = wxEmptyString; |
91fa114d VS |
844 | m_CurItem = NULL; |
845 | thepage = m_Data->m_contents[i].page; | |
8ec2b484 | 846 | |
b5a7b000 VS |
847 | m_Active = (++m_CurIndex < m_MaxIndex); |
848 | // check if it is same page with different anchor: | |
3912b3f8 | 849 | if (!m_LastPage.empty()) |
b5a7b000 | 850 | { |
3912b3f8 VS |
851 | const wxChar *p1, *p2; |
852 | for (p1 = thepage.c_str(), p2 = m_LastPage.c_str(); | |
9a83f860 | 853 | *p1 != 0 && *p1 != wxT('#') && *p1 == *p2; p1++, p2++) {} |
b5a7b000 VS |
854 | |
855 | m_LastPage = thepage; | |
856 | ||
9a83f860 | 857 | if (*p1 == 0 || *p1 == wxT('#')) |
3912b3f8 | 858 | return false; |
b5a7b000 VS |
859 | } |
860 | else m_LastPage = thepage; | |
04dbb646 | 861 | |
f35822af | 862 | wxFileSystem fsys; |
91fa114d | 863 | file = fsys.OpenFile(m_Data->m_contents[i].book->GetFullPath(thepage)); |
04dbb646 | 864 | if (file) |
b5a7b000 | 865 | { |
2b5f62a0 | 866 | if (m_Engine.Scan(*file)) |
468ae730 | 867 | { |
91fa114d VS |
868 | m_Name = m_Data->m_contents[i].name; |
869 | m_CurItem = &m_Data->m_contents[i]; | |
870 | found = true; | |
d5bb85a0 VS |
871 | } |
872 | delete file; | |
8ec2b484 | 873 | } |
8ec2b484 HH |
874 | return found; |
875 | } | |
876 | ||
d5bb85a0 VS |
877 | |
878 | ||
879 | ||
880 | ||
881 | ||
882 | ||
883 | ||
884 | //-------------------------------------------------------------------------------- | |
2b5f62a0 | 885 | // wxHtmlSearchEngine |
d5bb85a0 VS |
886 | //-------------------------------------------------------------------------------- |
887 | ||
2b5f62a0 | 888 | void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only) |
d5bb85a0 | 889 | { |
c4971147 VS |
890 | m_CaseSensitive = case_sensitive; |
891 | m_WholeWords = whole_words_only; | |
3912b3f8 | 892 | m_Keyword = keyword; |
04dbb646 | 893 | |
3444e4a8 | 894 | if (!m_CaseSensitive) |
3912b3f8 | 895 | m_Keyword.LowerCase(); |
d5bb85a0 VS |
896 | } |
897 | ||
898 | ||
1a1dac18 MB |
899 | static inline bool WHITESPACE(wxChar c) |
900 | { | |
9a83f860 | 901 | return c == wxT(' ') || c == wxT('\n') || c == wxT('\r') || c == wxT('\t'); |
1a1dac18 | 902 | } |
c4971147 | 903 | |
21dc4638 VZ |
904 | // replace continuous spaces by one single space |
905 | static inline wxString CompressSpaces(const wxString & str) | |
906 | { | |
907 | wxString buf; | |
908 | buf.reserve( str.size() ); | |
909 | ||
910 | bool space_counted = false; | |
911 | for( const wxChar * pstr = str.c_str(); *pstr; ++pstr ) | |
912 | { | |
913 | wxChar ch = *pstr; | |
914 | if( WHITESPACE( ch ) ) | |
915 | { | |
916 | if( space_counted ) | |
917 | { | |
918 | continue; | |
919 | } | |
9a83f860 | 920 | ch = wxT(' '); |
21dc4638 VZ |
921 | space_counted = true; |
922 | } | |
923 | else | |
924 | { | |
925 | space_counted = false; | |
926 | } | |
927 | buf += ch; | |
928 | } | |
929 | ||
930 | return buf; | |
931 | } | |
932 | ||
2b5f62a0 | 933 | bool wxHtmlSearchEngine::Scan(const wxFSFile& file) |
d5bb85a0 | 934 | { |
3912b3f8 | 935 | wxASSERT_MSG(!m_Keyword.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!")); |
d5bb85a0 | 936 | |
2b5f62a0 | 937 | wxHtmlFilterHTML filter; |
21dc4638 | 938 | wxString bufStr = filter.ReadFile(file); |
d5bb85a0 | 939 | |
c4971147 | 940 | if (!m_CaseSensitive) |
21dc4638 | 941 | bufStr.LowerCase(); |
d5bb85a0 | 942 | |
21dc4638 VZ |
943 | { // remove html tags |
944 | wxString bufStrCopy; | |
945 | bufStrCopy.reserve( bufStr.size() ); | |
946 | bool insideTag = false; | |
947 | for (const wxChar * pBufStr = bufStr.c_str(); *pBufStr; ++pBufStr) | |
04dbb646 | 948 | { |
21dc4638 VZ |
949 | wxChar c = *pBufStr; |
950 | if (insideTag) | |
951 | { | |
9a83f860 | 952 | if (c == wxT('>')) |
21dc4638 VZ |
953 | { |
954 | insideTag = false; | |
955 | // replace the tag by an empty space | |
9a83f860 | 956 | c = wxT(' '); |
21dc4638 VZ |
957 | } |
958 | else | |
959 | continue; | |
960 | } | |
9a83f860 | 961 | else if (c == wxT('<')) |
21dc4638 VZ |
962 | { |
963 | wxChar nextCh = *(pBufStr + 1); | |
9a83f860 | 964 | if (nextCh == wxT('/') || !WHITESPACE(nextCh)) |
21dc4638 VZ |
965 | { |
966 | insideTag = true; | |
967 | continue; | |
968 | } | |
969 | } | |
970 | bufStrCopy += c; | |
c4971147 | 971 | } |
21dc4638 | 972 | bufStr.swap( bufStrCopy ); |
c4971147 | 973 | } |
04dbb646 | 974 | |
21dc4638 VZ |
975 | wxString keyword = m_Keyword; |
976 | ||
977 | if (m_WholeWords) | |
c4971147 | 978 | { |
21dc4638 | 979 | // insert ' ' at the beginning and at the end |
9a83f860 VZ |
980 | keyword.insert( 0, wxT(" ") ); |
981 | keyword.append( wxT(" ") ); | |
982 | bufStr.insert( 0, wxT(" ") ); | |
983 | bufStr.append( wxT(" ") ); | |
d5bb85a0 VS |
984 | } |
985 | ||
21dc4638 VZ |
986 | // remove continuous spaces |
987 | keyword = CompressSpaces( keyword ); | |
988 | bufStr = CompressSpaces( bufStr ); | |
d5bb85a0 | 989 | |
21dc4638 VZ |
990 | // finally do the search |
991 | return bufStr.find( keyword ) != wxString::npos; | |
992 | } | |
d5bb85a0 | 993 | |
8ec2b484 | 994 | #endif |