patch 580632 - fixes incorrect path problem in wxrc
[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 #if wxUSE_GUI && !defined(__WXMSW__)
104 // VS: I need reasonable output to console from wxCmdLineParser
105 // - temporary, will hopefully be fixed in future in wxWin
106 wxLog::SetTimestamp(NULL);
107 delete wxLog::SetActiveTarget(new wxLogStderr);
108 #endif
109
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);
121 if (flagGettext)
122 OutputGettext();
123 else
124 CompileRes();
125 #if wxUSE_GUI
126 return FALSE;
127 #else
128 return retCode;
129 #endif
130 break;
131
132 default:
133 #if wxUSE_GUI
134 return FALSE;
135 #else
136 return 1;
137 #endif
138 break;
139 }
140 }
141
142
143
144
145 void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
146 {
147 flagGettext = cmdline.Found("g");
148 flagVerbose = cmdline.Found("v");
149 flagCPP = cmdline.Found("c");
150
151 if (!cmdline.Found("o", &parOutput))
152 {
153 if (flagGettext)
154 parOutput = wxEmptyString;
155 else
156 parOutput = flagCPP ? "resource.cpp" : "resource.xrs";
157 }
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
171 void XmlResApp::CompileRes()
172 {
173 wxArrayString files = PrepareTempFiles();
174
175 wxRemoveFile(parOutput);
176
177 if (!retCode)
178 {
179 if (flagCPP)
180 MakePackageCPP(files);
181 else
182 MakePackageZIP(files);
183 }
184
185 DeleteTempFiles(files);
186 }
187
188
189 wxString 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 }
211
212 wxArrayString 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
230 wxString name, ext, path;
231 wxSplitPath(parFiles[i], &path, &name, &ext);
232
233 FindFilesInXML(doc.GetRoot(), flist, path);
234
235 wxString internalName = GetInternalFileName(parFiles[i], flist);
236
237 doc.Save(parOutputPath + "/" + internalName);
238 flist.Add(internalName);
239 }
240
241 return flist;
242 }
243
244
245
246 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
247 void 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...
260 ((node/*not n!*/->GetName() == "bitmap") ||
261 (node/*not n!*/->GetName() == "url")))
262 // ...and known to contain filename
263 {
264 wxString fullname;
265 if (wxIsAbsolutePath(n->GetContent()) || inputPath == "") fullname = n->GetContent();
266 else fullname = inputPath + "/" + n->GetContent();
267
268 if (flagVerbose)
269 wxPrintf("adding " + fullname + "...\n");
270
271 wxString filename = GetInternalFileName(n->GetContent(), flist);
272 n->SetContent(filename);
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
291 void 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
299 void 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
323 static wxString FileToCppArray(wxString filename, int num)
324 {
325 wxString output;
326 wxString tmp;
327 wxString snum;
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);
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 :(
336
337 unsigned char *buffer = new unsigned char[lng];
338 file.Read(buffer, lng);
339
340 for (size_t i = 0, linelng = 0; i < lng; i++)
341 {
342 tmp.Printf("%i", buffer[i]);
343 if (i != 0) output << ',';
344 if (linelng > 70)
345 {
346 linelng = 0;
347 output << "\n";
348 }
349 output << tmp;
350 linelng += tmp.Length()+1;
351 }
352
353 delete[] buffer;
354
355 output += "};\n\n";
356
357 return output;
358 }
359
360
361 void 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("\
370 #include <wx/wxprec.h>\n\
371 \n\
372 #ifdef __BORLANDC__\n\
373 #pragma hdrstop\n\
374 #endif\n\
375 \n\
376 #ifndef WX_PRECOMP\n\
377 #include <wx/wx.h>\n\
378 #endif\n\
379 \
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\
384 \n");
385
386 for (i = 0; i < flist.Count(); i++)
387 file.Write(FileToCppArray(flist[i], i));
388
389 file.Write("\
390 void " + parFuncname + "()\n\
391 {\n\
392 \n\
393 // Check for memory FS. If not present, load the handler:\n\
394 {\n\
395 wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\
396 wxFileSystem fsys;\n\
397 wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\
398 wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\
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;
407 s.Printf(" wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist[i] +
408 "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
409 file.Write(s);
410 }
411
412 for (i = 0; i < parFiles.Count(); i++)
413 {
414 file.Write(" wxXmlResource::Get()->Load(\"memory:XRC_resource/" +
415 GetInternalFileName(parFiles[i], flist) + "\");\n");
416 }
417
418 file.Write("}\n");
419
420
421 }
422
423
424
425 void 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++)
434 fout.Write(_T("_(\"") + str[i] + _T("\")\n"));
435
436 if (!parOutput) fout.Detach();
437 }
438
439
440
441 wxArrayString 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
466 static 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
496 wxArrayString 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 ))
521 // ...and known to contain translatable string
522 {
523 arr.Add(ConvertText(n->GetContent()));
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 }