]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlhelp_io.cpp
Replaced wxUSE_LIBGIF with wxUSE_GIF
[wxWidgets.git] / src / html / htmlhelp_io.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmlhelp.cpp
3// Purpose: Help controller
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
6// Licence: wxWindows Licence
7/////////////////////////////////////////////////////////////////////////////
8
9
10//#ifdef __GNUG__
11//#pragma implementation "htmlhelp.h"
12//#endif
13// --- already in htmlhelp.cpp
14
15#include <wx/wxprec.h>
16
17#include "wx/defs.h"
18#if wxUSE_HTML
19
20#ifdef __BORDLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WXPRECOMP
25#include <wx/wx.h>
26#endif
27
28
29#include <wx/wxhtml.h>
30#include <wx/busyinfo.h>
31
32
33
34
35class HP_Parser : public wxHtmlParser
36{
37 public:
38 void AddText(const char* text) {}
39 wxObject* GetProduct() {return NULL;}
40};
41
42
43
44class HP_TagHandler : public wxHtmlTagHandler
45{
46 private:
47 wxString m_Name, m_Page;
48 int m_Level;
49 int m_ID;
50 int m_Index;
51 HtmlContentsItem *m_Items;
52 int m_ItemsCnt;
53 HtmlBookRecord *m_Book;
54
55 public:
56 HP_TagHandler(HtmlBookRecord *b) : wxHtmlTagHandler() {m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; m_Level = 0;}
57 wxString GetSupportedTags() {return "UL,OBJECT,PARAM";}
58 bool HandleTag(const wxHtmlTag& tag);
59 void WriteOut(HtmlContentsItem*& array, int& size);
60 void ReadIn(HtmlContentsItem* array, int size);
61};
62
63
64bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
65{
66 if (tag.GetName() == "UL") {
67 m_Level++;
68 ParseInner(tag);
69 m_Level--;
70 return TRUE;
71 }
72
73 else if (tag.GetName() == "OBJECT") {
74 m_Name = m_Page = wxEmptyString;
75 ParseInner(tag);
76 if (m_Page != wxEmptyString) {
77 if (m_ItemsCnt % HTML_REALLOC_STEP == 0)
78 m_Items = (HtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + HTML_REALLOC_STEP) * sizeof(HtmlContentsItem));
79 m_Items[m_ItemsCnt].m_Level = m_Level;
80 m_Items[m_ItemsCnt].m_ID = m_ID;
2776d7c3 81 m_Items[m_ItemsCnt].m_Page = new char[m_Page.Length() + 1];
5526e819 82 strcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str());
2776d7c3 83 m_Items[m_ItemsCnt].m_Name = new char [m_Name.Length() + 1];
5526e819
VS
84 strcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str());
85 m_Items[m_ItemsCnt].m_Book = m_Book;
86 m_ItemsCnt++;
87 }
88 return TRUE;
89 }
90
91 else { // "PARAM"
92 if (m_Name == wxEmptyString && tag.GetParam("NAME") == "Name") m_Name = tag.GetParam("VALUE");
93 if (tag.GetParam("NAME") == "Local") m_Page = tag.GetParam("VALUE");
94 if (tag.GetParam("NAME") == "ID") tag.ScanParam("VALUE", "%i", &m_ID);
95 return FALSE;
96 }
97}
98
99
100
101void HP_TagHandler::WriteOut(HtmlContentsItem*& array, int& size)
102{
103 array = m_Items;
104 size = m_ItemsCnt;
105 m_Items = NULL;
106 m_ItemsCnt = 0;
107}
108
109void HP_TagHandler::ReadIn(HtmlContentsItem* array, int size)
110{
111 m_Items = array;
112 m_ItemsCnt = size;
113}
114
115
116
117
118void wxHtmlHelpController::LoadMSProject(HtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile, bool show_wait_msg)
119{
120 wxFSFile *f;
121 char *buf;
122 int sz;
123 wxString string;
124 wxBusyInfo *busyinfo = (show_wait_msg) ? new wxBusyInfo(_("Importing help file : \n") + book -> m_Title) : NULL;
125
126 HP_Parser parser;
127 HP_TagHandler *handler = new HP_TagHandler(book);
128 parser.AddTagHandler(handler);
129
6245e5b6
HH
130 // Don't panic if no index or contensfile is supplied.
131 // (without contents is a bit useless, but leaving out the index is sometimes handy)
132 f = ( contentsfile.IsEmpty() ? NULL : fsys.OpenFile(contentsfile) );
5526e819 133 if (f) {
259d1674 134 sz = f -> GetStream() -> GetSize();
2776d7c3 135 buf = new char[sz+1];
5526e819
VS
136 buf[sz] = 0;
137 f -> GetStream() -> Read(buf, sz);
138 delete f;
139 handler -> ReadIn(m_Contents, m_ContentsCnt);
140 parser.Parse(buf);
141 handler -> WriteOut(m_Contents, m_ContentsCnt);
2776d7c3 142 delete[] buf;
5526e819
VS
143 }
144
6245e5b6 145 f = ( indexfile.IsEmpty() ? NULL : fsys.OpenFile(indexfile) );
5526e819 146 if (f) {
259d1674 147 sz = f -> GetStream() -> GetSize();
2776d7c3 148 buf = new char[sz+1];
5526e819
VS
149 buf[sz] = 0;
150 f -> GetStream() -> Read(buf, sz);
151 delete f;
152 handler -> ReadIn(m_Index, m_IndexCnt);
153 parser.Parse(buf);
154 handler -> WriteOut(m_Index, m_IndexCnt);
2776d7c3 155 delete[] buf;
5526e819
VS
156 }
157 if (show_wait_msg) delete busyinfo;
158}
159
160
161
162
163
164
165void wxHtmlHelpController::LoadCachedBook(HtmlBookRecord *book, wxInputStream *f)
166{
167 int i, st;
168 int x;
169
170 /* load contents : */
171
172 f -> Read(&x, sizeof(x));
173 st = m_ContentsCnt;
174 m_ContentsCnt += x;
175 m_Contents = (HtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt / HTML_REALLOC_STEP + 1) * HTML_REALLOC_STEP * sizeof(HtmlContentsItem));
176 for (i = st; i < m_ContentsCnt; i++) {
177 f -> Read(&x, sizeof(x));
178 m_Contents[i].m_Level = x;
179 f -> Read(&x, sizeof(x));
180 m_Contents[i].m_ID = x;
181 f -> Read(&x, sizeof(x));
2776d7c3 182 m_Contents[i].m_Name = new char[x];
5526e819
VS
183 f -> Read(m_Contents[i].m_Name, x);
184 f -> Read(&x, sizeof(x));
2776d7c3 185 m_Contents[i].m_Page = new char[x];
5526e819
VS
186 f -> Read(m_Contents[i].m_Page, x);
187 m_Contents[i].m_Book = book;
188 }
189
190 /* load index : */
191
192 f -> Read(&x, sizeof(x));
193 st = m_IndexCnt;
194 m_IndexCnt += x;
195 m_Index = (HtmlContentsItem*) realloc(m_Index, (m_IndexCnt / HTML_REALLOC_STEP + 1) * HTML_REALLOC_STEP * sizeof(HtmlContentsItem));
196 for (i = st; i < m_IndexCnt; i++) {
197 f -> Read(&x, sizeof(x));
2776d7c3 198 m_Index[i].m_Name = new char[x];
5526e819
VS
199 f -> Read(m_Index[i].m_Name, x);
200 f -> Read(&x, sizeof(x));
2776d7c3 201 m_Index[i].m_Page = new char[x];
5526e819
VS
202 f -> Read(m_Index[i].m_Page, x);
203 m_Index[i].m_Book = book;
204 }
205}
206
207
208
209
210
211
212void wxHtmlHelpController::SaveCachedBook(HtmlBookRecord *book, wxOutputStream *f)
213{
214 int i;
215 int x;
216
217 /* save contents : */
218
219 x = 0;
220 for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++;
221 f -> Write(&x, sizeof(x));
222 for (i = 0; i < m_ContentsCnt; i++) {
223 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue;
224 x = m_Contents[i].m_Level;
225 f -> Write(&x, sizeof(x));
226 x = m_Contents[i].m_ID;
227 f -> Write(&x, sizeof(x));
228 x = strlen(m_Contents[i].m_Name) + 1;
229 f -> Write(&x, sizeof(x));
230 f -> Write(m_Contents[i].m_Name, x);
231 x = strlen(m_Contents[i].m_Page) + 1;
232 f -> Write(&x, sizeof(x));
233 f -> Write(m_Contents[i].m_Page, x);
234 }
235
236 /* save index : */
237
238 x = 0;
239 for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++;
240 f -> Write(&x, sizeof(x));
241 for (i = 0; i < m_IndexCnt; i++) {
242 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue;
243 x = strlen(m_Index[i].m_Name) + 1;
244 f -> Write(&x, sizeof(x));
245 f -> Write(m_Index[i].m_Name, x);
246 x = strlen(m_Index[i].m_Page) + 1;
247 f -> Write(&x, sizeof(x));
248 f -> Write(m_Index[i].m_Page, x);
249 }
250}
251
6245e5b6 252#endif