]> git.saurik.com Git - wxWidgets.git/blame - utils/wxrc/wxrc.cpp
removed libxpm dependencies from makefiles
[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);
c8b7a961
VS
65
66 void OutputGettext();
67 wxArrayString FindStrings();
68 wxArrayString FindStrings(wxXmlNode *node);
56d2f750 69
c8b7a961 70 bool flagVerbose, flagCPP, flagCompress, flagGettext;
56d2f750
VS
71 wxString parOutput, parFuncname, parOutputPath;
72 wxArrayString parFiles;
73 int retCode;
74};
75
76IMPLEMENT_APP(XmlResApp)
77
031dfec8
JS
78#if wxUSE_GUI
79bool XmlResApp::OnInit()
80#else
56d2f750 81int XmlResApp::OnRun()
031dfec8 82#endif
56d2f750
VS
83{
84 static const wxCmdLineEntryDesc cmdLineDesc[] =
85 {
c8b7a961 86 { wxCMD_LINE_SWITCH, "h", "help", "show help message" },
56d2f750
VS
87 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
88 { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
89 { wxCMD_LINE_SWITCH, "u", "uncompressed", "do not compress .xml files (C++ only)" },
c8b7a961 90 { wxCMD_LINE_SWITCH, "g", "gettext", "output .po catalog (to stdout or file if -o used)" },
56d2f750 91 { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
e066e256 92 { wxCMD_LINE_OPTION, "o", "output", "output file [resource.xrs/cpp]" },
a793c17b 93 { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
56d2f750 94
c8b7a961 95 { wxCMD_LINE_PARAM, NULL, NULL, "input file(s)",
56d2f750
VS
96 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
97
98 { wxCMD_LINE_NONE }
99 };
100
a793c17b
VS
101 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ);
102
56d2f750
VS
103 wxCmdLineParser parser(cmdLineDesc, argc, argv);
104
105 switch (parser.Parse())
106 {
107 case -1:
108 return 0;
109 break;
110
111 case 0:
112 retCode = 0;
113 ParseParams(parser);
c8b7a961
VS
114 if (flagGettext)
115 OutputGettext();
116 else
117 CompileRes();
031dfec8
JS
118#if wxUSE_GUI
119 return FALSE;
120#else
56d2f750 121 return retCode;
031dfec8 122#endif
56d2f750
VS
123 break;
124
125 default:
031dfec8
JS
126#if wxUSE_GUI
127 return FALSE;
128#else
56d2f750 129 return 1;
031dfec8 130#endif
56d2f750
VS
131 break;
132 }
133}
134
135
136
137
138void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
139{
c8b7a961 140 flagGettext = cmdline.Found("g");
56d2f750
VS
141 flagVerbose = cmdline.Found("v");
142 flagCPP = cmdline.Found("c");
143 flagCompress = flagCPP && !cmdline.Found("u");
144
145 if (!cmdline.Found("o", &parOutput))
c8b7a961
VS
146 {
147 if (flagGettext)
148 parOutput = wxEmptyString;
149 else
150 parOutput = flagCPP ? "resource.cpp" : "resource.xrs";
151 }
56d2f750
VS
152 parOutputPath = wxPathOnly(parOutput);
153 if (!parOutputPath) parOutputPath = ".";
154
155 if (!cmdline.Found("n", &parFuncname))
156 parFuncname = "InitXmlResource";
157
158 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
159 parFiles.Add(cmdline.GetParam(i));
160}
161
162
163
164
165void XmlResApp::CompileRes()
166{
167 wxArrayString files = PrepareTempFiles();
168
169 wxRemoveFile(parOutput);
170
56d2f750
VS
171 if (!retCode)
172 {
173 if (flagCPP)
174 MakePackageCPP(files);
175 else
176 MakePackageZIP(files);
177 }
178
179 DeleteTempFiles(files);
180}
181
182
183
184wxArrayString XmlResApp::PrepareTempFiles()
185{
186 wxArrayString flist;
187
188 for (size_t i = 0; i < parFiles.Count(); i++)
189 {
190 if (flagVerbose)
191 wxPrintf("processing " + parFiles[i] + "...\n");
192
193 wxXmlDocument doc;
194
195 if (!doc.Load(parFiles[i]))
196 {
197 wxLogError("Error parsing file " + parFiles[i]);
198 retCode = 1;
199 continue;
200 }
201
f6853b4a
VS
202 wxString name, ext, path;
203 wxSplitPath(parFiles[i], &path, &name, &ext);
204
205 FindFilesInXML(doc.GetRoot(), flist, path);
56d2f750 206
e066e256
VS
207 doc.Save(parOutputPath + "/" + name + ".xrc", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN);
208 flist.Add(name + ".xrc");
56d2f750
VS
209 }
210
211 return flist;
212}
213
214
215
f6853b4a
VS
216// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
217void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
218{
219 wxXmlNode *n = node;
220 if (n == NULL) return;
221 n = n->GetChildren();
222
223 while (n)
224 {
225 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
226 // parent is an element, i.e. has subnodes...
227 (n->GetType() == wxXML_TEXT_NODE ||
228 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
229 // ...it is textnode...
230 (node/*not n!*/->GetName() == "bitmap"))
231 // ...and known to contain filename
232 {
233 wxString fullname;
234 wxString filename = n->GetContent();
235 if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent();
236 else fullname = inputPath + "/" + n->GetContent();
237
238 filename.Replace("/", "_");
239 filename.Replace("\\", "_");
240 filename.Replace("*", "_");
241 filename.Replace("?", "_");
242 n->SetContent(filename);
243
244 if (flagVerbose)
245 wxPrintf("adding " + filename + "...\n");
246
247 flist.Add(filename);
248
249 wxFileInputStream sin(fullname);
250 wxFileOutputStream sout(parOutputPath + "/" + filename);
251 sin.Read(sout); // copy the stream
252 }
253
254 // subnodes:
255 if (n->GetType() == wxXML_ELEMENT_NODE)
256 FindFilesInXML(n, flist, inputPath);
257
258 n = n->GetNext();
259 }
260}
261
262
263
56d2f750
VS
264void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
265{
266 for (size_t i = 0; i < flist.Count(); i++)
267 wxRemoveFile(parOutputPath + "/" + flist[i]);
268}
269
270
271
272void XmlResApp::MakePackageZIP(const wxArrayString& flist)
273{
274 wxString files;
275
276 for (size_t i = 0; i < flist.Count(); i++)
277 files += flist[i] + " ";
278 files.RemoveLast();
279
280 if (flagVerbose)
281 wxPrintf("compressing " + parOutput + "...\n");
282
283 if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") +
284 parOutput + " " + files, TRUE) == -1)
285 {
286 wxLogError("Unable to execute zip program. Make sure it is in the path.");
287 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
288 retCode = 1;
289 return;
290 }
291}
292
293
294
295
296static wxString FileToCppArray(wxString filename, int num)
297{
298 wxString output;
56d2f750 299 wxString tmp;
f6853b4a 300 wxString snum;
56d2f750
VS
301 wxFFile file(filename, "rb");
302 size_t lng = file.Length();
303
304 snum.Printf("%i", num);
305 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
e066e256
VS
306 output += "static unsigned char xml_res_file_" + snum + "[] = {\n";
307 // we cannot use string literals because MSVC is dumb wannabe compiler
308 // with arbitrary limitation to 2048 strings :(
56d2f750
VS
309
310 unsigned char *buffer = new unsigned char[lng];
311 file.Read(buffer, lng);
312
f6853b4a 313 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 314 {
e066e256
VS
315 tmp.Printf("%i", buffer[i]);
316 if (i != 0) output << ',';
317 if (linelng > 70)
f6853b4a
VS
318 {
319 linelng = 0;
e066e256 320 output << "\n";
f6853b4a 321 }
e066e256
VS
322 output << tmp;
323 linelng += tmp.Length()+1;
56d2f750
VS
324 }
325
326 delete[] buffer;
327
e066e256 328 output += "};\n\n";
56d2f750
VS
329
330 return output;
331}
332
333
334void XmlResApp::MakePackageCPP(const wxArrayString& flist)
335{
336 wxFFile file(parOutput, "wt");
337 size_t i;
338
339 if (flagVerbose)
340 wxPrintf("creating C++ source file " + parOutput + "...\n");
341
342 file.Write("\
343#include \"wx/wxprec.h\"\n\
344\n\
345#ifdef __BORLANDC__\n\
346 #pragma hdrstop\n\
347#endif\n\
348\n\
349#ifndef WX_PRECOMP\n\
350 #include \"wx/wx.h\"\n\
351#endif\n\
352\
353#include \"wx/filesys.h\"\n\
354#include \"wx/fs_mem.h\"\n\
355#include \"wx/xml/xmlres.h\"\n\
356#include \"wx/xml/xh_all.h\"\n\
357\n");
358
359 for (i = 0; i < flist.Count(); i++)
360 file.Write(FileToCppArray(flist[i], i));
361
362 file.Write("\
363void " + parFuncname + "()\n\
364{\n\
365\n\
366 // Check for memory FS. If not present, load the handler:\n\
367 {\n\
368 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
369 wxFileSystem fsys;\n\
a793c17b 370 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
56d2f750
VS
371 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
372 if (f) delete f;\n\
373 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
374 }\n\
375\n");
376
377 for (i = 0; i < flist.Count(); i++)
378 {
379 wxString s;
380 s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
f6853b4a 381 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
56d2f750
VS
382 file.Write(s);
383 }
f6853b4a
VS
384
385 for (i = 0; i < parFiles.Count(); i++)
386 {
387 wxString name, ext, path;
388 wxSplitPath(parFiles[i], &path, &name, &ext);
389 file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" +
e066e256 390 name + ".xrc" + "\");\n");
f6853b4a 391 }
56d2f750
VS
392
393 file.Write("\n}\n");
394
f6853b4a 395
56d2f750 396}
c8b7a961
VS
397
398
399
400void XmlResApp::OutputGettext()
401{
402 wxArrayString str = FindStrings();
403
404 wxFFile fout;
405 if (!parOutput) fout.Attach(stdout);
406 else fout.Open(parOutput, _T("wt"));
407
408 for (size_t i = 0; i < str.GetCount(); i++)
409 fout.Write(_T("msgid \"") + str[i] + _T("\"\nmsgstr \"\"\n\n"));
410
411 if (!parOutput) fout.Detach();
412}
413
414
415
416wxArrayString XmlResApp::FindStrings()
417{
418 wxArrayString arr, a2;
419
420 for (size_t i = 0; i < parFiles.Count(); i++)
421 {
422 if (flagVerbose)
423 wxPrintf("processing " + parFiles[i] + "...\n");
424
425 wxXmlDocument doc;
426 if (!doc.Load(parFiles[i]))
427 {
428 wxLogError("Error parsing file " + parFiles[i]);
429 retCode = 1;
430 continue;
431 }
432 a2 = FindStrings(doc.GetRoot());
433 WX_APPEND_ARRAY(arr, a2);
434 }
435
436 return arr;
437}
438
439
440
441wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
442{
443 wxArrayString arr;
444
445 wxXmlNode *n = node;
446 if (n == NULL) return arr;
447 n = n->GetChildren();
448
449 while (n)
450 {
451 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
452 // parent is an element, i.e. has subnodes...
453 (n->GetType() == wxXML_TEXT_NODE ||
454 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
455 // ...it is textnode...
456 (
457 node/*not n!*/->GetName() == _T("label") ||
458 (node/*not n!*/->GetName() == _T("value") &&
459 !n->GetContent().IsNumber()) ||
460 node/*not n!*/->GetName() == _T("help") ||
461 node/*not n!*/->GetName() == _T("longhelp") ||
462 node/*not n!*/->GetName() == _T("tooltip") ||
463 node/*not n!*/->GetName() == _T("htmlcode") ||
464 node/*not n!*/->GetName() == _T("title")
465 ))
466 // ...and known to contain filename
467 {
468 arr.Add(n->GetContent());
469 }
470
471 // subnodes:
472 if (n->GetType() == wxXML_ELEMENT_NODE)
473 {
474 wxArrayString a2 = FindStrings(n);
475 WX_APPEND_ARRAY(arr, a2);
476 }
477
478 n = n->GetNext();
479 }
480 return arr;
481}