Skip to content

Commit

Permalink
wxrc: FileToCppArray: Byte-array initialization integer literals ar…
Browse files Browse the repository at this point in the history
…e now hex; progress is optionally printed.

16 bytes are output on each line.

Previously, a variable number of bytes used to be output on each line,
depending on the character length of the decimal representation of each bytes.
Indeed, the number of output characters used to be lower;
the increase is around 20%; but the representation is nicer for some people (I hope).

Progress (offset, total size, percentage) is printed (as C++ comments)
if the user so demands (via e.g. `--dump-section-size 256`);
in this case, progress is also printed at the beginning and at the end (of the per-file dump).

Example of output (generated C++ source code):

```
// Temporary file   64h of   65h ( 99.01%) (of size     2723h) 'C:\Adder\Projects\com_github\wxWidgets\Example2.cpp$._Example2.xrc':
const size_t xml_res_size_100 = 10019;
const unsigned char xml_res_file_100[] = {
// Offset        0h of     2723h (   0.00%):
0x3c,0x3f,0x78,0x6d, 0x6c,0x20,0x76,0x65, 0x72,0x73,0x69,0x6f, 0x6e,0x3d,0x22,0x31,
0x2e,0x30,0x22,0x20, 0x65,0x6e,0x63,0x6f, 0x64,0x69,0x6e,0x67, 0x3d,0x22,0x55,0x54,
0x46,0x2d,0x38,0x22, 0x3f,0x3e,0x0a,0x3c, 0x72,0x65,0x73,0x6f, 0x75,0x72,0x63,0x65,
0x20,0x78,0x6d,0x6c, 0x6e,0x73,0x3d,0x22, 0x68,0x74,0x74,0x70, 0x3a,0x2f,0x2f,0x77,
0x77,0x77,0x2e,0x77, 0x78,0x77,0x69,0x64, 0x67,0x65,0x74,0x73, 0x2e,0x6f,0x72,0x67,
0x2f,0x77,0x78,0x78, 0x72,0x63,0x22,0x20, 0x76,0x65,0x72,0x73, 0x69,0x6f,0x6e,0x3d,
0x22,0x32,0x2e,0x35, 0x2e,0x33,0x2e,0x30, 0x22,0x3e,0x0a,0x20, 0x20,0x3c,0x6f,0x62,
0x6a,0x65,0x63,0x74, 0x20,0x63,0x6c,0x61, 0x73,0x73,0x3d,0x22, 0x77,0x78,0x42,0x69,
0x74,0x6d,0x61,0x70, 0x22,0x20,0x6e,0x61, 0x6d,0x65,0x3d,0x22, 0x43,0x61,0x74,0x5f,
0x30,0x30,0x30,0x30, 0x68,0x22,0x3e,0x45, 0x78,0x61,0x6d,0x70, 0x6c,0x65,0x32,0x2e,
0x63,0x70,0x70,0x24, 0x45,0x78,0x61,0x6d, 0x70,0x6c,0x65,0x5f, 0x49,0x6d,0x61,0x67,
0x65,0x73,0x5f,0x45, 0x78,0x61,0x6d,0x70, 0x6c,0x65,0x5f,0x30, 0x30,0x30,0x30,0x68,
0x2e,0x70,0x6e,0x67, 0x3c,0x2f,0x6f,0x62, 0x6a,0x65,0x63,0x74, 0x3e,0x0a,0x20,0x20,
0x3c,0x6f,0x62,0x6a, 0x65,0x63,0x74,0x20, 0x63,0x6c,0x61,0x73, 0x73,0x3d,0x22,0x77,
0x78,0x42,0x69,0x74, 0x6d,0x61,0x70,0x22, 0x20,0x6e,0x61,0x6d, 0x65,0x3d,0x22,0x43,
0x61,0x74,0x5f,0x30, 0x30,0x30,0x31,0x68, 0x22,0x3e,0x45,0x78, 0x61,0x6d,0x70,0x6c,
// Offset      100h of     2723h (   2.56%):
0x65,0x32,0x2e,0x63, 0x70,0x70,0x24,0x45, 0x78,0x61,0x6d,0x70, 0x6c,0x65,0x5f,0x49,
...
// Offset     2700h of     2723h (  99.65%):
0x70,0x6c,0x65,0x5f, 0x30,0x30,0x36,0x33, 0x68,0x2e,0x70,0x6e, 0x67,0x3c,0x2f,0x6f,
0x62,0x6a,0x65,0x63, 0x74,0x3e,0x0a,0x3c, 0x2f,0x72,0x65,0x73, 0x6f,0x75,0x72,0x63,
0x65,0x3e,0x0a
// Offset     2723h of     2723h ( 100.00%).
};
```
  • Loading branch information
DoctorNoobingstoneIPresume authored and DoctorNoobingstoneIPresume committed Apr 10, 2024
1 parent 4f82001 commit 73a99c5
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions utils/wxrc/wxrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "wx/vector.h"

#include <vector>
#include <limits>

