forked from mirkokiefer/LivelyC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCPipe.c
56 lines (44 loc) · 1.18 KB
/
LCPipe.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
#include "LCPipe.h"
#include "LCUtils.h"
#include "unistd.h"
typedef struct pipeData* pipeDataRef;
void pipeDealloc(LCPipeRef object);
struct pipeData {
FILE* write;
FILE* read;
};
struct LCType pipeType = {
.dealloc = pipeDealloc,
};
LCTypeRef LCTypeMemoryStream = &pipeType;
LCPipeRef LCPipeCreate() {
pipeDataRef stream = malloc(sizeof(struct pipeData));
if (stream) {
int filedes[2];
pipe(filedes);
stream->write = fdopen(filedes[1], "w");
stream->read = fdopen(filedes[0], "r");
return objectCreate(LCTypeMemoryStream, stream);
}
return NULL;
}
FILE* LCPipeWriteFile(LCPipeRef streamObj) {
pipeDataRef data = objectData(streamObj);
return data->write;
}
FILE* LCPipeReadFile(LCPipeRef streamObj) {
pipeDataRef data = objectData(streamObj);
return data->read;
}
size_t LCPipeLength(LCPipeRef streamObj) {
return fileLength(LCPipeReadFile(streamObj));
}
void LCPipeData(LCPipeRef streamObj, LCByte buffer[], size_t length) {
readFromFile(LCPipeReadFile(streamObj), buffer, length);
}
void pipeDealloc(LCPipeRef object) {
pipeDataRef streamData = objectData(object);
fclose(streamData->write);
fclose(streamData->read);
lcFree(streamData);
}