1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resource compiler
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 // for all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWindows headers
29 #include "wx/cmdline.h"
30 #include "wx/xrc/xml.h"
31 #include "wx/xrc/xmlio.h"
33 #include "wx/wfstream.h"
41 #error "You must compile the resource compiler with wxBase!"
45 class XmlResApp
: public wxApp
57 void ParseParams(const wxCmdLineParser
& cmdline
);
59 wxArrayString
PrepareTempFiles();
60 void FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
);
62 void DeleteTempFiles(const wxArrayString
& flist
);
63 void MakePackageZIP(const wxArrayString
& flist
);
64 void MakePackageCPP(const wxArrayString
& flist
);
67 wxArrayString
FindStrings();
68 wxArrayString
FindStrings(wxXmlNode
*node
);
70 bool flagVerbose
, flagCPP
, flagCompress
, flagGettext
;
71 wxString parOutput
, parFuncname
, parOutputPath
;
72 wxArrayString parFiles
;
76 IMPLEMENT_APP(XmlResApp
)
79 bool XmlResApp::OnInit()
81 int XmlResApp::OnRun()
84 static const wxCmdLineEntryDesc cmdLineDesc
[] =
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" },
95 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file(s)",
96 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
102 // VS: I need reasonable output to console from wxCmdLineParser
103 wxLog::SetTimestamp(NULL
);
104 delete wxLog::SetActiveTarget(new wxLogStderr
);
107 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ
);
109 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
111 switch (parser
.Parse())
144 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
146 flagGettext
= cmdline
.Found("g");
147 flagVerbose
= cmdline
.Found("v");
148 flagCPP
= cmdline
.Found("c");
149 flagCompress
= flagCPP
&& !cmdline
.Found("u");
151 if (!cmdline
.Found("o", &parOutput
))
154 parOutput
= wxEmptyString
;
156 parOutput
= flagCPP
? "resource.cpp" : "resource.xrs";
158 parOutputPath
= wxPathOnly(parOutput
);
159 if (!parOutputPath
) parOutputPath
= ".";
161 if (!cmdline
.Found("n", &parFuncname
))
162 parFuncname
= "InitXmlResource";
164 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
165 parFiles
.Add(cmdline
.GetParam(i
));
171 void XmlResApp::CompileRes()
173 wxArrayString files
= PrepareTempFiles();
175 wxRemoveFile(parOutput
);
180 MakePackageCPP(files
);
182 MakePackageZIP(files
);
185 DeleteTempFiles(files
);
190 wxArrayString
XmlResApp::PrepareTempFiles()
194 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
197 wxPrintf("processing " + parFiles
[i
] + "...\n");
201 if (!doc
.Load(parFiles
[i
]))
203 wxLogError("Error parsing file " + parFiles
[i
]);
208 wxString name
, ext
, path
;
209 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
211 FindFilesInXML(doc
.GetRoot(), flist
, path
);
213 doc
.Save(parOutputPath
+ "/" + name
+ ".xrc", flagCompress
? wxXML_IO_BINZ
: wxXML_IO_BIN
);
214 flist
.Add(name
+ ".xrc");
222 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
223 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
226 if (n
== NULL
) return;
227 n
= n
->GetChildren();
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
240 wxString filename
= n
->GetContent();
241 if (wxIsAbsolutePath(n
->GetContent())) fullname
= n
->GetContent();
242 else fullname
= inputPath
+ "/" + n
->GetContent();
244 filename
.Replace("/", "_");
245 filename
.Replace("\\", "_");
246 filename
.Replace("*", "_");
247 filename
.Replace("?", "_");
248 n
->SetContent(filename
);
251 wxPrintf("adding " + filename
+ "...\n");
255 wxFileInputStream
sin(fullname
);
256 wxFileOutputStream
sout(parOutputPath
+ "/" + filename
);
257 sin
.Read(sout
); // copy the stream
261 if (n
->GetType() == wxXML_ELEMENT_NODE
)
262 FindFilesInXML(n
, flist
, inputPath
);
270 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
272 for (size_t i
= 0; i
< flist
.Count(); i
++)
273 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
278 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
282 for (size_t i
= 0; i
< flist
.Count(); i
++)
283 files
+= flist
[i
] + " ";
287 wxPrintf("compressing " + parOutput
+ "...\n");
289 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
290 parOutput
+ " " + files
, TRUE
) == -1)
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/");
302 static wxString
FileToCppArray(wxString filename
, int num
)
307 wxFFile
file(filename
, "rb");
308 size_t lng
= file
.Length();
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 :(
316 unsigned char *buffer
= new unsigned char[lng
];
317 file
.Read(buffer
, lng
);
319 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
321 tmp
.Printf("%i", buffer
[i
]);
322 if (i
!= 0) output
<< ',';
329 linelng
+= tmp
.Length()+1;
340 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
342 wxFFile
file(parOutput
, "wt");
346 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
349 #include \"wx/wxprec.h\"\n\
351 #ifdef __BORLANDC__\n\
355 #ifndef WX_PRECOMP\n\
356 #include \"wx/wx.h\"\n\
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\
365 for (i
= 0; i
< flist
.Count(); i
++)
366 file
.Write(FileToCppArray(flist
[i
], i
));
369 void " + parFuncname
+ "()\n\
372 // Check for memory FS. If not present, load the handler:\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\
379 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
383 for (i
= 0; i
< flist
.Count(); i
++)
386 s
.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist
[i
] +
387 "\", xml_res_file_%i, xml_res_size_%i);\n", i
, i
);
391 for (i
= 0; i
< parFiles
.Count(); i
++)
393 wxString name
, ext
, path
;
394 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
395 file
.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" +
396 name
+ ".xrc" + "\");\n");
406 void XmlResApp::OutputGettext()
408 wxArrayString str
= FindStrings();
411 if (!parOutput
) fout
.Attach(stdout
);
412 else fout
.Open(parOutput
, _T("wt"));
414 for (size_t i
= 0; i
< str
.GetCount(); i
++)
415 fout
.Write(_T("msgid \"") + str
[i
] + _T("\"\nmsgstr \"\"\n\n"));
417 if (!parOutput
) fout
.Detach();
422 wxArrayString
XmlResApp::FindStrings()
424 wxArrayString arr
, a2
;
426 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
429 wxPrintf("processing " + parFiles
[i
] + "...\n");
432 if (!doc
.Load(parFiles
[i
]))
434 wxLogError("Error parsing file " + parFiles
[i
]);
438 a2
= FindStrings(doc
.GetRoot());
439 WX_APPEND_ARRAY(arr
, a2
);
447 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
452 if (n
== NULL
) return arr
;
453 n
= n
->GetChildren();
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...
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")
472 // ...and known to contain filename
474 arr
.Add(n
->GetContent());
478 if (n
->GetType() == wxXML_ELEMENT_NODE
)
480 wxArrayString a2
= FindStrings(n
);
481 WX_APPEND_ARRAY(arr
, a2
);