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 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
105 switch (parser
.Parse())
138 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
140 flagGettext
= cmdline
.Found("g");
141 flagVerbose
= cmdline
.Found("v");
142 flagCPP
= cmdline
.Found("c");
144 if (!cmdline
.Found("o", &parOutput
))
147 parOutput
= wxEmptyString
;
149 parOutput
= flagCPP
? "resource.cpp" : "resource.xrs";
151 parOutputPath
= wxPathOnly(parOutput
);
152 if (!parOutputPath
) parOutputPath
= ".";
154 if (!cmdline
.Found("n", &parFuncname
))
155 parFuncname
= "InitXmlResource";
157 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
158 parFiles
.Add(cmdline
.GetParam(i
));
164 void XmlResApp::CompileRes()
166 wxArrayString files
= PrepareTempFiles();
168 wxRemoveFile(parOutput
);
173 MakePackageCPP(files
);
175 MakePackageZIP(files
);
178 DeleteTempFiles(files
);
182 wxString
XmlResApp::GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
)
184 wxString name2
= name
;
185 name2
.Replace(":", "_");
186 name2
.Replace("/", "_");
187 name2
.Replace("\\", "_");
188 name2
.Replace("*", "_");
189 name2
.Replace("?", "_");
191 wxString s
= wxFileNameFromPath(parOutput
) + "$" + name2
;
193 if (wxFileExists(s
) && flist
.Index(s
) == wxNOT_FOUND
)
195 for (int i
= 0;; i
++)
197 s
.Printf(wxFileNameFromPath(parOutput
) + "$%03i-" + name2
, i
);
198 if (!wxFileExists(s
) || flist
.Index(s
) != wxNOT_FOUND
)
205 wxArrayString
XmlResApp::PrepareTempFiles()
209 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
212 wxPrintf("processing " + parFiles
[i
] + "...\n");
216 if (!doc
.Load(parFiles
[i
]))
218 wxLogError("Error parsing file " + parFiles
[i
]);
223 wxString name
, ext
, path
;
224 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
226 FindFilesInXML(doc
.GetRoot(), flist
, path
);
228 wxString internalName
= GetInternalFileName(parFiles
[i
], flist
);
230 doc
.Save(parOutputPath
+ "/" + internalName
);
231 flist
.Add(internalName
);
239 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
240 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
243 if (n
== NULL
) return;
244 n
= n
->GetChildren();
248 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
249 // parent is an element, i.e. has subnodes...
250 (n
->GetType() == wxXML_TEXT_NODE
||
251 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
252 // ...it is textnode...
253 ((node
/*not n!*/->GetName() == "bitmap") ||
254 (node
/*not n!*/->GetName() == "url")))
255 // ...and known to contain filename
258 if (wxIsAbsolutePath(n
->GetContent()) || inputPath
== "") fullname
= n
->GetContent();
259 else fullname
= inputPath
+ "/" + n
->GetContent();
262 wxPrintf("adding " + fullname
+ "...\n");
264 wxString filename
= GetInternalFileName(n
->GetContent(), flist
);
265 n
->SetContent(filename
);
269 wxFileInputStream
sin(fullname
);
270 wxFileOutputStream
sout(parOutputPath
+ "/" + filename
);
271 sin
.Read(sout
); // copy the stream
275 if (n
->GetType() == wxXML_ELEMENT_NODE
)
276 FindFilesInXML(n
, flist
, inputPath
);
284 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
286 for (size_t i
= 0; i
< flist
.Count(); i
++)
287 wxRemoveFile(parOutputPath
+ "/" + flist
[i
]);
292 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
296 for (size_t i
= 0; i
< flist
.Count(); i
++)
297 files
+= flist
[i
] + " ";
301 wxPrintf("compressing " + parOutput
+ "...\n");
303 if (wxExecute("zip -9 -j " + wxString(flagVerbose
? "" : "-q ") +
304 parOutput
+ " " + files
, TRUE
) == -1)
306 wxLogError("Unable to execute zip program. Make sure it is in the path.");
307 wxLogError("You can download it at http://www.cdrom.com/pub/infozip/");
316 static wxString
FileToCppArray(wxString filename
, int num
)
321 wxFFile
file(filename
, "rb");
322 size_t lng
= file
.Length();
324 snum
.Printf("%i", num
);
325 output
.Printf("static size_t xml_res_size_" + snum
+ " = %i;\n", lng
);
326 output
+= "static unsigned char xml_res_file_" + snum
+ "[] = {\n";
327 // we cannot use string literals because MSVC is dumb wannabe compiler
328 // with arbitrary limitation to 2048 strings :(
330 unsigned char *buffer
= new unsigned char[lng
];
331 file
.Read(buffer
, lng
);
333 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
335 tmp
.Printf("%i", buffer
[i
]);
336 if (i
!= 0) output
<< ',';
343 linelng
+= tmp
.Length()+1;
354 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
356 wxFFile
file(parOutput
, "wt");
360 wxPrintf("creating C++ source file " + parOutput
+ "...\n");
363 #include <wx/wxprec.h>\n\
365 #ifdef __BORLANDC__\n\
369 #ifndef WX_PRECOMP\n\
370 #include <wx/wx.h>\n\
373 #include <wx/filesys.h>\n\
374 #include <wx/fs_mem.h>\n\
375 #include <wx/xrc/xmlres.h>\n\
376 #include <wx/xrc/xh_all.h>\n\
379 for (i
= 0; i
< flist
.Count(); i
++)
380 file
.Write(FileToCppArray(flist
[i
], i
));
383 void " + parFuncname
+ "()\n\
386 // Check for memory FS. If not present, load the handler:\n\
388 wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\
389 wxFileSystem fsys;\n\
390 wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\
391 wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\
393 else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
397 for (i
= 0; i
< flist
.Count(); i
++)
400 s
.Printf(" wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist
[i
] +
401 "\", xml_res_file_%i, xml_res_size_%i);\n", i
, i
);
405 for (i
= 0; i
< parFiles
.Count(); i
++)
407 file
.Write(" wxXmlResource::Get()->Load(\"memory:XRC_resource/" +
408 GetInternalFileName(parFiles
[i
], flist
) + "\");\n");
418 void XmlResApp::OutputGettext()
420 wxArrayString str
= FindStrings();
423 if (!parOutput
) fout
.Attach(stdout
);
424 else fout
.Open(parOutput
, _T("wt"));
426 for (size_t i
= 0; i
< str
.GetCount(); i
++)
427 fout
.Write(_T("_(\"") + str
[i
] + _T("\")\n"));
429 if (!parOutput
) fout
.Detach();
434 wxArrayString
XmlResApp::FindStrings()
436 wxArrayString arr
, a2
;
438 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
441 wxPrintf("processing " + parFiles
[i
] + "...\n");
444 if (!doc
.Load(parFiles
[i
]))
446 wxLogError("Error parsing file " + parFiles
[i
]);
450 a2
= FindStrings(doc
.GetRoot());
451 WX_APPEND_ARRAY(arr
, a2
);
459 static wxString
ConvertText(const wxString
& str
)
464 for (dt
= str
.c_str(); *dt
; dt
++)
468 if ( *(++dt
) == wxT('_') )
471 str2
<< wxT('&') << *dt
;
477 case wxT('\n') : str2
<< wxT("\\n"); break;
478 case wxT('\t') : str2
<< wxT("\\t"); break;
479 case wxT('\r') : str2
<< wxT("\\r"); break;
480 default : str2
<< *dt
; break;
489 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
494 if (n
== NULL
) return arr
;
495 n
= n
->GetChildren();
499 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
500 // parent is an element, i.e. has subnodes...
501 (n
->GetType() == wxXML_TEXT_NODE
||
502 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
503 // ...it is textnode...
505 node
/*not n!*/->GetName() == _T("label") ||
506 (node
/*not n!*/->GetName() == _T("value") &&
507 !n
->GetContent().IsNumber()) ||
508 node
/*not n!*/->GetName() == _T("help") ||
509 node
/*not n!*/->GetName() == _T("longhelp") ||
510 node
/*not n!*/->GetName() == _T("tooltip") ||
511 node
/*not n!*/->GetName() == _T("htmlcode") ||
512 node
/*not n!*/->GetName() == _T("title")
514 // ...and known to contain translatable string
516 arr
.Add(ConvertText(n
->GetContent()));
520 if (n
->GetType() == wxXML_ELEMENT_NODE
)
522 wxArrayString a2
= FindStrings(n
);
523 WX_APPEND_ARRAY(arr
, a2
);