-
Notifications
You must be signed in to change notification settings - Fork 3
/
LCMemoryStore.c
59 lines (48 loc) · 1.87 KB
/
LCMemoryStore.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
#include "LCMemoryStore.h"
#include "LCMutableDictionary.h"
#include "LCMemoryStream.h"
#include "LCString.h"
#include "LCMutableData.h"
FILE* memoryStoreWrite(void *cookie, LCTypeRef type, char *key);
void memoryStoreDelete(void *cookie, LCTypeRef type, char *key);
FILE* memoryStoreRead(void *cookie, LCTypeRef type, char *key);
void memoryStoreDealloc(LCObjectRef store);
struct LCType typeMemoryStore = {
.dealloc = memoryStoreDealloc
};
LCTypeRef LCTypeMemoryStore = &typeMemoryStore;
LCMemoryStoreRef LCMemoryStoreCreate() {
return objectCreate(LCTypeMemoryStore, LCMutableDictionaryCreate(NULL, 0));
}
static LCMutableDictionaryRef memoryStoreData(LCMemoryStoreRef store) {
return objectData(store);
}
LCStoreRef LCMemoryStoreStoreObject(LCMemoryStoreRef store) {
return storeCreate(store, memoryStoreWrite, memoryStoreDelete, memoryStoreRead);
}
FILE* memoryStoreWrite(void *cookie, LCTypeRef type, char *key) {
LCMutableDataRef data = LCMutableDataCreate(NULL, 0);
LCStringRef hashObj = LCStringCreate(key);
LCMutableDictionarySetValueForKey(memoryStoreData(cookie), hashObj, data);
objectRelease(data);
objectRelease(hashObj);
return createMemoryWriteStream(data, LCMutableDataAppendAlt, NULL);
}
void memoryStoreDelete(void *cookie, LCTypeRef type, char *key) {
LCStringRef hashObj = LCStringCreate(key);
LCMutableDictionaryDeleteKey(memoryStoreData(cookie), hashObj);
objectRelease(hashObj);
}
FILE* memoryStoreRead(void *cookie, LCTypeRef type, char *key) {
LCStringRef hashObj = LCStringCreate(key);
LCMutableDataRef data = LCMutableDictionaryValueForKey(memoryStoreData(cookie), hashObj);
objectRelease(hashObj);
if (data) {
return createMemoryReadStream(NULL, LCMutableDataDataRef(data), LCMutableDataLength(data), false, NULL);
} else {
return NULL;
}
}
void memoryStoreDealloc(LCObjectRef store) {
objectRelease(objectData(store));
}