forked from piti-diablotin/agate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile2obj.cpp
44 lines (41 loc) · 1.41 KB
/
file2obj.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
for (int arg = 1; arg < argc; ++arg) {
string filename(argv[arg]);
ifstream file(filename,ios::in|ios::binary);
if (!file) {
cerr << filename << " in not readable" << endl;
continue;
}
file.seekg(0,file.end);
int length = file.tellg();
file.seekg(0,file.beg);
clog << filename << " is " << length << " bytes" << endl;
string suffix = filename.substr(0,filename.find('.'));
unsigned char *buffer = new unsigned char[length];
file.read((char*)buffer,length);
file.close();
stringstream output;
output << "const unsigned char file" << suffix << "[] = {" << endl << " ";
output << hex;
for (int c = 0; c < length ; ++c ) {
output << "0x" << setfill('0') << setw(sizeof(char)*2) << (unsigned short)buffer[c] << ", ";
if ( c%16 == 15 && c != length-1) {
output << endl << " ";
}
}
output << dec;
output.seekp(-2,output.end);
output << endl << "};" << endl;
output << "std::ofstream ofile" << suffix << "(\"ref_" << filename << "\",std::ios::out|std::ios::binary);" << endl;
output << "ofile" << suffix << ".write((char*)file" << suffix << "," << length <<");" << endl;
output << "ofile" << suffix << ".close();" << endl;
cout << output.str();
}
return 0;
}