]>
Commit | Line | Data |
---|---|---|
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 | |
44 | class XmlResApp : public wxApp | |
45 | { | |
46 | public: | |
031dfec8 JS |
47 | |
48 | #if wxUSE_GUI | |
49 | bool OnInit(); | |
50 | #else | |
56d2f750 | 51 | virtual int OnRun(); |
031dfec8 | 52 | #endif |
56d2f750 VS |
53 | |
54 | private: | |
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 | ||
76 | IMPLEMENT_APP(XmlResApp) | |
77 | ||
031dfec8 JS |
78 | #if wxUSE_GUI |
79 | bool XmlResApp::OnInit() | |
80 | #else | |
56d2f750 | 81 | int 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 | ||
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); | |
c8b7a961 VS |
114 | if (flagGettext) |
115 | OutputGettext(); | |
116 | else | |
117 | CompileRes(); | |
031dfec8 JS |
118 | #if wxUSE_GUI |
119 | return FALSE; | |
120 | #else | |
56d2f750 | 121 | return retCode; |
031dfec8 | 122 | #endif |
56d2f750 VS |
123 | break; |
124 | ||
125 | default: | |
031dfec8 JS |
126 | #if wxUSE_GUI |
127 | return FALSE; | |
128 | #else | |
56d2f750 | 129 | return 1; |
031dfec8 | 130 | #endif |
56d2f750 VS |
131 | break; |
132 | } | |
133 | } | |
134 | ||
135 | ||
136 | ||
137 | ||
138 | void XmlResApp::ParseParams(const wxCmdLineParser& cmdline) | |
139 | { | |
c8b7a961 | 140 | flagGettext = cmdline.Found("g"); |
56d2f750 VS |
141 | flagVerbose = cmdline.Found("v"); |
142 | flagCPP = cmdline.Found("c"); | |
56d2f750 VS |
143 | |
144 | if (!cmdline.Found("o", &parOutput)) | |
c8b7a961 VS |
145 | { |
146 | if (flagGettext) | |
147 | parOutput = wxEmptyString; | |
148 | else | |
149 | parOutput = flagCPP ? "resource.cpp" : "resource.xrs"; | |
150 | } | |
56d2f750 VS |
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 | ||
56d2f750 VS |
170 | if (!retCode) |
171 | { | |
172 | if (flagCPP) | |
173 | MakePackageCPP(files); | |
174 | else | |
175 | MakePackageZIP(files); | |
176 | } | |
177 | ||
178 | DeleteTempFiles(files); | |
179 | } | |
180 | ||
181 | ||
a7501aeb VS |
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 | } | |
56d2f750 VS |
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 | ||
f6853b4a VS |
223 | wxString name, ext, path; |
224 | wxSplitPath(parFiles[i], &path, &name, &ext); | |
225 | ||
226 | FindFilesInXML(doc.GetRoot(), flist, path); | |
56d2f750 | 227 | |
a7501aeb VS |
228 | wxString internalName = GetInternalFileName(parFiles[i], flist); |
229 | ||
230 | doc.Save(parOutputPath + "/" + internalName); | |
231 | flist.Add(internalName); | |
56d2f750 VS |
232 | } |
233 | ||
234 | return flist; | |
235 | } | |
236 | ||
237 | ||
238 | ||
f6853b4a VS |
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... | |
c109ef11 VS |
253 | ((node/*not n!*/->GetName() == "bitmap") || |
254 | (node/*not n!*/->GetName() == "url"))) | |
f6853b4a VS |
255 | // ...and known to contain filename |
256 | { | |
257 | wxString fullname; | |
87b454d6 | 258 | if (wxIsAbsolutePath(n->GetContent()) || inputPath == "") fullname = n->GetContent(); |
f6853b4a | 259 | else fullname = inputPath + "/" + n->GetContent(); |
a7501aeb VS |
260 | |
261 | if (flagVerbose) | |
262 | wxPrintf("adding " + fullname + "...\n"); | |
f6853b4a | 263 | |
a7501aeb | 264 | wxString filename = GetInternalFileName(n->GetContent(), flist); |
f6853b4a | 265 | n->SetContent(filename); |
f6853b4a VS |
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 | ||
56d2f750 VS |
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; | |
56d2f750 | 319 | wxString tmp; |
f6853b4a | 320 | wxString snum; |
56d2f750 VS |
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); | |
e066e256 VS |
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 :( | |
56d2f750 VS |
329 | |
330 | unsigned char *buffer = new unsigned char[lng]; | |
331 | file.Read(buffer, lng); | |
332 | ||
f6853b4a | 333 | for (size_t i = 0, linelng = 0; i < lng; i++) |
56d2f750 | 334 | { |
e066e256 VS |
335 | tmp.Printf("%i", buffer[i]); |
336 | if (i != 0) output << ','; | |
337 | if (linelng > 70) | |
f6853b4a VS |
338 | { |
339 | linelng = 0; | |
e066e256 | 340 | output << "\n"; |
f6853b4a | 341 | } |
e066e256 VS |
342 | output << tmp; |
343 | linelng += tmp.Length()+1; | |
56d2f750 VS |
344 | } |
345 | ||
346 | delete[] buffer; | |
347 | ||
e066e256 | 348 | output += "};\n\n"; |
56d2f750 VS |
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("\ | |
a7501aeb | 363 | #include <wx/wxprec.h>\n\ |
56d2f750 VS |
364 | \n\ |
365 | #ifdef __BORLANDC__\n\ | |
366 | #pragma hdrstop\n\ | |
367 | #endif\n\ | |
368 | \n\ | |
369 | #ifndef WX_PRECOMP\n\ | |
a7501aeb | 370 | #include <wx/wx.h>\n\ |
56d2f750 VS |
371 | #endif\n\ |
372 | \ | |
a7501aeb VS |
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\ | |
56d2f750 VS |
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\ | |
a7501aeb | 388 | wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\ |
56d2f750 | 389 | wxFileSystem fsys;\n\ |
a7501aeb VS |
390 | wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\ |
391 | wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\ | |
56d2f750 VS |
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; | |
a7501aeb | 400 | s.Printf(" wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist[i] + |
f6853b4a | 401 | "\", xml_res_file_%i, xml_res_size_%i);\n", i, i); |
56d2f750 VS |
402 | file.Write(s); |
403 | } | |
f6853b4a VS |
404 | |
405 | for (i = 0; i < parFiles.Count(); i++) | |
406 | { | |
a7501aeb VS |
407 | file.Write(" wxXmlResource::Get()->Load(\"memory:XRC_resource/" + |
408 | GetInternalFileName(parFiles[i], flist) + "\");\n"); | |
f6853b4a | 409 | } |
56d2f750 | 410 | |
89a28180 | 411 | file.Write("}\n"); |
56d2f750 | 412 | |
f6853b4a | 413 | |
56d2f750 | 414 | } |
c8b7a961 VS |
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++) | |
89a28180 | 427 | fout.Write(_T("_(\"") + str[i] + _T("\")\n")); |
c8b7a961 VS |
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 | ||
c109ef11 VS |
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 | ||
c8b7a961 VS |
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 | )) | |
c109ef11 | 514 | // ...and known to contain translatable string |
c8b7a961 | 515 | { |
c109ef11 | 516 | arr.Add(ConvertText(n->GetContent())); |
c8b7a961 VS |
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 | } |