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"
35 #error "You must compile the resource compiler with wxBase!"
39 class XmlResApp
: public wxApp
51 void ParseParams(const wxCmdLineParser
& cmdline
);
53 wxArrayString
PrepareTempFiles();
54 void DeleteTempFiles(const wxArrayString
& flist
);
55 void MakePackageZIP(const wxArrayString
& flist
);
56 void MakePackageCPP(const wxArrayString
& flist
);
58 bool flagVerbose
, flagCPP
, flagCompress
;
59 wxString parOutput
, parFuncname
, parOutputPath
;
60 wxArrayString parFiles
;
64 IMPLEMENT_APP(XmlResApp
)
67 bool XmlResApp::OnInit()
69 int XmlResApp::OnRun()
72 static const wxCmdLineEntryDesc cmdLineDesc
[] =
74 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
75 { wxCMD_LINE_SWITCH
, "c", "cpp-code", "output C++ source rather than .rsc file" },
76 { wxCMD_LINE_SWITCH
, "u", "uncompressed", "do not compress .xml files (C++ only)" },
77 { wxCMD_LINE_OPTION
, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
78 { wxCMD_LINE_OPTION
, "o", "output", "output file [resource.rsc/cpp]" },
79 { wxCMD_LINE_OPTION
, "h", "handlers", "output list of neccessary handlers to this file" },
81 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
82 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
87 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
89 switch (parser
.Parse())
119 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
121 flagVerbose
= cmdline
.Found("v");
122 flagCPP
= cmdline
.Found("c");
123 flagCompress
= flagCPP
&& !cmdline
.Found("u");
125 if (!cmdline
.Found("o", &parOutput
))
126 parOutput
= flagCPP
? "resource.cpp" : "resource.rsc";
127 parOutputPath
= wxPathOnly(parOutput
);
128 if (!parOutputPath
) parOutputPath
= ".";
130 if (!cmdline
.Found("n", &parFuncname
))
131 parFuncname
= "InitXmlResource";
133 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
134 parFiles
.Add(cmdline
.GetParam(i
));
140 void XmlResApp::CompileRes()
142 wxArrayString files
= PrepareTempFiles();
144 wxRemoveFile(parOutput
);
146 printf("TODO: include bitmaps, list of handlers\n");
151 MakePackageCPP(files
);
153 MakePackageZIP(files
);
156 DeleteTempFiles(files
);
161 wxArrayString
XmlResApp::PrepareTempFiles()
165 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
168 wxPrintf("processing " + parFiles
[i
] + "...\n");
172 if (!doc
.Load(parFiles
[i
]))
174 wxLogError("Error parsing file " + parFiles
[i
]);
180 wxSplitPath(parFiles
[i
], NULL
, &name
, &ext
);
182 doc
.Save(parOutputPath
+ "/" + name
+ ".xmb", flagCompress
? wxXML_IO_BINZ
: wxXML_IO_BIN
);
183 flist
.Add(name
+ ".xmb");
191 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
193 for (size_t i
= 0; i
< flist
.Count(); i
++)
194 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
199 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
203 for (size_t i
= 0; i
< flist
.Count(); i
++)
204 files
+= flist
[i
] + " ";
208 wxPrintf("compressing " + parOutput
+ "...\n");
210 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
211 parOutput
+ " " + files
, TRUE
) == -1)
213 wxLogError("Unable to execute zip program. Make sure it is in the path.");
214 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
223 static wxString
FileToCppArray(wxString filename
, int num
)
228 wxFFile
file(filename
, "rb");
229 size_t lng
= file
.Length();
231 snum
.Printf("%i", num
);
232 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
233 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {";
235 unsigned char *buffer
= new unsigned char[lng
];
236 file
.Read(buffer
, lng
);
238 for (size_t i
= 0; i
< lng
; i
++)
240 if (i
% 16 == 0) output
+= "\n";
241 tmp
.Printf("0x%02X", buffer
[i
]);
243 if (i
!= lng
-1) output
+= ",";
248 output
+= "\n};\n\n";
254 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
256 wxFFile
file(parOutput
, "wt");
260 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
263 #include \"wx/wxprec.h\"\n\
265 #ifdef __BORLANDC__\n\
269 #ifndef WX_PRECOMP\n\
270 #include \"wx/wx.h\"\n\
273 #include \"wx/filesys.h\"\n\
274 #include \"wx/fs_mem.h\"\n\
275 #include \"wx/xml/xmlres.h\"\n\
276 #include \"wx/xml/xh_all.h\"\n\
279 for (i
= 0; i
< flist
.Count(); i
++)
280 file
.Write(FileToCppArray(flist
[i
], i
));
283 void " + parFuncname
+ "()\n\
286 // Check for memory FS. If not present, load the handler:\n\
288 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
289 wxFileSystem fsys;\n\
290 wxFSFile *f = fsys.OpenFile(\"xml_resource/dummy_file\");\n\
291 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
293 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
297 for (i
= 0; i
< flist
.Count(); i
++)
300 s
.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist
[i
] +
301 "\", xml_res_file_%i, xml_res_size_%i);\n"
302 " wxTheXmlResource->Read(\"xml_resource/" + flist
[i
] +
303 "\", wxXML_BINARY);\n", i
, i
);