]> git.saurik.com Git - wxWidgets.git/blob - src/html/helpdata.cpp
warning fixed
[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(":", "_", TRUE);
62 res.Replace(" ", "_", TRUE);
63 res.Replace("/", "_", TRUE);
64 res.Replace("\\", "_", TRUE);
65 res.Replace("#", "_", TRUE);
66 res.Replace(".", "_", TRUE);
67 return res;
68 }
69
70
71 static int IndexCompareFunc(const void *a, const void *b)
72 {
73 return strcmp(((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 "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() == "UL") {
116 m_Level++;
117 ParseInner(tag);
118 m_Level--;
119 return TRUE;
120 }
121
122 else if (tag.GetName() == "OBJECT") {
123 m_Name = m_Page = wxEmptyString;
124 ParseInner(tag);
125 if (m_Page != wxEmptyString) {
126 if (m_ItemsCnt % HTML_REALLOC_STEP == 0)
127 m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + HTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
128 m_Items[m_ItemsCnt].m_Level = m_Level;
129 m_Items[m_ItemsCnt].m_ID = m_ID;
130 m_Items[m_ItemsCnt].m_Page = new char[m_Page.Length() + 1];
131 strcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str());
132 m_Items[m_ItemsCnt].m_Name = new char [m_Name.Length() + 1];
133 strcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str());
134 m_Items[m_ItemsCnt].m_Book = m_Book;
135 m_ItemsCnt++;
136 }
137 return TRUE;
138 }
139
140 else { // "PARAM"
141 if (m_Name == wxEmptyString && tag.GetParam("NAME") == "Name") m_Name = tag.GetParam("VALUE");
142 if (tag.GetParam("NAME") == "Local") m_Page = tag.GetParam("VALUE");
143 if (tag.GetParam("NAME") == "ID") tag.ScanParam("VALUE", "%i", &m_ID);
144 return FALSE;
145 }
146 }
147
148
149
150 void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size)
151 {
152 array = m_Items;
153 size = m_ItemsCnt;
154 m_Items = NULL;
155 m_ItemsCnt = 0;
156 }
157
158 void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size)
159 {
160 m_Items = array;
161 m_ItemsCnt = size;
162 }
163
164 //-----------------------------------------------------------------------------
165 // wxHtmlHelpData
166 //-----------------------------------------------------------------------------
167
168 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
169
170 wxHtmlHelpData::wxHtmlHelpData()
171 {
172 m_TempPath = wxEmptyString;
173
174 m_Contents = NULL;
175 m_ContentsCnt = 0;
176 m_Index = NULL;
177 m_IndexCnt = 0;
178 }
179
180 wxHtmlHelpData::~wxHtmlHelpData()
181 {
182 int i;
183
184 m_BookRecords.Empty();
185 if (m_Contents) {
186 for (i = 0; i < m_ContentsCnt; i++) {
187 delete[] m_Contents[i].m_Page;
188 delete[] m_Contents[i].m_Name;
189 }
190 free(m_Contents);
191 }
192 if (m_Index) {
193 for (i = 0; i < m_IndexCnt; i++) {
194 delete[] m_Index[i].m_Page;
195 delete[] m_Index[i].m_Name;
196 }
197 free(m_Index);
198 }
199 }
200
201 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile)
202 {
203 wxFSFile *f;
204 char *buf;
205 int sz;
206 wxString string;
207
208 HP_Parser parser;
209 HP_TagHandler *handler = new HP_TagHandler(book);
210 parser.AddTagHandler(handler);
211
212 f = ( contentsfile.IsEmpty() ? NULL : fsys.OpenFile(contentsfile) );
213 if (f) {
214 sz = f -> GetStream() -> GetSize();
215 buf = new char[sz+1];
216 buf[sz] = 0;
217 f -> GetStream() -> Read(buf, sz);
218 delete f;
219 handler -> ReadIn(m_Contents, m_ContentsCnt);
220 parser.Parse(buf);
221 handler -> WriteOut(m_Contents, m_ContentsCnt);
222 delete[] buf;
223 }
224
225 f = ( indexfile.IsEmpty() ? NULL : fsys.OpenFile(indexfile) );
226 if (f) {
227 sz = f -> GetStream() -> GetSize();
228 buf = new char[sz+1];
229 buf[sz] = 0;
230 f -> GetStream() -> Read(buf, sz);
231 delete f;
232 handler -> ReadIn(m_Index, m_IndexCnt);
233 parser.Parse(buf);
234 handler -> WriteOut(m_Index, m_IndexCnt);
235 delete[] buf;
236 }
237 return TRUE;
238 }
239
240
241 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
242 {
243 int i, st;
244 int x;
245
246 /* load contents : */
247
248 f -> Read(&x, sizeof(x));
249 st = m_ContentsCnt;
250 m_ContentsCnt += x;
251 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt / HTML_REALLOC_STEP + 1) * HTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
252 for (i = st; i < m_ContentsCnt; i++) {
253 f -> Read(&x, sizeof(x));
254 m_Contents[i].m_Level = x;
255 f -> Read(&x, sizeof(x));
256 m_Contents[i].m_ID = x;
257 f -> Read(&x, sizeof(x));
258 m_Contents[i].m_Name = new char[x];
259 f -> Read(m_Contents[i].m_Name, x);
260 f -> Read(&x, sizeof(x));
261 m_Contents[i].m_Page = new char[x];
262 f -> Read(m_Contents[i].m_Page, x);
263 m_Contents[i].m_Book = book;
264 }
265
266 /* load index : */
267
268 f -> Read(&x, sizeof(x));
269 st = m_IndexCnt;
270 m_IndexCnt += x;
271 m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / HTML_REALLOC_STEP + 1) * HTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
272 for (i = st; i < m_IndexCnt; i++) {
273 f -> Read(&x, sizeof(x));
274 m_Index[i].m_Name = new char[x];
275 f -> Read(m_Index[i].m_Name, x);
276 f -> Read(&x, sizeof(x));
277 m_Index[i].m_Page = new char[x];
278 f -> Read(m_Index[i].m_Page, x);
279 m_Index[i].m_Book = book;
280 }
281 return TRUE;
282 }
283
284
285 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
286 {
287 int i;
288 int x;
289
290 /* save contents : */
291
292 x = 0;
293 for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++;
294 f -> Write(&x, sizeof(x));
295 for (i = 0; i < m_ContentsCnt; i++) {
296 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue;
297 x = m_Contents[i].m_Level;
298 f -> Write(&x, sizeof(x));
299 x = m_Contents[i].m_ID;
300 f -> Write(&x, sizeof(x));
301 x = strlen(m_Contents[i].m_Name) + 1;
302 f -> Write(&x, sizeof(x));
303 f -> Write(m_Contents[i].m_Name, x);
304 x = strlen(m_Contents[i].m_Page) + 1;
305 f -> Write(&x, sizeof(x));
306 f -> Write(m_Contents[i].m_Page, x);
307 }
308
309 /* save index : */
310
311 x = 0;
312 for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++;
313 f -> Write(&x, sizeof(x));
314 for (i = 0; i < m_IndexCnt; i++) {
315 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue;
316 x = strlen(m_Index[i].m_Name) + 1;
317 f -> Write(&x, sizeof(x));
318 f -> Write(m_Index[i].m_Name, x);
319 x = strlen(m_Index[i].m_Page) + 1;
320 f -> Write(&x, sizeof(x));
321 f -> Write(m_Index[i].m_Page, x);
322 }
323 return TRUE;
324 }
325
326
327 void wxHtmlHelpData::SetTempDir(const wxString& path)
328 {
329 if (path == wxEmptyString) m_TempPath = path;
330 else {
331 if (wxIsAbsolutePath(path)) m_TempPath = path;
332 else m_TempPath = wxGetCwd() + "/" + path;
333
334 if (m_TempPath[m_TempPath.Length() - 1] != '/')
335 m_TempPath << "/";
336 }
337 }
338
339
340 bool wxHtmlHelpData::AddBookParam(const wxString& title, const wxString& contfile,
341 const wxString& indexfile, const wxString& deftopic,
342 const wxString& path)
343 {
344 wxFileSystem fsys;
345 wxFSFile *fi;
346 wxHtmlBookRecord *bookr;
347 wxString safetitle;
348
349 if (! path.IsEmpty())
350 // workaround for bug in ChangePathTo(name, TRUE)
351 fsys.ChangePathTo(path+"/gaga");
352
353 bookr = new wxHtmlBookRecord(path+'/', title, deftopic);
354
355 if (m_ContentsCnt % HTML_REALLOC_STEP == 0)
356 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + HTML_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 char[deftopic.Length() + 1];
360 strcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str());
361 m_Contents[m_ContentsCnt].m_Name = new char [title.Length() + 1];
362 strcpy(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 + ".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) {
375 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath + safetitle + ".cached");
376 SaveCachedBook(bookr, outs);
377 delete outs;
378 }
379 }
380 else {
381 LoadCachedBook(bookr, fi -> GetStream());
382 delete fi;
383 }
384
385 // Now store the contents range
386 bookr->SetContentsRange(cont_start, m_ContentsCnt);
387
388 m_BookRecords.Add(bookr);
389 if (m_IndexCnt > 0)
390 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), IndexCompareFunc);
391
392 return TRUE;
393 }
394
395
396 bool wxHtmlHelpData::AddBook(const wxString& book)
397 {
398 wxFSFile *fi;
399 wxFileSystem fsys;
400 wxInputStream *s;
401 wxString bookFull;
402
403 int sz;
404 char *buff, *lineptr;
405 char linebuf[300];
406
407 wxString title = _("noname"),
408 safetitle,
409 start = wxEmptyString,
410 contents = wxEmptyString, index = wxEmptyString;
411
412 if (wxIsAbsolutePath(book)) bookFull = book;
413 else bookFull = wxGetCwd() + "/" + book;
414
415 fi = fsys.OpenFile(bookFull);
416 if (fi == NULL) return FALSE;
417 fsys.ChangePathTo(bookFull);
418 s = fi -> GetStream();
419 sz = s -> GetSize();
420 buff = new char[sz+1];
421 buff[sz] = 0;
422 s -> Read(buff, sz);
423 lineptr = buff;
424 delete fi;
425
426 while ((lineptr = ReadLine(lineptr, linebuf)) != NULL) {
427 if (strstr(linebuf, "Title=") == linebuf)
428 title = linebuf + strlen("Title=");
429 if (strstr(linebuf, "Default topic=") == linebuf)
430 start = linebuf + strlen("Default topic=");
431 if (strstr(linebuf, "Index file=") == linebuf)
432 index = linebuf + strlen("Index file=");
433 if (strstr(linebuf, "Contents file=") == linebuf)
434 contents = linebuf + strlen("Contents file=");
435 }
436 delete[] buff;
437
438 return AddBookParam(title, contents, index, start, fsys.GetPath());
439 }
440
441 wxString wxHtmlHelpData::FindPageByName(const wxString& x)
442 {
443 int cnt;
444 int i;
445 wxFileSystem fsys;
446 wxFSFile *f;
447 wxString url(wxEmptyString);
448
449 /* 1. try to open given file: */
450
451 cnt = m_BookRecords.GetCount();
452 for (i = 0; i < cnt; i++) {
453 f = fsys.OpenFile(m_BookRecords[i].GetBasePath() + x);
454 if (f) {
455 url = m_BookRecords[i].GetBasePath() + x;
456 delete f;
457 return url;
458 }
459 }
460
461
462 /* 2. try to find a book: */
463
464 for (i = 0; i < cnt; i++) {
465 if (m_BookRecords[i].GetTitle() == x) {
466 url = m_BookRecords[i].GetBasePath() + m_BookRecords[i].GetStart();
467 return url;
468 }
469 }
470
471 /* 3. try to find in contents: */
472
473 cnt = m_ContentsCnt;
474 for (i = 0; i < cnt; i++) {
475 if (strcmp(m_Contents[i].m_Name, x) == 0) {
476 url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page;
477 return url;
478 }
479 }
480
481
482 /* 4. try to find in index: */
483
484 cnt = m_IndexCnt;
485 for (i = 0; i < cnt; i++) {
486 if (strcmp(m_Index[i].m_Name, x) == 0) {
487 url = m_Index[i].m_Book -> GetBasePath() + m_Index[i].m_Page;
488 return url;
489 }
490 }
491
492 return url;
493 }
494
495 wxString wxHtmlHelpData::FindPageById(int id)
496 {
497 int i;
498 wxString url(wxEmptyString);
499
500 for (i = 0; i < m_ContentsCnt; i++) {
501 if (m_Contents[i].m_ID == id) {
502 url = m_Contents[i].m_Book -> GetBasePath() + m_Contents[i].m_Page;
503 return url;
504 }
505 }
506
507 return url;
508 }
509
510 //----------------------------------------------------------------------------------
511 // wxHtmlSearchStatus functions
512 //----------------------------------------------------------------------------------
513
514 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
515 const wxString& book)
516 {
517 m_Data = data;
518 m_Keyword = keyword;
519 wxHtmlBookRecord* bookr = NULL;
520 if (book != wxEmptyString) {
521 // we have to search in a specific book. Find it first
522 int i,cnt = data->m_BookRecords.GetCount();
523 for (i=0; i<cnt; i++)
524 if (data->m_BookRecords[i].GetTitle() == book) {
525 bookr = &(data->m_BookRecords[i]);
526 m_CurIndex = bookr->GetContentsStart();
527 m_MaxIndex = bookr->GetContentsEnd();
528 break;
529 }
530 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
531 wxASSERT(bookr);
532 }
533 if (! bookr) {
534 // no book specified; search all books
535 m_CurIndex = 0;
536 m_MaxIndex = m_Data->m_ContentsCnt;
537 }
538 m_Engine.LookFor(keyword);
539 m_Active = (m_CurIndex < m_MaxIndex);
540 m_LastPage = wxEmptyString;
541 }
542
543 bool wxHtmlSearchStatus::Search()
544 {
545 wxFileSystem fsys;
546 wxFSFile *file;
547 int i = m_CurIndex; // shortcut
548 bool found = FALSE;
549
550 if (! m_Active) {
551 // sanity check. Illegal use, but we'll try to prevent a crash anyway
552 wxASSERT(0);
553 return FALSE;
554 }
555
556 m_ContentsItem = NULL;
557 m_Name = wxEmptyString;
558
559 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book -> GetBasePath() +
560 m_Data->m_Contents[i].m_Page);
561 if (file) {
562 if (m_LastPage != file->GetLocation()) {
563 m_LastPage = file->GetLocation();
564 if (m_Engine.Scan(file -> GetStream())) {
565 m_Name = m_Data->m_Contents[i].m_Name;
566 m_ContentsItem = m_Data->m_Contents + i;
567 found = TRUE;
568 }
569 }
570 delete file;
571 }
572 m_Active = (++m_CurIndex < m_MaxIndex);
573 return found;
574 }
575
576 #endif