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