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
46 void ParseParams(const wxCmdLineParser
& cmdline
);
48 wxArrayString
PrepareTempFiles();
49 void DeleteTempFiles(const wxArrayString
& flist
);
50 void MakePackageZIP(const wxArrayString
& flist
);
51 void MakePackageCPP(const wxArrayString
& flist
);
53 bool flagVerbose
, flagCPP
, flagCompress
;
54 wxString parOutput
, parFuncname
, parOutputPath
;
55 wxArrayString parFiles
;
59 IMPLEMENT_APP(XmlResApp
)
61 int XmlResApp
::OnRun()
63 static const wxCmdLineEntryDesc cmdLineDesc
[] =
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" },
72 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
73 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
78 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
80 switch (parser
.Parse())
102 void XmlResApp
::ParseParams(const wxCmdLineParser
& cmdline
)
104 flagVerbose
= cmdline
.Found("v");
105 flagCPP
= cmdline
.Found("c");
106 flagCompress
= flagCPP
&& !cmdline
.Found("u");
108 if (!cmdline
.Found("o", &parOutput
))
109 parOutput
= flagCPP ?
"resource.cpp" : "resource.rsc";
110 parOutputPath
= wxPathOnly(parOutput
);
111 if (!parOutputPath
) parOutputPath
= ".";
113 if (!cmdline
.Found("n", &parFuncname
))
114 parFuncname
= "InitXmlResource";
116 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
117 parFiles
.Add(cmdline
.GetParam(i
));
123 void XmlResApp
::CompileRes()
125 wxArrayString files
= PrepareTempFiles();
127 wxRemoveFile(parOutput
);
129 printf("TODO: include bitmaps, list of handlers\n");
134 MakePackageCPP(files
);
136 MakePackageZIP(files
);
139 DeleteTempFiles(files
);
144 wxArrayString XmlResApp
::PrepareTempFiles()
148 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
151 wxPrintf("processing " + parFiles
[i
] + "...\n");
155 if (!doc
.Load(parFiles
[i
]))
157 wxLogError("Error parsing file " + parFiles
[i
]);
163 wxSplitPath(parFiles
[i
], NULL
, &name
, &ext
);
165 doc
.Save(parOutputPath
+ "/" + name
+ ".xmb", flagCompress ? wxXML_IO_BINZ
: wxXML_IO_BIN
);
166 flist
.Add(name
+ ".xmb");
174 void XmlResApp
::DeleteTempFiles(const wxArrayString
& flist
)
176 for (size_t i
= 0; i
< flist
.Count(); i
++)
177 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
182 void XmlResApp
::MakePackageZIP(const wxArrayString
& flist
)
186 for (size_t i
= 0; i
< flist
.Count(); i
++)
187 files
+= flist
[i
] + " ";
191 wxPrintf("compressing " + parOutput
+ "...\n");
193 if (wxExecute("zip -9 -j " + wxString(flagVerbose ?
"" : "-q ") +
194 parOutput
+ " " + files
, TRUE
) == -1)
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/");
206 static wxString
FileToCppArray(wxString filename
, int num
)
211 wxFFile
file(filename
, "rb");
212 size_t lng
= file
.Length();
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
+ "[] = {";
218 unsigned char *buffer
= new unsigned char[lng
];
219 file
.Read(buffer
, lng
);
221 for (size_t i
= 0; i
< lng
; i
++)
223 if (i
% 16 == 0) output
+= "\n";
224 tmp
.Printf("0x%02X", buffer
[i
]);
226 if (i
!= lng
-1) output
+= ",";
231 output
+= "\n};\n\n";
237 void XmlResApp
::MakePackageCPP(const wxArrayString
& flist
)
239 wxFFile
file(parOutput
, "wt");
243 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
246 #include \"wx/wxprec.h\"\n\
248 #ifdef __BORLANDC__\n\
252 #ifndef WX_PRECOMP\n\
253 #include \"wx/wx.h\"\n\
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\
262 for (i
= 0; i
< flist
.Count(); i
++)
263 file
.Write(FileToCppArray(flist
[i
], i
));
266 void " + parFuncname
+ "()\n\
269 // Check for memory FS. If not present, load the handler:\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\
276 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
280 for (i
= 0; i
< flist
.Count(); i
++)
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
);