-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresattr.cpp
More file actions
313 lines (279 loc) · 7.9 KB
/
resattr.cpp
File metadata and controls
313 lines (279 loc) · 7.9 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// resattr.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Entry.h>
#include <File.h>
#include <fs_attr.h>
#include <Resources.h>
// usage
static const char *kUsage =
"Usage: %s [ <options> ] -o <outFile> [ <inFile> ... ]\n"
"\n"
"Reads resources from zero or more input files and adds them as attributes\n"
"to the specified output file, or (in reverse mode) reads attributes from\n"
"zero or more input files and adds them as resources to the specified output\n"
"file. If not existent the output file is created as an empty file.\n"
"\n"
"Options:\n"
" -h, --help - Print this text.\n"
" -o <outfile> - Specifies the output file.\n"
" -O, --overwrite - Overwrite existing attributes. regardless of whether\n"
" an attribute does already exist, it is always written\n"
" when a respective resource is encountered in an input\n"
" file. The last input file specifying the attribute\n"
" wins. If the options is not given, already existing\n"
" attributes remain untouched. Otherwise the first input\n"
" file specifying an attribute wins.\n"
" -r, --reverse - Reverse mode: Reads attributes from input files and\n"
" writes resources. A unique resource ID is assigned to\n"
" each attribute, so there will be no conflicts if two\n"
" input files feature the same attribute.\n"
;
// command line args
static int sArgc;
static const char *const *sArgv;
// print_usage
void
print_usage(bool error)
{
// get nice program name
const char *programName = (sArgc > 0 ? sArgv[0] : "resattr");
if (const char *lastSlash = strrchr(programName, '/'))
programName = lastSlash + 1;
// print usage
fprintf((error ? stderr : stdout), kUsage, programName);
}
// print_usage_and_exit
static
void
print_usage_and_exit(bool error)
{
print_usage(error);
exit(error ? 1 : 0);
}
// next_arg
static
const char*
next_arg(int argc, const char* const* argv, int& argi, bool dontFail = false)
{
if (argi + 1 >= argc) {
if (dontFail)
return NULL;
print_usage_and_exit(true);
}
return argv[++argi];
}
// write_attributes
static
void
write_attributes(BNode &out, const char *inFileName, BResources &resources,
bool overwrite)
{
// iterate through the resources
type_code type;
int32 id;
const char *name;
size_t size;
for (int resIndex = 0;
resources.GetResourceInfo(resIndex, &type, &id, &name, &size);
resIndex++) {
// if we shall not overwrite attributes, we skip the attribute, if it
// already exists
attr_info attrInfo;
if (!overwrite && out.GetAttrInfo(name, &attrInfo) == B_OK)
continue;
// get the resource
const void *data = resources.LoadResource(type, id, &size);
if (!data && size > 0) {
// should not happen
fprintf(stderr, "Failed to get resource `%s', type: %" B_PRIx32
", id: %" B_PRId32 " from input file `%s'\n", name, type, id,
inFileName);
exit(1);
}
// construct a name, if the resource doesn't have one
char nameBuffer[32];
if (!name) {
sprintf(nameBuffer, "unnamed_%d\n", resIndex);
name = nameBuffer;
}
// write the attribute
ssize_t bytesWritten = out.WriteAttr(name, type, 0LL, data, size);
if (bytesWritten < 0) {
fprintf(stderr, "Failed to write attribute `%s' to output file: "
"%s\n", name, strerror(bytesWritten));
}
}
}
// write_resources
static
void
write_resources(BResources &resources, const char *inFileName, BNode &in,
int32 &resID)
{
// iterate through the attributes
char name[B_ATTR_NAME_LENGTH];
while (in.GetNextAttrName(name) == B_OK) {
// get attribute info
attr_info attrInfo;
status_t error = in.GetAttrInfo(name, &attrInfo);
if (error != B_OK) {
fprintf(stderr, "Failed to get info for attribute `%s' of input "
"file `%s': %s\n", name, inFileName, strerror(error));
exit(1);
}
// read attribute
char *data = new char[attrInfo.size];
ssize_t bytesRead = in.ReadAttr(name, attrInfo.type, 0LL, data,
attrInfo.size);
if (bytesRead < 0) {
fprintf(stderr, "Failed to read attribute `%s' of input "
"file `%s': %s\n", name, inFileName, strerror(bytesRead));
delete[] data;
exit(1);
}
// find unique ID
const char *existingName;
size_t existingSize;
while (resources.GetResourceInfo(attrInfo.type, resID, &existingName,
&existingSize)) {
resID++;
}
// write resource
error = resources.AddResource(attrInfo.type, resID++, data,
attrInfo.size, name);
if (error != B_OK) {
fprintf(stderr, "Failed to write resource `%s' to output "
"file: %s\n", name, strerror(error));
}
delete[] data;
}
}
// resources_to_attributes
static
void
resources_to_attributes(const char *outputFile, const char **inputFiles,
int inputFileCount, bool overwrite)
{
// create output file, if it doesn't exist
if (!BEntry(outputFile).Exists()) {
BFile createdOut;
status_t error = createdOut.SetTo(outputFile,
B_READ_WRITE | B_CREATE_FILE);
if (error != B_OK) {
fprintf(stderr, "Failed to create output file `%s': %s\n",
outputFile, strerror(error));
exit(1);
}
}
// open output file
BNode out;
status_t error = out.SetTo(outputFile);
if (error != B_OK) {
fprintf(stderr, "Failed to open output file `%s': %s\n", outputFile,
strerror(error));
exit(1);
}
// iterate through the input files
for (int i = 0; i < inputFileCount; i++) {
// open input file
BFile in;
error = in.SetTo(inputFiles[i], B_READ_ONLY);
if (error != B_OK) {
fprintf(stderr, "Failed to open input file `%s': %s\n",
inputFiles[i], strerror(error));
exit(1);
}
// open resources
BResources resources;
error = resources.SetTo(&in, false);
if (error != B_OK) {
fprintf(stderr, "Failed to read resources of input file `%s': %s\n",
inputFiles[i], strerror(error));
exit(1);
}
// add the attributes
write_attributes(out, inputFiles[i], resources, overwrite);
}
}
// resources_to_attributes
static
void
attributes_to_resources(const char *outputFile, const char **inputFiles,
int inputFileCount)
{
// open output file
BFile out;
status_t error = out.SetTo(outputFile, B_READ_WRITE | B_CREATE_FILE);
if (error != B_OK) {
fprintf(stderr, "Failed to open output file `%s': %s\n", outputFile,
strerror(error));
exit(1);
}
// init output resources
BResources resources;
error = resources.SetTo(&out, false);
if (error != B_OK) {
fprintf(stderr, "Failed to init resources of output file `%s': %s\n",
outputFile, strerror(error));
exit(1);
}
int32 resID = 0;
// iterate through the input files
for (int i = 0; i < inputFileCount; i++) {
// open input file
BNode in;
error = in.SetTo(inputFiles[i]);
if (error != B_OK) {
fprintf(stderr, "Failed to open input file `%s': %s\n",
inputFiles[i], strerror(error));
exit(1);
}
// add the resources
write_resources(resources, inputFiles[i], in, resID);
}
}
// main
int
main(int argc, const char *const *argv)
{
sArgc = argc;
sArgv = argv;
// parameters
const char *outputFile = NULL;
bool overwrite = false;
bool reverse = false;
const char **inputFiles = new const char*[argc];
int inputFileCount = 0;
// parse arguments
for (int argi = 1; argi < argc; argi++) {
const char *arg = argv[argi];
if (arg[0] == '-') {
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
print_usage_and_exit(false);
} else if (strcmp(arg, "-o") == 0) {
outputFile = next_arg(argc, argv, argi);
} else if (strcmp(arg, "-O") == 0) {
overwrite = true;
} else if (strcmp(arg, "-r") == 0
|| strcmp(arg, "--reverse") == 0) {
reverse = true;
} else {
print_usage_and_exit(true);
}
} else {
inputFiles[inputFileCount++] = arg;
}
}
// check parameters
if (!outputFile)
print_usage_and_exit(true);
if (reverse) {
attributes_to_resources(outputFile, inputFiles, inputFileCount);
} else {
resources_to_attributes(outputFile, inputFiles, inputFileCount,
overwrite);
}
return 0;
}