protot/3rdparty/bx/tools/bin2c/bin2c.cpp

174 lines
3.6 KiB
C++
Raw Normal View History

2016-08-29 22:31:11 +02:00
/*
2018-02-03 17:39:28 +01:00
* Copyright 2011-2018 Branimir Karadzic. All rights reserved.
2016-08-29 22:31:11 +02:00
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include <string>
#include <vector>
#include <bx/commandline.h>
2018-02-03 17:39:28 +01:00
#include <bx/file.h>
2016-08-29 22:31:11 +02:00
#include <bx/string.h>
class Bin2cWriter : public bx::WriterI
{
public:
Bin2cWriter(bx::WriterI* _writer, const char* _name)
: m_writer(_writer)
, m_name(_name)
{
}
virtual ~Bin2cWriter()
{
}
2018-02-03 17:39:28 +01:00
virtual int32_t write(const void* _data, int32_t _size, bx::Error* /*_err*/ = NULL) override
2016-08-29 22:31:11 +02:00
{
const char* data = (const char*)_data;
m_buffer.insert(m_buffer.end(), data, data+_size);
return _size;
}
void finish()
{
#define HEX_DUMP_WIDTH 16
#define HEX_DUMP_SPACE_WIDTH 96
#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
const uint8_t* data = &m_buffer[0];
uint32_t size = (uint32_t)m_buffer.size();
bx::writePrintf(m_writer, "static const uint8_t %s[%d] =\n{\n", m_name.c_str(), size);
if (NULL != data)
{
char hex[HEX_DUMP_SPACE_WIDTH+1];
char ascii[HEX_DUMP_WIDTH+1];
uint32_t hexPos = 0;
uint32_t asciiPos = 0;
for (uint32_t ii = 0; ii < size; ++ii)
{
bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
hexPos += 6;
ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' ? data[asciiPos] : '.';
asciiPos++;
if (HEX_DUMP_WIDTH == asciiPos)
{
ascii[asciiPos] = '\0';
bx::writePrintf(m_writer, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
data += asciiPos;
2018-02-03 17:39:28 +01:00
hexPos = 0;
2016-08-29 22:31:11 +02:00
asciiPos = 0;
}
}
if (0 != asciiPos)
{
ascii[asciiPos] = '\0';
bx::writePrintf(m_writer, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
}
}
bx::writePrintf(m_writer, "};\n");
#undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT
m_buffer.clear();
}
bx::WriterI* m_writer;
std::string m_name;
typedef std::vector<uint8_t> Buffer;
Buffer m_buffer;
};
void help(const char* _error = NULL)
{
2018-02-03 17:39:28 +01:00
bx::WriterI* stdOut = bx::getStdOut();
2016-08-29 22:31:11 +02:00
if (NULL != _error)
{
2018-02-03 17:39:28 +01:00
bx::writePrintf(stdOut, "Error:\n%s\n\n", _error);
2016-08-29 22:31:11 +02:00
}
2018-02-03 17:39:28 +01:00
bx::writePrintf(stdOut
2016-08-29 22:31:11 +02:00
, "bin2c, binary to C\n"
2018-02-03 17:39:28 +01:00
"Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n"
2016-08-29 22:31:11 +02:00
"License: https://github.com/bkaradzic/bx#license-bsd-2-clause\n\n"
);
2018-02-03 17:39:28 +01:00
bx::writePrintf(stdOut
2016-08-29 22:31:11 +02:00
, "Usage: bin2c -f <in> -o <out> -n <name>\n"
"\n"
"Options:\n"
" -f <file path> Input file path.\n"
" -o <file path> Output file path.\n"
" -n <name> Array name.\n"
"\n"
"For additional information, see https://github.com/bkaradzic/bx\n"
);
}
int main(int _argc, const char* _argv[])
{
bx::CommandLine cmdLine(_argc, _argv);
if (cmdLine.hasArg('h', "help") )
{
help();
2018-02-03 17:39:28 +01:00
return bx::kExitFailure;
2016-08-29 22:31:11 +02:00
}
const char* filePath = cmdLine.findOption('f');
if (NULL == filePath)
{
help("Input file name must be specified.");
2018-02-03 17:39:28 +01:00
return bx::kExitFailure;
2016-08-29 22:31:11 +02:00
}
const char* outFilePath = cmdLine.findOption('o');
if (NULL == outFilePath)
{
help("Output file name must be specified.");
2018-02-03 17:39:28 +01:00
return bx::kExitFailure;
2016-08-29 22:31:11 +02:00
}
const char* name = cmdLine.findOption('n');
if (NULL == name)
{
name = "data";
}
void* data = NULL;
2017-04-11 08:16:10 +02:00
uint32_t size = 0;
2016-08-29 22:31:11 +02:00
2018-02-03 17:39:28 +01:00
bx::FileReader fr;
2016-08-29 22:31:11 +02:00
if (bx::open(&fr, filePath) )
{
2017-04-11 08:16:10 +02:00
size = uint32_t(bx::getSize(&fr) );
2018-02-03 17:39:28 +01:00
bx::DefaultAllocator allocator;
data = BX_ALLOC(&allocator, size);
2016-08-29 22:31:11 +02:00
bx::read(&fr, data, size);
2018-02-03 17:39:28 +01:00
bx::FileWriter fw;
2016-08-29 22:31:11 +02:00
if (bx::open(&fw, outFilePath) )
{
Bin2cWriter writer(&fw, name);
bx::write(&writer, data, size);
writer.finish();
bx::close(&fw);
}
2018-02-03 17:39:28 +01:00
BX_FREE(&allocator, data);
2016-08-29 22:31:11 +02:00
}
return 0;
}