xCHM  1.23
chmfile.h
1 /*
2 
3  Copyright (C) 2003 - 2013 Razvan Cojocaru <rzvncj@gmail.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  MA 02110-1301, USA.
19 
20 */
21 
22 
23 #ifndef __CHMFILE_H_
24 #define __CHMFILE_H_
25 
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 #include <stdint.h>
32 #include <chm_lib.h>
33 #include <wx/filefn.h>
34 #include <wx/string.h>
35 #include <wx/hashmap.h>
36 #include <wx/font.h>
37 #include <wx/string.h>
38 #include <string>
39 
40 
41 // Forward declarations.
42 class wxTreeCtrl;
43 class CHMListCtrl;
44 class UCharPtr;
45 class wxCSConv;
46 
47 
49 WX_DECLARE_STRING_HASH_MAP(wxString, CHMSearchResults);
51 WX_DECLARE_HASH_MAP( int, wxString, wxIntegerHash, wxIntegerEqual, CHMIDMap );
52 
53 
55 #define MAX_SEARCH_RESULTS 512
56 
57 
58 
60 class CHMFile {
61 public:
63  CHMFile();
64 
70  CHMFile(const wxString& archiveName);
71 
73  ~CHMFile();
74 
75 
82  wxString HomePage() const { return _home; }
83 
91  wxString TopicsFile() const { return _topicsFile; }
92 
99  wxString ArchiveName() const { return _filename; }
100 
108  wxString IndexFile() const { return _indexFile; }
109 
115  wxString Title() const { return _title; }
116 
123  bool IsOk() const { return _chmFile != NULL; }
124 
126  bool HasChanged();
127 
133  wxFontEncoding DesiredEncoding() const { return _enc; }
134 
139  wxString DefaultFont() const { return _font; }
140 
146  bool LoadCHM(const wxString& archiveName);
147 
149  void CloseCHM();
150 
160  bool GetTopicsTree(wxTreeCtrl *toBuild);
161 
162 
172  bool GetIndex(CHMListCtrl* toBuild);
173 
174 
180  bool LoadContextIDs();
181 
188  bool IsValidCID( const int contextID );
189 
196  wxString GetPageByCID( const int contextID );
197 
202  bool AreContextIDsLoaded() const { return !_cidMap.empty(); }
203 
214  bool IndexSearch(const wxString& text, bool wholeWords,
215  bool titlesOnly, CHMSearchResults *results);
216 
227  bool ResolveObject(const wxString& fileName, chmUnitInfo *ui);
228 
238  size_t RetrieveObject(chmUnitInfo *ui, unsigned char *buffer,
239  off_t fileOffset, size_t bufferSize);
240 
241 private:
243  wxFontEncoding GetFontEncFromCharSet(int cs);
244 
246  wxFontEncoding GetFontEncFromLCID(uint32_t lcid);
247 
249  bool GetArchiveInfo();
250 
252  uint32_t GetLeafNodeOffset(const wxString& text,
253  uint32_t initalOffset,
254  uint32_t buffSize,
255  uint16_t treeDepth,
256  chmUnitInfo *ui);
257 
259  bool ProcessWLC(uint64_t wlc_count, uint64_t wlc_size,
260  uint32_t wlc_offset, unsigned char ds,
261  unsigned char dr, unsigned char cs,
262  unsigned char cr, unsigned char ls,
263  unsigned char lr, chmUnitInfo *uifmain,
264  chmUnitInfo* uitbl, chmUnitInfo *uistrings,
265  chmUnitInfo* topics, chmUnitInfo *urlstr,
266  CHMSearchResults *results);
267 
269  bool InfoFromWindows();
270 
272  bool InfoFromSystem();
273 
275  bool BinaryTOC(wxTreeCtrl *toBuild);
276 
278  void RecurseLoadBTOC(UCharPtr& topidx, UCharPtr& topics,
279  UCharPtr& strings, UCharPtr& urltbl,
280  UCharPtr& urlstr, uint32_t offset,
281  wxTreeCtrl *toBuild, int level);
282 
284  bool GetItem(UCharPtr& topics, UCharPtr& strings, UCharPtr& urltbl,
285  UCharPtr& urlstr, uint32_t index, wxTreeCtrl* tree,
286  CHMListCtrl* list, const wxString& idxName,
287  int level, bool local);
288 
290  bool BinaryIndex(CHMListCtrl* toBuild, const wxCSConv& cv);
291 
293  bool ConvertFromUnicode(std::string& value, unsigned char* buffer,
294  size_t bufferLength);
295 
296 private:
297  chmFile* _chmFile;
298  wxString _filename;
299  wxString _home;
300  wxString _topicsFile;
301  wxString _indexFile;
302  wxString _title;
303  wxString _font;
304  wxFontEncoding _enc;
305  CHMIDMap _cidMap;
306 
307 private:
309  CHMFile(const CHMFile&);
310 
312  CHMFile& operator=(const CHMFile&);
313 };
314 
315 
316 #endif // __CHMFILE_H_
317 
318 
319 /*
320  Local Variables:
321  mode: c++
322  c-basic-offset: 8
323  tab-width: 8
324  c-indent-comments-syntactically-p: t
325  c-tab-always-indent: t
326  indent-tabs-mode: t
327  End:
328 */
329 
330 // vim:shiftwidth=8:autoindent:tabstop=8:noexpandtab:softtabstop=8
331 
bool AreContextIDsLoaded() const
Have the context-IDs been loaded into memory or not.
Definition: chmfile.h:202
wxString IndexFile() const
Gets name of the .hhk file in the archive that could potentially be used to generate content informat...
Definition: chmfile.h:108
bool IndexSearch(const wxString &text, bool wholeWords, bool titlesOnly, CHMSearchResults *results)
Fast search using the $FIftiMain file in the .chm.
Definition: chmfile.cpp:764
bool InfoFromSystem()
Looks up as much information as possible from #SYSTEM.
Definition: chmfile.cpp:1208
CHMFile & operator=(const CHMFile &)
No assignments.
~CHMFile()
Destructor. If a file has been succesfully opened, it closes it.
Definition: chmfile.cpp:180
wxString Title() const
Gets the name of the opened .chm.
Definition: chmfile.h:115
bool ResolveObject(const wxString &fileName, chmUnitInfo *ui)
Looks up fileName in the archive.
Definition: chmfile.cpp:909
bool ProcessWLC(uint64_t wlc_count, uint64_t wlc_size, uint32_t wlc_offset, unsigned char ds, unsigned char dr, unsigned char cs, unsigned char cr, unsigned char ls, unsigned char lr, chmUnitInfo *uifmain, chmUnitInfo *uitbl, chmUnitInfo *uistrings, chmUnitInfo *topics, chmUnitInfo *urlstr, CHMSearchResults *results)
Helper. Processes the word location code entries while searching.
Definition: chmfile.cpp:1003
wxString DefaultFont() const
Determines the font to use for special charsets.
Definition: chmfile.h:139
wxFontEncoding DesiredEncoding() const
Determines the encoding that the CHM creator would like me to use. To be used in combination with Def...
Definition: chmfile.h:133
void CloseCHM()
Closes the currently opened .chm, or does nothing if none opened.
Definition: chmfile.cpp:210
bool GetItem(UCharPtr &topics, UCharPtr &strings, UCharPtr &urltbl, UCharPtr &urlstr, uint32_t index, wxTreeCtrl *tree, CHMListCtrl *list, const wxString &idxName, int level, bool local)
Retrieve the data (name/URL) for a single entry (TOC or index)
Definition: chmfile.cpp:324
Definition: chmfile.cpp:43
bool IsValidCID(const int contextID)
Checks whether or not the context ID is valid for the loaded file.
Definition: chmfile.cpp:738
bool GetArchiveInfo()
Helper. Initializes most of the private data members.
Definition: chmfile.cpp:928
bool GetIndex(CHMListCtrl *toBuild)
Attempts to fill a CHMListCtrl by parsing the index file.
Definition: chmfile.cpp:634
wxString HomePage() const
Gets the name of the default page in the archive.
Definition: chmfile.h:82
wxString GetPageByCID(const int contextID)
Looks up the page referred to by the context-ID.
Definition: chmfile.cpp:750
Mostly a C++ wrapper around the CHMLIB facilities. Concrete class.
Definition: chmfile.h:60
wxString TopicsFile() const
Gets name of the .hhc file in the archive that could potentially be used to generate content informat...
Definition: chmfile.h:91
bool BinaryTOC(wxTreeCtrl *toBuild)
Load binary TOC (if available)
Definition: chmfile.cpp:230
size_t RetrieveObject(chmUnitInfo *ui, unsigned char *buffer, off_t fileOffset, size_t bufferSize)
Retrieves an uncompressed chunk of a file in the .chm.
Definition: chmfile.cpp:920
bool LoadCHM(const wxString &archiveName)
Attempts to load a .chm file from disk.
Definition: chmfile.cpp:186
List control class meant to emulate the look and feel of a wxListBox.
Definition: chmlistctrl.h:65
wxFontEncoding GetFontEncFromLCID(uint32_t lcid)
Helper. Translates from MS-specific LCID.
Definition: chmfile.cpp:1396
wxString ArchiveName() const
Gets the filename of the currently opened .chm file.
Definition: chmfile.h:99
bool HasChanged()
Checks if a &#39;ms-its&#39; link has been clicked and the chm changed.
wxFontEncoding GetFontEncFromCharSet(int cs)
Helper. Translates from Win32 encodings to generic wxWidgets ones.
Definition: chmfile.cpp:1338
bool LoadContextIDs()
Attempts to build an index of context-ID/page pairs from the file.
Definition: chmfile.cpp:677
void RecurseLoadBTOC(UCharPtr &topidx, UCharPtr &topics, UCharPtr &strings, UCharPtr &urltbl, UCharPtr &urlstr, uint32_t offset, wxTreeCtrl *toBuild, int level)
Try to recursively load the binary topics tree.
Definition: chmfile.cpp:286
CHMFile()
Default constructor.
Definition: chmfile.cpp:167
bool InfoFromWindows()
Looks up as much information as possible from #WINDOWS/#STRINGS.
Definition: chmfile.cpp:1106
bool ConvertFromUnicode(std::string &value, unsigned char *buffer, size_t bufferLength)
Get a regular string from a generic buffer of length bufferLength.
Definition: chmfile.cpp:467
uint32_t GetLeafNodeOffset(const wxString &text, uint32_t initalOffset, uint32_t buffSize, uint16_t treeDepth, chmUnitInfo *ui)
Helper. Returns the $FIftiMain offset of leaf node or 0.
Definition: chmfile.cpp:939
bool IsOk() const
Checks if the last attempt to load a .chm file was succesful.
Definition: chmfile.h:123
bool BinaryIndex(CHMListCtrl *toBuild, const wxCSConv &cv)
Get the binary index (if available)
Definition: chmfile.cpp:491
bool GetTopicsTree(wxTreeCtrl *toBuild)
Attempts to fill a wxTreeCtrl by parsing the topics file.
Definition: chmfile.cpp:425