]> git.saurik.com Git - wxWidgets.git/blame - utils/wxrc/wxrc.cpp
Removed 'interface' pragma for gcc 2.96
[wxWidgets.git] / utils / wxrc / wxrc.cpp
CommitLineData
56d2f750
VS
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"
a793c17b 31#include "wx/xml/xmlio.h"
56d2f750 32#include "wx/ffile.h"
f6853b4a
VS
33#include "wx/wfstream.h"
34
35
36
37
56d2f750 38
031dfec8 39/*
56d2f750
VS
40#if wxUSE_GUI
41#error "You must compile the resource compiler with wxBase!"
42#endif
031dfec8 43*/
56d2f750
VS
44
45class XmlResApp : public wxApp
46{
47public:
031dfec8
JS
48
49#if wxUSE_GUI
50 bool OnInit();
51#else
56d2f750 52 virtual int OnRun();
031dfec8 53#endif
56d2f750
VS
54
55private:
56
57 void ParseParams(const wxCmdLineParser& cmdline);
58 void CompileRes();
59 wxArrayString PrepareTempFiles();
f6853b4a
VS
60 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
61
56d2f750
VS
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
72IMPLEMENT_APP(XmlResApp)
73
031dfec8
JS
74#if wxUSE_GUI
75bool XmlResApp::OnInit()
76#else
56d2f750 77int XmlResApp::OnRun()
031dfec8 78#endif
56d2f750
VS
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]" },
e066e256 86 { wxCMD_LINE_OPTION, "o", "output", "output file [resource.xrs/cpp]" },
a793c17b 87 { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
56d2f750
VS
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
a793c17b
VS
95 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ);
96
56d2f750
VS
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();
031dfec8
JS
109#if wxUSE_GUI
110 return FALSE;
111#else
56d2f750 112 return retCode;
031dfec8 113#endif
56d2f750
VS
114 break;
115
116 default:
031dfec8
JS
117#if wxUSE_GUI
118 return FALSE;
119#else
56d2f750 120 return 1;
031dfec8 121#endif
56d2f750
VS
122 break;
123 }
124}
125
126
127
128
129void 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))
e066e256 136 parOutput = flagCPP ? "resource.cpp" : "resource.xrs";
56d2f750
VS
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
150void XmlResApp::CompileRes()
151{
152 wxArrayString files = PrepareTempFiles();
153
154 wxRemoveFile(parOutput);
155
56d2f750
VS
156 if (!retCode)
157 {
158 if (flagCPP)
159 MakePackageCPP(files);
160 else
161 MakePackageZIP(files);
162 }
163
164 DeleteTempFiles(files);
165}
166
167
168
169wxArrayString 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
f6853b4a
VS
187 wxString name, ext, path;
188 wxSplitPath(parFiles[i], &path, &name, &ext);
189
190 FindFilesInXML(doc.GetRoot(), flist, path);
56d2f750 191
e066e256
VS
192 doc.Save(parOutputPath + "/" + name + ".xrc", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN);
193 flist.Add(name + ".xrc");
56d2f750
VS
194 }
195
196 return flist;
197}
198
199
200
f6853b4a
VS
201// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
202void 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
56d2f750
VS
249void 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
257void 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
281static wxString FileToCppArray(wxString filename, int num)
282{
283 wxString output;
56d2f750 284 wxString tmp;
f6853b4a 285 wxString snum;
56d2f750
VS
286 wxFFile file(filename, "rb");
287 size_t lng = file.Length();
288
289 snum.Printf("%i", num);
290 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
e066e256
VS
291 output += "static unsigned char xml_res_file_" + snum + "[] = {\n";
292 // we cannot use string literals because MSVC is dumb wannabe compiler
293 // with arbitrary limitation to 2048 strings :(
56d2f750
VS
294
295 unsigned char *buffer = new unsigned char[lng];
296 file.Read(buffer, lng);
297
f6853b4a 298 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 299 {
e066e256
VS
300 tmp.Printf("%i", buffer[i]);
301 if (i != 0) output << ',';
302 if (linelng > 70)
f6853b4a
VS
303 {
304 linelng = 0;
e066e256 305 output << "\n";
f6853b4a 306 }
e066e256
VS
307 output << tmp;
308 linelng += tmp.Length()+1;
56d2f750
VS
309 }
310
311 delete[] buffer;
312
e066e256 313 output += "};\n\n";
56d2f750
VS
314
315 return output;
316}
317
318
319void XmlResApp::MakePackageCPP(const wxArrayString& flist)
320{
321 wxFFile file(parOutput, "wt");
322 size_t i;
323
324 if (flagVerbose)
325 wxPrintf("creating C++ source file " + parOutput + "...\n");
326
327 file.Write("\
328#include \"wx/wxprec.h\"\n\
329\n\
330#ifdef __BORLANDC__\n\
331 #pragma hdrstop\n\
332#endif\n\
333\n\
334#ifndef WX_PRECOMP\n\
335 #include \"wx/wx.h\"\n\
336#endif\n\
337\
338#include \"wx/filesys.h\"\n\
339#include \"wx/fs_mem.h\"\n\
340#include \"wx/xml/xmlres.h\"\n\
341#include \"wx/xml/xh_all.h\"\n\
342\n");
343
344 for (i = 0; i < flist.Count(); i++)
345 file.Write(FileToCppArray(flist[i], i));
346
347 file.Write("\
348void " + parFuncname + "()\n\
349{\n\
350\n\
351 // Check for memory FS. If not present, load the handler:\n\
352 {\n\
353 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
354 wxFileSystem fsys;\n\
a793c17b 355 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
56d2f750
VS
356 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
357 if (f) delete f;\n\
358 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
359 }\n\
360\n");
361
362 for (i = 0; i < flist.Count(); i++)
363 {
364 wxString s;
365 s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
f6853b4a 366 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
56d2f750
VS
367 file.Write(s);
368 }
f6853b4a
VS
369
370 for (i = 0; i < parFiles.Count(); i++)
371 {
372 wxString name, ext, path;
373 wxSplitPath(parFiles[i], &path, &name, &ext);
374 file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" +
e066e256 375 name + ".xrc" + "\");\n");
f6853b4a 376 }
56d2f750
VS
377
378 file.Write("\n}\n");
379
f6853b4a 380
56d2f750 381}