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 wxString
GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
);
62 void DeleteTempFiles(const wxArrayString
& flist
);
63 void MakePackageZIP(const wxArrayString
& flist
);
64 void MakePackageCPP(const wxArrayString
& flist
);
67 wxArrayString
FindStrings();
68 wxArrayString
FindStrings(wxXmlNode
*node
);
70 bool flagVerbose
, flagCPP
, flagGettext
;
71 wxString parOutput
, parFuncname
, parOutputPath
;
72 wxArrayString parFiles
;
76 IMPLEMENT_APP(XmlResApp
)
79 bool XmlResApp::OnInit()
81 int XmlResApp::OnRun()
84 static const wxCmdLineEntryDesc cmdLineDesc
[] =
86 { wxCMD_LINE_SWITCH
, "h", "help", "show help message" },
87 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
88 { wxCMD_LINE_SWITCH
, "c", "cpp-code", "output C++ source rather than .rsc file" },
89 { wxCMD_LINE_SWITCH
, "g", "gettext", "output list of translatable strings (to stdout or file if -o used)" },
90 { wxCMD_LINE_OPTION
, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
91 { wxCMD_LINE_OPTION
, "o", "output", "output file [resource.xrs/cpp]" },
92 { wxCMD_LINE_OPTION
, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
94 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file(s)",
95 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
101 // VS: I need reasonable output to console from wxCmdLineParser
102 wxLog::SetTimestamp(NULL
);
103 delete wxLog::SetActiveTarget(new wxLogStderr
);
106 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
108 switch (parser
.Parse())
141 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
143 flagGettext
= cmdline
.Found("g");
144 flagVerbose
= cmdline
.Found("v");
145 flagCPP
= cmdline
.Found("c");
147 if (!cmdline
.Found("o", &parOutput
))
150 parOutput
= wxEmptyString
;
152 parOutput
= flagCPP
? "resource.cpp" : "resource.xrs";
154 parOutputPath
= wxPathOnly(parOutput
);
155 if (!parOutputPath
) parOutputPath
= ".";
157 if (!cmdline
.Found("n", &parFuncname
))
158 parFuncname
= "InitXmlResource";
160 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
161 parFiles
.Add(cmdline
.GetParam(i
));
167 void XmlResApp::CompileRes()
169 wxArrayString files
= PrepareTempFiles();
171 wxRemoveFile(parOutput
);
176 MakePackageCPP(files
);
178 MakePackageZIP(files
);
181 DeleteTempFiles(files
);
185 wxString
XmlResApp::GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
)
187 wxString name2
= name
;
188 name2
.Replace(":", "_");
189 name2
.Replace("/", "_");
190 name2
.Replace("\\", "_");
191 name2
.Replace("*", "_");
192 name2
.Replace("?", "_");
194 wxString s
= wxFileNameFromPath(parOutput
) + "$" + name2
;
196 if (wxFileExists(s
) && flist
.Index(s
) == wxNOT_FOUND
)
198 for (int i
= 0;; i
++)
200 s
.Printf(wxFileNameFromPath(parOutput
) + "$%03i-" + name2
, i
);
201 if (!wxFileExists(s
) || flist
.Index(s
) != wxNOT_FOUND
)
208 wxArrayString
XmlResApp::PrepareTempFiles()
212 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
215 wxPrintf("processing " + parFiles
[i
] + "...\n");
219 if (!doc
.Load(parFiles
[i
]))
221 wxLogError("Error parsing file " + parFiles
[i
]);
226 wxString name
, ext
, path
;
227 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
229 FindFilesInXML(doc
.GetRoot(), flist
, path
);
231 wxString internalName
= GetInternalFileName(parFiles
[i
], flist
);
233 doc
.Save(parOutputPath
+ "/" + internalName
);
234 flist
.Add(internalName
);
242 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
243 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
246 if (n
== NULL
) return;
247 n
= n
->GetChildren();
251 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
252 // parent is an element, i.e. has subnodes...
253 (n
->GetType() == wxXML_TEXT_NODE
||
254 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
255 // ...it is textnode...
256 ((node
/*not n!*/->GetName() == "bitmap") ||
257 (node
/*not n!*/->GetName() == "url")))
258 // ...and known to contain filename
261 if (wxIsAbsolutePath(n
->GetContent())) fullname
= n
->GetContent();
262 else fullname
= inputPath
+ "/" + n
->GetContent();
265 wxPrintf("adding " + fullname
+ "...\n");
267 wxString filename
= GetInternalFileName(n
->GetContent(), flist
);
268 n
->SetContent(filename
);
272 wxFileInputStream
sin(fullname
);
273 wxFileOutputStream
sout(parOutputPath
+ "/" + filename
);
274 sin
.Read(sout
); // copy the stream
278 if (n
->GetType() == wxXML_ELEMENT_NODE
)
279 FindFilesInXML(n
, flist
, inputPath
);
287 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
289 for (size_t i
= 0; i
< flist
.Count(); i
++)
290 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
295 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
299 for (size_t i
= 0; i
< flist
.Count(); i
++)
300 files
+= flist
[i
] + " ";
304 wxPrintf("compressing " + parOutput
+ "...\n");
306 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
307 parOutput
+ " " + files
, TRUE
) == -1)
309 wxLogError("Unable to execute zip program. Make sure it is in the path.");
310 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
319 static wxString
FileToCppArray(wxString filename
, int num
)
324 wxFFile
file(filename
, "rb");
325 size_t lng
= file
.Length();
327 snum
.Printf("%i", num
);
328 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
329 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {\n";
330 // we cannot use string literals because MSVC is dumb wannabe compiler
331 // with arbitrary limitation to 2048 strings :(
333 unsigned char *buffer
= new unsigned char[lng
];
334 file
.Read(buffer
, lng
);
336 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
338 tmp
.Printf("%i", buffer
[i
]);
339 if (i
!= 0) output
<< ',';
346 linelng
+= tmp
.Length()+1;
357 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
359 wxFFile
file(parOutput
, "wt");
363 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
366 #include <wx/wxprec.h>\n\
368 #ifdef __BORLANDC__\n\
372 #ifndef WX_PRECOMP\n\
373 #include <wx/wx.h>\n\
376 #include <wx/filesys.h>\n\
377 #include <wx/fs_mem.h>\n\
378 #include <wx/xrc/xmlres.h>\n\
379 #include <wx/xrc/xh_all.h>\n\
382 for (i
= 0; i
< flist
.Count(); i
++)
383 file
.Write(FileToCppArray(flist
[i
], i
));
386 void " + parFuncname
+ "()\n\
389 // Check for memory FS. If not present, load the handler:\n\
391 wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\
392 wxFileSystem fsys;\n\
393 wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\
394 wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\
396 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
400 for (i
= 0; i
< flist
.Count(); i
++)
403 s
.Printf(" wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist
[i
] +
404 "\", xml_res_file_%i, xml_res_size_%i);\n", i
, i
);
408 for (i
= 0; i
< parFiles
.Count(); i
++)
410 file
.Write(" wxXmlResource::Get()->Load(\"memory:XRC_resource/" +
411 GetInternalFileName(parFiles
[i
], flist
) + "\");\n");
421 void XmlResApp::OutputGettext()
423 wxArrayString str
= FindStrings();
426 if (!parOutput
) fout
.Attach(stdout
);
427 else fout
.Open(parOutput
, _T("wt"));
429 for (size_t i
= 0; i
< str
.GetCount(); i
++)
430 fout
.Write(_T("_(\"") + str
[i
] + _T("\")\n"));
432 if (!parOutput
) fout
.Detach();
437 wxArrayString
XmlResApp::FindStrings()
439 wxArrayString arr
, a2
;
441 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
444 wxPrintf("processing " + parFiles
[i
] + "...\n");
447 if (!doc
.Load(parFiles
[i
]))
449 wxLogError("Error parsing file " + parFiles
[i
]);
453 a2
= FindStrings(doc
.GetRoot());
454 WX_APPEND_ARRAY(arr
, a2
);
462 static wxString
ConvertText(const wxString
& str
)
467 for (dt
= str
.c_str(); *dt
; dt
++)
471 if ( *(++dt
) == wxT('_') )
474 str2
<< wxT('&') << *dt
;
480 case wxT('\n') : str2
<< wxT("\\n"); break;
481 case wxT('\t') : str2
<< wxT("\\t"); break;
482 case wxT('\r') : str2
<< wxT("\\r"); break;
483 default : str2
<< *dt
; break;
492 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
497 if (n
== NULL
) return arr
;
498 n
= n
->GetChildren();
502 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
503 // parent is an element, i.e. has subnodes...
504 (n
->GetType() == wxXML_TEXT_NODE
||
505 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
506 // ...it is textnode...
508 node
/*not n!*/->GetName() == _T("label") ||
509 (node
/*not n!*/->GetName() == _T("value") &&
510 !n
->GetContent().IsNumber()) ||
511 node
/*not n!*/->GetName() == _T("help") ||
512 node
/*not n!*/->GetName() == _T("longhelp") ||
513 node
/*not n!*/->GetName() == _T("tooltip") ||
514 node
/*not n!*/->GetName() == _T("htmlcode") ||
515 node
/*not n!*/->GetName() == _T("title")
517 // ...and known to contain translatable string
519 arr
.Add(ConvertText(n
->GetContent()));
523 if (n
->GetType() == wxXML_ELEMENT_NODE
)
525 wxArrayString a2
= FindStrings(n
);
526 WX_APPEND_ARRAY(arr
, a2
);