forked from mirkokiefer/LivelyC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCMemoryStream.c
90 lines (80 loc) · 2.79 KB
/
LCMemoryStream.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
#include "LCMemoryStream.h"
#include "unistd.h"
int memoryStreamWrite(void *cookie, const char data[], int length);
int memoryStreamRead(void *cookie, char *buffer, int length);
int memoryStreamWriteClose(void *cookie);
int memoryStreamReadClose(void *cookie);
struct internalWriteCookie {
void *cookie;
writeStreamFun writeFun;
closeStreamFun closeFun;
};
struct internalReadCookie {
void *cookie;
LCByte *data;
size_t length;
LCInteger position;
bool freeOnClose;
closeStreamFun closeFun;
};
FILE* createMemoryReadStream(void *cookie, LCByte data[], size_t length, bool freeOnClose, closeStreamFun closeFun) {
struct internalReadCookie *internalCookie = malloc(sizeof(struct internalReadCookie));
if (internalCookie) {
internalCookie->cookie = cookie;
internalCookie->data = data;
internalCookie->length = length;
internalCookie->position = 0;
internalCookie->freeOnClose = freeOnClose;
internalCookie->closeFun = closeFun;
return funopen(internalCookie, memoryStreamRead, NULL, NULL, NULL);
} else {
return NULL;
}
}
FILE* createMemoryWriteStream(void *cookie, writeStreamFun writeFun, closeStreamFun closeFun) {
struct internalWriteCookie *internalCookie = malloc(sizeof(struct internalWriteCookie));
if (internalCookie) {
internalCookie->cookie = cookie;
internalCookie->writeFun = writeFun;
internalCookie->closeFun = closeFun;
return funopen(internalCookie, NULL, memoryStreamWrite, NULL, NULL);
} else {
return NULL;
}
}
int memoryStreamWrite(void *cookie, const char data[], int length) {
struct internalWriteCookie *internalCookie = (struct internalWriteCookie*)cookie;
internalCookie->writeFun(internalCookie->cookie, (LCByte*)data, length);
return length;
}
int memoryStreamRead(void *cookie, char *buffer, int length) {
struct internalReadCookie *internalCookie = (struct internalReadCookie*)cookie;
if (internalCookie->position >= internalCookie->length) {
return 0;
}
if (internalCookie->position + length > internalCookie->length) {
length = internalCookie->length - internalCookie->position;
}
memcpy(buffer, &(internalCookie->data[internalCookie->position]), sizeof(char)*length);
internalCookie->position = internalCookie->position + length;
return length;
}
int memoryStreamWriteClose(void *cookie) {
struct internalWriteCookie *internalCookie = (struct internalWriteCookie*)cookie;
if (internalCookie->closeFun) {
internalCookie->closeFun(internalCookie->cookie);
}
lcFree(cookie);
return 0;
}
int memoryStreamReadClose(void *cookie) {
struct internalReadCookie *internalCookie = (struct internalReadCookie*)cookie;
if (internalCookie->freeOnClose) {
lcFree(internalCookie->data);
}
if (internalCookie->closeFun) {
internalCookie->closeFun(cookie);
}
lcFree(cookie);
return 0;
}