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