added XML resources library
[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/xml/xml.h"
31 #include "wx/ffile.h"
32
33
34 #if wxUSE_GUI
35 #error "You must compile the resource compiler with wxBase!"
36 #endif
37
38
39 class XmlResApp : public wxApp
40 {
41 public:
42 virtual int OnRun();
43
44 private:
45
46 void ParseParams(const wxCmdLineParser& cmdline);
47 void CompileRes();
48 wxArrayString PrepareTempFiles();
49 void DeleteTempFiles(const wxArrayString& flist);
50 void MakePackageZIP(const wxArrayString& flist);
51 void MakePackageCPP(const wxArrayString& flist);
52
53 bool flagVerbose, flagCPP, flagCompress;
54 wxString parOutput, parFuncname, parOutputPath;
55 wxArrayString parFiles;
56 int retCode;
57 };
58
59 IMPLEMENT_APP(XmlResApp)
60
61 int XmlResApp::OnRun()
62 {
63 static const wxCmdLineEntryDesc cmdLineDesc[] =
64 {
65 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
66 { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
67 { wxCMD_LINE_SWITCH, "u", "uncompressed", "do not compress .xml files (C++ only)" },
68 { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
69 { wxCMD_LINE_OPTION, "o", "output", "output file [resource.rsc/cpp]" },
70 { wxCMD_LINE_OPTION, "h", "handlers", "output list of neccessary handlers to this file" },
71
72 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
73 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
74
75 { wxCMD_LINE_NONE }
76 };
77
78 wxCmdLineParser parser(cmdLineDesc, argc, argv);
79
80 switch (parser.Parse())
81 {
82 case -1:
83 return 0;
84 break;
85
86 case 0:
87 retCode = 0;
88 ParseParams(parser);
89 CompileRes();
90 return retCode;
91 break;
92
93 default:
94 return 1;
95 break;
96 }
97 }
98
99
100
101
102 void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
103 {
104 flagVerbose = cmdline.Found("v");
105 flagCPP = cmdline.Found("c");
106 flagCompress = flagCPP && !cmdline.Found("u");
107
108 if (!cmdline.Found("o", &parOutput))
109 parOutput = flagCPP ? "resource.cpp" : "resource.rsc";
110 parOutputPath = wxPathOnly(parOutput);
111 if (!parOutputPath) parOutputPath = ".";
112
113 if (!cmdline.Found("n", &parFuncname))
114 parFuncname = "InitXmlResource";
115
116 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
117 parFiles.Add(cmdline.GetParam(i));
118 }
119
120
121
122
123 void XmlResApp::CompileRes()
124 {
125 wxArrayString files = PrepareTempFiles();
126
127 wxRemoveFile(parOutput);
128
129 printf("TODO: include bitmaps, list of handlers\n");
130
131 if (!retCode)
132 {
133 if (flagCPP)
134 MakePackageCPP(files);
135 else
136 MakePackageZIP(files);
137 }
138
139 DeleteTempFiles(files);
140 }
141
142
143
144 wxArrayString XmlResApp::PrepareTempFiles()
145 {
146 wxArrayString flist;
147
148 for (size_t i = 0; i < parFiles.Count(); i++)
149 {
150 if (flagVerbose)
151 wxPrintf("processing " + parFiles[i] + "...\n");
152
153 wxXmlDocument doc;
154
155 if (!doc.Load(parFiles[i]))
156 {
157 wxLogError("Error parsing file " + parFiles[i]);
158 retCode = 1;
159 continue;
160 }
161
162 wxString name, ext;
163 wxSplitPath(parFiles[i], NULL, &name, &ext);
164
165 doc.Save(parOutputPath + "/" + name + ".xmb", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN);
166 flist.Add(name + ".xmb");
167 }
168
169 return flist;
170 }
171
172
173
174 void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
175 {
176 for (size_t i = 0; i < flist.Count(); i++)
177 wxRemoveFile(parOutputPath + "/" + flist[i]);
178 }
179
180
181
182 void XmlResApp::MakePackageZIP(const wxArrayString& flist)
183 {
184 wxString files;
185
186 for (size_t i = 0; i < flist.Count(); i++)
187 files += flist[i] + " ";
188 files.RemoveLast();
189
190 if (flagVerbose)
191 wxPrintf("compressing " + parOutput + "...\n");
192
193 if (wxExecute("zip -9 -j " + wxString(flagVerbose ? "" : "-q ") +
194 parOutput + " " + files, TRUE) == -1)
195 {
196 wxLogError("Unable to execute zip program. Make sure it is in the path.");
197 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
198 retCode = 1;
199 return;
200 }
201 }
202
203
204
205
206 static wxString FileToCppArray(wxString filename, int num)
207 {
208 wxString output;
209 wxString snum;
210 wxString tmp;
211 wxFFile file(filename, "rb");
212 size_t lng = file.Length();
213
214 snum.Printf("%i", num);
215 output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng);
216 output += "static unsigned char xml_res_file_" + snum + "[] = {";
217
218 unsigned char *buffer = new unsigned char[lng];
219 file.Read(buffer, lng);
220
221 for (size_t i = 0; i < lng; i++)
222 {
223 if (i % 16 == 0) output += "\n";
224 tmp.Printf("0x%02X", buffer[i]);
225 output += tmp;
226 if (i != lng-1) output += ",";
227 }
228
229 delete[] buffer;
230
231 output += "\n};\n\n";
232
233 return output;
234 }
235
236
237 void XmlResApp::MakePackageCPP(const wxArrayString& flist)
238 {
239 wxFFile file(parOutput, "wt");
240 size_t i;
241
242 if (flagVerbose)
243 wxPrintf("creating C++ source file " + parOutput + "...\n");
244
245 file.Write("\
246 #include \"wx/wxprec.h\"\n\
247 \n\
248 #ifdef __BORLANDC__\n\
249 #pragma hdrstop\n\
250 #endif\n\
251 \n\
252 #ifndef WX_PRECOMP\n\
253 #include \"wx/wx.h\"\n\
254 #endif\n\
255 \
256 #include \"wx/filesys.h\"\n\
257 #include \"wx/fs_mem.h\"\n\
258 #include \"wx/xml/xmlres.h\"\n\
259 #include \"wx/xml/xh_all.h\"\n\
260 \n");
261
262 for (i = 0; i < flist.Count(); i++)
263 file.Write(FileToCppArray(flist[i], i));
264
265 file.Write("\
266 void " + parFuncname + "()\n\
267 {\n\
268 \n\
269 // Check for memory FS. If not present, load the handler:\n\
270 {\n\
271 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
272 wxFileSystem fsys;\n\
273 wxFSFile *f = fsys.OpenFile(\"xml_resource/dummy_file\");\n\
274 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
275 if (f) delete f;\n\
276 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
277 }\n\
278 \n");
279
280 for (i = 0; i < flist.Count(); i++)
281 {
282 wxString s;
283 s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
284 "\", xml_res_file_%i, xml_res_size_%i);\n"
285 " wxTheXmlResource->Read(\"xml_resource/" + flist[i] +
286 "\", wxXML_BINARY);\n", i, i);
287 file.Write(s);
288 }
289
290 file.Write("\n}\n");
291
292 }