-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileToCSource.cpp
More file actions
66 lines (59 loc) · 1.31 KB
/
FileToCSource.cpp
File metadata and controls
66 lines (59 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Convert file into C/C++ source code byte array
// for embedding binary data directly into
// source code.
//
// by Hydranix
// This source code is hereby released into the
// Public Domain
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdint>
#include <sys/stat.h>
int Usage(char * self = 0);
int main(int argc, char** argv)
{
if(argc != 3)
{
return Usage(argv[0]);
}
std::ifstream infile(argv[1], std::ifstream::binary);
std::ofstream outfile(argv[2]);
struct stat stats = {0};
stat(argv[1], &stats);
int filesize = stats.st_size;
outfile << "char filedata[] = {";
std::vector<uint8_t> buffer(filesize);
if(infile.good() && outfile.good())
{
infile.read(reinterpret_cast<char*>(&buffer[0]), buffer.size());
for(int i=0;i<filesize;++i)
{
outfile << "0x" << std::hex << std::setw(2) << std::setfill('0') <<
+buffer[i];
if(i == filesize-1)
{
outfile << "};";
break;
}
if(i % 16)
{
outfile << ", ";
}
else
{
outfile << ",\n ";
}
}
}
infile.close();
outfile.close();
return 0;
}
int Usage(char * self)
{
std::cout << self << " usage:\n\n" <<
self << " <input file> <output file>" << std::endl;
return 1;
}