Applied patch [ 793000 ] "unreachable code" changes in wxrc contrib util
[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 #if defined(__GNUG__) && !defined(__APPLE__)
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/ffile.h"
32 #include "wx/filename.h"
33 #include "wx/wfstream.h"
34
35
36 class XmlResApp : public wxAppConsole
37 {
38 public:
39 // don't use builtin cmd line parsing:
40 virtual bool OnInit() { return true; }
41
42 virtual int OnRun();
43
44 private:
45
46 void ParseParams(const wxCmdLineParser& cmdline);
47 void CompileRes();
48 wxArrayString PrepareTempFiles();
49 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
50
51 wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
52 void DeleteTempFiles(const wxArrayString& flist);
53 void MakePackageZIP(const wxArrayString& flist);
54 void MakePackageCPP(const wxArrayString& flist);
55 void MakePackagePython(const wxArrayString& flist);
56
57 void OutputGettext();
58 wxArrayString FindStrings();
59 wxArrayString FindStrings(wxXmlNode *node);
60
61 bool flagVerbose, flagCPP, flagPython, flagGettext;
62 wxString parOutput, parFuncname, parOutputPath;
63 wxArrayString parFiles;
64 int retCode;
65 };
66
67 IMPLEMENT_APP_CONSOLE(XmlResApp)
68
69 int XmlResApp::OnRun()
70 {
71 static const wxCmdLineEntryDesc cmdLineDesc[] =
72 {
73 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show help message"),
74 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
75 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
76 { wxCMD_LINE_SWITCH, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") },
77 { wxCMD_LINE_SWITCH, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") },
78 { wxCMD_LINE_SWITCH, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") },
79 { wxCMD_LINE_OPTION, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") },
80 { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") },
81 #if 0 // not yet implemented
82 { wxCMD_LINE_OPTION, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
83 #endif
84 { wxCMD_LINE_PARAM, NULL, NULL, _T("input file(s)"),
85 wxCMD_LINE_VAL_STRING,
86 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
87
88 { wxCMD_LINE_NONE }
89 };
90
91 wxCmdLineParser parser(cmdLineDesc, argc, argv);
92
93 switch (parser.Parse())
94 {
95 case -1:
96 return 0;
97 #if 0
98 // break is unreachable because of return earlier
99 break;
100 #endif
101
102 case 0:
103 retCode = 0;
104 ParseParams(parser);
105 if (flagGettext)
106 OutputGettext();
107 else
108 CompileRes();
109 return retCode;
110 #if 0
111 // break is unreachable because of return earlier
112 break;
113 #endif
114
115 #if 0
116 // default return moved outside of switch to avoid warning about lack of return in function
117 default:
118 return 1;
119 break;
120 #endif
121 }
122 return 1;
123 }
124
125
126
127
128 void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
129 {
130 flagGettext = cmdline.Found(_T("g"));
131 flagVerbose = cmdline.Found(_T("v"));
132 flagCPP = cmdline.Found(_T("c"));
133 flagPython = cmdline.Found(_T("p"));
134
135 if (!cmdline.Found(_T("o"), &parOutput))
136 {
137 if (flagGettext)
138 parOutput = wxEmptyString;
139 else
140 {
141 if (flagCPP)
142 parOutput = _T("resource.cpp");
143 else if (flagPython)
144 parOutput = _T("resource.py");
145 else
146 parOutput = _T("resource.xrs");
147 }
148 }
149 wxFileName fn(parOutput);
150 fn.Normalize();
151 parOutput = fn.GetFullPath();
152 parOutputPath = wxPathOnly(parOutput);
153 if (!parOutputPath) parOutputPath = _T(".");
154
155 if (!cmdline.Found(_T("n"), &parFuncname))
156 parFuncname = _T("InitXmlResource");
157
158 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
159 {
160 #ifdef __WINDOWS__
161 wxString fn=wxFindFirstFile(cmdline.GetParam(i), wxFILE);
162 while (!fn.IsEmpty())
163 {
164 parFiles.Add(fn);
165 fn=wxFindNextFile();
166 }
167 #else
168 parFiles.Add(cmdline.GetParam(i));
169 #endif
170 }
171 }
172
173
174
175
176 void XmlResApp::CompileRes()
177 {
178 wxArrayString files = PrepareTempFiles();
179
180 wxRemoveFile(parOutput);
181
182 if (!retCode)
183 {
184 if (flagCPP)
185 MakePackageCPP(files);
186 else if (flagPython)
187 MakePackagePython(files);
188 else
189 MakePackageZIP(files);
190 }
191
192 DeleteTempFiles(files);
193 }
194
195
196 wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
197 {
198 wxString name2 = name;
199 name2.Replace(_T(":"), _T("_"));
200 name2.Replace(_T("/"), _T("_"));
201 name2.Replace(_T("\\"), _T("_"));
202 name2.Replace(_T("*"), _T("_"));
203 name2.Replace(_T("?"), _T("_"));
204
205 wxString s = wxFileNameFromPath(parOutput) + _T("$") + name2;
206
207 if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
208 {
209 for (int i = 0;; i++)
210 {
211 s.Printf(wxFileNameFromPath(parOutput) + _T("$%03i-") + name2, i);
212 if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
213 break;
214 }
215 }
216 return s;
217 }
218
219 wxArrayString XmlResApp::PrepareTempFiles()
220 {
221 wxArrayString flist;
222
223 for (size_t i = 0; i < parFiles.Count(); i++)
224 {
225 if (flagVerbose)
226 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
227
228 wxXmlDocument doc;
229
230 if (!doc.Load(parFiles[i]))
231 {
232 wxLogError(_T("Error parsing file ") + parFiles[i]);
233 retCode = 1;
234 continue;
235 }
236
237 wxString name, ext, path;
238 wxSplitPath(parFiles[i], &path, &name, &ext);
239
240 FindFilesInXML(doc.GetRoot(), flist, path);
241
242 wxString internalName = GetInternalFileName(parFiles[i], flist);
243
244 doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName);
245 flist.Add(internalName);
246 }
247
248 return flist;
249 }
250
251
252 // Does 'node' contain filename information at all?
253 static bool NodeContainsFilename(wxXmlNode *node)
254 {
255 // Any bitmaps:
256 if (node->GetName() == _T("bitmap"))
257 return TRUE;
258
259 // URLs in wxHtmlWindow:
260 if (node->GetName() == _T("url"))
261 return TRUE;
262
263 // wxBitmapButton:
264 wxXmlNode *parent = node->GetParent();
265 if (parent != NULL &&
266 parent->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
267 (node->GetName() == _T("focus") ||
268 node->GetName() == _T("disabled") ||
269 node->GetName() == _T("selected")))
270 return TRUE;
271
272 // wxBitmap or wxIcon toplevel resources:
273 if (node->GetName() == _T("object"))
274 {
275 wxString klass = node->GetPropVal(_T("class"), wxEmptyString);
276 if (klass == _T("wxBitmap") || klass == _T("wxIcon"))
277 return TRUE;
278 }
279
280 return FALSE;
281 }
282
283 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
284 void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
285 {
286 // Is 'node' XML node element?
287 if (node == NULL) return;
288 if (node->GetType() != wxXML_ELEMENT_NODE) return;
289
290 bool containsFilename = NodeContainsFilename(node);
291
292 wxXmlNode *n = node->GetChildren();
293 while (n)
294 {
295 if (containsFilename &&
296 (n->GetType() == wxXML_TEXT_NODE ||
297 n->GetType() == wxXML_CDATA_SECTION_NODE))
298 {
299 wxString fullname;
300 if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty())
301 fullname = n->GetContent();
302 else
303 fullname = inputPath + wxFILE_SEP_PATH + n->GetContent();
304
305 if (flagVerbose)
306 wxPrintf(_T("adding ") + fullname + _T("...\n"));
307
308 wxString filename = GetInternalFileName(n->GetContent(), flist);
309 n->SetContent(filename);
310
311 if (flist.Index(filename) == wxNOT_FOUND)
312 flist.Add(filename);
313
314 wxFileInputStream sin(fullname);
315 wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename);
316 sin.Read(sout); // copy the stream
317 }
318
319 // subnodes:
320 if (n->GetType() == wxXML_ELEMENT_NODE)
321 FindFilesInXML(n, flist, inputPath);
322
323 n = n->GetNext();
324 }
325 }
326
327
328
329 void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
330 {
331 for (size_t i = 0; i < flist.Count(); i++)
332 wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]);
333 }
334
335
336
337 void XmlResApp::MakePackageZIP(const wxArrayString& flist)
338 {
339 wxString files;
340
341 for (size_t i = 0; i < flist.Count(); i++)
342 files += flist[i] + _T(" ");
343 files.RemoveLast();
344
345 if (flagVerbose)
346 wxPrintf(_T("compressing ") + parOutput + _T("...\n"));
347
348 wxString cwd = wxGetCwd();
349 wxSetWorkingDirectory(parOutputPath);
350 int execres = wxExecute(_T("zip -9 -j ") +
351 wxString(flagVerbose ? _T("") : _T("-q ")) +
352 parOutput + _T(" ") + files, TRUE);
353 wxSetWorkingDirectory(cwd);
354 if (execres == -1)
355 {
356 wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
357 wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
358 retCode = 1;
359 return;
360 }
361 }
362
363
364
365
366 static wxString FileToCppArray(wxString filename, int num)
367 {
368 wxString output;
369 wxString tmp;
370 wxString snum;
371 wxFFile file(filename, wxT("rb"));
372 size_t lng = file.Length();
373
374 snum.Printf(_T("%i"), num);
375 output.Printf(_T("static size_t xml_res_size_") + snum + _T(" = %i;\n"), lng);
376 output += _T("static unsigned char xml_res_file_") + snum + _T("[] = {\n");
377 // we cannot use string literals because MSVC is dumb wannabe compiler
378 // with arbitrary limitation to 2048 strings :(
379
380 unsigned char *buffer = new unsigned char[lng];
381 file.Read(buffer, lng);
382
383 for (size_t i = 0, linelng = 0; i < lng; i++)
384 {
385 tmp.Printf(_T("%i"), buffer[i]);
386 if (i != 0) output << _T(',');
387 if (linelng > 70)
388 {
389 linelng = 0;
390 output << _T("\n");
391 }
392 output << tmp;
393 linelng += tmp.Length()+1;
394 }
395
396 delete[] buffer;
397
398 output += _T("};\n\n");
399
400 return output;
401 }
402
403
404 void XmlResApp::MakePackageCPP(const wxArrayString& flist)
405 {
406 wxFFile file(parOutput, wxT("wt"));
407 size_t i;
408
409 if (flagVerbose)
410 wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n"));
411
412 file.Write(_T("")
413 _T("//\n")
414 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
415 _T("//\n\n")
416 _T("#include <wx/wxprec.h>\n")
417 _T("\n")
418 _T("#ifdef __BORLANDC__\n")
419 _T(" #pragma hdrstop\n")
420 _T("#endif\n")
421 _T("\n")
422 _T("#ifndef WX_PRECOMP\n")
423 _T(" #include <wx/wx.h>\n")
424 _T("#endif\n")
425 _T("")
426 _T("#include <wx/filesys.h>\n")
427 _T("#include <wx/fs_mem.h>\n")
428 _T("#include <wx/xrc/xmlres.h>\n")
429 _T("#include <wx/xrc/xh_all.h>\n")
430 _T("\n"));
431
432 for (i = 0; i < flist.Count(); i++)
433 file.Write(
434 FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
435
436 file.Write(_T("")
437 _T("void ") + parFuncname + wxT("()\n")
438 _T("{\n")
439 _T("\n")
440 _T(" // Check for memory FS. If not present, load the handler:\n")
441 _T(" {\n")
442 _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
443 _T(" wxFileSystem fsys;\n")
444 _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
445 _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
446 _T(" if (f) delete f;\n")
447 _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
448 _T(" }\n")
449 _T("\n"));
450
451 for (i = 0; i < flist.Count(); i++)
452 {
453 wxString s;
454 s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] +
455 _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i);
456 file.Write(s);
457 }
458
459 for (i = 0; i < parFiles.Count(); i++)
460 {
461 file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
462 GetInternalFileName(parFiles[i], flist) + _T("\"));\n"));
463 }
464
465 file.Write(_T("}\n"));
466
467
468 }
469
470 static wxString FileToPythonArray(wxString filename, int num)
471 {
472 wxString output;
473 wxString tmp;
474 wxString snum;
475 wxFFile file(filename, wxT("rb"));
476 size_t lng = file.Length();
477
478 snum.Printf(_T("%i"), num);
479 output = _T(" xml_res_file_") + snum + _T(" = \"\"\"\\\n");
480
481 unsigned char *buffer = new unsigned char[lng];
482 file.Read(buffer, lng);
483
484 for (size_t i = 0, linelng = 0; i < lng; i++)
485 {
486 unsigned char c = buffer[i];
487 if (c == '\n')
488 {
489 tmp = (wxChar)c;
490 linelng = 0;
491 }
492 else if (c < 32 || c > 127)
493 tmp.Printf(_T("\\x%02x"), c);
494 else if (c == '\\')
495 tmp = _T("\\\\");
496 else
497 tmp = (wxChar)c;
498 if (linelng > 70)
499 {
500 linelng = 0;
501 output << _T("\\\n");
502 }
503 output << tmp;
504 linelng += tmp.Length();
505 }
506
507 delete[] buffer;
508
509 output += _T("\"\"\"\n\n");
510
511 return output;
512 }
513
514
515 void XmlResApp::MakePackagePython(const wxArrayString& flist)
516 {
517 wxFFile file(parOutput, wxT("wt"));
518 size_t i;
519
520 if (flagVerbose)
521 wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n"));
522
523 file.Write(
524 _T("#\n")
525 _T("# This file was automatically generated by wxrc, do not edit by hand.\n")
526 _T("#\n\n")
527 _T("from wxPython.wx import *\n")
528 _T("from wxPython.xrc import *\n\n")
529 );
530
531
532 file.Write(_T("def ") + parFuncname + _T("():\n"));
533
534 for (i = 0; i < flist.Count(); i++)
535 file.Write(
536 FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
537
538 for (i = 0; i < flist.Count(); i++)
539 {
540 wxString s;
541 s.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i);
542 file.Write(s);
543 }
544 }
545
546
547
548 void XmlResApp::OutputGettext()
549 {
550 wxArrayString str = FindStrings();
551
552 wxFFile fout;
553 if (!parOutput) fout.Attach(stdout);
554 else fout.Open(parOutput, wxT("wt"));
555
556 for (size_t i = 0; i < str.GetCount(); i++)
557 fout.Write(_T("_(\"") + str[i] + _T("\");\n"));
558
559 if (!parOutput) fout.Detach();
560 }
561
562
563
564 wxArrayString XmlResApp::FindStrings()
565 {
566 wxArrayString arr, a2;
567
568 for (size_t i = 0; i < parFiles.Count(); i++)
569 {
570 if (flagVerbose)
571 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
572
573 wxXmlDocument doc;
574 if (!doc.Load(parFiles[i]))
575 {
576 wxLogError(_T("Error parsing file ") + parFiles[i]);
577 retCode = 1;
578 continue;
579 }
580 a2 = FindStrings(doc.GetRoot());
581 WX_APPEND_ARRAY(arr, a2);
582 }
583
584 return arr;
585 }
586
587
588
589 static wxString ConvertText(const wxString& str)
590 {
591 wxString str2;
592 const wxChar *dt;
593
594 for (dt = str.c_str(); *dt; dt++)
595 {
596 if (*dt == wxT('_'))
597 {
598 if ( *(++dt) == wxT('_') )
599 str2 << wxT('_');
600 else
601 str2 << wxT('&') << *dt;
602 }
603 else
604 {
605 switch (*dt)
606 {
607 case wxT('\n') : str2 << wxT("\\n"); break;
608 case wxT('\t') : str2 << wxT("\\t"); break;
609 case wxT('\r') : str2 << wxT("\\r"); break;
610 case wxT('\\') : if ((*(dt+1) != 'n') &&
611 (*(dt+1) != 't') &&
612 (*(dt+1) != 'r'))
613 str2 << wxT("\\\\");
614 else
615 str2 << wxT("\\");
616 break;
617 case wxT('"') : str2 << wxT("\\\""); break;
618 default : str2 << *dt; break;
619 }
620 }
621 }
622
623 return str2;
624 }
625
626
627 wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
628 {
629 wxArrayString arr;
630
631 wxXmlNode *n = node;
632 if (n == NULL) return arr;
633 n = n->GetChildren();
634
635 while (n)
636 {
637 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
638 // parent is an element, i.e. has subnodes...
639 (n->GetType() == wxXML_TEXT_NODE ||
640 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
641 // ...it is textnode...
642 (
643 node/*not n!*/->GetName() == _T("label") ||
644 (node/*not n!*/->GetName() == _T("value") &&
645 !n->GetContent().IsNumber()) ||
646 node/*not n!*/->GetName() == _T("help") ||
647 node/*not n!*/->GetName() == _T("longhelp") ||
648 node/*not n!*/->GetName() == _T("tooltip") ||
649 node/*not n!*/->GetName() == _T("htmlcode") ||
650 node/*not n!*/->GetName() == _T("title") ||
651 node/*not n!*/->GetName() == _T("item")
652 ))
653 // ...and known to contain translatable string
654 {
655 arr.Add(ConvertText(n->GetContent()));
656 }
657
658 // subnodes:
659 if (n->GetType() == wxXML_ELEMENT_NODE)
660 {
661 wxArrayString a2 = FindStrings(n);
662 WX_APPEND_ARRAY(arr, a2);
663 }
664
665 n = n->GetNext();
666 }
667 return arr;
668 }