stored bitmaps in generated resources
[wxWidgets.git] / utils / wxrc / wxrc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxrc.cpp
3 // Purpose: XML resource compiler
4 // Author: Vaclav Slavik
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #pragma interface
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 // for all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWindows headers
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #include "wx/cmdline.h"
30 #include "wx/xml/xml.h"
31 #include "wx/xml/xmlio.h"
32 #include "wx/ffile.h"
33 #include "wx/wfstream.h"
34
35
36
37
38
39 /*
40 #if wxUSE_GUI
41 #error "You must compile the resource compiler with wxBase!"
42 #endif
43 */
44
45 class XmlResApp : public wxApp
46 {
47 public:
48
49 #if wxUSE_GUI
50 bool OnInit();
51 #else
52 virtual int OnRun();
53 #endif
54
55 private:
56
57 void ParseParams(const wxCmdLineParser& cmdline);
58 void CompileRes();
59 wxArrayString PrepareTempFiles();
60 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
61
62 void DeleteTempFiles(const wxArrayString& flist);
63 void MakePackageZIP(const wxArrayString& flist);
64 void MakePackageCPP(const wxArrayString& flist);
65
66 bool flagVerbose, flagCPP, flagCompress;
67 wxString parOutput, parFuncname, parOutputPath;
68 wxArrayString parFiles;
69 int retCode;
70 };
71
72 IMPLEMENT_APP(XmlResApp)
73
74 #if wxUSE_GUI
75 bool XmlResApp::OnInit()
76 #else
77 int XmlResApp::OnRun()
78 #endif
79 {
80 static const wxCmdLineEntryDesc cmdLineDesc[] =
81 {
82 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
83 { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
84 { wxCMD_LINE_SWITCH, "u", "uncompressed", "do not compress .xml files (C++ only)" },
85 { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
86 { wxCMD_LINE_OPTION, "o", "output", "output file [resource.rsc/cpp]" },
87 { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
88
89 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
90 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
91
92 { wxCMD_LINE_NONE }
93 };
94
95 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ);
96
97 wxCmdLineParser parser(cmdLineDesc, argc, argv);
98
99 switch (parser.Parse())
100 {
101 case -1:
102 return 0;
103 break;
104
105 case 0:
106 retCode = 0;
107 ParseParams(parser);
108 CompileRes();
109 #if wxUSE_GUI
110 return FALSE;
111 #else
112 return retCode;
113 #endif
114 break;
115
116 default:
117 #if wxUSE_GUI
118 return FALSE;
119 #else
120 return 1;
121 #endif
122 break;
123 }
124 }
125
126
127
128
129 void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
130 {
131 flagVerbose = cmdline.Found("v");
132 flagCPP = cmdline.Found("c");
133 flagCompress = flagCPP && !cmdline.Found("u");
134
135 if (!cmdline.Found("o", &parOutput))
136 parOutput = flagCPP ? "resource.cpp" : "resource.rsc";
137 parOutputPath = wxPathOnly(parOutput);
138 if (!parOutputPath) parOutputPath = ".";
139
140 if (!cmdline.Found("n", &parFuncname))
141 parFuncname = "InitXmlResource";
142
143 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
144 parFiles.Add(cmdline.GetParam(i));
145 }
146
147
148
149
150 void XmlResApp::CompileRes()
151 {
152 wxArrayString files = PrepareTempFiles();
153
154 wxRemoveFile(parOutput);
155
156 if (!retCode)
157 {
158 if (flagCPP)
159 MakePackageCPP(files);
160 else
161 MakePackageZIP(files);
162 }
163
164 DeleteTempFiles(files);
165 }
166
167
168
169 wxArrayString XmlResApp::PrepareTempFiles()
170 {
171 wxArrayString flist;
172
173 for (size_t i = 0; i < parFiles.Count(); i++)
174 {
175 if (flagVerbose)
176 wxPrintf("processing " + parFiles[i] + "...\n");
177
178 wxXmlDocument doc;
179
180 if (!doc.Load(parFiles[i]))
181 {
182 wxLogError("Error parsing file " + parFiles[i]);
183 retCode = 1;
184 continue;
185 }
186
187 wxString name, ext, path;
188 wxSplitPath(parFiles[i], &path, &name, &ext);
189
190 FindFilesInXML(doc.GetRoot(), flist, path);
191
192 doc.Save(parOutputPath + "/" + name + ".xmb", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN);
193 flist.Add(name + ".xmb");
194 }
195
196 return flist;
197 }
198
199
200
201 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
202 void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
203 {
204 wxXmlNode *n = node;
205 if (n == NULL) return;
206 n = n->GetChildren();
207
208 while (n)
209 {
210 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
211 // parent is an element, i.e. has subnodes...
212 (n->GetType() == wxXML_TEXT_NODE ||
213 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
214 // ...it is textnode...
215 (node/*not n!*/->GetName() == "bitmap"))
216 // ...and known to contain filename
217 {
218 wxString fullname;
219 wxString filename = n->GetContent();
220 if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent();
221 else fullname = inputPath + "/" + n->GetContent();
222
223 filename.Replace("/", "_");
224 filename.Replace("\\", "_");
225 filename.Replace("*", "_");
226 filename.Replace("?", "_");
227 n->SetContent(filename);
228
229 if (flagVerbose)
230 wxPrintf("adding " + filename + "...\n");
231
232 flist.Add(filename);
233
234 wxFileInputStream sin(fullname);
235 wxFileOutputStream sout(parOutputPath + "/" + filename);
236 sin.Read(sout); // copy the stream
237 }
238
239 // subnodes:
240 if (n->GetType() == wxXML_ELEMENT_NODE)
241 FindFilesInXML(n, flist, inputPath);
242
243 n = n->GetNext();
244 }
245 }
246
247
248
249 void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
250 {
251 for (size_t i = 0; i < flist.Count(); i++)
252 wxRemoveFile(parOutputPath + "/" + flist[i]);
253 }
254
255
256
257 void XmlResApp::MakePackageZIP(const wxArrayString& flist)
258 {
259 wxString files;
260
261 for (size_t i = 0; i < flist.Count(); i++)
262 files += flist[i] + " ";
263 files.RemoveLast();
264
265 if (flagVerbose)
266 wxPrintf("compressing " + parOutput + "...\n");
267
268 if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") +
269 parOutput + " " + files, TRUE) == -1)
270 {
271 wxLogError("Unable to execute zip program. Make sure it is in the path.");
272 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
273 retCode = 1;
274 return;
275 }
276 }
277
278
279
280
281 static wxString FileToCppArray(wxString filename, int num)
282 {
283 wxString output;
284 wxString tmp;
285 wxString snum;
286 wxFFile file(filename, "rb");
287 size_t lng = file.Length();
288 int linelng;
289
290 snum.Printf("%i", num);
291 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
292 output += "static unsigned char xml_res_file_" + snum + "[] = \"\\\n";
293
294 unsigned char *buffer = new unsigned char[lng];
295 file.Read(buffer, lng);
296
297 for (size_t i = 0, linelng = 0; i < lng; i++)
298 {
299 if (linelng > 70)
300 {
301 linelng = 0;
302 output += "\\\n";
303 }
304 if (buffer[i] < 32 || buffer[i] == '"' || buffer[i] == '\\')
305 {
306 tmp.Printf("\\%03o", buffer[i]);
307 output += tmp;
308 linelng += 4;
309 }
310 else
311 {
312 output << (wxChar)buffer[i];
313 linelng++;
314 }
315 }
316
317 delete[] buffer;
318
319 output += "\"\n;\n\n";
320
321 return output;
322 }
323
324
325 void XmlResApp::MakePackageCPP(const wxArrayString& flist)
326 {
327 wxFFile file(parOutput, "wt");
328 size_t i;
329
330 if (flagVerbose)
331 wxPrintf("creating C++ source file " + parOutput + "...\n");
332
333 file.Write("\
334 #include \"wx/wxprec.h\"\n\
335 \n\
336 #ifdef __BORLANDC__\n\
337 #pragma hdrstop\n\
338 #endif\n\
339 \n\
340 #ifndef WX_PRECOMP\n\
341 #include \"wx/wx.h\"\n\
342 #endif\n\
343 \
344 #include \"wx/filesys.h\"\n\
345 #include \"wx/fs_mem.h\"\n\
346 #include \"wx/xml/xmlres.h\"\n\
347 #include \"wx/xml/xh_all.h\"\n\
348 \n");
349
350 for (i = 0; i < flist.Count(); i++)
351 file.Write(FileToCppArray(flist[i], i));
352
353 file.Write("\
354 void " + parFuncname + "()\n\
355 {\n\
356 \n\
357 // Check for memory FS. If not present, load the handler:\n\
358 {\n\
359 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
360 wxFileSystem fsys;\n\
361 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
362 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
363 if (f) delete f;\n\
364 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
365 }\n\
366 \n");
367
368 for (i = 0; i < flist.Count(); i++)
369 {
370 wxString s;
371 s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
372 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
373 file.Write(s);
374 }
375
376 for (i = 0; i < parFiles.Count(); i++)
377 {
378 wxString name, ext, path;
379 wxSplitPath(parFiles[i], &path, &name, &ext);
380 file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" +
381 name + ".xmb" + "\");\n");
382 }
383
384 file.Write("\n}\n");
385
386
387 }