21int fileToArray(
const std::string &source,
const std::string &dest,
const std::string &arrName)
23 FILE *fs = fopen(source.c_str(),
"rb");
26 std::cerr <<
"source file not found: <" << source <<
">" << std::endl;
30 std::ofstream fo(dest.c_str());
33 std::cerr <<
"cannot generate file: <" << dest <<
">" << std::endl;
38 std::cout <<
"generating <" << dest <<
">" << std::endl;
40 fo <<
"#ifndef _" << arrName <<
"_H_" << std::endl;
41 fo <<
"#define _" << arrName <<
"_H_" << std::endl;
43 fo <<
"const char " << arrName <<
"[] = {" << std::endl;
45 int is_error = fseek(fs, 0L, SEEK_SET);
52 bytes = fread(buf, 1,
sizeof(buf), fs);
53 assert(!ferror(fs) &&
"file read error");
56 for (
size_t i = 0; i < bytes; i++)
58 fo <<
"0x" << std::hex << static_cast<int>(buf[i]) <<
", ";
62 fo <<
"};" << std::endl;
65 fo <<
"#endif /* _" << arrName <<
"_H_ */" << std::endl;
75 auto pos = path.find_last_of(
'/');
76 if (pos != std::string::npos)
77 path = path.substr(pos + 1);
79 pos = path.find_first_of(
'.');
80 if (pos != std::string::npos)
81 path = path.substr(0, pos);
86int main(
int argc,
char *argv[])
91 std::string OutPutDir = argv[1];
93 for (
int i = 2; i < argc; i++)
95 std::string sourceFullFileName = argv[i];
98 std::string outputFileName = OutPutDir +
"/" + filename +
".generated.h";
100 if (
fileToArray(sourceFullFileName, outputFileName, filename) != 0)