-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrongtypes.c
More file actions
490 lines (401 loc) · 11.3 KB
/
strongtypes.c
File metadata and controls
490 lines (401 loc) · 11.3 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* The internal precision can be set via compilation flags.
* The number of digits must be coherent with the power.
* For example, an internal precision for 0.0001 is
*
* -DTYPE_DECIMAL_DIGITS=4 -DTYPE_DECIMAL_POWER=10000
*
* If not set, the default is
* -DTYPE_DECIMAL_DIGITS=3 -DTYPE_DECIMAL_POWER=1000
*
* Version: v1.0.0
* Author: Omar Rampado <omar@ognibit.it>
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // exp10
#endif
#include "strongtypes.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <err.h>
#include <assert.h>
#ifndef TYPE_DECIMAL_DIGITS
#define TYPE_DECIMAL_DIGITS 3
#define TYPE_DECIMAL_POWER 1000
#endif
static const struct TypeConf *config = NULL;
static int configLen = 0;
static
bool validate_type(int type)
{
return (type >= 0) && (type < configLen);
}
static
bool validate_range(const TypeValue tv)
{
return (tv.value >= config[tv.type].rangeMin) &&
(tv.value <= config[tv.type].rangeMax);
}
#ifndef NDEBUG
static
bool validate_value(const TypeValue tv)
{
return validate_type(tv.type) && validate_range(tv);
}
static
bool validate_precision(int precision)
{
return (precision >= 0) && (precision <= TYPE_DECIMAL_DIGITS);
}
#endif
struct TypeConf type_conf_int(type_value_store min, type_value_store max)
{
struct TypeConf c = {.category=INTEGER,
.rangeMin=min,
.rangeMax=max,
.precision=0};
return c;
}
struct TypeConf type_conf_dec(type_decimal min, type_decimal max, int precision)
{
assert(validate_precision(precision));
struct TypeConf c = {.category=DECIMAL,
.rangeMin=min,
.rangeMax=max,
.precision=precision};
return c;
}
struct TypeConf type_conf_nom(int count)
{
assert(count > 0);
struct TypeConf c = {.category=NOMINAL,
.rangeMin=0,
.rangeMax=count,
.precision=0};
return c;
}
type_decimal type_dec(double v)
{
return v * TYPE_DECIMAL_POWER;
}
void type_config(const struct TypeConf *table, int len)
{
/* sanity checks */
for (int i=0; i < len; i++){
assert(table[i].category >= 0);
assert(table[i].category <= DECIMAL);
/* can be equals in the corner case of a NOMINAL type with just
* one possible value */
assert(table[i].rangeMin <= table[i].rangeMax);
assert(validate_precision(table[i].precision));
assert((table[i].category != DECIMAL && table[i].precision == 0) ||
(table[i].category == DECIMAL && table[i].precision > 0));
}
/* set globals */
config = table;
configLen = len;
}/* type_config */
TypeValue type_init(int type)
{
assert(validate_type(type));
TypeValue t = {.type = type, .value = 0};
#ifdef TYPE_TIMESTAMP
t.timestamp = 0;
#endif
return t;
}/* type_init */
int type_type(const TypeValue tv)
{
assert(validate_type(tv.type));
return tv.type;
}/* type_type */
type_value_store type_int(const TypeValue tv)
{
assert(validate_value(tv));
return tv.value;
}/* type_int */
double type_float(const TypeValue tv)
{
assert(validate_value(tv));
return ((double)(tv.value)) / (double)TYPE_DECIMAL_POWER;
}/* type_float */
int type_nom(const TypeValue tv)
{
assert(validate_value(tv));
return tv.value;
}/* type_nom */
TypeResult type_seti(const TypeValue tv, type_value_store v)
{
TypeValue t = {.type = tv.type, .value = v};
#ifdef TYPE_TIMESTAMP
t.timestamp = type_now();
#endif
TypeResult res = {.status = TS_OK, .out = t};
if ((validate_type(tv.type)) &&
(config[tv.type].category != INTEGER)){
res.status = TS_INCOMPATIBLE;
return res;
}
if (!validate_range(t)){
res.status = TS_OUTRANGE;
return res;
}
return res;
}/* type_seti */
TypeResult type_setd(const TypeValue tv, double val)
{
type_value_store v = type_dec(val);
/* enforce precision */
int prec = config[tv.type].precision;
type_value_store cut = exp10((double)(TYPE_DECIMAL_DIGITS - prec));
v = (v / cut) * cut; /* integer operations, remove righmost digits */
TypeValue t = {.type = tv.type, .value = v};
#ifdef TYPE_TIMESTAMP
t.timestamp = type_now();
#endif
TypeResult res = {.status = TS_OK, .out = t};
if ((validate_type(tv.type)) &&
(config[tv.type].category != DECIMAL)){
res.status = TS_INCOMPATIBLE;
return res;
}
if (!validate_range(t)){
res.status = TS_OUTRANGE;
return res;
}
return res;
}/* type_setd */
TypeResult type_setn(const TypeValue tv, int name)
{
TypeValue t = {.type = tv.type, .value = name};
#ifdef TYPE_TIMESTAMP
t.timestamp = type_now();
#endif
TypeResult res = {.status = TS_OK, .out = t};
if ((validate_type(tv.type)) &&
(config[tv.type].category != NOMINAL)){
res.status = TS_INCOMPATIBLE;
return res;
}
if (!validate_range(t)){
res.status = TS_OUTRANGE;
return res;
}
return res;
}/* type_setn */
/* for internal use only, no input validation needed,
* valid for INTEGER and DECIMAL since the last one is represented as long too
*/
static
TypeResult value_sum(const TypeValue a, const TypeValue b)
{
type_value_store va = a.value;
type_value_store vb = b.value;
type_value_store sum = 0;
enum TypeStatus status = TS_OK;
//sum = va + vb;
if (__builtin_add_overflow(va, vb, &sum)){
status = TS_OUTRANGE;
}
TypeValue t = {.type = a.type, .value = sum};
if (!validate_range(t)){
status = TS_OUTRANGE;
}
TypeResult res = {.status = status, .out = t};
return res;
}/* value_sum */
TypeResult type_sum(const TypeValue a, const TypeValue b)
{
assert(validate_value(a));
assert(validate_value(b));
TypeValue t = {.type = a.type, .value = 0};
TypeResult res = {.status = TS_INCOMPATIBLE, .out = t};
if (a.type != b.type){
return res;
}
switch (config[a.type].category){
case NOMINAL:
res.status = TS_INCOMPATIBLE;
break;
case INTEGER: /* fall through */
case DECIMAL:
res = value_sum(a, b);
break;
default:
errx(EXIT_FAILURE, "Invalid category configuration for type: %i", a.type);
break;
}/* switch */
#ifdef TYPE_TIMESTAMP
res.out.timestamp = type_now();
#endif
return res;
} /* type_sum */
/* for internal use only, no input validation needed */
static
TypeResult integer_mul(const TypeValue a, const TypeValue b)
{
type_value_store va = a.value;
type_value_store vb = b.value;
type_value_store mul = 0;
enum TypeStatus status = TS_OK;
//mul = va * vb;
if (__builtin_mul_overflow(va, vb, &mul)){
status = TS_OUTRANGE;
}
TypeValue t = {.type = a.type, .value = mul};
if (!validate_range(t)){
status = TS_OUTRANGE;
}
TypeResult res = {.status = status, .out = t};
return res;
}/* integer_mul */
/* for internal use only, no input validation needed */
static
TypeResult decimal_mul(const TypeValue a, const TypeValue b)
{
type_value_store va = a.value;
type_value_store vb = b.value;
type_value_store mul = 0;
enum TypeStatus status = TS_OK;
//mul = va * vb / POWER;
if (__builtin_mul_overflow(va, vb, &mul)){
status = TS_OUTRANGE;
}
mul = mul / TYPE_DECIMAL_POWER;
TypeValue t = {.type = a.type, .value = mul};
if (!validate_range(t)){
status = TS_OUTRANGE;
}
TypeResult res = {.status = status, .out = t};
return res;
}/* decimal_mul */
TypeResult type_mul(const TypeValue a, const TypeValue b)
{
assert(validate_value(a));
assert(validate_value(b));
TypeValue t = {.type = a.type, .value = 0};
TypeResult res = {.status = TS_INCOMPATIBLE, .out = t};
if (a.type != b.type){
return res;
}
switch (config[a.type].category){
case NOMINAL:
res.status = TS_INCOMPATIBLE;
break;
case INTEGER:
res = integer_mul(a, b);
break;
case DECIMAL:
res = decimal_mul(a, b);
break;
default:
errx(EXIT_FAILURE, "Invalid category configuration for type: %i", a.type);
break;
}/* switch */
#ifdef TYPE_TIMESTAMP
res.out.timestamp = type_now();
#endif
return res;
} /* type_mul */
static
TypeResult decimal_div(const TypeValue a, const TypeValue b)
{
long double va = (long double)a.value;
long double vb = (long double)b.value;
type_value_store div = 0;
enum TypeStatus status = TS_OK;
div = (va / vb) * TYPE_DECIMAL_POWER;
/* enforce precision */
int prec = config[a.type].precision;
type_value_store cut = exp10((double)(TYPE_DECIMAL_DIGITS - prec));
div = (div / cut) * cut; /* integer operations, remove righmost digits */
TypeValue t = {.type = a.type, .value = div};
if (!validate_range(t)){
status = TS_OUTRANGE;
}
TypeResult res = {.status = status, .out = t};
return res;
} /* decimal_div */
TypeResult type_div(const TypeValue a, const TypeValue b)
{
assert(validate_value(a));
assert(validate_value(b));
TypeValue t = {.type = a.type, .value = 0};
TypeResult res = {.status = TS_INCOMPATIBLE, .out = t};
if (a.type != b.type){
return res;
}
/* avoid division by zero */
if (config[a.type].category != NOMINAL && b.value == 0) {
res.status = TS_OUTRANGE;
return res;
}
switch (config[a.type].category){
case NOMINAL:
res.status = TS_INCOMPATIBLE;
break;
case INTEGER:
t.value = a.value / b.value;
res.status = TS_OK;
res.out = t;
break;
case DECIMAL:
res = decimal_div(a, b);
break;
default:
errx(EXIT_FAILURE, "Invalid category configuration for type: %i", a.type);
break;
}/* switch */
#ifdef TYPE_TIMESTAMP
res.out.timestamp = type_now();
#endif
return res;
} /* type_div */
int type_dec_units(const TypeValue tv)
{
assert(config[tv.type].category == DECIMAL);
return tv.value / TYPE_DECIMAL_POWER;
}/* type_dec_unites */
int type_dec_decimals(const TypeValue tv)
{
assert(config[tv.type].category == DECIMAL);
int precision = config[tv.type].precision;
type_value_store power = exp10((double)(TYPE_DECIMAL_DIGITS - precision));
return (tv.value % TYPE_DECIMAL_POWER) / power;
}/* type_dec_decimals */
/* Convert DECIMAL to string */
static
void str_decimals(char *buf, const TypeValue tv)
{
/* format string with decimal precision */
char fmt[10] = {'\0'};
sprintf(fmt, "%%i.%%0%ii", config[tv.type].precision);
snprintf(buf, TYPE_STR_LEN-1, fmt,
type_dec_units(tv),
type_dec_decimals(tv));
}/* str_decimals */
void type_str(char *buf, const TypeValue tv)
{
memset(buf, 0, TYPE_STR_LEN);
switch (config[tv.type].category){
case NOMINAL: /* fall through */
case INTEGER:
snprintf(buf, TYPE_STR_LEN-1, "%lli", tv.value);
break;
case DECIMAL:
str_decimals(buf, tv);
break;
default:
errx(EXIT_FAILURE, "Invalid category configuration for type: %i", tv.type);
break;
}/* switch */
}/* type_str */
#ifdef TYPE_TIMESTAMP
type_millisecs type_get_time(const TypeValue tv)
{
return tv.timestamp;
}/* type_get_time */
#endif