]> git.saurik.com Git - wxWidgets.git/blame - utils/wxrc/wxrc.cpp
fix linking on AIX/RS6000 by using -mminimal-toc option
[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"
999d9a9f 30#include "wx/xrc/xml.h"
56d2f750 31#include "wx/ffile.h"
f6853b4a
VS
32#include "wx/wfstream.h"
33
34
35
36
56d2f750 37
031dfec8 38/*
56d2f750
VS
39#if wxUSE_GUI
40#error "You must compile the resource compiler with wxBase!"
41#endif
031dfec8 42*/
56d2f750
VS
43
44class XmlResApp : public wxApp
45{
46public:
031dfec8
JS
47
48#if wxUSE_GUI
49 bool OnInit();
50#else
56d2f750 51 virtual int OnRun();
031dfec8 52#endif
56d2f750
VS
53
54private:
55
56 void ParseParams(const wxCmdLineParser& cmdline);
57 void CompileRes();
58 wxArrayString PrepareTempFiles();
f6853b4a
VS
59 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
60
a7501aeb 61 wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
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
4d876ee3 70 bool flagVerbose, flagCPP, 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 {
99cd20be
VS
86 { wxCMD_LINE_SWITCH, "h", "help", "show help message",
87 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
56d2f750
VS
88 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
89 { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
89a28180 90 { wxCMD_LINE_SWITCH, "g", "gettext", "output list of translatable strings (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]" },
99cd20be 93#if 0 // not yet implemented
a793c17b 94 { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
99cd20be 95#endif
c8b7a961 96 { wxCMD_LINE_PARAM, NULL, NULL, "input file(s)",
99cd20be
VS
97 wxCMD_LINE_VAL_STRING,
98 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
56d2f750
VS
99
100 { wxCMD_LINE_NONE }
101 };
102
99cd20be 103#if wxUSE_GUI && !defined(__WXMSW__)
cb0b3950 104 // VS: I need reasonable output to console from wxCmdLineParser
99cd20be 105 // - temporary, will hopefully be fixed in future in wxWin
cb0b3950
VS
106 wxLog::SetTimestamp(NULL);
107 delete wxLog::SetActiveTarget(new wxLogStderr);
108#endif
109
56d2f750
VS
110 wxCmdLineParser parser(cmdLineDesc, argc, argv);
111
112 switch (parser.Parse())
113 {
114 case -1:
115 return 0;
116 break;
117
118 case 0:
119 retCode = 0;
120 ParseParams(parser);
c8b7a961
VS
121 if (flagGettext)
122 OutputGettext();
123 else
124 CompileRes();
031dfec8
JS
125#if wxUSE_GUI
126 return FALSE;
127#else
56d2f750 128 return retCode;
031dfec8 129#endif
56d2f750
VS
130 break;
131
132 default:
031dfec8
JS
133#if wxUSE_GUI
134 return FALSE;
135#else
56d2f750 136 return 1;
031dfec8 137#endif
56d2f750
VS
138 break;
139 }
140}
141
142
143
144
145void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
146{
c8b7a961 147 flagGettext = cmdline.Found("g");
56d2f750
VS
148 flagVerbose = cmdline.Found("v");
149 flagCPP = cmdline.Found("c");
56d2f750
VS
150
151 if (!cmdline.Found("o", &parOutput))
c8b7a961
VS
152 {
153 if (flagGettext)
154 parOutput = wxEmptyString;
155 else
156 parOutput = flagCPP ? "resource.cpp" : "resource.xrs";
157 }
56d2f750
VS
158 parOutputPath = wxPathOnly(parOutput);
159 if (!parOutputPath) parOutputPath = ".";
160
161 if (!cmdline.Found("n", &parFuncname))
162 parFuncname = "InitXmlResource";
163
164 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
165 parFiles.Add(cmdline.GetParam(i));
166}
167
168
169
170
171void XmlResApp::CompileRes()
172{
173 wxArrayString files = PrepareTempFiles();
174
175 wxRemoveFile(parOutput);
176
56d2f750
VS
177 if (!retCode)
178 {
179 if (flagCPP)
180 MakePackageCPP(files);
181 else
182 MakePackageZIP(files);
183 }
184
185 DeleteTempFiles(files);
186}
187
188
a7501aeb
VS
189wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
190{
191 wxString name2 = name;
192 name2.Replace(":", "_");
193 name2.Replace("/", "_");
194 name2.Replace("\\", "_");
195 name2.Replace("*", "_");
196 name2.Replace("?", "_");
197
198 wxString s = wxFileNameFromPath(parOutput) + "$" + name2;
199
200 if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
201 {
202 for (int i = 0;; i++)
203 {
204 s.Printf(wxFileNameFromPath(parOutput) + "$%03i-" + name2, i);
205 if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
206 break;
207 }
208 }
209 return s;
210}
56d2f750
VS
211
212wxArrayString XmlResApp::PrepareTempFiles()
213{
214 wxArrayString flist;
215
216 for (size_t i = 0; i < parFiles.Count(); i++)
217 {
218 if (flagVerbose)
219 wxPrintf("processing " + parFiles[i] + "...\n");
220
221 wxXmlDocument doc;
222
223 if (!doc.Load(parFiles[i]))
224 {
225 wxLogError("Error parsing file " + parFiles[i]);
226 retCode = 1;
227 continue;
228 }
229
f6853b4a
VS
230 wxString name, ext, path;
231 wxSplitPath(parFiles[i], &path, &name, &ext);
232
233 FindFilesInXML(doc.GetRoot(), flist, path);
56d2f750 234
a7501aeb
VS
235 wxString internalName = GetInternalFileName(parFiles[i], flist);
236
237 doc.Save(parOutputPath + "/" + internalName);
238 flist.Add(internalName);
56d2f750
VS
239 }
240
241 return flist;
242}
243
244
245
f6853b4a
VS
246// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
247void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
248{
249 wxXmlNode *n = node;
250 if (n == NULL) return;
251 n = n->GetChildren();
252
253 while (n)
254 {
255 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
256 // parent is an element, i.e. has subnodes...
257 (n->GetType() == wxXML_TEXT_NODE ||
258 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
259 // ...it is textnode...
c109ef11
VS
260 ((node/*not n!*/->GetName() == "bitmap") ||
261 (node/*not n!*/->GetName() == "url")))
f6853b4a
VS
262 // ...and known to contain filename
263 {
264 wxString fullname;
f6853b4a
VS
265 if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent();
266 else fullname = inputPath + "/" + n->GetContent();
a7501aeb
VS
267
268 if (flagVerbose)
269 wxPrintf("adding " + fullname + "...\n");
f6853b4a 270
a7501aeb 271 wxString filename = GetInternalFileName(n->GetContent(), flist);
f6853b4a 272 n->SetContent(filename);
f6853b4a
VS
273
274 flist.Add(filename);
275
276 wxFileInputStream sin(fullname);
277 wxFileOutputStream sout(parOutputPath + "/" + filename);
278 sin.Read(sout); // copy the stream
279 }
280
281 // subnodes:
282 if (n->GetType() == wxXML_ELEMENT_NODE)
283 FindFilesInXML(n, flist, inputPath);
284
285 n = n->GetNext();
286 }
287}
288
289
290
56d2f750
VS
291void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
292{
293 for (size_t i = 0; i < flist.Count(); i++)
294 wxRemoveFile(parOutputPath + "/" + flist[i]);
295}
296
297
298
299void XmlResApp::MakePackageZIP(const wxArrayString& flist)
300{
301 wxString files;
302
303 for (size_t i = 0; i < flist.Count(); i++)
304 files += flist[i] + " ";
305 files.RemoveLast();
306
307 if (flagVerbose)
308 wxPrintf("compressing " + parOutput + "...\n");
309
310 if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") +
311 parOutput + " " + files, TRUE) == -1)
312 {
313 wxLogError("Unable to execute zip program. Make sure it is in the path.");
314 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
315 retCode = 1;
316 return;
317 }
318}
319
320
321
322
323static wxString FileToCppArray(wxString filename, int num)
324{
325 wxString output;
56d2f750 326 wxString tmp;
f6853b4a 327 wxString snum;
56d2f750
VS
328 wxFFile file(filename, "rb");
329 size_t lng = file.Length();
330
331 snum.Printf("%i", num);
332 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
e066e256
VS
333 output += "static unsigned char xml_res_file_" + snum + "[] = {\n";
334 // we cannot use string literals because MSVC is dumb wannabe compiler
335 // with arbitrary limitation to 2048 strings :(
56d2f750
VS
336
337 unsigned char *buffer = new unsigned char[lng];
338 file.Read(buffer, lng);
339
f6853b4a 340 for (size_t i = 0, linelng = 0; i < lng; i++)
56d2f750 341 {
e066e256
VS
342 tmp.Printf("%i", buffer[i]);
343 if (i != 0) output << ',';
344 if (linelng > 70)
f6853b4a
VS
345 {
346 linelng = 0;
e066e256 347 output << "\n";
f6853b4a 348 }
e066e256
VS
349 output << tmp;
350 linelng += tmp.Length()+1;
56d2f750
VS
351 }
352
353 delete[] buffer;
354
e066e256 355 output += "};\n\n";
56d2f750
VS
356
357 return output;
358}
359
360
361void XmlResApp::MakePackageCPP(const wxArrayString& flist)
362{
363 wxFFile file(parOutput, "wt");
364 size_t i;
365
366 if (flagVerbose)
367 wxPrintf("creating C++ source file " + parOutput + "...\n");
368
369 file.Write("\
a7501aeb 370#include <wx/wxprec.h>\n\
56d2f750
VS
371\n\
372#ifdef __BORLANDC__\n\
373 #pragma hdrstop\n\
374#endif\n\
375\n\
376#ifndef WX_PRECOMP\n\
a7501aeb 377 #include <wx/wx.h>\n\
56d2f750
VS
378#endif\n\
379\
a7501aeb
VS
380#include <wx/filesys.h>\n\
381#include <wx/fs_mem.h>\n\
382#include <wx/xrc/xmlres.h>\n\
383#include <wx/xrc/xh_all.h>\n\
56d2f750
VS
384\n");
385
386 for (i = 0; i < flist.Count(); i++)
387 file.Write(FileToCppArray(flist[i], i));
388
389 file.Write("\
390void " + parFuncname + "()\n\
391{\n\
392\n\
393 // Check for memory FS. If not present, load the handler:\n\
394 {\n\
a7501aeb 395 wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\
56d2f750 396 wxFileSystem fsys;\n\
a7501aeb
VS
397 wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\
398 wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\
56d2f750
VS
399 if (f) delete f;\n\
400 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
401 }\n\
402\n");
403
404 for (i = 0; i < flist.Count(); i++)
405 {
406 wxString s;
a7501aeb 407 s.Printf(" wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist[i] +
f6853b4a 408 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
56d2f750
VS
409 file.Write(s);
410 }
f6853b4a
VS
411
412 for (i = 0; i < parFiles.Count(); i++)
413 {
a7501aeb
VS
414 file.Write(" wxXmlResource::Get()->Load(\"memory:XRC_resource/" +
415 GetInternalFileName(parFiles[i], flist) + "\");\n");
f6853b4a 416 }
56d2f750 417
89a28180 418 file.Write("}\n");
56d2f750 419
f6853b4a 420
56d2f750 421}
c8b7a961
VS
422
423
424
425void XmlResApp::OutputGettext()
426{
427 wxArrayString str = FindStrings();
428
429 wxFFile fout;
430 if (!parOutput) fout.Attach(stdout);
431 else fout.Open(parOutput, _T("wt"));
432
433 for (size_t i = 0; i < str.GetCount(); i++)
89a28180 434 fout.Write(_T("_(\"") + str[i] + _T("\")\n"));
c8b7a961
VS
435
436 if (!parOutput) fout.Detach();
437}
438
439
440
441wxArrayString XmlResApp::FindStrings()
442{
443 wxArrayString arr, a2;
444
445 for (size_t i = 0; i < parFiles.Count(); i++)
446 {
447 if (flagVerbose)
448 wxPrintf("processing " + parFiles[i] + "...\n");
449
450 wxXmlDocument doc;
451 if (!doc.Load(parFiles[i]))
452 {
453 wxLogError("Error parsing file " + parFiles[i]);
454 retCode = 1;
455 continue;
456 }
457 a2 = FindStrings(doc.GetRoot());
458 WX_APPEND_ARRAY(arr, a2);
459 }
460
461 return arr;
462}
463
464
465
c109ef11
VS
466static wxString ConvertText(const wxString& str)
467{
468 wxString str2;
469 const wxChar *dt;
470
471 for (dt = str.c_str(); *dt; dt++)
472 {
473 if (*dt == wxT('_'))
474 {
475 if ( *(++dt) == wxT('_') )
476 str2 << wxT('_');
477 else
478 str2 << wxT('&') << *dt;
479 }
480 else
481 {
482 switch (*dt)
483 {
484 case wxT('\n') : str2 << wxT("\\n"); break;
485 case wxT('\t') : str2 << wxT("\\t"); break;
486 case wxT('\r') : str2 << wxT("\\r"); break;
487 default : str2 << *dt; break;
488 }
489 }
490 }
491
492 return str2;
493}
494
495
c8b7a961
VS
496wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
497{
498 wxArrayString arr;
499
500 wxXmlNode *n = node;
501 if (n == NULL) return arr;
502 n = n->GetChildren();
503
504 while (n)
505 {
506 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
507 // parent is an element, i.e. has subnodes...
508 (n->GetType() == wxXML_TEXT_NODE ||
509 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
510 // ...it is textnode...
511 (
512 node/*not n!*/->GetName() == _T("label") ||
513 (node/*not n!*/->GetName() == _T("value") &&
514 !n->GetContent().IsNumber()) ||
515 node/*not n!*/->GetName() == _T("help") ||
516 node/*not n!*/->GetName() == _T("longhelp") ||
517 node/*not n!*/->GetName() == _T("tooltip") ||
518 node/*not n!*/->GetName() == _T("htmlcode") ||
519 node/*not n!*/->GetName() == _T("title")
520 ))
c109ef11 521 // ...and known to contain translatable string
c8b7a961 522 {
c109ef11 523 arr.Add(ConvertText(n->GetContent()));
c8b7a961
VS
524 }
525
526 // subnodes:
527 if (n->GetType() == wxXML_ELEMENT_NODE)
528 {
529 wxArrayString a2 = FindStrings(n);
530 WX_APPEND_ARRAY(arr, a2);
531 }
532
533 n = n->GetNext();
534 }
535 return arr;
536}