-
Notifications
You must be signed in to change notification settings - Fork 1
/
binfile.c
64 lines (56 loc) · 1.32 KB
/
binfile.c
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
#include "binfile.h"
#include <stdlib.h>
void bin_write_1(FILE* fp, unsigned char d) {
if (fwrite(&d, sizeof(d), 1, fp) != 1) {
fprintf(stderr, "error writing to file\n");
exit(-1);
}
}
void bin_write_2(FILE* fp, unsigned short d) {
if (fwrite(&d, sizeof(d), 1, fp) != 1) {
fprintf(stderr, "error writing to file\n");
exit(-1);
}
}
void bin_write_4(FILE* fp, unsigned int d) {
if (fwrite(&d, sizeof(d), 1, fp) != 1) {
fprintf(stderr, "error writing to file\n");
exit(-1);
}
}
void bin_write_tag(FILE* fp, const char* c) {
while (*c) {
bin_write_1(fp, *c);
++c;
}
}
void bin_read_1(FILE* fp, unsigned char* d) {
if (fread(d, sizeof(*d), 1, fp) != 1) {
fprintf(stderr, "error reading from file\n");
exit(-1);
}
}
void bin_read_2(FILE* fp, unsigned short* d) {
if (fread(d, sizeof(*d), 1, fp) != 1) {
fprintf(stderr, "error reading from file\n");
exit(-1);
}
}
void bin_read_4(FILE* fp, unsigned int* d) {
if (fread(d, sizeof(*d), 1, fp) != 1) {
fprintf(stderr, "error reading from file\n");
exit(-1);
}
}
void bin_match_tag(FILE* fp, const char* c) {
const char* corig = c;
unsigned char t;
while (*c) {
bin_read_1(fp, &t);
if ((char)t != *c) {
fprintf(stderr, "error reading %s tag from file\n", corig);
exit(-1);
}
++c;
}
}