-
Notifications
You must be signed in to change notification settings - Fork 283
/
memfile.c
206 lines (165 loc) · 3.85 KB
/
memfile.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
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
#include <allegro5/allegro.h>
#include "allegro5/allegro_memfile.h"
typedef struct ALLEGRO_FILE_MEMFILE ALLEGRO_FILE_MEMFILE;
struct ALLEGRO_FILE_MEMFILE {
bool readable;
bool writable;
bool eof;
int64_t size;
int64_t pos;
char *mem;
};
static bool memfile_fclose(ALLEGRO_FILE *fp)
{
al_free(al_get_file_userdata(fp));
return true;
}
static size_t memfile_fread(ALLEGRO_FILE *fp, void *ptr, size_t size)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
size_t n = 0;
if (!mf->readable) {
al_set_errno(EPERM);
return 0;
}
if (mf->size - mf->pos < (int64_t)size) {
/* partial read */
n = mf->size - mf->pos;
mf->eof = true;
}
else {
n = size;
}
memcpy(ptr, mf->mem + mf->pos, n);
mf->pos += n;
return n;
}
static size_t memfile_fwrite(ALLEGRO_FILE *fp, const void *ptr, size_t size)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
size_t n;
if (!mf->writable) {
al_set_errno(EPERM);
return 0;
}
if (mf->size - mf->pos < (int64_t)size) {
/* partial write */
n = mf->size - mf->pos;
mf->eof = true;
}
else {
n = size;
}
memcpy(mf->mem + mf->pos, ptr, n);
mf->pos += n;
return n;
}
static bool memfile_fflush(ALLEGRO_FILE *fp)
{
(void)fp;
return true;
}
static int64_t memfile_ftell(ALLEGRO_FILE *fp)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
return mf->pos;
}
static bool memfile_fseek(ALLEGRO_FILE *fp, int64_t offset,
int whence)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
int64_t pos = mf->pos;
switch (whence) {
case ALLEGRO_SEEK_SET:
pos = offset;
break;
case ALLEGRO_SEEK_CUR:
pos = mf->pos + offset;
break;
case ALLEGRO_SEEK_END:
pos = mf->size + offset;
break;
}
if (pos >= mf->size)
pos = mf->size;
else if (pos < 0)
pos = 0;
mf->pos = pos;
mf->eof = false;
return true;
}
/* doesn't quite match up to stdio here,
an feof after a seek will return false,
even if it seeks past the end of the file */
static bool memfile_feof(ALLEGRO_FILE *fp)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
return mf->eof;
}
static int memfile_ferror(ALLEGRO_FILE *fp)
{
(void)fp;
return 0;
}
static const char *memfile_ferrmsg(ALLEGRO_FILE *fp)
{
(void)fp;
return "";
}
static void memfile_fclearerr(ALLEGRO_FILE *fp)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
mf->eof = false;
}
static off_t memfile_fsize(ALLEGRO_FILE *fp)
{
ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
return mf->size;
}
static struct ALLEGRO_FILE_INTERFACE memfile_vtable = {
NULL, /* open */
memfile_fclose,
memfile_fread,
memfile_fwrite,
memfile_fflush,
memfile_ftell,
memfile_fseek,
memfile_feof,
memfile_ferror,
memfile_ferrmsg,
memfile_fclearerr,
NULL, /* ungetc */
memfile_fsize
};
/* Function: al_open_memfile
*/
ALLEGRO_FILE *al_open_memfile(void *mem, int64_t size, const char *mode)
{
ALLEGRO_FILE *memfile;
ALLEGRO_FILE_MEMFILE *userdata = NULL;
ASSERT(mem);
ASSERT(size > 0);
userdata = al_malloc(sizeof(ALLEGRO_FILE_MEMFILE));
if (!userdata) {
al_set_errno(ENOMEM);
return NULL;
}
memset(userdata, 0, sizeof(*userdata));
userdata->size = size;
userdata->pos = 0;
userdata->mem = mem;
userdata->readable = strchr(mode, 'r') || strchr(mode, 'R');
userdata->writable = strchr(mode, 'w') || strchr(mode, 'W');
memfile = al_create_file_handle(&memfile_vtable, userdata);
if (!memfile) {
al_free(userdata);
}
return memfile;
}
/* Function: al_get_allegro_memfile_version
*/
uint32_t al_get_allegro_memfile_version(void)
{
return ALLEGRO_VERSION_INT;
}
/* vim: set sts=3 sw=3 et: */