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/xrc/xml.h"
32 #include "wx/wfstream.h"
40 #error "You must compile the resource compiler with wxBase!"
44 class XmlResApp
: public wxApp
56 void ParseParams(const wxCmdLineParser
& cmdline
);
58 wxArrayString
PrepareTempFiles();
59 void FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
);
61 void DeleteTempFiles(const wxArrayString
& flist
);
62 void MakePackageZIP(const wxArrayString
& flist
);
63 void MakePackageCPP(const wxArrayString
& flist
);
66 wxArrayString
FindStrings();
67 wxArrayString
FindStrings(wxXmlNode
*node
);
69 bool flagVerbose
, flagCPP
, flagGettext
;
70 wxString parOutput
, parFuncname
, parOutputPath
;
71 wxArrayString parFiles
;
75 IMPLEMENT_APP(XmlResApp
)
78 bool XmlResApp::OnInit()
80 int XmlResApp::OnRun()
83 static const wxCmdLineEntryDesc cmdLineDesc
[] =
85 { wxCMD_LINE_SWITCH
, "h", "help", "show help message" },
86 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
87 { wxCMD_LINE_SWITCH
, "c", "cpp-code", "output C++ source rather than .rsc file" },
88 { wxCMD_LINE_SWITCH
, "g", "gettext", "output list of translatable strings (to stdout or file if -o used)" },
89 { wxCMD_LINE_OPTION
, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
90 { wxCMD_LINE_OPTION
, "o", "output", "output file [resource.xrs/cpp]" },
91 { wxCMD_LINE_OPTION
, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
93 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file(s)",
94 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
100 // VS: I need reasonable output to console from wxCmdLineParser
101 wxLog::SetTimestamp(NULL
);
102 delete wxLog::SetActiveTarget(new wxLogStderr
);
105 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
107 switch (parser
.Parse())
140 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
142 flagGettext
= cmdline
.Found("g");
143 flagVerbose
= cmdline
.Found("v");
144 flagCPP
= cmdline
.Found("c");
146 if (!cmdline
.Found("o", &parOutput
))
149 parOutput
= wxEmptyString
;
151 parOutput
= flagCPP
? "resource.cpp" : "resource.xrs";
153 parOutputPath
= wxPathOnly(parOutput
);
154 if (!parOutputPath
) parOutputPath
= ".";
156 if (!cmdline
.Found("n", &parFuncname
))
157 parFuncname
= "InitXmlResource";
159 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
160 parFiles
.Add(cmdline
.GetParam(i
));
166 void XmlResApp::CompileRes()
168 wxArrayString files
= PrepareTempFiles();
170 wxRemoveFile(parOutput
);
175 MakePackageCPP(files
);
177 MakePackageZIP(files
);
180 DeleteTempFiles(files
);
185 wxArrayString
XmlResApp::PrepareTempFiles()
189 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
192 wxPrintf("processing " + parFiles
[i
] + "...\n");
196 if (!doc
.Load(parFiles
[i
]))
198 wxLogError("Error parsing file " + parFiles
[i
]);
203 wxString name
, ext
, path
;
204 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
206 FindFilesInXML(doc
.GetRoot(), flist
, path
);
208 doc
.Save(parOutputPath
+ "/" + name
+ ".xrc");
209 flist
.Add(name
+ ".xrc");
217 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
218 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
221 if (n
== NULL
) return;
222 n
= n
->GetChildren();
226 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
227 // parent is an element, i.e. has subnodes...
228 (n
->GetType() == wxXML_TEXT_NODE
||
229 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
230 // ...it is textnode...
231 ((node
/*not n!*/->GetName() == "bitmap") ||
232 (node
/*not n!*/->GetName() == "url")))
233 // ...and known to contain filename
236 wxString filename
= n
->GetContent();
237 if (wxIsAbsolutePath(n
->GetContent())) fullname
= n
->GetContent();
238 else fullname
= inputPath
+ "/" + n
->GetContent();
240 filename
.Replace("/", "_");
241 filename
.Replace("\\", "_");
242 filename
.Replace("*", "_");
243 filename
.Replace("?", "_");
244 n
->SetContent(filename
);
247 wxPrintf("adding " + filename
+ "...\n");
251 wxFileInputStream
sin(fullname
);
252 wxFileOutputStream
sout(parOutputPath
+ "/" + filename
);
253 sin
.Read(sout
); // copy the stream
257 if (n
->GetType() == wxXML_ELEMENT_NODE
)
258 FindFilesInXML(n
, flist
, inputPath
);
266 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
268 for (size_t i
= 0; i
< flist
.Count(); i
++)
269 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
274 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
278 for (size_t i
= 0; i
< flist
.Count(); i
++)
279 files
+= flist
[i
] + " ";
283 wxPrintf("compressing " + parOutput
+ "...\n");
285 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
286 parOutput
+ " " + files
, TRUE
) == -1)
288 wxLogError("Unable to execute zip program. Make sure it is in the path.");
289 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
298 static wxString
FileToCppArray(wxString filename
, int num
)
303 wxFFile
file(filename
, "rb");
304 size_t lng
= file
.Length();
306 snum
.Printf("%i", num
);
307 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
308 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {\n";
309 // we cannot use string literals because MSVC is dumb wannabe compiler
310 // with arbitrary limitation to 2048 strings :(
312 unsigned char *buffer
= new unsigned char[lng
];
313 file
.Read(buffer
, lng
);
315 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
317 tmp
.Printf("%i", buffer
[i
]);
318 if (i
!= 0) output
<< ',';
325 linelng
+= tmp
.Length()+1;
336 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
338 wxFFile
file(parOutput
, "wt");
342 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
345 #include \"wx/wxprec.h\"\n\
347 #ifdef __BORLANDC__\n\
351 #ifndef WX_PRECOMP\n\
352 #include \"wx/wx.h\"\n\
355 #include \"wx/filesys.h\"\n\
356 #include \"wx/fs_mem.h\"\n\
357 #include \"wx/xrc/xmlres.h\"\n\
358 #include \"wx/xrc/xh_all.h\"\n\
361 for (i
= 0; i
< flist
.Count(); i
++)
362 file
.Write(FileToCppArray(flist
[i
], i
));
365 void " + parFuncname
+ "()\n\
368 // Check for memory FS. If not present, load the handler:\n\
370 wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
371 wxFileSystem fsys;\n\
372 wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
373 wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
375 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
379 for (i
= 0; i
< flist
.Count(); i
++)
382 s
.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist
[i
] +
383 "\", xml_res_file_%i, xml_res_size_%i);\n", i
, i
);
387 for (i
= 0; i
< parFiles
.Count(); i
++)
389 wxString name
, ext
, path
;
390 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
391 file
.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" +
392 name
+ ".xrc" + "\");\n");
402 void XmlResApp::OutputGettext()
404 wxArrayString str
= FindStrings();
407 if (!parOutput
) fout
.Attach(stdout
);
408 else fout
.Open(parOutput
, _T("wt"));
410 for (size_t i
= 0; i
< str
.GetCount(); i
++)
411 fout
.Write(_T("_(\"") + str
[i
] + _T("\")\n"));
413 if (!parOutput
) fout
.Detach();
418 wxArrayString
XmlResApp::FindStrings()
420 wxArrayString arr
, a2
;
422 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
425 wxPrintf("processing " + parFiles
[i
] + "...\n");
428 if (!doc
.Load(parFiles
[i
]))
430 wxLogError("Error parsing file " + parFiles
[i
]);
434 a2
= FindStrings(doc
.GetRoot());
435 WX_APPEND_ARRAY(arr
, a2
);
443 static wxString
ConvertText(const wxString
& str
)
448 for (dt
= str
.c_str(); *dt
; dt
++)
452 if ( *(++dt
) == wxT('_') )
455 str2
<< wxT('&') << *dt
;
461 case wxT('\n') : str2
<< wxT("\\n"); break;
462 case wxT('\t') : str2
<< wxT("\\t"); break;
463 case wxT('\r') : str2
<< wxT("\\r"); break;
464 default : str2
<< *dt
; break;
473 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
478 if (n
== NULL
) return arr
;
479 n
= n
->GetChildren();
483 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
484 // parent is an element, i.e. has subnodes...
485 (n
->GetType() == wxXML_TEXT_NODE
||
486 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
487 // ...it is textnode...
489 node
/*not n!*/->GetName() == _T("label") ||
490 (node
/*not n!*/->GetName() == _T("value") &&
491 !n
->GetContent().IsNumber()) ||
492 node
/*not n!*/->GetName() == _T("help") ||
493 node
/*not n!*/->GetName() == _T("longhelp") ||
494 node
/*not n!*/->GetName() == _T("tooltip") ||
495 node
/*not n!*/->GetName() == _T("htmlcode") ||
496 node
/*not n!*/->GetName() == _T("title")
498 // ...and known to contain translatable string
500 arr
.Add(ConvertText(n
->GetContent()));
504 if (n
->GetType() == wxXML_ELEMENT_NODE
)
506 wxArrayString a2
= FindStrings(n
);
507 WX_APPEND_ARRAY(arr
, a2
);