class XRCWidgetData
{
Expand Down Expand Up @@ -180,7 +181,7 @@ class XmlResApp : public wxAppConsole
virtual int OnRun() override;

private:
void ParseParams(const wxCmdLineParser& cmdline);
bool ParseParams(const wxCmdLineParser& cmdline);
void CompileRes();
wxArrayString PrepareTempFiles();
void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
Expand All @@ -200,6 +201,7 @@ class XmlResApp : public wxAppConsole
bool flagVerbose, flagCPP, flagPython, flagGettext, flagValidate, flagValidateOnly;
wxString parOutput, parFuncname, parOutputPath, parSchemaFile;
wxArrayString parFiles;
wxUint32 sectionmaxlng{0};
int retCode;

ArrayOfXRCWndClassData aXRCWndClassData;
Expand All @@ -219,6 +221,7 @@ int XmlResApp::OnRun()
{ wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
{ wxCMD_LINE_SWITCH, "e", "extra-cpp-code", "output C++ header file with XRC derived classes" },
{ wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" },
{ wxCMD_LINE_OPTION, "" , "dump-section-size", "print section header every <num> bytes", wxCMD_LINE_VAL_NUMBER },
{ wxCMD_LINE_SWITCH, "p", "python-code", "output wxPython source rather than .rsc file" },
{ wxCMD_LINE_SWITCH, "g", "gettext", "output list of translatable strings (to stdout or file if -o used)" },
{ wxCMD_LINE_OPTION, "n", "function", "C++/Python function name (with -c or -p) [InitXmlResource]" },
Expand Down Expand Up @@ -247,7 +250,11 @@ int XmlResApp::OnRun()

case 0:
retCode = 0;
ParseParams(parser);
if (! ParseParams(parser))
{
wxLogError(wxT("Error parsing command line."));
return 3;
}

if (flagValidate)
{
Expand All @@ -269,7 +276,7 @@ int XmlResApp::OnRun()



void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
bool XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
{
flagGettext = cmdline.Found("g");
flagVerbose = cmdline.Found("v");
Expand Down Expand Up @@ -318,10 +325,24 @@ void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
parFiles.Add(cmdline.GetParam(i));
#endif
}
}


sectionmaxlng = 0;
{
long val;
if (cmdline.Found("dump-section-size", &val))
{
if (val < 0 || static_cast<unsigned long>(val) > static_cast<unsigned long>(std::numeric_limits<wxUint32>::max()))
{
wxString msg; msg.Printf("dump-section-size: Value out of range: '%ld'.", val);
wxLogError(msg);
return false;
}
sectionmaxlng = static_cast<wxUint32>(val);
}
}

return true;
}

void XmlResApp::CompileRes()
{
Expand Down Expand Up @@ -583,7 +604,7 @@ void XmlResApp::MakePackageZIP(const wxArrayString& flist)



static wxString FileToCppArray(wxString filename, unsigned num, unsigned numTotal)
static wxString FileToCppArray(wxString filename, unsigned num, unsigned numTotal, wxUint32 sectionmaxlng)
{
// Some of the `wxString` objects (e.g. `tmp`) are defined at the beginning of the function
// (before the actual use, and with larger scope than strictly needed)
Expand Down Expand Up @@ -647,17 +668,51 @@ static wxString FileToCppArray(wxString filename, unsigned num, unsigned numTota
std::vector<unsigned char> buffer(lng);
file.Read(buffer.data(), lng);

for (wxUint32 i = 0, linelng = 0; i < lng; i++)
bool newline{true};
for (wxUint32 i = 0, linelng = 0, sectionlng = 0; ; )
{
tmp.Printf(wxT("%i"), buffer[i]);
if (i != 0) output << wxT(',');
if (linelng > 70)
// If instructed by the user, we print a section header showing offset and percentage/progress.
if (sectionmaxlng && (! i || i >= lng || sectionlng >= sectionmaxlng))
{
linelng = 0;
output << wxT("\n");
if (linelng) wxFAIL;

tmp.Printf
(
wxT("// Offset %8Xh of %8Xh (%7.2lf%%)%c\n"),
static_cast<unsigned>(i),
static_cast<unsigned>(lng),
lng ? 100.0 * i / lng : 0,
i < lng ? wxT(':') : wxT('.')
);
if(! newline)
output << '\n';
output << tmp;

sectionlng = 0;
}

if (i >= lng)
break;

tmp.Printf(wxT("0x%02x"), buffer[i]);
output << tmp;
linelng += tmp.length()+1;

++i;
++linelng;
++sectionlng;

newline = i == lng || linelng >= 0x10;
if (i < lng)
{
output << wxT(',');
if (! newline && ! (linelng % 4))
output << wxT(' ');
}
if (newline)
{
output << wxT('\n');
linelng = 0;
}
}

output += wxT("};\n\n");
Expand Down Expand Up @@ -697,7 +752,7 @@ void XmlResApp::MakePackageCPP(const wxArrayString& flist)
file.Write("namespace\n{\n");
for (unsigned i = 0, n = flist.GetCount(); i < n; ++i)
file.Write(
FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i, n));
FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i, n, sectionmaxlng));
file.Write("} // End of anonymous namespace.\n\n");

file.Write(""
Expand Down

0 comments on commit 73a99c5

Please sign in to comment.