-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcValue.c
287 lines (264 loc) · 5.82 KB
/
bcValue.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <bcPrivate.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
BCAPI bcStatus_t bcValueCleanup(BC_VALUE value)
{
if (value == NULL)
{
return BC_INVALID_ARG;
}
--value->refCount;
if (value->refCount == 0)
{
switch (value->type)
{
case BC_INTEGER:
case BC_NUMBER:
case BC_STRING:
free(value);
return BC_OK;
case BC_CODE:
{
bcCode_t* code = (bcCode_t*) value;
bcCodeStreamCleanup(&code->code);
free(code);
}
return BC_OK;
default:
return BC_NOT_IMPLEMENTED;
}
}
return BC_OK;
}
BCAPI BC_VALUE bcValueCopy(const BC_VALUE val)
{
if (val == NULL)
{
return NULL;
}
BC_VALUE result = (BC_VALUE) val;
++result->refCount;
return result;
}
BCAPI BC_VALUE bcValueInteger(int64_t val)
{
bcInteger_t* result = (bcInteger_t*) malloc(sizeof(bcInteger_t));
if (result == NULL)
{
return NULL;
}
result->head.type = BC_INTEGER;
result->head.refCount = 1;
result->data = val;
return &result->head;
}
BCAPI BC_VALUE bcValueNumber(double val)
{
bcNumber_t* result = (bcNumber_t*) malloc(sizeof(bcNumber_t));
if (result == NULL)
{
return NULL;
}
result->head.type = BC_NUMBER;
result->head.refCount = 1;
result->data = val;
return &result->head;
}
BCAPI BC_VALUE bcValueString(const char* str)
{
size_t len = strlen(str) + 1;
bcString_t* result = (bcString_t*) malloc(sizeof(bcString_t) + len);
if (result == NULL)
{
return NULL;
}
result->head.type = BC_STRING;
result->head.refCount = 1;
result->len = len;
memcpy(result->data, str, len);
return &result->head;
}
BCAPI bcStatus_t bcValueAsInteger(const BC_VALUE val, int64_t* oval)
{
if ((val == NULL) || (oval == NULL))
{
return BC_INVALID_ARG;
}
switch (val->type)
{
case BC_INTEGER:
{
const bcInteger_t* ival = (const bcInteger_t*)val;
*oval = ival->data;
return BC_OK;
}
case BC_NUMBER:
{
const bcNumber_t* nval = (const bcNumber_t*)val;
*oval = (int64_t) nval->data;
return BC_OK;
}
case BC_STRING:
{
const bcString_t* sval = (const bcString_t*)val;
errno = 0;
int64_t tmpVal = strtoll(sval->data, NULL, 10);
if (errno != 0)
{
return BC_CANT_CONVERT;
}
*oval = tmpVal;
return BC_OK;
}
default:
return BC_NOT_IMPLEMENTED;
}
}
BCAPI bcStatus_t bcValueAsString(const BC_VALUE val, char** pBuf, size_t bufSize)
{
if ((val == NULL) || (pBuf == NULL))
{
return BC_INVALID_ARG;
}
if (((*pBuf != NULL) && (bufSize == 0)) || ((*pBuf == NULL) && (bufSize != 0)))
{
return BC_INVALID_ARG;
}
if (*pBuf == NULL)
{
switch (val->type)
{
case BC_INTEGER:
{
char* tbuf = NULL;
const bcInteger_t* iVal = (const bcInteger_t*) val;
int printResult = asprintf(&tbuf, "%ld", iVal->data);
if (printResult < 0)
{
return BC_NO_MEMORY;
}
*pBuf = tbuf;
return BC_OK;
}
case BC_NUMBER:
{
char* tbuf = NULL;
const bcNumber_t* dVal = (const bcNumber_t*) val;
int printResult = asprintf(&tbuf, "%g", dVal->data);
if (printResult < 0)
{
return BC_NO_MEMORY;
}
*pBuf = tbuf;
return BC_OK;
}
case BC_STRING:
{
char* tbuf = NULL;
const bcString_t* sVal = (const bcString_t*) val;
int printResult = asprintf(&tbuf, "%s", sVal->data);
if (printResult < 0)
{
return BC_NO_MEMORY;
}
*pBuf = tbuf;
return BC_OK;
}
default:
return BC_NOT_IMPLEMENTED;
}
}
else
{
switch (val->type)
{
case BC_INTEGER:
{
const bcInteger_t* iVal = (const bcInteger_t*) val;
int result = snprintf(*pBuf, bufSize, "%ld", iVal->data);
if ((result < 0) || (bufSize <= (size_t)result))
{
return BC_TOO_SMALL;
}
return BC_OK;
}
case BC_NUMBER:
{
const bcNumber_t* dVal = (const bcNumber_t*) val;
int result = snprintf(*pBuf, bufSize, "%g", dVal->data);
if ((result < 0) || (bufSize <= (size_t)result))
{
return BC_TOO_SMALL;
}
return BC_OK;
}
case BC_STRING:
{
const bcString_t* sVal = (const bcString_t*) val;
int result = snprintf(*pBuf, bufSize, "%s", sVal->data);
if ((result < 0) || (bufSize <= (size_t)result))
{
return BC_TOO_SMALL;
}
return BC_OK;
}
default:
return BC_NOT_IMPLEMENTED;
}
}
// can't reach here
assert(0);
return BC_NOT_IMPLEMENTED;
}
BCAPI bcStatus_t bcValueAsNumber(const BC_VALUE val, double* oval)
{
if ((val == NULL) || (oval == NULL))
{
return BC_INVALID_ARG;
}
switch (val->type)
{
case BC_INTEGER:
{
const bcInteger_t* ival = (const bcInteger_t*)val;
*oval = (double) ival->data;
return BC_OK;
}
case BC_NUMBER:
{
const bcNumber_t* nval = (const bcNumber_t*)val;
*oval = nval->data;
return BC_OK;
}
case BC_STRING:
{
const bcString_t* sval = (const bcString_t*)val;
errno = 0;
double tmpVal = strtod(sval->data, NULL);
if (errno != 0)
{
return BC_CANT_CONVERT;
}
*oval = tmpVal;
return BC_OK;
}
default:
return BC_NOT_IMPLEMENTED;
}
}
BCAPI int bcValuePrint(FILE* stream, const BC_VALUE val)
{
switch (val->type)
{
case BC_INTEGER:
return fprintf(stream, "%ld", ((const bcInteger_t*)val)->data);
case BC_NUMBER:
return fprintf(stream, "%g", ((const bcNumber_t*)val)->data);
case BC_STRING:
return fprintf(stream, "%s", ((const bcString_t*)val)->data);
default:
return fprintf(stream, "%s", "NOT-IMPLEMENTED");
}
}