Fixed some compile errors (MSVC++ 6) and some very strange link errors when
[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 // Created:
8 // RCS-ID:
9 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "helpdata.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #include "wx/defs.h"
25
26 #if wxUSE_HTML
27
28 #ifndef WXPRECOMP
29 #include "wx/wx.h"
30 #endif
31
32 #include "wx/html/helpdata.h"
33 #include "wx/tokenzr.h"
34 #include "wx/wfstream.h"
35 #include "wx/busyinfo.h"
36 #include "wx/html/htmlparser.h"
37 #include "wx/html/htmldefs.h"
38
39 #include "wx/arrimpl.cpp"
40 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray)
41
42 //-----------------------------------------------------------------------------
43 // static helper functions
44 //-----------------------------------------------------------------------------
45
46 // Reads one line, stores it into buf and returns pointer to new line or NULL.
47 static char* ReadLine(char *line, char *buf)
48 {
49 char *writeptr = buf, *readptr = line;
50
51 while (*readptr != 0 && *readptr != '\r' && *readptr != '\n') *(writeptr++) = *(readptr++);
52 *writeptr = 0;
53 while (*readptr == '\r' || *readptr == '\n') readptr++;
54 if (*readptr == 0) return NULL;
55 else return readptr;
56 }
57
58
59 static wxString SafeFileName(const wxString& s)
60 {
61 wxString res = s;
62 res.Replace(":", "_", TRUE);
63 res.Replace(" ", "_", TRUE);
64 res.Replace("/", "_", TRUE);
65 res.Replace("\\", "_", TRUE);
66 res.Replace("#", "_", TRUE);
67 res.Replace(".", "_", TRUE);
68 return res;
69 }
70
71
72 static int IndexCompareFunc(const void *a, const void *b)
73 {
74 return strcmp(((wxHtmlContentsItem*)a) -> m_Name, ((wxHtmlContentsItem*)b) -> m_Name);
75 }
76
77
78 //-----------------------------------------------------------------------------
79 // HP_Parser
80 //-----------------------------------------------------------------------------
81
82 class HP_Parser : public wxHtmlParser
83 {
84 public:
85 void AddText(const char* text) {}
86 wxObject* GetProduct() {return NULL;}
87 };
88
89
90 //-----------------------------------------------------------------------------
91 // HP_TagHandler
92 //-----------------------------------------------------------------------------
93
94 class HP_TagHandler : public wxHtmlTagHandler
95 {
96 private:
97 wxString m_Name, m_Page;
98 int m_Level;
99 int m_ID;
100 int m_Index;
101 wxHtmlContentsItem *m_Items;
102 int m_ItemsCnt;
103 wxHtmlBookRecord *m_Book;
104
105 public:
106 HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler() {m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; m_Level = 0;}
107 wxString GetSupportedTags() {return "UL,OBJECT,PARAM";}
108 bool HandleTag(const wxHtmlTag& tag);
109 void WriteOut(wxHtmlContentsItem*& array, int& size);
110 void ReadIn(wxHtmlContentsItem* array, int size);
111 };
112
113
114 bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
115 {
116 if (tag.GetName() == "UL") {
117 m_Level++;
118 ParseInner(tag);
119 m_Level--;
120 return TRUE;
121 }
122
123 else if (tag.GetName() == "OBJECT") {
124 m_Name = m_Page = wxEmptyString;
125 ParseInner(tag);
126 if (m_Page != wxEmptyString) {
127 if (m_ItemsCnt % HTML_REALLOC_STEP == 0)
128 m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + HTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
129 m_Items[m_ItemsCnt].m_Level = m_Level;
130 m_Items[m_ItemsCnt].m_ID = m_ID;
131 m_Items[m_ItemsCnt].m_Page = new char[m_Page.Length() + 1];
132 strcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str());
133 m_Items[m_ItemsCnt].m_Name = new char [m_Name.Length() + 1];
134 strcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str());
135 m_Items[m_ItemsCnt].m_Book = m_Book;
136 m_ItemsCnt++;
137 }
138 return TRUE;
139 }
140
141 else { // "PARAM"
142 if (m_Name == wxEmptyString && tag.GetParam("NAME") == "Name") m_Name = tag.GetParam("VALUE");
143 if (tag.GetParam("NAME") == "Local") m_Page = tag.GetParam("VALUE");
144 if (tag.GetParam("NAME") == "ID") tag.ScanParam("VALUE", "%i", &m_ID);
145 return FALSE;
146 }
147 }
148
149
150
151 void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size)
152 {
153 array = m_Items;
154 size = m_ItemsCnt;
155 m_Items = NULL;
156 m_ItemsCnt = 0;
157 }
158
159 void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size)
160 {
161 m_Items = array;
162 m_ItemsCnt = size;
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() ? NULL : 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() ? NULL : 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 / HTML_REALLOC_STEP + 1) * HTML_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 char[x];
260 f -> Read(m_Contents[i].m_Name, x);
261 f -> Read(&x, sizeof(x));
262 m_Contents[i].m_Page = new char[x];
263 f -> Read(m_Contents[i].m_Page, x);
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 / HTML_REALLOC_STEP + 1) * HTML_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 char[x];
276 f -> Read(m_Index[i].m_Name, x);
277 f -> Read(&x, sizeof(x));
278 m_Index[i].m_Page = new char[x];
279 f -> Read(m_Index[i].m_Page, x);
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 = strlen(m_Contents[i].m_Name) + 1;
303 f -> Write(&x, sizeof(x));
304 f -> Write(m_Contents[i].m_Name, x);
305 x = strlen(m_Contents[i].m_Page) + 1;
306 f -> Write(&x, sizeof(x));
307 f -> Write(m_Contents[i].m_Page, x);
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 = strlen(m_Index[i].m_Name) + 1;
318 f -> Write(&x, sizeof(x));
319 f -> Write(m_Index[i].m_Name, x);
320 x = strlen(m_Index[i].m_Page) + 1;
321 f -> Write(&x, sizeof(x));
322 f -> Write(m_Index[i].m_Page, x);
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 // workaround for bug in ChangePathTo(name, TRUE)
352 fsys.ChangePathTo(path+"/gaga");
353
354 bookr = new wxHtmlBookRecord(path+'/', title, deftopic);
355
356 if (m_ContentsCnt % HTML_REALLOC_STEP == 0)
357 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + HTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
358 m_Contents[m_ContentsCnt].m_Level = 0;
359 m_Contents[m_ContentsCnt].m_ID = 0;
360 m_Contents[m_ContentsCnt].m_Page = new char[deftopic.Length() + 1];
361 strcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str());
362 m_Contents[m_ContentsCnt].m_Name = new char [title.Length() + 1];
363 strcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str());
364 m_Contents[m_ContentsCnt].m_Book = bookr;
365
366 // store the contents index for later
367 int cont_start = m_ContentsCnt++;
368
369 // Try to find cached binary versions:
370 safetitle = SafeFileName(title);
371 fi = fsys.OpenFile(safetitle + ".cached");
372 if (fi == NULL) fi = fsys.OpenFile(m_TempPath + safetitle + ".cached");
373 if ((fi == NULL) || (m_TempPath == wxEmptyString)) {
374 LoadMSProject(bookr, fsys, indexfile, contfile);
375 if (m_TempPath != wxEmptyString) {
376 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath + safetitle + ".cached");
377 SaveCachedBook(bookr, outs);
378 delete outs;
379 }
380 }
381 else {
382 LoadCachedBook(bookr, fi -> GetStream());
383 delete fi;
384 }
385
386 // Now store the contents range
387 bookr->SetContentsRange(cont_start, m_ContentsCnt);
388
389 m_BookRecords.Add(bookr);
390 if (m_IndexCnt > 0)
391 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), IndexCompareFunc);
392
393 return TRUE;
394 }
395
396
397 bool wxHtmlHelpData::AddBook(const wxString& book)
398 {
399 wxFSFile *fi;
400 wxFileSystem fsys;
401 wxInputStream *s;
402 wxString bookFull;
403
404 int sz;
405 char *buff, *lineptr;
406 char linebuf[300];
407
408 wxString title = _("noname"),
409 safetitle,
410 start = wxEmptyString,
411 contents = wxEmptyString, index = wxEmptyString;
412
413 if (wxIsAbsolutePath(book)) bookFull = book;
414 else bookFull = wxGetCwd() + "/" + book;
415
416 fi = fsys.OpenFile(bookFull);
417 if (fi == NULL) return FALSE;
418 fsys.ChangePathTo(bookFull);
419 s = fi -> GetStream();
420 sz = s -> GetSize();
421 buff = new char[sz+1];
422 buff[sz] = 0;
423 s -> Read(buff, sz);
424 lineptr = buff;
425 delete fi;
426
427 while ((lineptr = ReadLine(lineptr, linebuf)) != NULL) {
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=");
436 }
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++) {
476 if (strcmp(m_Contents[i].m_Name, x) == 0) {
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++) {
487 if (strcmp(m_Index[i].m_Name, x) == 0) {
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)
497 {
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,
516 const wxString& book)
517 {
518 m_Data = data;
519 m_Keyword = keyword;
520 wxHtmlBookRecord* bookr = NULL;
521 if (book != wxEmptyString) {
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);
533 }
534 if (! bookr) {
535 // no book specified; search all books
536 m_CurIndex = 0;
537 m_MaxIndex = m_Data->m_ContentsCnt;
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;
548 int i = m_CurIndex; // shortcut
549 bool found = FALSE;
550
551 if (! m_Active) {
552 // sanity check. Illegal use, but we'll try to prevent a crash anyway
553 wxASSERT(0);
554 return FALSE;
555 }
556
557 m_ContentsItem = NULL;
558 m_Name = wxEmptyString;
559
560 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book -> GetBasePath() +
561 m_Data->m_Contents[i].m_Page);
562 if (file) {
563 if (m_LastPage != file->GetLocation()) {
564 m_LastPage = file->GetLocation();
565 if (m_Engine.Scan(file -> GetStream())) {
566 m_Name = m_Data->m_Contents[i].m_Name;
567 m_ContentsItem = m_Data->m_Contents + i;
568 found = true;
569 }
570 }
571 delete file;
572 }
573 m_Active = (++m_CurIndex < m_MaxIndex);
574 return found;
575 }
576
577 #endif