]>
Commit | Line | Data |
---|---|---|
8ec2b484 HH |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpdata.cpp | |
3 | // Purpose: wxHtmlHelpData | |
f42b1601 | 4 | // Notes: Based on htmlhelp.cpp, implementing a monolithic |
8ec2b484 HH |
5 | // HTML Help controller class, by Vaclav Slavik |
6 | // Author: Harm van der Heijden and Vaclav Slavik | |
69941f05 | 7 | // RCS-ID: $Id$ |
8ec2b484 HH |
8 | // Copyright: (c) Harm van der Heijden and Vaclav Slavik |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
69941f05 | 13 | #pragma implementation |
8ec2b484 HH |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #include "wx/defs.h" | |
24 | ||
25 | #if wxUSE_HTML | |
26 | ||
27 | #ifndef WXPRECOMP | |
28 | #include "wx/wx.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/html/helpdata.h" | |
32 | #include "wx/tokenzr.h" | |
33 | #include "wx/wfstream.h" | |
34 | #include "wx/busyinfo.h" | |
69941f05 | 35 | #include "wx/html/htmlpars.h" |
8ec2b484 HH |
36 | #include "wx/html/htmldefs.h" |
37 | ||
38 | #include "wx/arrimpl.cpp" | |
39 | WX_DEFINE_OBJARRAY(wxHtmlBookRecArray) | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // static helper functions | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | // Reads one line, stores it into buf and returns pointer to new line or NULL. | |
46 | static char* ReadLine(char *line, char *buf) | |
47 | { | |
48 | char *writeptr = buf, *readptr = line; | |
49 | ||
50 | while (*readptr != 0 && *readptr != '\r' && *readptr != '\n') *(writeptr++) = *(readptr++); | |
51 | *writeptr = 0; | |
52 | while (*readptr == '\r' || *readptr == '\n') readptr++; | |
53 | if (*readptr == 0) return NULL; | |
54 | else return readptr; | |
55 | } | |
56 | ||
57 | ||
58 | static wxString SafeFileName(const wxString& s) | |
59 | { | |
60 | wxString res = s; | |
66a77a74 OK |
61 | res.Replace(wxT(":"), wxT("_"), TRUE); |
62 | res.Replace(wxT(" "), wxT("_"), TRUE); | |
63 | res.Replace(wxT("/"), wxT("_"), TRUE); | |
64 | res.Replace(wxT("\\"), wxT("_"), TRUE); | |
65 | res.Replace(wxT("#"), wxT("_"), TRUE); | |
66 | res.Replace(wxT("."), wxT("_"), TRUE); | |
8ec2b484 HH |
67 | return res; |
68 | } | |
69 | ||
70 | ||
16a12a3d | 71 | static int LINKAGEMODE IndexCompareFunc(const void *a, const void *b) |
8ec2b484 | 72 | { |
66a77a74 | 73 | return wxStrcmp(((wxHtmlContentsItem*)a) -> m_Name, ((wxHtmlContentsItem*)b) -> m_Name); |
8ec2b484 HH |
74 | } |
75 | ||
76 | ||
77 | //----------------------------------------------------------------------------- | |
78 | // HP_Parser | |
79 | //----------------------------------------------------------------------------- | |
80 | ||
81 | class HP_Parser : public wxHtmlParser | |
82 | { | |
83 | public: | |
0c5d3e1c VZ |
84 | void AddText(const char* WXUNUSED(text)) { } |
85 | wxObject* GetProduct() { return NULL; } | |
8ec2b484 HH |
86 | }; |
87 | ||
88 | ||
89 | //----------------------------------------------------------------------------- | |
90 | // HP_TagHandler | |
91 | //----------------------------------------------------------------------------- | |
92 | ||
93 | class HP_TagHandler : public wxHtmlTagHandler | |
94 | { | |
95 | private: | |
96 | wxString m_Name, m_Page; | |
97 | int m_Level; | |
d5bb85a0 | 98 | int m_ID; |
8ec2b484 HH |
99 | int m_Index; |
100 | wxHtmlContentsItem *m_Items; | |
101 | int m_ItemsCnt; | |
102 | wxHtmlBookRecord *m_Book; | |
103 | ||
104 | public: | |
d5bb85a0 | 105 | HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler() {m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; m_Level = 0; } |
66a77a74 | 106 | wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); } |
8ec2b484 HH |
107 | bool HandleTag(const wxHtmlTag& tag); |
108 | void WriteOut(wxHtmlContentsItem*& array, int& size); | |
109 | void ReadIn(wxHtmlContentsItem* array, int size); | |
110 | }; | |
111 | ||
112 | ||
113 | bool HP_TagHandler::HandleTag(const wxHtmlTag& tag) | |
114 | { | |
115 | if (tag.GetName() == "UL") { | |
116 | m_Level++; | |
117 | ParseInner(tag); | |
118 | m_Level--; | |
119 | return TRUE; | |
120 | } | |
66a77a74 | 121 | else if (tag.GetName() == wxT("OBJECT")) { |
8ec2b484 HH |
122 | m_Name = m_Page = wxEmptyString; |
123 | ParseInner(tag); | |
124 | if (m_Page != wxEmptyString) { | |
efba2b89 VS |
125 | if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0) |
126 | m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem)); | |
8ec2b484 HH |
127 | m_Items[m_ItemsCnt].m_Level = m_Level; |
128 | m_Items[m_ItemsCnt].m_ID = m_ID; | |
66a77a74 OK |
129 | m_Items[m_ItemsCnt].m_Page = new wxChar[m_Page.Length() + 1]; |
130 | wxStrcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str()); | |
131 | m_Items[m_ItemsCnt].m_Name = new wxChar [m_Name.Length() + 1]; | |
132 | wxStrcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str()); | |
8ec2b484 HH |
133 | m_Items[m_ItemsCnt].m_Book = m_Book; |
134 | m_ItemsCnt++; | |
135 | } | |
136 | return TRUE; | |
137 | } | |
8ec2b484 | 138 | else { // "PARAM" |
66a77a74 OK |
139 | if (m_Name == wxEmptyString && tag.GetParam(wxT("NAME")) == wxT("Name")) m_Name = tag.GetParam(wxT("VALUE")); |
140 | if (tag.GetParam(wxT("NAME")) == wxT("Local")) m_Page = tag.GetParam(wxT("VALUE")); | |
141 | if (tag.GetParam(wxT("NAME")) == wxT("ID")) tag.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID); | |
8ec2b484 HH |
142 | return FALSE; |
143 | } | |
144 | } | |
145 | ||
146 | ||
147 | ||
148 | void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size) | |
149 | { | |
150 | array = m_Items; | |
151 | size = m_ItemsCnt; | |
152 | m_Items = NULL; | |
153 | m_ItemsCnt = 0; | |
154 | } | |
155 | ||
156 | void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size) | |
157 | { | |
158 | m_Items = array; | |
159 | m_ItemsCnt = size; | |
160 | } | |
161 | ||
d5bb85a0 VS |
162 | |
163 | ||
164 | ||
8ec2b484 HH |
165 | //----------------------------------------------------------------------------- |
166 | // wxHtmlHelpData | |
167 | //----------------------------------------------------------------------------- | |
168 | ||
169 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject) | |
170 | ||
f42b1601 | 171 | wxHtmlHelpData::wxHtmlHelpData() |
8ec2b484 HH |
172 | { |
173 | m_TempPath = wxEmptyString; | |
174 | ||
175 | m_Contents = NULL; | |
176 | m_ContentsCnt = 0; | |
177 | m_Index = NULL; | |
178 | m_IndexCnt = 0; | |
179 | } | |
180 | ||
181 | wxHtmlHelpData::~wxHtmlHelpData() | |
182 | { | |
183 | int i; | |
184 | ||
185 | m_BookRecords.Empty(); | |
186 | if (m_Contents) { | |
187 | for (i = 0; i < m_ContentsCnt; i++) { | |
188 | delete[] m_Contents[i].m_Page; | |
189 | delete[] m_Contents[i].m_Name; | |
190 | } | |
191 | free(m_Contents); | |
192 | } | |
193 | if (m_Index) { | |
194 | for (i = 0; i < m_IndexCnt; i++) { | |
195 | delete[] m_Index[i].m_Page; | |
196 | delete[] m_Index[i].m_Name; | |
197 | } | |
198 | free(m_Index); | |
199 | } | |
200 | } | |
201 | ||
202 | bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile) | |
203 | { | |
204 | wxFSFile *f; | |
205 | char *buf; | |
206 | int sz; | |
207 | wxString string; | |
f42b1601 | 208 | |
8ec2b484 HH |
209 | HP_Parser parser; |
210 | HP_TagHandler *handler = new HP_TagHandler(book); | |
211 | parser.AddTagHandler(handler); | |
212 | ||
af111fc3 | 213 | f = ( contentsfile.IsEmpty() ? 0 : fsys.OpenFile(contentsfile) ); |
8ec2b484 HH |
214 | if (f) { |
215 | sz = f -> GetStream() -> GetSize(); | |
d5bb85a0 | 216 | buf = new char[sz + 1]; |
8ec2b484 HH |
217 | buf[sz] = 0; |
218 | f -> GetStream() -> Read(buf, sz); | |
219 | delete f; | |
220 | handler -> ReadIn(m_Contents, m_ContentsCnt); | |
221 | parser.Parse(buf); | |
222 | handler -> WriteOut(m_Contents, m_ContentsCnt); | |
223 | delete[] buf; | |
224 | } | |
225 | ||
af111fc3 | 226 | f = ( indexfile.IsEmpty() ? 0 : fsys.OpenFile(indexfile) ); |
8ec2b484 HH |
227 | if (f) { |
228 | sz = f -> GetStream() -> GetSize(); | |
d5bb85a0 | 229 | buf = new char[sz + 1]; |
8ec2b484 HH |
230 | buf[sz] = 0; |
231 | f -> GetStream() -> Read(buf, sz); | |
232 | delete f; | |
233 | handler -> ReadIn(m_Index, m_IndexCnt); | |
234 | parser.Parse(buf); | |
235 | handler -> WriteOut(m_Index, m_IndexCnt); | |
236 | delete[] buf; | |
237 | } | |
238 | return TRUE; | |
239 | } | |
240 | ||
241 | ||
242 | bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f) | |
243 | { | |
244 | int i, st; | |
245 | int x; | |
246 | ||
247 | /* load contents : */ | |
248 | ||
249 | f -> Read(&x, sizeof(x)); | |
250 | st = m_ContentsCnt; | |
251 | m_ContentsCnt += x; | |
efba2b89 | 252 | m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) * wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem)); |
8ec2b484 HH |
253 | for (i = st; i < m_ContentsCnt; i++) { |
254 | f -> Read(&x, sizeof(x)); | |
255 | m_Contents[i].m_Level = x; | |
256 | f -> Read(&x, sizeof(x)); | |
257 | m_Contents[i].m_ID = x; | |
258 | f -> Read(&x, sizeof(x)); | |
66a77a74 OK |
259 | m_Contents[i].m_Name = new wxChar[x]; |
260 | f -> Read(m_Contents[i].m_Name, x*sizeof(wxChar)); | |
8ec2b484 | 261 | f -> Read(&x, sizeof(x)); |
66a77a74 OK |
262 | m_Contents[i].m_Page = new wxChar[x]; |
263 | f -> Read(m_Contents[i].m_Page, x*sizeof(wxChar)); | |
8ec2b484 HH |
264 | m_Contents[i].m_Book = book; |
265 | } | |
266 | ||
267 | /* load index : */ | |
268 | ||
269 | f -> Read(&x, sizeof(x)); | |
270 | st = m_IndexCnt; | |
271 | m_IndexCnt += x; | |
efba2b89 | 272 | m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) * wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem)); |
8ec2b484 HH |
273 | for (i = st; i < m_IndexCnt; i++) { |
274 | f -> Read(&x, sizeof(x)); | |
66a77a74 OK |
275 | m_Index[i].m_Name = new wxChar[x]; |
276 | f -> Read(m_Index[i].m_Name, x*sizeof(wxChar)); | |
8ec2b484 | 277 | f -> Read(&x, sizeof(x)); |
66a77a74 OK |
278 | m_Index[i].m_Page = new wxChar[x]; |
279 | f -> Read(m_Index[i].m_Page, x*sizeof(wxChar)); | |
8ec2b484 HH |
280 | m_Index[i].m_Book = book; |
281 | } | |
282 | return TRUE; | |
283 | } | |
284 | ||
285 | ||
286 | bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f) | |
287 | { | |
288 | int i; | |
289 | int x; | |
290 | ||
291 | /* save contents : */ | |
292 | ||
293 | x = 0; | |
294 | for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++; | |
295 | f -> Write(&x, sizeof(x)); | |
296 | for (i = 0; i < m_ContentsCnt; i++) { | |
297 | if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue; | |
298 | x = m_Contents[i].m_Level; | |
299 | f -> Write(&x, sizeof(x)); | |
300 | x = m_Contents[i].m_ID; | |
301 | f -> Write(&x, sizeof(x)); | |
66a77a74 | 302 | x = wxStrlen(m_Contents[i].m_Name) + 1; |
8ec2b484 | 303 | f -> Write(&x, sizeof(x)); |
66a77a74 OK |
304 | f -> Write(m_Contents[i].m_Name, x*sizeof(wxChar)); |
305 | x = wxStrlen(m_Contents[i].m_Page) + 1; | |
8ec2b484 | 306 | f -> Write(&x, sizeof(x)); |
66a77a74 | 307 | f -> Write(m_Contents[i].m_Page, x*sizeof(wxChar)); |
8ec2b484 HH |
308 | } |
309 | ||
310 | /* save index : */ | |
311 | ||
312 | x = 0; | |
313 | for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++; | |
314 | f -> Write(&x, sizeof(x)); | |
315 | for (i = 0; i < m_IndexCnt; i++) { | |
316 | if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue; | |
66a77a74 | 317 | x = wxStrlen(m_Index[i].m_Name) + 1; |
8ec2b484 | 318 | f -> Write(&x, sizeof(x)); |
66a77a74 OK |
319 | f -> Write(m_Index[i].m_Name, x*sizeof(wxChar)); |
320 | x = wxStrlen(m_Index[i].m_Page) + 1; | |
8ec2b484 | 321 | f -> Write(&x, sizeof(x)); |
66a77a74 | 322 | f -> Write(m_Index[i].m_Page, x*sizeof(wxChar)); |
8ec2b484 HH |
323 | } |
324 | return TRUE; | |
325 | } | |
326 | ||
327 | ||
328 | void wxHtmlHelpData::SetTempDir(const wxString& path) | |
329 | { | |
330 | if (path == wxEmptyString) m_TempPath = path; | |
331 | else { | |
d5bb85a0 VS |
332 | if (wxIsAbsolutePath(path)) m_TempPath = path; |
333 | else m_TempPath = wxGetCwd() + "/" + path; | |
8ec2b484 | 334 | |
d5bb85a0 | 335 | if (m_TempPath[m_TempPath.Length() - 1] != '/') |
8ec2b484 HH |
336 | m_TempPath << "/"; |
337 | } | |
338 | } | |
339 | ||
340 | ||
f42b1601 | 341 | bool wxHtmlHelpData::AddBookParam(const wxString& title, const wxString& contfile, |
d5bb85a0 VS |
342 | const wxString& indexfile, const wxString& deftopic, |
343 | const wxString& path) | |
8ec2b484 HH |
344 | { |
345 | wxFileSystem fsys; | |
346 | wxFSFile *fi; | |
347 | wxHtmlBookRecord *bookr; | |
348 | wxString safetitle; | |
f42b1601 | 349 | |
8ec2b484 | 350 | if (! path.IsEmpty()) |
d5bb85a0 | 351 | fsys.ChangePathTo(path, TRUE); |
8ec2b484 | 352 | |
d5bb85a0 | 353 | bookr = new wxHtmlBookRecord(path + '/', title, deftopic); |
8ec2b484 | 354 | |
efba2b89 VS |
355 | if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0) |
356 | m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem)); | |
8ec2b484 HH |
357 | m_Contents[m_ContentsCnt].m_Level = 0; |
358 | m_Contents[m_ContentsCnt].m_ID = 0; | |
66a77a74 OK |
359 | m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1]; |
360 | wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str()); | |
361 | m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1]; | |
362 | wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str()); | |
8ec2b484 HH |
363 | m_Contents[m_ContentsCnt].m_Book = bookr; |
364 | ||
365 | // store the contents index for later | |
366 | int cont_start = m_ContentsCnt++; | |
367 | ||
368 | // Try to find cached binary versions: | |
369 | safetitle = SafeFileName(title); | |
370 | fi = fsys.OpenFile(safetitle + ".cached"); | |
371 | if (fi == NULL) fi = fsys.OpenFile(m_TempPath + safetitle + ".cached"); | |
372 | if ((fi == NULL) || (m_TempPath == wxEmptyString)) { | |
373 | LoadMSProject(bookr, fsys, indexfile, contfile); | |
374 | if (m_TempPath != wxEmptyString) { | |
d5bb85a0 | 375 | wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath + safetitle + ".cached"); |
8ec2b484 HH |
376 | SaveCachedBook(bookr, outs); |
377 | delete outs; | |
d5bb85a0 VS |
378 | } |
379 | } else { | |
8ec2b484 HH |
380 | LoadCachedBook(bookr, fi -> GetStream()); |
381 | delete fi; | |
382 | } | |
383 | ||
384 | // Now store the contents range | |
385 | bookr->SetContentsRange(cont_start, m_ContentsCnt); | |
386 | ||
387 | m_BookRecords.Add(bookr); | |
388 | if (m_IndexCnt > 0) | |
389 | qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), IndexCompareFunc); | |
f42b1601 | 390 | |
8ec2b484 HH |
391 | return TRUE; |
392 | } | |
393 | ||
394 | ||
395 | bool wxHtmlHelpData::AddBook(const wxString& book) | |
396 | { | |
397 | wxFSFile *fi; | |
398 | wxFileSystem fsys; | |
399 | wxInputStream *s; | |
400 | wxString bookFull; | |
401 | ||
402 | int sz; | |
403 | char *buff, *lineptr; | |
404 | char linebuf[300]; | |
f42b1601 | 405 | |
8ec2b484 | 406 | wxString title = _("noname"), |
d5bb85a0 VS |
407 | safetitle, |
408 | start = wxEmptyString, | |
409 | contents = wxEmptyString, index = wxEmptyString; | |
f42b1601 | 410 | |
8ec2b484 HH |
411 | if (wxIsAbsolutePath(book)) bookFull = book; |
412 | else bookFull = wxGetCwd() + "/" + book; | |
413 | ||
414 | fi = fsys.OpenFile(bookFull); | |
415 | if (fi == NULL) return FALSE; | |
416 | fsys.ChangePathTo(bookFull); | |
417 | s = fi -> GetStream(); | |
418 | sz = s -> GetSize(); | |
d5bb85a0 | 419 | buff = new char[sz + 1]; |
8ec2b484 HH |
420 | buff[sz] = 0; |
421 | s -> Read(buff, sz); | |
422 | lineptr = buff; | |
423 | delete fi; | |
424 | ||
d5bb85a0 VS |
425 | do { |
426 | lineptr = ReadLine(lineptr, linebuf); | |
427 | ||
8ec2b484 HH |
428 | if (strstr(linebuf, "Title=") == linebuf) |
429 | title = linebuf + strlen("Title="); | |
430 | if (strstr(linebuf, "Default topic=") == linebuf) | |
431 | start = linebuf + strlen("Default topic="); | |
432 | if (strstr(linebuf, "Index file=") == linebuf) | |
433 | index = linebuf + strlen("Index file="); | |
434 | if (strstr(linebuf, "Contents file=") == linebuf) | |
435 | contents = linebuf + strlen("Contents file="); | |
d5bb85a0 | 436 | } while (lineptr != NULL); |
8ec2b484 HH |
437 | delete[] buff; |
438 | ||
439 | return AddBookParam(title, contents, index, start, fsys.GetPath()); | |
440 | } | |
441 | ||
442 | wxString wxHtmlHelpData::FindPageByName(const wxString& x) | |
443 | { | |
444 | int cnt; | |
445 | int i; | |
446 | wxFileSystem fsys; | |
447 | wxFSFile *f; | |
448 | wxString url(wxEmptyString); | |
449 | ||
450 | /* 1. try to open given file: */ | |
451 | ||
452 | cnt = m_BookRecords.GetCount(); | |
453 | for (i = 0; i < cnt; i++) { | |
454 | f = fsys.OpenFile(m_BookRecords[i].GetBasePath() + x); | |
455 | if (f) { | |
456 | url = m_BookRecords[i].GetBasePath() + x; | |
457 | delete f; | |
458 | return url; | |
459 | } | |
460 | } | |
461 | ||
462 | ||
463 | /* 2. try to find a book: */ | |
464 | ||
465 | for (i = 0; i < cnt; i++) { | |
466 | if (m_BookRecords[i].GetTitle() == x) { | |
467 | url = m_BookRecords[i].GetBasePath() + m_BookRecords[i].GetStart(); | |
468 | return url; | |
469 | } | |
470 | } | |
471 | ||
472 | /* 3. try to find in contents: */ | |
473 | ||
474 | cnt = m_ContentsCnt; | |
475 | for (i = 0; i < cnt; i++) { | |
66a77a74 | 476 | if (wxStrcmp(m_Contents[i].m_Name, x) == 0) { |
8ec2b484 HH |
477 | url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page; |
478 | return url; | |
479 | } | |
480 | } | |
481 | ||
482 | ||
483 | /* 4. try to find in index: */ | |
484 | ||
485 | cnt = m_IndexCnt; | |
486 | for (i = 0; i < cnt; i++) { | |
66a77a74 | 487 | if (wxStrcmp(m_Index[i].m_Name, x) == 0) { |
8ec2b484 HH |
488 | url = m_Index[i].m_Book -> GetBasePath() + m_Index[i].m_Page; |
489 | return url; | |
490 | } | |
491 | } | |
492 | ||
493 | return url; | |
494 | } | |
495 | ||
496 | wxString wxHtmlHelpData::FindPageById(int id) | |
f42b1601 | 497 | { |
8ec2b484 HH |
498 | int i; |
499 | wxString url(wxEmptyString); | |
500 | ||
501 | for (i = 0; i < m_ContentsCnt; i++) { | |
502 | if (m_Contents[i].m_ID == id) { | |
503 | url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page; | |
504 | return url; | |
505 | } | |
506 | } | |
507 | ||
508 | return url; | |
509 | } | |
510 | ||
511 | //---------------------------------------------------------------------------------- | |
512 | // wxHtmlSearchStatus functions | |
513 | //---------------------------------------------------------------------------------- | |
514 | ||
515 | wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword, | |
d5bb85a0 | 516 | const wxString& book) |
8ec2b484 HH |
517 | { |
518 | m_Data = data; | |
519 | m_Keyword = keyword; | |
520 | wxHtmlBookRecord* bookr = NULL; | |
521 | if (book != wxEmptyString) { | |
d5bb85a0 VS |
522 | // we have to search in a specific book. Find it first |
523 | int i, cnt = data->m_BookRecords.GetCount(); | |
524 | for (i = 0; i < cnt; i++) | |
525 | if (data->m_BookRecords[i].GetTitle() == book) { | |
526 | bookr = &(data->m_BookRecords[i]); | |
527 | m_CurIndex = bookr->GetContentsStart(); | |
528 | m_MaxIndex = bookr->GetContentsEnd(); | |
529 | break; | |
530 | } | |
531 | // check; we won't crash if the book doesn't exist, but it's Bad Anyway. | |
532 | wxASSERT(bookr); | |
8ec2b484 HH |
533 | } |
534 | if (! bookr) { | |
d5bb85a0 VS |
535 | // no book specified; search all books |
536 | m_CurIndex = 0; | |
537 | m_MaxIndex = m_Data->m_ContentsCnt; | |
8ec2b484 HH |
538 | } |
539 | m_Engine.LookFor(keyword); | |
540 | m_Active = (m_CurIndex < m_MaxIndex); | |
541 | m_LastPage = wxEmptyString; | |
542 | } | |
543 | ||
544 | bool wxHtmlSearchStatus::Search() | |
545 | { | |
546 | wxFileSystem fsys; | |
547 | wxFSFile *file; | |
d5bb85a0 | 548 | int i = m_CurIndex; // shortcut |
8ec2b484 HH |
549 | bool found = FALSE; |
550 | ||
551 | if (! m_Active) { | |
552 | // sanity check. Illegal use, but we'll try to prevent a crash anyway | |
16a12a3d DW |
553 | #if !defined(__VISAGECPP__) |
554 | wxASSERT(0); | |
555 | #else | |
556 | wxASSERT(m_Active); | |
557 | #endif | |
8ec2b484 HH |
558 | return FALSE; |
559 | } | |
560 | ||
561 | m_ContentsItem = NULL; | |
562 | m_Name = wxEmptyString; | |
563 | ||
f42b1601 | 564 | file = fsys.OpenFile(m_Data->m_Contents[i].m_Book -> GetBasePath() + |
d5bb85a0 | 565 | m_Data->m_Contents[i].m_Page); |
8ec2b484 | 566 | if (file) { |
d5bb85a0 VS |
567 | if (m_LastPage != file->GetLocation()) { |
568 | m_LastPage = file->GetLocation(); | |
569 | if (m_Engine.Scan(file -> GetStream())) { | |
570 | m_Name = m_Data->m_Contents[i].m_Name; | |
571 | m_ContentsItem = m_Data->m_Contents + i; | |
572 | found = TRUE; | |
573 | } | |
574 | } | |
575 | delete file; | |
8ec2b484 HH |
576 | } |
577 | m_Active = (++m_CurIndex < m_MaxIndex); | |
578 | return found; | |
579 | } | |
580 | ||
d5bb85a0 VS |
581 | |
582 | ||
583 | ||
584 | ||
585 | ||
586 | ||
587 | ||
588 | //-------------------------------------------------------------------------------- | |
589 | // wxSearchEngine | |
590 | //-------------------------------------------------------------------------------- | |
591 | ||
592 | void wxSearchEngine::LookFor(const wxString& keyword) | |
593 | { | |
594 | if (m_Keyword) delete[] m_Keyword; | |
66a77a74 OK |
595 | m_Keyword = new wxChar[keyword.Length() + 1]; |
596 | wxStrcpy(m_Keyword, keyword.c_str()); | |
597 | for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--) | |
598 | if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z'))) | |
599 | m_Keyword[i] += wxT('a') - wxT('A'); | |
d5bb85a0 VS |
600 | } |
601 | ||
602 | ||
603 | ||
604 | bool wxSearchEngine::Scan(wxInputStream *stream) | |
605 | { | |
606 | wxASSERT_MSG(m_Keyword != NULL, _("wxSearchEngine::LookFor must be called before scanning!")); | |
607 | ||
608 | int i, j; | |
609 | int lng = stream ->GetSize(); | |
66a77a74 | 610 | int wrd = wxStrlen(m_Keyword); |
d5bb85a0 VS |
611 | bool found = FALSE; |
612 | char *buf = new char[lng + 1]; | |
613 | stream -> Read(buf, lng); | |
614 | buf[lng] = 0; | |
615 | ||
616 | for (i = 0; i < lng; i++) | |
617 | if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A'; | |
618 | ||
619 | for (i = 0; i < lng - wrd; i++) { | |
620 | j = 0; | |
621 | while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++; | |
622 | if (j == wrd) {found = TRUE; break; } | |
623 | } | |
624 | ||
625 | delete[] buf; | |
626 | return found; | |
627 | } | |
628 | ||
629 | ||
630 | ||
631 | ||
8ec2b484 | 632 | #endif |