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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <sstream>
#include <getoptpp.hpp>
std::string usage(const std::string& name, bool brief = false)
{
std::ostringstream output;
output <<
"Usage: " << name << " [options]\n";
if(!brief)
{
output <<
"\n"
"Create resource file from list of input files.\n"
"\n";
}
return output.str();
}
int main(int argc, char *argv[])
{
bool verbose{false};
std::vector<std::string> stripPrefixes;
std::string dirRoot;
std::string outfile;
dg::Options opt;
opt.add("strip-path", required_argument, 's',
"Strip supplied path prefix from resource names.",
[&]()
{
stripPrefixes.push_back(optarg);
return 0;
});
opt.add("dir-root", required_argument, 'd',
"Change to supplied root dir before reading files.",
[&]()
{
dirRoot = optarg;
return 0;
});
opt.add("output", required_argument, 'o',
"Write output to specificed file, defaults to stdout.",
[&]()
{
outfile = optarg;
return 0;
});
opt.add("verbose", no_argument, 'v',
"Print verbose output during processing.",
[&]()
{
verbose = true;
return 0;
});
opt.add("help", no_argument, 'h',
"Print this message and exit.",
[&]()
{
std::cout << usage(argv[0]);
std::cout << "Options:\n";
opt.help();
exit(0);
return 0;
});
if(opt.process(argc, argv) != 0)
{
return 1;
}
FILE* out = stdout;
if(!outfile.empty())
{
out = fopen(outfile.data(), "wb");
if(!out)
{
fprintf(stderr, "Could not write to file '%s' - quitting\n",
outfile.data());
return 1;
}
}
fprintf(out, "/* This file is autogenerated by rcgen. Do not modify! */\n");
fprintf(out, "#include \"resource_data.h\"\n");
fprintf(out, "\n");
fprintf(out, "const rc_data_t rc_data[] =\n");
fprintf(out, "{\n");
if(!dirRoot.empty())
{
if(verbose)
{
fprintf(stderr, "Change to dir: %s\n", dirRoot.data());
}
if(chdir(dirRoot.data()))
{
return 1;
}
}
for(const auto& arg : opt.arguments())
{
std::string resourceName = arg;
for(const auto& stripPrefix : stripPrefixes)
{
if(stripPrefix == resourceName.substr(0, stripPrefix.length()))
{
resourceName = resourceName.substr(stripPrefix.length());
break;
}
}
fprintf(out, " {\n \":%s\", ", resourceName.data());
if(verbose)
{
fprintf(stderr, "Process: %s\n", arg.data());
}
std::string data;
FILE *fp = fopen(arg.data(), "rb");
if(!fp)
{
fprintf(stderr, "Could not read file '%s' - quitting\n", arg.data());
return 1;
}
char buf[32];
while(!feof(fp))
{
std::size_t sz = fread(buf, 1, sizeof(buf), fp);
data.append(buf, sz);
}
fclose(fp);
fprintf(out, "%d,\n \"", (int)data.length());
for(std::size_t j = 0; j < data.length(); ++j)
{
if((j != 0) && (j % 16) == 0)
{
fprintf(out, "\"\n \"");
}
fprintf(out, "\\%o", (unsigned char)data[j]);
}
fprintf(out, "\"\n },\n");
}
fprintf(out, " { \"\", 0, 0 }\n");
fprintf(out, "};\n");
return 0;
}
|