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"
36 #error "You must compile the resource compiler with wxBase!"
40 class XmlResApp
: public wxApp
52 void ParseParams(const wxCmdLineParser
& cmdline
);
54 wxArrayString
PrepareTempFiles();
55 void DeleteTempFiles(const wxArrayString
& flist
);
56 void MakePackageZIP(const wxArrayString
& flist
);
57 void MakePackageCPP(const wxArrayString
& flist
);
59 bool flagVerbose
, flagCPP
, flagCompress
;
60 wxString parOutput
, parFuncname
, parOutputPath
;
61 wxArrayString parFiles
;
65 IMPLEMENT_APP(XmlResApp
)
68 bool XmlResApp::OnInit()
70 int XmlResApp::OnRun()
73 static const wxCmdLineEntryDesc cmdLineDesc
[] =
75 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
76 { wxCMD_LINE_SWITCH
, "c", "cpp-code", "output C++ source rather than .rsc file" },
77 { wxCMD_LINE_SWITCH
, "u", "uncompressed", "do not compress .xml files (C++ only)" },
78 { wxCMD_LINE_OPTION
, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
79 { wxCMD_LINE_OPTION
, "o", "output", "output file [resource.rsc/cpp]" },
80 { wxCMD_LINE_OPTION
, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
82 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
83 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
88 wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ
);
90 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
92 switch (parser
.Parse())
122 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
124 flagVerbose
= cmdline
.Found("v");
125 flagCPP
= cmdline
.Found("c");
126 flagCompress
= flagCPP
&& !cmdline
.Found("u");
128 if (!cmdline
.Found("o", &parOutput
))
129 parOutput
= flagCPP
? "resource.cpp" : "resource.rsc";
130 parOutputPath
= wxPathOnly(parOutput
);
131 if (!parOutputPath
) parOutputPath
= ".";
133 if (!cmdline
.Found("n", &parFuncname
))
134 parFuncname
= "InitXmlResource";
136 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
137 parFiles
.Add(cmdline
.GetParam(i
));
143 void XmlResApp::CompileRes()
145 wxArrayString files
= PrepareTempFiles();
147 wxRemoveFile(parOutput
);
149 printf("TODO: include bitmaps, list of handlers\n");
154 MakePackageCPP(files
);
156 MakePackageZIP(files
);
159 DeleteTempFiles(files
);
164 wxArrayString
XmlResApp::PrepareTempFiles()
168 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
171 wxPrintf("processing " + parFiles
[i
] + "...\n");
175 if (!doc
.Load(parFiles
[i
]))
177 wxLogError("Error parsing file " + parFiles
[i
]);
183 wxSplitPath(parFiles
[i
], NULL
, &name
, &ext
);
185 doc
.Save(parOutputPath
+ "/" + name
+ ".xmb", flagCompress
? wxXML_IO_BINZ
: wxXML_IO_BIN
);
186 flist
.Add(name
+ ".xmb");
194 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
196 for (size_t i
= 0; i
< flist
.Count(); i
++)
197 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
202 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
206 for (size_t i
= 0; i
< flist
.Count(); i
++)
207 files
+= flist
[i
] + " ";
211 wxPrintf("compressing " + parOutput
+ "...\n");
213 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
214 parOutput
+ " " + files
, TRUE
) == -1)
216 wxLogError("Unable to execute zip program. Make sure it is in the path.");
217 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
226 static wxString
FileToCppArray(wxString filename
, int num
)
231 wxFFile
file(filename
, "rb");
232 size_t lng
= file
.Length();
234 snum
.Printf("%i", num
);
235 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
236 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {";
238 unsigned char *buffer
= new unsigned char[lng
];
239 file
.Read(buffer
, lng
);
241 for (size_t i
= 0; i
< lng
; i
++)
243 if (i
% 16 == 0) output
+= "\n";
244 tmp
.Printf("0x%02X", buffer
[i
]);
246 if (i
!= lng
-1) output
+= ",";
251 output
+= "\n};\n\n";
257 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
259 wxFFile
file(parOutput
, "wt");
263 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
266 #include \"wx/wxprec.h\"\n\
268 #ifdef __BORLANDC__\n\
272 #ifndef WX_PRECOMP\n\
273 #include \"wx/wx.h\"\n\
276 #include \"wx/filesys.h\"\n\
277 #include \"wx/fs_mem.h\"\n\
278 #include \"wx/xml/xmlres.h\"\n\
279 #include \"wx/xml/xh_all.h\"\n\
282 for (i
= 0; i
< flist
.Count(); i
++)
283 file
.Write(FileToCppArray(flist
[i
], i
));
286 void " + parFuncname
+ "()\n\
289 // Check for memory FS. If not present, load the handler:\n\
291 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
292 wxFileSystem fsys;\n\
293 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
294 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
296 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
300 for (i
= 0; i
< flist
.Count(); i
++)
303 s
.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist
[i
] +
304 "\", xml_res_file_%i, xml_res_size_%i);\n"
305 " wxTheXmlResource->Load(\"memory:xml_resource/" + flist
[i
] +
306 "\", wxXML_BINARY);\n", i
, i
);