]>
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 VS |
30 | #include "wx/xrc/xml.h" |
31 | #include "wx/xrc/xmlio.h" | |
56d2f750 | 32 | #include "wx/ffile.h" |
f6853b4a VS |
33 | #include "wx/wfstream.h" |
34 | ||
35 | ||
36 | ||
37 | ||
56d2f750 | 38 | |
031dfec8 | 39 | /* |
56d2f750 VS |
40 | #if wxUSE_GUI |
41 | #error "You must compile the resource compiler with wxBase!" | |
42 | #endif | |
031dfec8 | 43 | */ |
56d2f750 VS |
44 | |
45 | class XmlResApp : public wxApp | |
46 | { | |
47 | public: | |
031dfec8 JS |
48 | |
49 | #if wxUSE_GUI | |
50 | bool OnInit(); | |
51 | #else | |
56d2f750 | 52 | virtual int OnRun(); |
031dfec8 | 53 | #endif |
56d2f750 VS |
54 | |
55 | private: | |
56 | ||
57 | void ParseParams(const wxCmdLineParser& cmdline); | |
58 | void CompileRes(); | |
59 | wxArrayString PrepareTempFiles(); | |
f6853b4a VS |
60 | void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath); |
61 | ||
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 | |
c8b7a961 | 70 | bool flagVerbose, flagCPP, flagCompress, 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 | { | |
c8b7a961 | 86 | { wxCMD_LINE_SWITCH, "h", "help", "show help message" }, |
56d2f750 VS |
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, "u", "uncompressed", "do not compress .xml files (C++ only)" }, | |
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]" }, |
a793c17b | 93 | { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" }, |
56d2f750 | 94 | |
c8b7a961 | 95 | { wxCMD_LINE_PARAM, NULL, NULL, "input file(s)", |
56d2f750 VS |
96 | wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, |
97 | ||
98 | { wxCMD_LINE_NONE } | |
99 | }; | |
100 | ||
cb0b3950 VS |
101 | #if wxUSE_GUI |
102 | // VS: I need reasonable output to console from wxCmdLineParser | |
103 | wxLog::SetTimestamp(NULL); | |
104 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
105 | #endif | |
106 | ||
a793c17b VS |
107 | wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ); |
108 | ||
56d2f750 VS |
109 | wxCmdLineParser parser(cmdLineDesc, argc, argv); |
110 | ||
111 | switch (parser.Parse()) | |
112 | { | |
113 | case -1: | |
114 | return 0; | |
115 | break; | |
116 | ||
117 | case 0: | |
118 | retCode = 0; | |
119 | ParseParams(parser); | |
c8b7a961 VS |
120 | if (flagGettext) |
121 | OutputGettext(); | |
122 | else | |
123 | CompileRes(); | |
031dfec8 JS |
124 | #if wxUSE_GUI |
125 | return FALSE; | |
126 | #else | |
56d2f750 | 127 | return retCode; |
031dfec8 | 128 | #endif |
56d2f750 VS |
129 | break; |
130 | ||
131 | default: | |
031dfec8 JS |
132 | #if wxUSE_GUI |
133 | return FALSE; | |
134 | #else | |
56d2f750 | 135 | return 1; |
031dfec8 | 136 | #endif |
56d2f750 VS |
137 | break; |
138 | } | |
139 | } | |
140 | ||
141 | ||
142 | ||
143 | ||
144 | void XmlResApp::ParseParams(const wxCmdLineParser& cmdline) | |
145 | { | |
c8b7a961 | 146 | flagGettext = cmdline.Found("g"); |
56d2f750 VS |
147 | flagVerbose = cmdline.Found("v"); |
148 | flagCPP = cmdline.Found("c"); | |
149 | flagCompress = flagCPP && !cmdline.Found("u"); | |
150 | ||
151 | if (!cmdline.Found("o", &parOutput)) | |
c8b7a961 VS |
152 | { |
153 | if (flagGettext) | |
154 | parOutput = wxEmptyString; | |
155 | else | |
156 | parOutput = flagCPP ? "resource.cpp" : "resource.xrs"; | |
157 | } | |
56d2f750 VS |
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 | ||
56d2f750 VS |
177 | if (!retCode) |
178 | { | |
179 | if (flagCPP) | |
180 | MakePackageCPP(files); | |
181 | else | |
182 | MakePackageZIP(files); | |
183 | } | |
184 | ||
185 | DeleteTempFiles(files); | |
186 | } | |
187 | ||
188 | ||
189 | ||
190 | wxArrayString XmlResApp::PrepareTempFiles() | |
191 | { | |
192 | wxArrayString flist; | |
193 | ||
194 | for (size_t i = 0; i < parFiles.Count(); i++) | |
195 | { | |
196 | if (flagVerbose) | |
197 | wxPrintf("processing " + parFiles[i] + "...\n"); | |
198 | ||
199 | wxXmlDocument doc; | |
200 | ||
201 | if (!doc.Load(parFiles[i])) | |
202 | { | |
203 | wxLogError("Error parsing file " + parFiles[i]); | |
204 | retCode = 1; | |
205 | continue; | |
206 | } | |
207 | ||
f6853b4a VS |
208 | wxString name, ext, path; |
209 | wxSplitPath(parFiles[i], &path, &name, &ext); | |
210 | ||
211 | FindFilesInXML(doc.GetRoot(), flist, path); | |
56d2f750 | 212 | |
89a28180 VS |
213 | doc.Save(parOutputPath + "/" + name + ".xmlbin", |
214 | flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN); | |
215 | flist.Add(name + ".xmlbin"); | |
56d2f750 VS |
216 | } |
217 | ||
218 | return flist; | |
219 | } | |
220 | ||
221 | ||
222 | ||
f6853b4a VS |
223 | // find all files mentioned in structure, e.g. <bitmap>filename</bitmap> |
224 | void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath) | |
225 | { | |
226 | wxXmlNode *n = node; | |
227 | if (n == NULL) return; | |
228 | n = n->GetChildren(); | |
229 | ||
230 | while (n) | |
231 | { | |
232 | if ((node->GetType() == wxXML_ELEMENT_NODE) && | |
233 | // parent is an element, i.e. has subnodes... | |
234 | (n->GetType() == wxXML_TEXT_NODE || | |
235 | n->GetType() == wxXML_CDATA_SECTION_NODE) && | |
236 | // ...it is textnode... | |
237 | (node/*not n!*/->GetName() == "bitmap")) | |
238 | // ...and known to contain filename | |
239 | { | |
240 | wxString fullname; | |
241 | wxString filename = n->GetContent(); | |
242 | if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent(); | |
243 | else fullname = inputPath + "/" + n->GetContent(); | |
244 | ||
245 | filename.Replace("/", "_"); | |
246 | filename.Replace("\\", "_"); | |
247 | filename.Replace("*", "_"); | |
248 | filename.Replace("?", "_"); | |
249 | n->SetContent(filename); | |
250 | ||
251 | if (flagVerbose) | |
252 | wxPrintf("adding " + filename + "...\n"); | |
253 | ||
254 | flist.Add(filename); | |
255 | ||
256 | wxFileInputStream sin(fullname); | |
257 | wxFileOutputStream sout(parOutputPath + "/" + filename); | |
258 | sin.Read(sout); // copy the stream | |
259 | } | |
260 | ||
261 | // subnodes: | |
262 | if (n->GetType() == wxXML_ELEMENT_NODE) | |
263 | FindFilesInXML(n, flist, inputPath); | |
264 | ||
265 | n = n->GetNext(); | |
266 | } | |
267 | } | |
268 | ||
269 | ||
270 | ||
56d2f750 VS |
271 | void XmlResApp::DeleteTempFiles(const wxArrayString& flist) |
272 | { | |
273 | for (size_t i = 0; i < flist.Count(); i++) | |
274 | wxRemoveFile(parOutputPath + "/" + flist[i]); | |
275 | } | |
276 | ||
277 | ||
278 | ||
279 | void XmlResApp::MakePackageZIP(const wxArrayString& flist) | |
280 | { | |
281 | wxString files; | |
282 | ||
283 | for (size_t i = 0; i < flist.Count(); i++) | |
284 | files += flist[i] + " "; | |
285 | files.RemoveLast(); | |
286 | ||
287 | if (flagVerbose) | |
288 | wxPrintf("compressing " + parOutput + "...\n"); | |
289 | ||
290 | if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") + | |
291 | parOutput + " " + files, TRUE) == -1) | |
292 | { | |
293 | wxLogError("Unable to execute zip program. Make sure it is in the path."); | |
294 | wxLogError("You can download it at http://www.cdrom.com/pub/infozip/"); | |
295 | retCode = 1; | |
296 | return; | |
297 | } | |
298 | } | |
299 | ||
300 | ||
301 | ||
302 | ||
303 | static wxString FileToCppArray(wxString filename, int num) | |
304 | { | |
305 | wxString output; | |
56d2f750 | 306 | wxString tmp; |
f6853b4a | 307 | wxString snum; |
56d2f750 VS |
308 | wxFFile file(filename, "rb"); |
309 | size_t lng = file.Length(); | |
310 | ||
311 | snum.Printf("%i", num); | |
312 | output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng); | |
e066e256 VS |
313 | output += "static unsigned char xml_res_file_" + snum + "[] = {\n"; |
314 | // we cannot use string literals because MSVC is dumb wannabe compiler | |
315 | // with arbitrary limitation to 2048 strings :( | |
56d2f750 VS |
316 | |
317 | unsigned char *buffer = new unsigned char[lng]; | |
318 | file.Read(buffer, lng); | |
319 | ||
f6853b4a | 320 | for (size_t i = 0, linelng = 0; i < lng; i++) |
56d2f750 | 321 | { |
e066e256 VS |
322 | tmp.Printf("%i", buffer[i]); |
323 | if (i != 0) output << ','; | |
324 | if (linelng > 70) | |
f6853b4a VS |
325 | { |
326 | linelng = 0; | |
e066e256 | 327 | output << "\n"; |
f6853b4a | 328 | } |
e066e256 VS |
329 | output << tmp; |
330 | linelng += tmp.Length()+1; | |
56d2f750 VS |
331 | } |
332 | ||
333 | delete[] buffer; | |
334 | ||
e066e256 | 335 | output += "};\n\n"; |
56d2f750 VS |
336 | |
337 | return output; | |
338 | } | |
339 | ||
340 | ||
341 | void XmlResApp::MakePackageCPP(const wxArrayString& flist) | |
342 | { | |
343 | wxFFile file(parOutput, "wt"); | |
344 | size_t i; | |
345 | ||
346 | if (flagVerbose) | |
347 | wxPrintf("creating C++ source file " + parOutput + "...\n"); | |
348 | ||
349 | file.Write("\ | |
350 | #include \"wx/wxprec.h\"\n\ | |
351 | \n\ | |
352 | #ifdef __BORLANDC__\n\ | |
353 | #pragma hdrstop\n\ | |
354 | #endif\n\ | |
355 | \n\ | |
356 | #ifndef WX_PRECOMP\n\ | |
357 | #include \"wx/wx.h\"\n\ | |
358 | #endif\n\ | |
359 | \ | |
360 | #include \"wx/filesys.h\"\n\ | |
361 | #include \"wx/fs_mem.h\"\n\ | |
999d9a9f VS |
362 | #include \"wx/xrc/xmlres.h\"\n\ |
363 | #include \"wx/xrc/xh_all.h\"\n\ | |
56d2f750 VS |
364 | \n"); |
365 | ||
366 | for (i = 0; i < flist.Count(); i++) | |
367 | file.Write(FileToCppArray(flist[i], i)); | |
368 | ||
369 | file.Write("\ | |
370 | void " + parFuncname + "()\n\ | |
371 | {\n\ | |
372 | \n\ | |
373 | // Check for memory FS. If not present, load the handler:\n\ | |
374 | {\n\ | |
375 | wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\ | |
376 | wxFileSystem fsys;\n\ | |
a793c17b | 377 | wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\ |
56d2f750 VS |
378 | wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\ |
379 | if (f) delete f;\n\ | |
380 | else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\ | |
381 | }\n\ | |
382 | \n"); | |
383 | ||
384 | for (i = 0; i < flist.Count(); i++) | |
385 | { | |
386 | wxString s; | |
387 | s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] + | |
f6853b4a | 388 | "\", xml_res_file_%i, xml_res_size_%i);\n", i, i); |
56d2f750 VS |
389 | file.Write(s); |
390 | } | |
f6853b4a VS |
391 | |
392 | for (i = 0; i < parFiles.Count(); i++) | |
393 | { | |
394 | wxString name, ext, path; | |
395 | wxSplitPath(parFiles[i], &path, &name, &ext); | |
824e8eaa | 396 | file.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" + |
89a28180 | 397 | name + ".xmlbin" + "\");\n"); |
f6853b4a | 398 | } |
56d2f750 | 399 | |
89a28180 | 400 | file.Write("}\n"); |
56d2f750 | 401 | |
f6853b4a | 402 | |
56d2f750 | 403 | } |
c8b7a961 VS |
404 | |
405 | ||
406 | ||
407 | void XmlResApp::OutputGettext() | |
408 | { | |
409 | wxArrayString str = FindStrings(); | |
410 | ||
411 | wxFFile fout; | |
412 | if (!parOutput) fout.Attach(stdout); | |
413 | else fout.Open(parOutput, _T("wt")); | |
414 | ||
415 | for (size_t i = 0; i < str.GetCount(); i++) | |
89a28180 | 416 | fout.Write(_T("_(\"") + str[i] + _T("\")\n")); |
c8b7a961 VS |
417 | |
418 | if (!parOutput) fout.Detach(); | |
419 | } | |
420 | ||
421 | ||
422 | ||
423 | wxArrayString XmlResApp::FindStrings() | |
424 | { | |
425 | wxArrayString arr, a2; | |
426 | ||
427 | for (size_t i = 0; i < parFiles.Count(); i++) | |
428 | { | |
429 | if (flagVerbose) | |
430 | wxPrintf("processing " + parFiles[i] + "...\n"); | |
431 | ||
432 | wxXmlDocument doc; | |
433 | if (!doc.Load(parFiles[i])) | |
434 | { | |
435 | wxLogError("Error parsing file " + parFiles[i]); | |
436 | retCode = 1; | |
437 | continue; | |
438 | } | |
439 | a2 = FindStrings(doc.GetRoot()); | |
440 | WX_APPEND_ARRAY(arr, a2); | |
441 | } | |
442 | ||
443 | return arr; | |
444 | } | |
445 | ||
446 | ||
447 | ||
448 | wxArrayString XmlResApp::FindStrings(wxXmlNode *node) | |
449 | { | |
450 | wxArrayString arr; | |
451 | ||
452 | wxXmlNode *n = node; | |
453 | if (n == NULL) return arr; | |
454 | n = n->GetChildren(); | |
455 | ||
456 | while (n) | |
457 | { | |
458 | if ((node->GetType() == wxXML_ELEMENT_NODE) && | |
459 | // parent is an element, i.e. has subnodes... | |
460 | (n->GetType() == wxXML_TEXT_NODE || | |
461 | n->GetType() == wxXML_CDATA_SECTION_NODE) && | |
462 | // ...it is textnode... | |
463 | ( | |
464 | node/*not n!*/->GetName() == _T("label") || | |
465 | (node/*not n!*/->GetName() == _T("value") && | |
466 | !n->GetContent().IsNumber()) || | |
467 | node/*not n!*/->GetName() == _T("help") || | |
468 | node/*not n!*/->GetName() == _T("longhelp") || | |
469 | node/*not n!*/->GetName() == _T("tooltip") || | |
470 | node/*not n!*/->GetName() == _T("htmlcode") || | |
471 | node/*not n!*/->GetName() == _T("title") | |
472 | )) | |
473 | // ...and known to contain filename | |
474 | { | |
475 | arr.Add(n->GetContent()); | |
476 | } | |
477 | ||
478 | // subnodes: | |
479 | if (n->GetType() == wxXML_ELEMENT_NODE) | |
480 | { | |
481 | wxArrayString a2 = FindStrings(n); | |
482 | WX_APPEND_ARRAY(arr, a2); | |
483 | } | |
484 | ||
485 | n = n->GetNext(); | |
486 | } | |
487 | return arr; | |
488 | } |