-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrp.c
More file actions
265 lines (225 loc) · 4.73 KB
/
Copy pathgrp.c
File metadata and controls
265 lines (225 loc) · 4.73 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
/* SPDX-License-Identifier: MIT */
/**
* @file grp.c
* @author Marc-Alexandre Espiaut <ma.dev@espiaut.fr>
* @date 2026-01-30
* @version 1.1
* @brief Creates a GRP file from a list of files.
*
* @copyright Copyright (c) 2026 Marc-Alexandre Espiaut
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GRP_VERSION "1.1"
enum
{
grp_name_len = 12
};
typedef struct
{
char s[grp_name_len];
} str12;
enum
{
error_fclose = 2,
error_fopen,
error_fseek,
error_ftell,
error_fwrite,
error_name_upper
};
static long
safe_ftell(FILE* stream)
{
long result = 0;
if (!stream)
{
exit(error_ftell);
}
result = ftell(stream);
if (result == -1)
{
perror("ftell");
exit(error_ftell);
}
else
{
return result;
}
}
static int
safe_fseek(FILE* stream, long offset, int whence)
{
int result = 0;
if (!stream)
{
exit(error_fseek);
}
result = fseek(stream, offset, whence);
if (result == -1)
{
perror("fseek");
exit(error_fseek);
}
else
{
return result;
}
}
static void
safe_fclose(FILE* stream)
{
if (!stream)
{
exit(error_fclose);
}
if (fclose(stream) == EOF)
{
perror("fclose");
exit(error_fclose);
}
}
static void
safe_fwrite(const void* ptr, size_t size, size_t n, FILE* stream)
{
if (!ptr || !stream)
{
exit(error_fwrite);
}
if (fwrite(ptr, size, n, stream) != n)
{
perror("fwrite");
safe_fclose(stream);
exit(error_fwrite);
}
}
static FILE*
safe_fopen(const char* path, const char* mode)
{
FILE* fp = NULL;
if (!path || !mode)
{
exit(error_fopen);
}
fp = fopen(path, mode);
if (!fp)
{
perror("fopen");
exit(error_fopen);
}
return fp;
}
static void
abort_if_exists(const char* path)
{
FILE* fp = fopen(path, "rb");
if (fp)
{
safe_fclose(fp);
fprintf(stderr, "ERROR: %s already exists! Quitting!\n", path);
exit(EXIT_FAILURE);
}
}
static void
name_upper(str12* name)
{
size_t i;
if (!name)
{
exit(error_name_upper);
}
for (i = 0; i < grp_name_len && name->s[i] != '\0'; ++i)
{
if (name->s[i] >= 'a' && name->s[i] <= 'z')
{
name->s[i] = (char)(name->s[i] - ('a' - 'A'));
}
}
}
static void
grp_write(const char* out, const char* in[], size_t n)
{
size_t i;
FILE* out_fp = NULL;
fprintf(stdout, "Checking in %s already exists.\n", out);
abort_if_exists(out);
fprintf(stdout, "Creating %s.\n", out);
out_fp = safe_fopen(out, "wb+");
safe_fwrite("KenSilverman", 1, grp_name_len, out_fp);
safe_fwrite(&n, 1, 4, out_fp);
for (i = 0; i < n; ++i)
{
FILE* in_fp = safe_fopen(in[i], "rb");
fprintf(stdout, "Adding %s to list.\n", in[i]);
{
str12 name = {0};
strncpy(name.s, in[i], grp_name_len);
name_upper(&name);
safe_fwrite(name.s, 1, grp_name_len, out_fp);
fprintf(stdout, "File name %s", name.s);
}
{
long in_size = 0L;
safe_fseek(in_fp, 0, SEEK_END);
in_size = safe_ftell(in_fp);
safe_fseek(in_fp, 0, SEEK_SET);
safe_fwrite(&in_size, 1, 4, out_fp);
fprintf(stdout, " of size %ld.\n", in_size);
}
safe_fclose(in_fp);
}
for (i = 0; i < n; ++i)
{
FILE* in_fp = safe_fopen(in[i], "rb");
fprintf(stdout, "Adding %s\n", in[i]);
{
size_t j = 0;
char buf[8192] = {0};
while ((j = fread(buf, 1, sizeof(buf), in_fp)) > 0)
{
safe_fwrite(buf, 1, j, out_fp);
}
}
safe_fclose(in_fp);
}
safe_fclose(out_fp);
return;
}
static void
usage(const char* prgname, const char* prgver)
{
fprintf(stderr, "%s %s : Copyright (c) 2026 Marc-Alexandre Espiaut\n", prgname, prgver);
fprintf(stderr, "\n");
fprintf(stderr, "%s is a tool for making group (.grp) files for Build engine games.\n", prgname);
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [options] [files]\n", prgname);
fprintf(stderr, "\n");
fprintf(stderr, "Process input files and creates a GRP output file.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -h, --help Show this help message and exit\n");
/*
fprintf(stderr, " -v, --verbose Enable verbose output\n");
fprintf(stderr, " -g, --group Group files by extensions\n");
fprintf(stderr, " -l, --list LIST.TXT Add files as listed in LIST.TXT\n");
fprintf(stderr, " -o, --output FILE.GRP Write output to FILE\n");
fprintf(stderr, "\n");
*/
fprintf(stderr, "Examples:\n");
fprintf(stderr, " %s output.grp input.map input.dmo input.art\n", prgname);
exit(EXIT_FAILURE);
}
int
main(int argc, char* argv[])
{
if (argc < 3)
{
usage(argv[0], GRP_VERSION);
}
else
{
grp_write(argv[1], (const char**)(argv + 2), (size_t)(argc - 2));
}
return EXIT_SUCCESS;
}