-
Notifications
You must be signed in to change notification settings - Fork 7
/
builtin_handler.cpp
191 lines (159 loc) · 4.52 KB
/
builtin_handler.cpp
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
190
191
// int handler implementaion
#include "builtin_handler.h"
#include <stdlib.h>
#define CHECK_PARAM(p, q) if (p) { ERRLOG(q); return PARAM_ERROR; }
ERR_CODE int_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(int) || src_len <= 0),
"param error!");
char *pend = NULL;
*(int *)dest = (int)strtol((char *)src, &pend, 10);
if (((char *)src)[0] != '\0' && *pend == '\0')
{
return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}
}
ERR_CODE float_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(float) || src_len <= 0),
"param error!");
char *pend = NULL;
*(float *)dest = strtof((const char *)src, &pend);
if (((char *)src)[0] != '\0' && *pend == '\0')
{
return RET_RIGHT;
}
else
{
return RET_WRONG;
}
}
ERR_CODE double_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(double) || src_len <= 0),
"param error!");
char *pend = NULL;
*(double *)dest = strtod((char *)src, &pend);
if (((char *)src)[0] != '\0' && *pend == '\0')
{
return RET_RIGHT;
}
else
{
return RET_WRONG;
}
}
ERR_CODE string_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < src_len), "param error!");
memcpy(dest, src, src_len);
return RET_RIGHT;
}
ERR_CODE int_array_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(int) || src_len <= 0),
"param error!");
int *pint = (int *)dest;
char *str1 = NULL;
char *str2 = NULL;
char *pstr = (char *) src;
str1 = strtok_r(pstr, ",", &str2);
while (str1)
{
char *pend = NULL;
*pint = (int)strtol((char *)str1, &pend, 10);
if (((char *)src)[0] != '\0' && *pend == '\0')
{
//return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}
++pint;
if (pint >= (int *)dest + sizeof(int) * dest_len)
{
ERRLOG("buffer not enough");
return RET_WRONG;
}
str1 = strtok_r(NULL, ",", &str2);
}
return RET_RIGHT;
}
ERR_CODE float_array_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(float) || src_len <= 0),
"param error!");
float *pfloat = (float *)dest;
char *str1 = NULL;
char *str2 = NULL;
char *pstr = (char *) src;
str1 = strtok_r(pstr, ",", &str2);
while (str1)
{
char *pend = NULL;
*pfloat = strtof((char *)src, &pend);
if (((char *)src)[0] != '\0' && *pend == '\0')
{
//return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}
++pfloat;
if (pfloat >= (float *)dest + sizeof(float) * dest_len)
{
ERRLOG("buffer not enough[%d/%d]", src_len, dest_len);
return RET_WRONG;
}
str1 = strtok_r(NULL, ",", &str2);
}
return RET_RIGHT;
}
ERR_CODE string_array_handler(void *dest, int dest_len, const void *src, int src_len)
{
//char *pstr = (char *) dest;
return RET_RIGHT;
}
ERR_CODE double_array_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(double) || src_len <= 0),
"param error!");
double *pdouble = (double *)dest;
char *str1 = NULL;
char *str2 = NULL;
char *pstr = (char *) src;
str1 = strtok_r(pstr, ",", &str2);
while (str1)
{
char *pend = NULL;
*pdouble = strtod((char *)src, &pend);
if (((char *)src)[0] != '\0' && *pend == '\0')
{
//return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}
//*pdouble = atof(str1);
++pdouble;
if (pdouble >= (double *)dest + sizeof(double) * dest_len)
{
ERRLOG("buffer not enough");
return RET_WRONG;
}
str1 = strtok_r(NULL, ",", &str2);
}
return RET_RIGHT;
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */