29 #define USE_FMEM_WRAPPER 1
33 #define USE_FMEM_WRAPPER 1
37 #define USE_FMEM_WRAPPER 1
40 #ifdef USE_FMEM_WRAPPER
51 FILE *
SCFmemopen(
void *buf,
size_t size,
const char *mode)
53 char temppath[MAX_PATH - 13];
54 if (0 == GetTempPath(
sizeof(temppath), temppath))
57 char filename[MAX_PATH + 1];
58 if (0 == GetTempFileName(temppath,
"SC", 0, filename))
61 FILE *f = fopen(filename,
"wb");
65 fwrite(buf, size, 1, f);
68 return fopen(filename, mode);
73 typedef struct SCFmem_ {
86 static fpos_t SeekFn(
void *handler, fpos_t
offset,
int whence)
89 SCFmem *mem = handler;
93 if (
offset >= 0 && (
size_t)offset <= mem->size) {
98 if (mem->pos + offset <= mem->size)
103 if (mem->size + offset <= mem->size)
104 return pos = mem->size +
offset;
118 static int ReadFn(
void *handler,
char *buf,
int size)
121 SCFmem *mem = handler;
122 size_t available = mem->size - mem->pos;
125 if (size < 0)
return - 1;
127 if ((
size_t)size > available) {
133 while (count < (
size_t)size)
134 buf[count++] = mem->buffer[mem->pos++];
149 static int WriteFn(
void *handler,
const char *buf,
int size)
152 SCFmem *mem = handler;
153 size_t available = mem->size - mem->pos;
155 if (size < 0)
return - 1;
157 if ((
size_t)size > available)
160 while (count < (
size_t)size)
161 mem->buffer[mem->pos++] = buf[count++];
171 static int CloseFn(
void *handler)
184 FILE *
SCFmemopen(
void *buf,
size_t size,
const char *mode)
186 SCFmem *mem = (SCFmem *)
SCCalloc(1,
sizeof(SCFmem));
190 mem->size = size, mem->buffer = buf;
192 return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn);