-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-data.c
More file actions
189 lines (164 loc) · 4.57 KB
/
Copy pathnode-data.c
File metadata and controls
189 lines (164 loc) · 4.57 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// copi143保留所有权利
// ----------------------------------------------------------------------------------------------------
#pragma once
// ----------------------------------------------------------------------------------------------------
#include "basic.c"
#include "node-data.h"
// ----------------------------------------------------------------------------------------------------
// key-value 数据库节点数据操作
// 数据读取部分
#define __kv_value_get__(__type__, __rets_type__) \
__rets_type__ kv_value_get_##__type__(kv_node node, __rets_type__ def) \
{ \
if (node) \
switch (node->type) \
{
#define __kv_value_get_end__ \
} \
; \
return def; \
}
// 复制节点数据
int kv_value_copy_data(kv_node node, void *data, int len)
{
if (!node)
return -1;
len = len < node->len ? len : node->len;
switch (node->type)
{
case kv_type_null:
return 0;
case kv_type_data:
memcpy(data, node->v_data, len);
break;
case kv_type_str:
memcpy(data, node->v_str, len);
break;
default:
memcpy(data, &node->value, len);
break;
};
return len;
}
// 复制节点字符串数据
void kv_value_copy_str(kv_node node, void *str, int len)
{
}
// 获取节点数据
__kv_value_get__(data, void *);
case kv_type_null:
return def;
case kv_type_data:
return node->v_data;
case kv_type_str:
return node->v_str;
default:
return &node->value;
__kv_value_get_end__;
// 获取 整数
__kv_value_get__(int, long long);
case kv_type_int:
return node->v_int;
case kv_type_uint:
return node->v_uint;
case kv_type_float:
return node->v_float;
case kv_type_bool:
return kv_bool(node->v_bool);
case kv_type_str:
long long value = def;
sscanf(node->v_str, "%lld", &value);
return value;
__kv_value_get_end__;
// 获取 无符号整数
__kv_value_get__(uint, unsigned long long);
case kv_type_int:
return node->v_int;
case kv_type_uint:
return node->v_uint;
case kv_type_float:
return node->v_float;
case kv_type_bool:
return kv_bool(node->v_bool);
case kv_type_str:
unsigned long long value = def;
sscanf(node->v_str, "%llu", &value);
return value;
__kv_value_get_end__;
// 获取 浮点数
__kv_value_get__(float, double);
case kv_type_int:
return node->v_int;
case kv_type_uint:
return node->v_uint;
case kv_type_float:
return node->v_float;
case kv_type_bool:
return kv_bool(node->v_bool);
case kv_type_str:
double value = def;
sscanf(node->v_str, "%lf", &value);
return value;
__kv_value_get_end__;
__kv_value_get__(bool, int);
case kv_type_int:
return kv_bool(node->v_int);
case kv_type_uint:
return kv_bool(node->v_uint);
case kv_type_float:
return kv_bool(node->v_float);
case kv_type_bool:
return kv_bool(node->v_bool);
case kv_type_str:
if (strcmp(node->v_str, "true") == 0)
return 1;
else if (strcmp(node->v_str, "false") == 0)
return 0;
else
{
int value = def;
sscanf(node->v_str, "%d", &value);
return kv_bool(value);
}
__kv_value_get_end__;
char *kv_value_get_str(kv_node node, char *def)
{
if (node && node->type == kv_type_str)
return node->v_str;
return def;
}
// 数据写入部分
#define __kv_value_set__(__type__, ...) \
void kv_value_set_##__type__(kv_node node, __VA_ARGS__) \
{ \
if (!node) \
return; \
kv_node_set_null(node); \
node->type = kv_type_##__type__;
#define __kv_value_set_end__ }
__kv_value_set__(data, void *data, int len);
node->v_data = kv_datacpy(data, len);
node->len = len;
__kv_value_set_end__;
__kv_value_set__(int, long long value);
node->v_int = value;
node->len = sizeof(long long);
__kv_value_set_end__;
__kv_value_set__(uint, unsigned long long value);
node->v_uint = value;
node->len = sizeof(unsigned long long);
__kv_value_set_end__;
__kv_value_set__(float, double value);
node->v_float = value;
node->len = sizeof(double);
__kv_value_set_end__;
__kv_value_set__(bool, int value);
node->v_uint = kv_bool(value);
node->len = 1;
__kv_value_set_end__;
__kv_value_set__(str, char *str);
node->v_str = kv_strcpy(str);
node->len = str ? strlen(str) : 0;
__kv_value_set_end__;
__kv_value_set__(null, void *useless_arg);
__kv_value_set_end__;