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