-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.cpp
More file actions
136 lines (129 loc) · 2.85 KB
/
DB.cpp
File metadata and controls
136 lines (129 loc) · 2.85 KB
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
#include "DB.h"
#ifdef SDSupport
#include <SD.h>
#endif
#ifdef SDSupport
DB::DB(File Load) {
cbufs = (uint)Load.size();
Buf = (char*)malloc(cbufs);
Load.read(Buf, cbufs);
s = cbufs;
Load.close();
}
void DB::Save(File Sa) {
Sa.write(Buf, s);
Sa.close();
}
#endif
DB::DB(int sizes) {
sizeincrements = sizes;
Buf = (char*)malloc(sizeincrements);
cbufs = sizeincrements;
}
// Universal
void DB::ADD(string Key, char* value, uint16_t length) {
while(available()<Key.size()+3+length){
Extend(sizeincrements);
}
if (Key.length() < 1) {
return;
}
char* pkey = CGet(Key);
if (pkey != 0) {
pkey[-1] = 1; // rename(change last letter) existing entry to make place
// for new entry
}
// 2 bytes length
Buf[s] = (char)(length & 0xFF);
Buf[s + 1] = (char)(length >> 8);
s += 2;
// Key Add \0 terminated string
for (int i = 0; Key[i] != '\0'; i++) {
Buf[s] = Key[i];
s++;
}
Buf[s] = '\0';
s++;
// Add Data
for (int i = 0; i < length; i++) {
Buf[s] = value[i];
s++;
}
Keycount++;
}
char* DB::CGet(string KeyW) {
int p = 0;
while (p < s) {
// Get Data Length
int leng = (Buf[p + 1] << 8) | Buf[p]; // Packet length;
// Get Key and Keylength
string Key = ""; // inefficient but lazy
int Keylength = 0;
for (int i = 0; Buf[2 + p + i] != '\0'; i++) {
Key += Buf[2 + p + i];
Keylength++;
}
// calculate datastart
int datastart = p + Keylength + 2;
if (Key == KeyW) {
return (&Buf[datastart]);
}
p += Keylength + 3 + leng;
}
return (0);
}
// stringEntry
void DB::Setstring(string KeyW, string Value) {
char D[Value.size() + 1];
copy(Value.begin(), Value.end(), D);
D[Value.size()] = '\0';
ADD(KeyW, D, Value.size() + 1);
}
string DB::Getstring(string KeyW) {
char* CA = CGet(KeyW);
string ret = "";
for (int i = 1; CA[i] != 0; i++) {
ret += CA[i];
}
return (ret);
}
// IntEntry
void DB::SetInt(string KeyW, int value) {
CONV.INT = value;
ADD(KeyW, &CONV.CHARS[0], 4); // 4 bytes
}
int DB::GetInt(string KeyW) {
char* A = CGet(KeyW);
if (A == 0) {
return ((int)0);
}
for (int i = 0; i < 4; i++) {
CONV.CHARS[i] = A[i + 1];
}
return (CONV.INT);
}
// FloatEntry
void DB::SetFloat(string KeyW, float value) {
CONV.FLOAT = value;
ADD(KeyW, &CONV.CHARS[0], 4); // 4 bytes
}
float DB::GetFloat(string KeyW) {
char* A = CGet(KeyW);
if (A == 0) {
return ((float)0);
}
for (int i = 0; i < 4; i++) {
CONV.CHARS[i] = A[i + 1];
}
return (CONV.FLOAT);
}
uint DB::RamUsage(){
return(s);
}
void DB::shrink(){
// DB temp(100);
}
void DB::Extend(int size) { Buf = (char*)realloc(Buf, cbufs + size);cbufs += size; }
uint DB::available(){
return(cbufs-s);
}