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