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/xml/xml.h"
31 #include "wx/xml/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
);
66 bool flagVerbose
, flagCPP
, flagCompress
;
67 wxString parOutput
, parFuncname
, parOutputPath
;
68 wxArrayString parFiles
;
72 IMPLEMENT_APP(XmlResApp
)
75 bool XmlResApp::OnInit()
77 int XmlResApp::OnRun()
80 static const wxCmdLineEntryDesc cmdLineDesc
[] =
82 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
83 { wxCMD_LINE_SWITCH
, "c", "cpp-code", "output C++ source rather than .rsc file" },
84 { wxCMD_LINE_SWITCH
, "u", "uncompressed", "do not compress .xml files (C++ only)" },
85 { wxCMD_LINE_OPTION
, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
86 { wxCMD_LINE_OPTION
, "o", "output", "output file [resource.xrs/cpp]" },
87 { wxCMD_LINE_OPTION
, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
89 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
90 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
95 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ
);
97 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
99 switch (parser
.Parse())
129 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
131 flagVerbose
= cmdline
.Found("v");
132 flagCPP
= cmdline
.Found("c");
133 flagCompress
= flagCPP
&& !cmdline
.Found("u");
135 if (!cmdline
.Found("o", &parOutput
))
136 parOutput
= flagCPP
? "resource.cpp" : "resource.xrs";
137 parOutputPath
= wxPathOnly(parOutput
);
138 if (!parOutputPath
) parOutputPath
= ".";
140 if (!cmdline
.Found("n", &parFuncname
))
141 parFuncname
= "InitXmlResource";
143 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
144 parFiles
.Add(cmdline
.GetParam(i
));
150 void XmlResApp::CompileRes()
152 wxArrayString files
= PrepareTempFiles();
154 wxRemoveFile(parOutput
);
159 MakePackageCPP(files
);
161 MakePackageZIP(files
);
164 DeleteTempFiles(files
);
169 wxArrayString
XmlResApp::PrepareTempFiles()
173 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
176 wxPrintf("processing " + parFiles
[i
] + "...\n");
180 if (!doc
.Load(parFiles
[i
]))
182 wxLogError("Error parsing file " + parFiles
[i
]);
187 wxString name
, ext
, path
;
188 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
190 FindFilesInXML(doc
.GetRoot(), flist
, path
);
192 doc
.Save(parOutputPath
+ "/" + name
+ ".xrc", flagCompress
? wxXML_IO_BINZ
: wxXML_IO_BIN
);
193 flist
.Add(name
+ ".xrc");
201 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
202 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
205 if (n
== NULL
) return;
206 n
= n
->GetChildren();
210 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
211 // parent is an element, i.e. has subnodes...
212 (n
->GetType() == wxXML_TEXT_NODE
||
213 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
214 // ...it is textnode...
215 (node
/*not n!*/->GetName() == "bitmap"))
216 // ...and known to contain filename
219 wxString filename
= n
->GetContent();
220 if (wxIsAbsolutePath(n
->GetContent())) fullname
= n
->GetContent();
221 else fullname
= inputPath
+ "/" + n
->GetContent();
223 filename
.Replace("/", "_");
224 filename
.Replace("\\", "_");
225 filename
.Replace("*", "_");
226 filename
.Replace("?", "_");
227 n
->SetContent(filename
);
230 wxPrintf("adding " + filename
+ "...\n");
234 wxFileInputStream
sin(fullname
);
235 wxFileOutputStream
sout(parOutputPath
+ "/" + filename
);
236 sin
.Read(sout
); // copy the stream
240 if (n
->GetType() == wxXML_ELEMENT_NODE
)
241 FindFilesInXML(n
, flist
, inputPath
);
249 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
251 for (size_t i
= 0; i
< flist
.Count(); i
++)
252 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
257 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
261 for (size_t i
= 0; i
< flist
.Count(); i
++)
262 files
+= flist
[i
] + " ";
266 wxPrintf("compressing " + parOutput
+ "...\n");
268 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
269 parOutput
+ " " + files
, TRUE
) == -1)
271 wxLogError("Unable to execute zip program. Make sure it is in the path.");
272 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
281 static wxString
FileToCppArray(wxString filename
, int num
)
286 wxFFile
file(filename
, "rb");
287 size_t lng
= file
.Length();
289 snum
.Printf("%i", num
);
290 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
291 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {\n";
292 // we cannot use string literals because MSVC is dumb wannabe compiler
293 // with arbitrary limitation to 2048 strings :(
295 unsigned char *buffer
= new unsigned char[lng
];
296 file
.Read(buffer
, lng
);
298 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
300 tmp
.Printf("%i", buffer
[i
]);
301 if (i
!= 0) output
<< ',';
308 linelng
+= tmp
.Length()+1;
319 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
321 wxFFile
file(parOutput
, "wt");
325 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
328 #include \"wx/wxprec.h\"\n\
330 #ifdef __BORLANDC__\n\
334 #ifndef WX_PRECOMP\n\
335 #include \"wx/wx.h\"\n\
338 #include \"wx/filesys.h\"\n\
339 #include \"wx/fs_mem.h\"\n\
340 #include \"wx/xml/xmlres.h\"\n\
341 #include \"wx/xml/xh_all.h\"\n\
344 for (i
= 0; i
< flist
.Count(); i
++)
345 file
.Write(FileToCppArray(flist
[i
], i
));
348 void " + parFuncname
+ "()\n\
351 // Check for memory FS. If not present, load the handler:\n\
353 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
354 wxFileSystem fsys;\n\
355 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
356 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
358 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
362 for (i
= 0; i
< flist
.Count(); i
++)
365 s
.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist
[i
] +
366 "\", xml_res_file_%i, xml_res_size_%i);\n", i
, i
);
370 for (i
= 0; i
< parFiles
.Count(); i
++)
372 wxString name
, ext
, path
;
373 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
374 file
.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" +
375 name
+ ".xrc" + "\");\n");