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_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
88 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
89 { wxCMD_LINE_SWITCH
, "c", "cpp-code", "output C++ source rather than .rsc file" },
90 { wxCMD_LINE_SWITCH
, "g", "gettext", "output list of translatable strings (to stdout or file if -o used)" },
91 { wxCMD_LINE_OPTION
, "n", "function", "C++ function name (with -c) [InitXmlResource]" },
92 { wxCMD_LINE_OPTION
, "o", "output", "output file [resource.xrs/cpp]" },
93 #if 0 // not yet implemented
94 { wxCMD_LINE_OPTION
, "l", "list-of-handlers", "output list of neccessary handlers to this file" },
96 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file(s)",
97 wxCMD_LINE_VAL_STRING
,
98 wxCMD_LINE_PARAM_MULTIPLE
| wxCMD_LINE_OPTION_MANDATORY
},
103 #if wxUSE_GUI && !defined(__WXMSW__)
104 // VS: I need reasonable output to console from wxCmdLineParser
105 // - temporary, will hopefully be fixed in future in wxWin
106 wxLog::SetTimestamp(NULL
);
107 delete wxLog::SetActiveTarget(new wxLogStderr
);
110 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
112 switch (parser
.Parse())
145 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
147 flagGettext
= cmdline
.Found("g");
148 flagVerbose
= cmdline
.Found("v");
149 flagCPP
= cmdline
.Found("c");
151 if (!cmdline
.Found("o", &parOutput
))
154 parOutput
= wxEmptyString
;
156 parOutput
= flagCPP
? "resource.cpp" : "resource.xrs";
158 parOutputPath
= wxPathOnly(parOutput
);
159 if (!parOutputPath
) parOutputPath
= ".";
161 if (!cmdline
.Found("n", &parFuncname
))
162 parFuncname
= "InitXmlResource";
164 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
165 parFiles
.Add(cmdline
.GetParam(i
));
171 void XmlResApp::CompileRes()
173 wxArrayString files
= PrepareTempFiles();
175 wxRemoveFile(parOutput
);
180 MakePackageCPP(files
);
182 MakePackageZIP(files
);
185 DeleteTempFiles(files
);
189 wxString
XmlResApp::GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
)
191 wxString name2
= name
;
192 name2
.Replace(":", "_");
193 name2
.Replace("/", "_");
194 name2
.Replace("\\", "_");
195 name2
.Replace("*", "_");
196 name2
.Replace("?", "_");
198 wxString s
= wxFileNameFromPath(parOutput
) + "$" + name2
;
200 if (wxFileExists(s
) && flist
.Index(s
) == wxNOT_FOUND
)
202 for (int i
= 0;; i
++)
204 s
.Printf(wxFileNameFromPath(parOutput
) + "$%03i-" + name2
, i
);
205 if (!wxFileExists(s
) || flist
.Index(s
) != wxNOT_FOUND
)
212 wxArrayString
XmlResApp::PrepareTempFiles()
216 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
219 wxPrintf("processing " + parFiles
[i
] + "...\n");
223 if (!doc
.Load(parFiles
[i
]))
225 wxLogError("Error parsing file " + parFiles
[i
]);
230 wxString name
, ext
, path
;
231 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
233 FindFilesInXML(doc
.GetRoot(), flist
, path
);
235 wxString internalName
= GetInternalFileName(parFiles
[i
], flist
);
237 doc
.Save(parOutputPath
+ "/" + internalName
);
238 flist
.Add(internalName
);
246 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
247 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
250 if (n
== NULL
) return;
251 n
= n
->GetChildren();
255 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
256 // parent is an element, i.e. has subnodes...
257 (n
->GetType() == wxXML_TEXT_NODE
||
258 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
259 // ...it is textnode...
260 ((node
/*not n!*/->GetName() == "bitmap") ||
261 (node
/*not n!*/->GetName() == "url")))
262 // ...and known to contain filename
265 if (wxIsAbsolutePath(n
->GetContent())) fullname
= n
->GetContent();
266 else fullname
= inputPath
+ "/" + n
->GetContent();
269 wxPrintf("adding " + fullname
+ "...\n");
271 wxString filename
= GetInternalFileName(n
->GetContent(), flist
);
272 n
->SetContent(filename
);
276 wxFileInputStream
sin(fullname
);
277 wxFileOutputStream
sout(parOutputPath
+ "/" + filename
);
278 sin
.Read(sout
); // copy the stream
282 if (n
->GetType() == wxXML_ELEMENT_NODE
)
283 FindFilesInXML(n
, flist
, inputPath
);
291 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
293 for (size_t i
= 0; i
< flist
.Count(); i
++)
294 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
299 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
303 for (size_t i
= 0; i
< flist
.Count(); i
++)
304 files
+= flist
[i
] + " ";
308 wxPrintf("compressing " + parOutput
+ "...\n");
310 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
311 parOutput
+ " " + files
, TRUE
) == -1)
313 wxLogError("Unable to execute zip program. Make sure it is in the path.");
314 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
323 static wxString
FileToCppArray(wxString filename
, int num
)
328 wxFFile
file(filename
, "rb");
329 size_t lng
= file
.Length();
331 snum
.Printf("%i", num
);
332 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
333 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {\n";
334 // we cannot use string literals because MSVC is dumb wannabe compiler
335 // with arbitrary limitation to 2048 strings :(
337 unsigned char *buffer
= new unsigned char[lng
];
338 file
.Read(buffer
, lng
);
340 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
342 tmp
.Printf("%i", buffer
[i
]);
343 if (i
!= 0) output
<< ',';
350 linelng
+= tmp
.Length()+1;
361 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
363 wxFFile
file(parOutput
, "wt");
367 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
370 #include <wx/wxprec.h>\n\
372 #ifdef __BORLANDC__\n\
376 #ifndef WX_PRECOMP\n\
377 #include <wx/wx.h>\n\
380 #include <wx/filesys.h>\n\
381 #include <wx/fs_mem.h>\n\
382 #include <wx/xrc/xmlres.h>\n\
383 #include <wx/xrc/xh_all.h>\n\
386 for (i
= 0; i
< flist
.Count(); i
++)
387 file
.Write(FileToCppArray(flist
[i
], i
));
390 void " + parFuncname
+ "()\n\
393 // Check for memory FS. If not present, load the handler:\n\
395 wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\
396 wxFileSystem fsys;\n\
397 wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\
398 wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\
400 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
404 for (i
= 0; i
< flist
.Count(); i
++)
407 s
.Printf(" wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist
[i
] +
408 "\", xml_res_file_%i, xml_res_size_%i);\n", i
, i
);
412 for (i
= 0; i
< parFiles
.Count(); i
++)
414 file
.Write(" wxXmlResource::Get()->Load(\"memory:XRC_resource/" +
415 GetInternalFileName(parFiles
[i
], flist
) + "\");\n");
425 void XmlResApp::OutputGettext()
427 wxArrayString str
= FindStrings();
430 if (!parOutput
) fout
.Attach(stdout
);
431 else fout
.Open(parOutput
, _T("wt"));
433 for (size_t i
= 0; i
< str
.GetCount(); i
++)
434 fout
.Write(_T("_(\"") + str
[i
] + _T("\")\n"));
436 if (!parOutput
) fout
.Detach();
441 wxArrayString
XmlResApp::FindStrings()
443 wxArrayString arr
, a2
;
445 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
448 wxPrintf("processing " + parFiles
[i
] + "...\n");
451 if (!doc
.Load(parFiles
[i
]))
453 wxLogError("Error parsing file " + parFiles
[i
]);
457 a2
= FindStrings(doc
.GetRoot());
458 WX_APPEND_ARRAY(arr
, a2
);
466 static wxString
ConvertText(const wxString
& str
)
471 for (dt
= str
.c_str(); *dt
; dt
++)
475 if ( *(++dt
) == wxT('_') )
478 str2
<< wxT('&') << *dt
;
484 case wxT('\n') : str2
<< wxT("\\n"); break;
485 case wxT('\t') : str2
<< wxT("\\t"); break;
486 case wxT('\r') : str2
<< wxT("\\r"); break;
487 default : str2
<< *dt
; break;
496 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
501 if (n
== NULL
) return arr
;
502 n
= n
->GetChildren();
506 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
507 // parent is an element, i.e. has subnodes...
508 (n
->GetType() == wxXML_TEXT_NODE
||
509 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
510 // ...it is textnode...
512 node
/*not n!*/->GetName() == _T("label") ||
513 (node
/*not n!*/->GetName() == _T("value") &&
514 !n
->GetContent().IsNumber()) ||
515 node
/*not n!*/->GetName() == _T("help") ||
516 node
/*not n!*/->GetName() == _T("longhelp") ||
517 node
/*not n!*/->GetName() == _T("tooltip") ||
518 node
/*not n!*/->GetName() == _T("htmlcode") ||
519 node
/*not n!*/->GetName() == _T("title")
521 // ...and known to contain translatable string
523 arr
.Add(ConvertText(n
->GetContent()));
527 if (n
->GetType() == wxXML_ELEMENT_NODE
)
529 wxArrayString a2
= FindStrings(n
);
530 WX_APPEND_ARRAY(arr
, a2
);