forked from Irame/embed-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedresource.cpp
More file actions
66 lines (52 loc) · 1.47 KB
/
embedresource.cpp
File metadata and controls
66 lines (52 loc) · 1.47 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
#include <fstream>
#include <Poco/File.h>
#include <Poco/Path.h>
using namespace std;
using Poco::File;
using Poco::Path;
void replace(std::string& str, string toSearch, string toReplace) {
size_t pos;
while ((pos = str.find(toSearch)) != string::npos) {
str.replace(pos, 1, toReplace);
pos++;
}
}
int main(int argc, char** argv)
{
if (argc < 3) {
fprintf(stderr, "USAGE: %s {sym} {rsrc}\n\n"
" Creates {sym}.c from the contents of {rsrc}\n",
argv[0]);
return EXIT_FAILURE;
}
Path dstPath{argv[1]};
File dstFile(dstPath);
Path srcPath{argv[2]};
File srcFile(srcPath);
string sym = srcPath.getFileName();
replace(sym, ".", "_");
replace(sym, "-", "_");
File(dstPath.parent()).createDirectories();
ofstream ofs{dstFile.path()};
ifstream ifs{srcFile.path()};
ofs << "#include <stdlib.h>" << endl;
ofs << "#pragma warning(disable: 4305)" << endl;
ofs << "#pragma warning(disable: 4309)" << endl;
ofs << "extern const char _resource_" << sym << "[] = {" << endl;
size_t lineCount = 0;
while (true)
{
char c;
ifs.get(c);
if (ifs.eof())
break;
ofs << "0x" << hex << (unsigned int)c << ", ";
if (++lineCount == 10) {
ofs << endl;
lineCount = 0;
}
}
ofs << "};" << endl;
ofs << "extern const size_t _resource_" << sym << "_len = sizeof(_resource_" << sym << ");" << endl;
return EXIT_SUCCESS;
}