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