]> git.saurik.com Git - wxWidgets.git/blob - src/html/helpdata.cpp
5620880bbd712132207c34e262fa2ccd68adae1a
[wxWidgets.git] / src / html / helpdata.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpdata.cpp
3 // Purpose: wxHtmlHelpData
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
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"
35 #include "wx/html/htmlpars.h"
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;
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);
67 return res;
68 }
69
70
71 static int LINKAGEMODE IndexCompareFunc(const void *a, const void *b)
72 {
73 return wxStrcmp(((wxHtmlContentsItem*)a) -> m_Name, ((wxHtmlContentsItem*)b) -> m_Name);
74 }
75
76
77 //-----------------------------------------------------------------------------
78 // HP_Parser
79 //-----------------------------------------------------------------------------
80
81 class HP_Parser : public wxHtmlParser
82 {
83 public:
84 void AddText(const char* WXUNUSED(text)) { }
85 wxObject* GetProduct() { return NULL; }
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;
98 int m_ID;
99 int m_Index;
100 wxHtmlContentsItem *m_Items;
101 int m_ItemsCnt;
102 wxHtmlBookRecord *m_Book;
103
104 public:
105 HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler() {m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; m_Level = 0; }
106 wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
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() == wxT("UL")) {
116 m_Level++;
117 ParseInner(tag);
118 m_Level--;
119 return TRUE;
120 }
121 else if (tag.GetName() == wxT("OBJECT")) {
122 m_Name = m_Page = wxEmptyString;
123 ParseInner(tag);
124 if (m_Page != wxEmptyString) {
125 if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0)
126 m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
127 m_Items[m_ItemsCnt].m_Level = m_Level;
128 m_Items[m_ItemsCnt].m_ID = m_ID;
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());
133 m_Items[m_ItemsCnt].m_Book = m_Book;
134 m_ItemsCnt++;
135 }
136 return TRUE;
137 }
138 else { // "PARAM"
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);
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
162
163
164
165 //-----------------------------------------------------------------------------
166 // wxHtmlHelpData
167 //-----------------------------------------------------------------------------
168
169 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
170
171 wxHtmlHelpData::wxHtmlHelpData()
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;
208
209 HP_Parser parser;
210 HP_TagHandler *handler = new HP_TagHandler(book);
211 parser.AddTagHandler(handler);
212
213 f = ( contentsfile.IsEmpty() ? 0 : fsys.OpenFile(contentsfile) );
214 if (f) {
215 sz = f -> GetStream() -> GetSize();
216 buf = new char[sz + 1];
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
226 f = ( indexfile.IsEmpty() ? 0 : fsys.OpenFile(indexfile) );
227 if (f) {
228 sz = f -> GetStream() -> GetSize();
229 buf = new char[sz + 1];
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;
252 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) * wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
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));
259 m_Contents[i].m_Name = new wxChar[x];
260 f -> Read(m_Contents[i].m_Name, x*sizeof(wxChar));
261 f -> Read(&x, sizeof(x));
262 m_Contents[i].m_Page = new wxChar[x];
263 f -> Read(m_Contents[i].m_Page, x*sizeof(wxChar));
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;
272 m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) * wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
273 for (i = st; i < m_IndexCnt; i++) {
274 f -> Read(&x, sizeof(x));
275 m_Index[i].m_Name = new wxChar[x];
276 f -> Read(m_Index[i].m_Name, x*sizeof(wxChar));
277 f -> Read(&x, sizeof(x));
278 m_Index[i].m_Page = new wxChar[x];
279 f -> Read(m_Index[i].m_Page, x*sizeof(wxChar));
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));
302 x = wxStrlen(m_Contents[i].m_Name) + 1;
303 f -> Write(&x, sizeof(x));
304 f -> Write(m_Contents[i].m_Name, x*sizeof(wxChar));
305 x = wxStrlen(m_Contents[i].m_Page) + 1;
306 f -> Write(&x, sizeof(x));
307 f -> Write(m_Contents[i].m_Page, x*sizeof(wxChar));
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;
317 x = wxStrlen(m_Index[i].m_Name) + 1;
318 f -> Write(&x, sizeof(x));
319 f -> Write(m_Index[i].m_Name, x*sizeof(wxChar));
320 x = wxStrlen(m_Index[i].m_Page) + 1;
321 f -> Write(&x, sizeof(x));
322 f -> Write(m_Index[i].m_Page, x*sizeof(wxChar));
323 }
324 return TRUE;
325 }
326
327
328 void wxHtmlHelpData::SetTempDir(const wxString& path)
329 {
330 if (path == wxEmptyString) m_TempPath = path;
331 else {
332 if (wxIsAbsolutePath(path)) m_TempPath = path;
333 else m_TempPath = wxGetCwd() + "/" + path;
334
335 if (m_TempPath[m_TempPath.Length() - 1] != '/')
336 m_TempPath << "/";
337 }
338 }
339
340
341 bool wxHtmlHelpData::AddBookParam(const wxString& title, const wxString& contfile,
342 const wxString& indexfile, const wxString& deftopic,
343 const wxString& path)
344 {
345 wxFileSystem fsys;
346 wxFSFile *fi;
347 wxHtmlBookRecord *bookr;
348 wxString safetitle;
349
350 if (! path.IsEmpty())
351 fsys.ChangePathTo(path, TRUE);
352
353 bookr = new wxHtmlBookRecord(fsys.GetPath(), title, deftopic);
354
355 if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0)
356 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
357 m_Contents[m_ContentsCnt].m_Level = 0;
358 m_Contents[m_ContentsCnt].m_ID = 0;
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());
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 + wxT(".cached"));
371 if (fi == NULL) fi = fsys.OpenFile(m_TempPath + safetitle + wxT(".cached"));
372 if ((fi == NULL) || (m_TempPath == wxEmptyString)) {
373 LoadMSProject(bookr, fsys, indexfile, contfile);
374 if (m_TempPath != wxEmptyString) {
375 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath + safetitle + wxT(".cached"));
376 SaveCachedBook(bookr, outs);
377 delete outs;
378 }
379 } else {
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);
390
391 return TRUE;
392 }
393
394
395 bool wxHtmlHelpData::AddBook(const wxString& book)
396 {
397 if (book.Right(4).Lower() == wxT(".zip") ||
398 book.Right(4).Lower() == wxT(".htb") /*html book*/)
399
400 {
401 wxFileSystem fsys;
402 wxString s;
403 bool rt = FALSE;
404
405 s = fsys.FindFirst(book + wxT("#zip:") + wxT("*.hhp"), wxFILE);
406 while (!s.IsEmpty())
407 {
408 if (AddBook(s)) rt = TRUE;
409 s = fsys.FindNext();
410 }
411
412 return rt;
413 }
414
415
416 else
417 {
418 wxFSFile *fi;
419 wxFileSystem fsys;
420 wxInputStream *s;
421 wxString bookFull;
422
423 int sz;
424 char *buff, *lineptr;
425 char linebuf[300];
426
427 wxString title = _("noname"),
428 safetitle,
429 start = wxEmptyString,
430 contents = wxEmptyString, index = wxEmptyString;
431
432 if (wxIsAbsolutePath(book)) bookFull = book;
433 else bookFull = wxGetCwd() + "/" + book;
434
435 fi = fsys.OpenFile(bookFull);
436 if (fi == NULL) return FALSE;
437 fsys.ChangePathTo(bookFull);
438 s = fi -> GetStream();
439 sz = s -> GetSize();
440 buff = new char[sz + 1];
441 buff[sz] = 0;
442 s -> Read(buff, sz);
443 lineptr = buff;
444 delete fi;
445
446 do {
447 lineptr = ReadLine(lineptr, linebuf);
448
449 if (strstr(linebuf, "Title=") == linebuf)
450 title = linebuf + strlen("Title=");
451 if (strstr(linebuf, "Default topic=") == linebuf)
452 start = linebuf + strlen("Default topic=");
453 if (strstr(linebuf, "Index file=") == linebuf)
454 index = linebuf + strlen("Index file=");
455 if (strstr(linebuf, "Contents file=") == linebuf)
456 contents = linebuf + strlen("Contents file=");
457 } while (lineptr != NULL);
458 delete[] buff;
459
460 return AddBookParam(title, contents, index, start, fsys.GetPath());
461 }
462 }
463
464 wxString wxHtmlHelpData::FindPageByName(const wxString& x)
465 {
466 int cnt;
467 int i;
468 wxFileSystem fsys;
469 wxFSFile *f;
470 wxString url(wxEmptyString);
471
472 /* 1. try to open given file: */
473
474 cnt = m_BookRecords.GetCount();
475 for (i = 0; i < cnt; i++) {
476 f = fsys.OpenFile(m_BookRecords[i].GetBasePath() + x);
477 if (f) {
478 url = m_BookRecords[i].GetBasePath() + x;
479 delete f;
480 return url;
481 }
482 }
483
484
485 /* 2. try to find a book: */
486
487 for (i = 0; i < cnt; i++) {
488 if (m_BookRecords[i].GetTitle() == x) {
489 url = m_BookRecords[i].GetBasePath() + m_BookRecords[i].GetStart();
490 return url;
491 }
492 }
493
494 /* 3. try to find in contents: */
495
496 cnt = m_ContentsCnt;
497 for (i = 0; i < cnt; i++) {
498 if (wxStrcmp(m_Contents[i].m_Name, x) == 0) {
499 url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page;
500 return url;
501 }
502 }
503
504
505 /* 4. try to find in index: */
506
507 cnt = m_IndexCnt;
508 for (i = 0; i < cnt; i++) {
509 if (wxStrcmp(m_Index[i].m_Name, x) == 0) {
510 url = m_Index[i].m_Book -> GetBasePath() + m_Index[i].m_Page;
511 return url;
512 }
513 }
514
515 return url;
516 }
517
518 wxString wxHtmlHelpData::FindPageById(int id)
519 {
520 int i;
521 wxString url(wxEmptyString);
522
523 for (i = 0; i < m_ContentsCnt; i++) {
524 if (m_Contents[i].m_ID == id) {
525 url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page;
526 return url;
527 }
528 }
529
530 return url;
531 }
532
533 //----------------------------------------------------------------------------------
534 // wxHtmlSearchStatus functions
535 //----------------------------------------------------------------------------------
536
537 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
538 const wxString& book)
539 {
540 m_Data = data;
541 m_Keyword = keyword;
542 wxHtmlBookRecord* bookr = NULL;
543 if (book != wxEmptyString) {
544 // we have to search in a specific book. Find it first
545 int i, cnt = data->m_BookRecords.GetCount();
546 for (i = 0; i < cnt; i++)
547 if (data->m_BookRecords[i].GetTitle() == book) {
548 bookr = &(data->m_BookRecords[i]);
549 m_CurIndex = bookr->GetContentsStart();
550 m_MaxIndex = bookr->GetContentsEnd();
551 break;
552 }
553 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
554 wxASSERT(bookr);
555 }
556 if (! bookr) {
557 // no book specified; search all books
558 m_CurIndex = 0;
559 m_MaxIndex = m_Data->m_ContentsCnt;
560 }
561 m_Engine.LookFor(keyword);
562 m_Active = (m_CurIndex < m_MaxIndex);
563 m_LastPage = wxEmptyString;
564 }
565
566 bool wxHtmlSearchStatus::Search()
567 {
568 wxFileSystem fsys;
569 wxFSFile *file;
570 int i = m_CurIndex; // shortcut
571 bool found = FALSE;
572
573 if (! m_Active) {
574 // sanity check. Illegal use, but we'll try to prevent a crash anyway
575 #if !defined(__VISAGECPP__)
576 wxASSERT(0);
577 #else
578 wxASSERT(m_Active);
579 #endif
580 return FALSE;
581 }
582
583 m_ContentsItem = NULL;
584 m_Name = wxEmptyString;
585
586 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book -> GetBasePath() +
587 m_Data->m_Contents[i].m_Page);
588 if (file) {
589 if (m_LastPage != file->GetLocation()) {
590 m_LastPage = file->GetLocation();
591 if (m_Engine.Scan(file -> GetStream())) {
592 m_Name = m_Data->m_Contents[i].m_Name;
593 m_ContentsItem = m_Data->m_Contents + i;
594 found = TRUE;
595 }
596 }
597 delete file;
598 }
599 m_Active = (++m_CurIndex < m_MaxIndex);
600 return found;
601 }
602
603
604
605
606
607
608
609
610 //--------------------------------------------------------------------------------
611 // wxSearchEngine
612 //--------------------------------------------------------------------------------
613
614 void wxSearchEngine::LookFor(const wxString& keyword)
615 {
616 if (m_Keyword) delete[] m_Keyword;
617 m_Keyword = new wxChar[keyword.Length() + 1];
618 wxStrcpy(m_Keyword, keyword.c_str());
619 for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--)
620 if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z')))
621 m_Keyword[i] += wxT('a') - wxT('A');
622 }
623
624
625
626 bool wxSearchEngine::Scan(wxInputStream *stream)
627 {
628 wxASSERT_MSG(m_Keyword != NULL, _("wxSearchEngine::LookFor must be called before scanning!"));
629
630 int i, j;
631 int lng = stream ->GetSize();
632 int wrd = wxStrlen(m_Keyword);
633 bool found = FALSE;
634 char *buf = new char[lng + 1];
635 stream -> Read(buf, lng);
636 buf[lng] = 0;
637
638 for (i = 0; i < lng; i++)
639 if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A';
640
641 for (i = 0; i < lng - wrd; i++) {
642 j = 0;
643 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
644 if (j == wrd) {found = TRUE; break; }
645 }
646
647 delete[] buf;
648 return found;
649 }
650
651
652
653
654 #endif