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