]>
git.saurik.com Git - wxWidgets.git/blob - src/html/helpdata.cpp
2781e510a719ee631184b0a7d02a45848baa5603
1 /////////////////////////////////////////////////////////////////////////////
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
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
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"
38 #include "wx/arrimpl.cpp"
39 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray
)
41 //-----------------------------------------------------------------------------
42 // static helper functions
43 //-----------------------------------------------------------------------------
45 // Reads one line, stores it into buf and returns pointer to new line or NULL.
46 static char* ReadLine(char *line
, char *buf
)
48 char *writeptr
= buf
, *readptr
= line
;
50 while (*readptr
!= 0 && *readptr
!= '\r' && *readptr
!= '\n') *(writeptr
++) = *(readptr
++);
52 while (*readptr
== '\r' || *readptr
== '\n') readptr
++;
53 if (*readptr
== 0) return NULL
;
58 static wxString
SafeFileName(const wxString
& 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
);
71 static int LINKAGEMODE
IndexCompareFunc(const void *a
, const void *b
)
73 return wxStrcmp(((wxHtmlContentsItem
*)a
) -> m_Name
, ((wxHtmlContentsItem
*)b
) -> m_Name
);
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
81 class HP_Parser
: public wxHtmlParser
84 void AddText(const char* WXUNUSED(text
)) { }
85 wxObject
* GetProduct() { return NULL
; }
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
93 class HP_TagHandler
: public wxHtmlTagHandler
96 wxString m_Name
, m_Page
;
100 wxHtmlContentsItem
*m_Items
;
102 wxHtmlBookRecord
*m_Book
;
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
);
113 bool HP_TagHandler::HandleTag(const wxHtmlTag
& tag
)
115 if (tag
.GetName() == wxT("UL")) {
121 else if (tag
.GetName() == wxT("OBJECT")) {
122 m_Name
= m_Page
= wxEmptyString
;
125 if (!m_Page
.IsEmpty())
126 /* should be 'if (tag.GetParam("TYPE") == "text/sitemap")'
127 but this works fine. Valid HHW's file may contain only two
130 <OBJECT type="text/site properties">
131 <param name="ImageType" value="Folder">
136 <OBJECT type="text/sitemap">
137 <param name="Name" value="main page">
138 <param name="Local" value="another.htm">
141 We're interested in the latter. !m_Page.IsEmpty() is valid
142 condition because text/site properties does not contain Local param
145 if (m_ItemsCnt
% wxHTML_REALLOC_STEP
== 0)
146 m_Items
= (wxHtmlContentsItem
*) realloc(m_Items
, (m_ItemsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
147 m_Items
[m_ItemsCnt
].m_Level
= m_Level
;
148 m_Items
[m_ItemsCnt
].m_ID
= m_ID
;
149 m_Items
[m_ItemsCnt
].m_Page
= new wxChar
[m_Page
.Length() + 1];
150 wxStrcpy(m_Items
[m_ItemsCnt
].m_Page
, m_Page
.c_str());
151 m_Items
[m_ItemsCnt
].m_Name
= new wxChar
[m_Name
.Length() + 1];
152 wxStrcpy(m_Items
[m_ItemsCnt
].m_Name
, m_Name
.c_str());
153 m_Items
[m_ItemsCnt
].m_Book
= m_Book
;
160 if (m_Name
== wxEmptyString
&& tag
.GetParam(wxT("NAME")) == wxT("Name")) m_Name
= tag
.GetParam(wxT("VALUE"));
161 if (tag
.GetParam(wxT("NAME")) == wxT("Local")) m_Page
= tag
.GetParam(wxT("VALUE"));
162 if (tag
.GetParam(wxT("NAME")) == wxT("ID")) tag
.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID
);
169 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
177 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
192 wxHtmlHelpData::wxHtmlHelpData()
194 m_TempPath
= wxEmptyString
;
202 wxHtmlHelpData::~wxHtmlHelpData()
206 m_BookRecords
.Empty();
208 for (i
= 0; i
< m_ContentsCnt
; i
++) {
209 delete[] m_Contents
[i
].m_Page
;
210 delete[] m_Contents
[i
].m_Name
;
215 for (i
= 0; i
< m_IndexCnt
; i
++) {
216 delete[] m_Index
[i
].m_Page
;
217 delete[] m_Index
[i
].m_Name
;
223 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
231 HP_TagHandler
*handler
= new HP_TagHandler(book
);
232 parser
.AddTagHandler(handler
);
234 f
= ( contentsfile
.IsEmpty() ? 0 : fsys
.OpenFile(contentsfile
) );
236 sz
= f
-> GetStream() -> GetSize();
237 buf
= new char[sz
+ 1];
239 f
-> GetStream() -> Read(buf
, sz
);
241 handler
-> ReadIn(m_Contents
, m_ContentsCnt
);
243 handler
-> WriteOut(m_Contents
, m_ContentsCnt
);
247 f
= ( indexfile
.IsEmpty() ? 0 : fsys
.OpenFile(indexfile
) );
249 sz
= f
-> GetStream() -> GetSize();
250 buf
= new char[sz
+ 1];
252 f
-> GetStream() -> Read(buf
, sz
);
254 handler
-> ReadIn(m_Index
, m_IndexCnt
);
256 handler
-> WriteOut(m_Index
, m_IndexCnt
);
263 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
268 /* load contents : */
270 f
-> Read(&x
, sizeof(x
));
273 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) * wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
274 for (i
= st
; i
< m_ContentsCnt
; i
++) {
275 f
-> Read(&x
, sizeof(x
));
276 m_Contents
[i
].m_Level
= x
;
277 f
-> Read(&x
, sizeof(x
));
278 m_Contents
[i
].m_ID
= x
;
279 f
-> Read(&x
, sizeof(x
));
280 m_Contents
[i
].m_Name
= new wxChar
[x
];
281 f
-> Read(m_Contents
[i
].m_Name
, x
*sizeof(wxChar
));
282 f
-> Read(&x
, sizeof(x
));
283 m_Contents
[i
].m_Page
= new wxChar
[x
];
284 f
-> Read(m_Contents
[i
].m_Page
, x
*sizeof(wxChar
));
285 m_Contents
[i
].m_Book
= book
;
290 f
-> Read(&x
, sizeof(x
));
293 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) * wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
294 for (i
= st
; i
< m_IndexCnt
; i
++) {
295 f
-> Read(&x
, sizeof(x
));
296 m_Index
[i
].m_Name
= new wxChar
[x
];
297 f
-> Read(m_Index
[i
].m_Name
, x
*sizeof(wxChar
));
298 f
-> Read(&x
, sizeof(x
));
299 m_Index
[i
].m_Page
= new wxChar
[x
];
300 f
-> Read(m_Index
[i
].m_Page
, x
*sizeof(wxChar
));
301 m_Index
[i
].m_Book
= book
;
307 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
312 /* save contents : */
315 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
316 f
-> Write(&x
, sizeof(x
));
317 for (i
= 0; i
< m_ContentsCnt
; i
++) {
318 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
319 x
= m_Contents
[i
].m_Level
;
320 f
-> Write(&x
, sizeof(x
));
321 x
= m_Contents
[i
].m_ID
;
322 f
-> Write(&x
, sizeof(x
));
323 x
= wxStrlen(m_Contents
[i
].m_Name
) + 1;
324 f
-> Write(&x
, sizeof(x
));
325 f
-> Write(m_Contents
[i
].m_Name
, x
*sizeof(wxChar
));
326 x
= wxStrlen(m_Contents
[i
].m_Page
) + 1;
327 f
-> Write(&x
, sizeof(x
));
328 f
-> Write(m_Contents
[i
].m_Page
, x
*sizeof(wxChar
));
334 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
335 f
-> Write(&x
, sizeof(x
));
336 for (i
= 0; i
< m_IndexCnt
; i
++) {
337 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
338 x
= wxStrlen(m_Index
[i
].m_Name
) + 1;
339 f
-> Write(&x
, sizeof(x
));
340 f
-> Write(m_Index
[i
].m_Name
, x
*sizeof(wxChar
));
341 x
= wxStrlen(m_Index
[i
].m_Page
) + 1;
342 f
-> Write(&x
, sizeof(x
));
343 f
-> Write(m_Index
[i
].m_Page
, x
*sizeof(wxChar
));
349 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
351 if (path
== wxEmptyString
) m_TempPath
= path
;
353 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
354 else m_TempPath
= wxGetCwd() + "/" + path
;
356 if (m_TempPath
[m_TempPath
.Length() - 1] != '/')
362 bool wxHtmlHelpData::AddBookParam(const wxString
& title
, const wxString
& contfile
,
363 const wxString
& indexfile
, const wxString
& deftopic
,
364 const wxString
& path
)
368 wxHtmlBookRecord
*bookr
;
371 if (! path
.IsEmpty())
372 fsys
.ChangePathTo(path
, TRUE
);
374 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
376 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
377 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
378 m_Contents
[m_ContentsCnt
].m_Level
= 0;
379 m_Contents
[m_ContentsCnt
].m_ID
= 0;
380 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
381 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
382 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
383 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
384 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
386 // store the contents index for later
387 int cont_start
= m_ContentsCnt
++;
389 // Try to find cached binary versions:
390 safetitle
= SafeFileName(title
);
391 fi
= fsys
.OpenFile(safetitle
+ wxT(".cached"));
392 if (fi
== NULL
) fi
= fsys
.OpenFile(m_TempPath
+ safetitle
+ wxT(".cached"));
393 if ((fi
== NULL
) || (m_TempPath
== wxEmptyString
)) {
394 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
395 if (m_TempPath
!= wxEmptyString
) {
396 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+ safetitle
+ wxT(".cached"));
397 SaveCachedBook(bookr
, outs
);
401 LoadCachedBook(bookr
, fi
-> GetStream());
405 // Now store the contents range
406 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
408 m_BookRecords
.Add(bookr
);
410 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
416 bool wxHtmlHelpData::AddBook(const wxString
& book
)
418 if (book
.Right(4).Lower() == wxT(".zip") ||
419 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
426 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
429 if (AddBook(s
)) rt
= TRUE
;
445 char *buff
, *lineptr
;
448 wxString title
= _("noname"),
450 start
= wxEmptyString
,
451 contents
= wxEmptyString
, index
= wxEmptyString
;
453 if (wxIsAbsolutePath(book
)) bookFull
= book
;
454 else bookFull
= wxGetCwd() + "/" + book
;
456 fi
= fsys
.OpenFile(bookFull
);
457 if (fi
== NULL
) return FALSE
;
458 fsys
.ChangePathTo(bookFull
);
459 s
= fi
-> GetStream();
461 buff
= new char[sz
+ 1];
468 lineptr
= ReadLine(lineptr
, linebuf
);
470 if (strstr(linebuf
, "Title=") == linebuf
)
471 title
= linebuf
+ strlen("Title=");
472 if (strstr(linebuf
, "Default topic=") == linebuf
)
473 start
= linebuf
+ strlen("Default topic=");
474 if (strstr(linebuf
, "Index file=") == linebuf
)
475 index
= linebuf
+ strlen("Index file=");
476 if (strstr(linebuf
, "Contents file=") == linebuf
)
477 contents
= linebuf
+ strlen("Contents file=");
478 } while (lineptr
!= NULL
);
481 return AddBookParam(title
, contents
, index
, start
, fsys
.GetPath());
485 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
491 wxString
url(wxEmptyString
);
493 /* 1. try to open given file: */
495 cnt
= m_BookRecords
.GetCount();
496 for (i
= 0; i
< cnt
; i
++) {
497 f
= fsys
.OpenFile(m_BookRecords
[i
].GetBasePath() + x
);
499 url
= m_BookRecords
[i
].GetBasePath() + x
;
506 /* 2. try to find a book: */
508 for (i
= 0; i
< cnt
; i
++) {
509 if (m_BookRecords
[i
].GetTitle() == x
) {
510 url
= m_BookRecords
[i
].GetBasePath() + m_BookRecords
[i
].GetStart();
515 /* 3. try to find in contents: */
518 for (i
= 0; i
< cnt
; i
++) {
519 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0) {
520 url
= m_Contents
[i
].m_Book
-> GetBasePath() + m_Contents
[i
].m_Page
;
526 /* 4. try to find in index: */
529 for (i
= 0; i
< cnt
; i
++) {
530 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0) {
531 url
= m_Index
[i
].m_Book
-> GetBasePath() + m_Index
[i
].m_Page
;
539 wxString
wxHtmlHelpData::FindPageById(int id
)
542 wxString
url(wxEmptyString
);
544 for (i
= 0; i
< m_ContentsCnt
; i
++) {
545 if (m_Contents
[i
].m_ID
== id
) {
546 url
= m_Contents
[i
].m_Book
-> GetBasePath() + m_Contents
[i
].m_Page
;
554 //----------------------------------------------------------------------------------
555 // wxHtmlSearchStatus functions
556 //----------------------------------------------------------------------------------
558 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
559 bool case_sensitive
, bool whole_words_only
,
560 const wxString
& book
)
564 wxHtmlBookRecord
* bookr
= NULL
;
565 if (book
!= wxEmptyString
) {
566 // we have to search in a specific book. Find it first
567 int i
, cnt
= data
->m_BookRecords
.GetCount();
568 for (i
= 0; i
< cnt
; i
++)
569 if (data
->m_BookRecords
[i
].GetTitle() == book
) {
570 bookr
= &(data
->m_BookRecords
[i
]);
571 m_CurIndex
= bookr
->GetContentsStart();
572 m_MaxIndex
= bookr
->GetContentsEnd();
575 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
579 // no book specified; search all books
581 m_MaxIndex
= m_Data
->m_ContentsCnt
;
583 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
584 m_Active
= (m_CurIndex
< m_MaxIndex
);
588 bool wxHtmlSearchStatus::Search()
592 int i
= m_CurIndex
; // shortcut
597 // sanity check. Illegal use, but we'll try to prevent a crash anyway
598 #if !defined(__VISAGECPP__)
606 m_Name
= wxEmptyString
;
607 m_ContentsItem
= NULL
;
608 thepage
= m_Data
->m_Contents
[i
].m_Page
;
610 m_Active
= (++m_CurIndex
< m_MaxIndex
);
611 // check if it is same page with different anchor:
612 if (m_LastPage
!= NULL
)
615 for (p1
= thepage
, p2
= m_LastPage
;
616 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
618 m_LastPage
= thepage
;
620 if (*p1
== 0 || *p1
== _T('#'))
623 else m_LastPage
= thepage
;
625 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
-> GetBasePath() + thepage
);
628 if (m_Engine
.Scan(file
-> GetStream())) {
629 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
630 m_ContentsItem
= m_Data
->m_Contents
+ i
;
645 //--------------------------------------------------------------------------------
647 //--------------------------------------------------------------------------------
649 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
651 m_CaseSensitive
= case_sensitive
;
652 m_WholeWords
= whole_words_only
;
653 if (m_Keyword
) delete[] m_Keyword
;
654 m_Keyword
= new wxChar
[keyword
.Length() + 1];
655 wxStrcpy(m_Keyword
, keyword
.c_str());
657 if (!m_CaseSensitive
)
658 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
659 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
660 m_Keyword
[i
] += wxT('a') - wxT('A');
665 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
667 bool wxSearchEngine::Scan(wxInputStream
*stream
)
669 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
672 int lng
= stream
->GetSize();
673 int wrd
= wxStrlen(m_Keyword
);
675 char *buf
= new char[lng
+ 1];
676 stream
-> Read(buf
, lng
);
679 if (!m_CaseSensitive
)
680 for (i
= 0; i
< lng
; i
++)
681 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
685 for (i
= 0; i
< lng
- wrd
; i
++) {
686 if (WHITESPACE(buf
[i
])) continue;
688 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
689 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) {found
= TRUE
; break; }
695 for (i
= 0; i
< lng
- wrd
; i
++) {
697 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
698 if (j
== wrd
) {found
= TRUE
; break; }