X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/56d2f75071fc2a29ec10abe97c5a908bb35f30f4..922ba68045d84789ab1dfceb4d1aa1b6e2c5e810:/utils/wxrc/wxrc.cpp diff --git a/utils/wxrc/wxrc.cpp b/utils/wxrc/wxrc.cpp index 64a81ea2c2..5741a87450 100644 --- a/utils/wxrc/wxrc.cpp +++ b/utils/wxrc/wxrc.cpp @@ -27,30 +27,47 @@ #endif #include "wx/cmdline.h" -#include "wx/xml/xml.h" +#include "wx/xrc/xml.h" +#include "wx/xrc/xmlio.h" #include "wx/ffile.h" +#include "wx/wfstream.h" + + + +/* #if wxUSE_GUI #error "You must compile the resource compiler with wxBase!" #endif - +*/ class XmlResApp : public wxApp { public: + +#if wxUSE_GUI + bool OnInit(); +#else virtual int OnRun(); +#endif private: void ParseParams(const wxCmdLineParser& cmdline); void CompileRes(); wxArrayString PrepareTempFiles(); + void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath); + void DeleteTempFiles(const wxArrayString& flist); void MakePackageZIP(const wxArrayString& flist); void MakePackageCPP(const wxArrayString& flist); + + void OutputGettext(); + wxArrayString FindStrings(); + wxArrayString FindStrings(wxXmlNode *node); - bool flagVerbose, flagCPP, flagCompress; + bool flagVerbose, flagCPP, flagCompress, flagGettext; wxString parOutput, parFuncname, parOutputPath; wxArrayString parFiles; int retCode; @@ -58,23 +75,37 @@ private: IMPLEMENT_APP(XmlResApp) +#if wxUSE_GUI +bool XmlResApp::OnInit() +#else int XmlResApp::OnRun() +#endif { static const wxCmdLineEntryDesc cmdLineDesc[] = { + { wxCMD_LINE_SWITCH, "h", "help", "show help message" }, { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" }, { wxCMD_LINE_SWITCH, "u", "uncompressed", "do not compress .xml files (C++ only)" }, + { wxCMD_LINE_SWITCH, "g", "gettext", "output .po catalog (to stdout or file if -o used)" }, { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" }, - { wxCMD_LINE_OPTION, "o", "output", "output file [resource.rsc/cpp]" }, - { wxCMD_LINE_OPTION, "h", "handlers", "output list of neccessary handlers to this file" }, + { wxCMD_LINE_OPTION, "o", "output", "output file [resource.xrs/cpp]" }, + { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" }, - { wxCMD_LINE_PARAM, NULL, NULL, "input file", + { wxCMD_LINE_PARAM, NULL, NULL, "input file(s)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, { wxCMD_LINE_NONE } }; +#if wxUSE_GUI + // VS: I need reasonable output to console from wxCmdLineParser + wxLog::SetTimestamp(NULL); + delete wxLog::SetActiveTarget(new wxLogStderr); +#endif + + wxXmlDocument::AddHandler(new wxXmlIOHandlerBinZ); + wxCmdLineParser parser(cmdLineDesc, argc, argv); switch (parser.Parse()) @@ -86,12 +117,23 @@ int XmlResApp::OnRun() case 0: retCode = 0; ParseParams(parser); - CompileRes(); + if (flagGettext) + OutputGettext(); + else + CompileRes(); +#if wxUSE_GUI + return FALSE; +#else return retCode; +#endif break; default: +#if wxUSE_GUI + return FALSE; +#else return 1; +#endif break; } } @@ -101,12 +143,18 @@ int XmlResApp::OnRun() void XmlResApp::ParseParams(const wxCmdLineParser& cmdline) { + flagGettext = cmdline.Found("g"); flagVerbose = cmdline.Found("v"); flagCPP = cmdline.Found("c"); flagCompress = flagCPP && !cmdline.Found("u"); if (!cmdline.Found("o", &parOutput)) - parOutput = flagCPP ? "resource.cpp" : "resource.rsc"; + { + if (flagGettext) + parOutput = wxEmptyString; + else + parOutput = flagCPP ? "resource.cpp" : "resource.xrs"; + } parOutputPath = wxPathOnly(parOutput); if (!parOutputPath) parOutputPath = "."; @@ -126,8 +174,6 @@ void XmlResApp::CompileRes() wxRemoveFile(parOutput); - printf("TODO: include bitmaps, list of handlers\n"); - if (!retCode) { if (flagCPP) @@ -159,11 +205,13 @@ wxArrayString XmlResApp::PrepareTempFiles() continue; } - wxString name, ext; - wxSplitPath(parFiles[i], NULL, &name, &ext); + wxString name, ext, path; + wxSplitPath(parFiles[i], &path, &name, &ext); - doc.Save(parOutputPath + "/" + name + ".xmb", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN); - flist.Add(name + ".xmb"); + FindFilesInXML(doc.GetRoot(), flist, path); + + doc.Save(parOutputPath + "/" + name + ".xrc", flagCompress ? wxXML_IO_BINZ : wxXML_IO_BIN); + flist.Add(name + ".xrc"); } return flist; @@ -171,6 +219,54 @@ wxArrayString XmlResApp::PrepareTempFiles() +// find all files mentioned in structure, e.g. filename +void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath) +{ + wxXmlNode *n = node; + if (n == NULL) return; + n = n->GetChildren(); + + while (n) + { + if ((node->GetType() == wxXML_ELEMENT_NODE) && + // parent is an element, i.e. has subnodes... + (n->GetType() == wxXML_TEXT_NODE || + n->GetType() == wxXML_CDATA_SECTION_NODE) && + // ...it is textnode... + (node/*not n!*/->GetName() == "bitmap")) + // ...and known to contain filename + { + wxString fullname; + wxString filename = n->GetContent(); + if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent(); + else fullname = inputPath + "/" + n->GetContent(); + + filename.Replace("/", "_"); + filename.Replace("\\", "_"); + filename.Replace("*", "_"); + filename.Replace("?", "_"); + n->SetContent(filename); + + if (flagVerbose) + wxPrintf("adding " + filename + "...\n"); + + flist.Add(filename); + + wxFileInputStream sin(fullname); + wxFileOutputStream sout(parOutputPath + "/" + filename); + sin.Read(sout); // copy the stream + } + + // subnodes: + if (n->GetType() == wxXML_ELEMENT_NODE) + FindFilesInXML(n, flist, inputPath); + + n = n->GetNext(); + } +} + + + void XmlResApp::DeleteTempFiles(const wxArrayString& flist) { for (size_t i = 0; i < flist.Count(); i++) @@ -206,29 +302,36 @@ void XmlResApp::MakePackageZIP(const wxArrayString& flist) static wxString FileToCppArray(wxString filename, int num) { wxString output; - wxString snum; wxString tmp; + wxString snum; wxFFile file(filename, "rb"); size_t lng = file.Length(); snum.Printf("%i", num); output.Printf("static size_t xml_res_size_" + snum + " = %i;\n", lng); - output += "static unsigned char xml_res_file_" + snum + "[] = {"; + output += "static unsigned char xml_res_file_" + snum + "[] = {\n"; + // we cannot use string literals because MSVC is dumb wannabe compiler + // with arbitrary limitation to 2048 strings :( unsigned char *buffer = new unsigned char[lng]; file.Read(buffer, lng); - for (size_t i = 0; i < lng; i++) + for (size_t i = 0, linelng = 0; i < lng; i++) { - if (i % 16 == 0) output += "\n"; - tmp.Printf("0x%02X", buffer[i]); - output += tmp; - if (i != lng-1) output += ","; + tmp.Printf("%i", buffer[i]); + if (i != 0) output << ','; + if (linelng > 70) + { + linelng = 0; + output << "\n"; + } + output << tmp; + linelng += tmp.Length()+1; } delete[] buffer; - output += "\n};\n\n"; + output += "};\n\n"; return output; } @@ -255,8 +358,8 @@ void XmlResApp::MakePackageCPP(const wxArrayString& flist) \ #include \"wx/filesys.h\"\n\ #include \"wx/fs_mem.h\"\n\ -#include \"wx/xml/xmlres.h\"\n\ -#include \"wx/xml/xh_all.h\"\n\ +#include \"wx/xrc/xmlres.h\"\n\ +#include \"wx/xrc/xh_all.h\"\n\ \n"); for (i = 0; i < flist.Count(); i++) @@ -270,7 +373,7 @@ void " + parFuncname + "()\n\ {\n\ wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\ wxFileSystem fsys;\n\ - wxFSFile *f = fsys.OpenFile(\"xml_resource/dummy_file\");\n\ + wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\ wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\ if (f) delete f;\n\ else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\ @@ -281,12 +384,104 @@ void " + parFuncname + "()\n\ { wxString s; s.Printf(" wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] + - "\", xml_res_file_%i, xml_res_size_%i);\n" - " wxTheXmlResource->Read(\"xml_resource/" + flist[i] + - "\", wxXML_BINARY);\n", i, i); + "\", xml_res_file_%i, xml_res_size_%i);\n", i, i); file.Write(s); } + + for (i = 0; i < parFiles.Count(); i++) + { + wxString name, ext, path; + wxSplitPath(parFiles[i], &path, &name, &ext); + file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" + + name + ".xrc" + "\");\n"); + } file.Write("\n}\n"); + +} + + + +void XmlResApp::OutputGettext() +{ + wxArrayString str = FindStrings(); + + wxFFile fout; + if (!parOutput) fout.Attach(stdout); + else fout.Open(parOutput, _T("wt")); + + for (size_t i = 0; i < str.GetCount(); i++) + fout.Write(_T("msgid \"") + str[i] + _T("\"\nmsgstr \"\"\n\n")); + + if (!parOutput) fout.Detach(); +} + + + +wxArrayString XmlResApp::FindStrings() +{ + wxArrayString arr, a2; + + for (size_t i = 0; i < parFiles.Count(); i++) + { + if (flagVerbose) + wxPrintf("processing " + parFiles[i] + "...\n"); + + wxXmlDocument doc; + if (!doc.Load(parFiles[i])) + { + wxLogError("Error parsing file " + parFiles[i]); + retCode = 1; + continue; + } + a2 = FindStrings(doc.GetRoot()); + WX_APPEND_ARRAY(arr, a2); + } + + return arr; +} + + + +wxArrayString XmlResApp::FindStrings(wxXmlNode *node) +{ + wxArrayString arr; + + wxXmlNode *n = node; + if (n == NULL) return arr; + n = n->GetChildren(); + + while (n) + { + if ((node->GetType() == wxXML_ELEMENT_NODE) && + // parent is an element, i.e. has subnodes... + (n->GetType() == wxXML_TEXT_NODE || + n->GetType() == wxXML_CDATA_SECTION_NODE) && + // ...it is textnode... + ( + node/*not n!*/->GetName() == _T("label") || + (node/*not n!*/->GetName() == _T("value") && + !n->GetContent().IsNumber()) || + node/*not n!*/->GetName() == _T("help") || + node/*not n!*/->GetName() == _T("longhelp") || + node/*not n!*/->GetName() == _T("tooltip") || + node/*not n!*/->GetName() == _T("htmlcode") || + node/*not n!*/->GetName() == _T("title") + )) + // ...and known to contain filename + { + arr.Add(n->GetContent()); + } + + // subnodes: + if (n->GetType() == wxXML_ELEMENT_NODE) + { + wxArrayString a2 = FindStrings(n); + WX_APPEND_ARRAY(arr, a2); + } + + n = n->GetNext(); + } + return arr; }