-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
571 lines (483 loc) · 9.48 KB
/
data.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
/* Data objects.
Create intelligent data objects that can handle automatic data conversions.
*/
/*
Tasks needed done:
O Add date types
O add list container type with iterators and selectors
O add function pointer types
O add bulk blob data type
o add getxml and store xml types
*/
#define SET 0
#define GET 1
#define STRING 0
#define INTEGER 1
#define HEX 2
#define REAL 3
#define LONG 4
#define BOOL 5
typedef struct Data * DataObj;
typedef intptr_t(*func_ptr)(DataObj, int, int, char *);
struct Data {
int type;
func_ptr call;
int str_set;
char * str_val;
int int_set;
int int_val;
int hex_set;
char * hex_val;
int real_set;
double real_val;
int long_set;
long long_val;
int bool_set;
char bool_val;
} Data;
char *
Real2Str(double val){
char * ret_val = malloc(13);
if (!ret_val)
return NULL;
sprintf(ret_val, "%f", val);
return ret_val;
}
char *
Int2Hex(int val){
char * ret_val = malloc(10);
if (!ret_val)
return NULL;
sprintf(ret_val, "%x", val);
return ret_val;
}
int
Hex2Int(char * val){
int ret_val = 0;
if (!val)
return 0;
while(val[0] && isxdigit(val[0])){
if (isdigit(val[0]))
ret_val = ret_val * 16 + val[0] - '0';
else
ret_val = ret_val * 16 + val[0] -'A' + 10;
val++;
}
return ret_val;
}
char * Int2Str(int val){
char * ret_val = malloc(10);
if (!ret_val)
return NULL;
sprintf(ret_val, "%d", val);
return ret_val;
}
int Str2Int(char * val){
int ret_val=0;
if (!val)
return 0;
while(val[0] && isspace(val[0])){
val++;
}
while(val[0] && isdigit(val[0])){
ret_val=ret_val*10+val[0]-'0';
val++;
}
return ret_val;
}
char * Long2Str(long val){
char * ret_val = malloc(20);
if (!ret_val)
return NULL;
sprintf(ret_val, "%llu", val);
return ret_val;
}
long Str2Long(char * val){
long ret_val=0;
if (!val)
return 0;
while(val[0] && isspace(val[0])){
val++;
}
while(val[0] && isdigit(val[0])){
ret_val=ret_val*10+val[0]-'0';
val++;
}
return ret_val;
}
char * dup (char * val){
int length;
char * ret_val = NULL;
if (val == NULL)
return NULL;
length = strlen (val);
ret_val = malloc (length+1);
if (!ret_val)
return NULL;
strncpy(ret_val, val, length+1);
return ret_val;
}
void clear (DataObj this){
this->str_set=0;
this->str_val=NULL;
this->int_set=0;
this->int_val=0;
this->hex_set=0;
this->hex_val=NULL;
this->real_set = 0;
this->real_val = 0;
this->long_set = 0;
this->long_val = 0;
}
void clearAll (DataObj this){
if (this->str_val)
free(this->str_val);
if (this->hex_val)
free(this->hex_val);
clear(this);
}
int convert(DataObj this, int type){
switch(this->type){
case STRING:
switch(type){
case INTEGER:
if (!this->int_set) {
this->int_val = Str2Int(this->str_val);
this->int_set=1;
}
return 1;
case HEX:
if (!this->hex_set) {
convert (this, INTEGER);
this->hex_val = Int2Hex(this->int_val);
this->hex_set=1;
}
return 1;
case REAL:
if (!this->real_set) {
convert (this, INTEGER);
this->real_val = this->int_val;
this->real_set=1;
}
return 1;
case LONG:
if (!this->long_set) {
this->long_val = Str2Long(this->str_val);
this->long_set=1;
}
return 1;
default:
break;
}
break;
case INTEGER:
switch(type){
case STRING:
if (!this->str_set) {
this->str_val = Int2Str(this->int_val);
this->str_set=1;
}
return 1;
case HEX:
if (!this->hex_set) {
this->hex_val = Int2Hex(this->int_val);
this->hex_set=1;
}
return 1;
case REAL:
if (!this->real_set) {
this->real_val=this->int_val;
this->real_set=1;
}
return 1;
default:
break;
}
break;
case HEX:
switch(type){
case STRING:
if (!this->str_set) {
this->str_val = dup(this->hex_val);
this->str_set = 1;
}
return 1;
case INTEGER:
if (!this->int_set) {
this->int_val = Hex2Int(this->hex_val);
this->int_set = 1;
}
return 1;
case REAL:
if (!this->real_set) {
convert (this, INTEGER);
this->real_val = this->int_val;
this->real_set=1;
}
return 1;
default:
break;
}
break;
case REAL:
switch(type){
case STRING:
if (!this->str_set) {
this->str_val=Real2Str(this->real_val);
this->str_set=1;
}
return 1;
case INTEGER:
if (!this->int_set) {
this->int_val=this->real_val;
this->int_set=1;
}
return 1;
case HEX:
if (!this->hex_set) {
convert (this, INTEGER);
this->hex_val = Int2Hex(this->int_val);
this->hex_set=1;
}
return 1;
default:
break;
}
break;
case LONG:
switch(type){
case STRING:
if (!this->str_set) {
this->str_val=Long2Str(this->long_val);
this->str_set=1;
}
return 1;
default:
break;
}
break;
default:
;
}
return 0;
}
intptr_t datafunc(DataObj this, int kind, int type, char * val){
if (kind == GET){
switch (type) {
case INTEGER:
if (!this->int_set) {
convert(this, INTEGER);
}
return this->int_val;
case STRING:
if (!this->str_set){
convert(this, STRING);
}
return (intptr_t)this->str_val;
case HEX:
if (!this->hex_set){
convert(this, HEX);
}
return (intptr_t)this->hex_val;
case REAL:
if (!this->real_set){
convert(this, REAL);
}
return (intptr_t)&this->real_val;
case LONG:
if (!this->long_set){
convert(this, LONG);
}
return (intptr_t)this->long_val;
default:
return 0;
}
} else {
clearAll(this);
switch (type){
case INTEGER:
this->int_val=(intptr_t)val;
this->int_set=1;
return 1;
case STRING:
this->str_val = dup(val);
if (!this->str_val)
return 0;
this->str_set = 1;
return 1;
case HEX:
this->hex_val = dup(val);
if (!this->hex_val)
return 0;
this->hex_set = 1;
return 1;
case REAL:
this->real_val = *(double*)val;
this->real_set = 1;
return 1;
case LONG:
this->long_val=(long)val;
this->long_set=1;
default:
return 0;
}
}
return 1;
}
DataObj
NewData(int type){
DataObj ret_val = malloc(sizeof(Data));
if (!ret_val)
return NULL;
clear(ret_val);
ret_val->type = type;
switch (type){
case INTEGER:
ret_val->call= &datafunc;
ret_val->int_set=1;
break;
case STRING:
ret_val->call= &datafunc;
ret_val->str_set=1;
ret_val->str_val = dup("");
break;
case HEX:
ret_val->call= &datafunc;
ret_val->hex_set=1;
ret_val->hex_val = dup("");
break;
case REAL:
ret_val->call= &datafunc;
ret_val->real_set=1;
ret_val->real_val = 0;
break;
case LONG:
ret_val->call= &datafunc;
ret_val->long_set=1;
break;
default:
free(ret_val);
return NULL;
}
return ret_val;
}
char *
GetStr(DataObj this){
if (!this)
return 0;
return (char *)(intptr_t)this->call(this, GET, STRING, NULL);
}
int
SetStr(DataObj this, char * value){
if (!this)
return 0;
return this->call(this, SET, STRING, value);
}
int
SetInt(DataObj this, int value){
if (!this)
return 0;
return this->call(this, SET, INTEGER, (char *)(intptr_t)value);
}
int
GetInt(DataObj this){
if (!this)
return 0;
return this->call(this, GET, INTEGER, NULL);
}
int
SetLong(DataObj this, long value){
if (!this)
return 0;
return this->call(this, SET, LONG, (char *)(intptr_t)value);
}
long
GetLong(DataObj this){
if (!this)
return 0;
return (long)(intptr_t)this->call(this, GET, LONG, NULL);
}
int
SetHex(DataObj this, char * value){
if (!this)
return 0;
return this->call(this, SET, HEX, (char *)(intptr_t)value);
}
char *
GetHex(DataObj this){
if (!this)
return 0;
return (char *)(intptr_t)this->call(this, GET, HEX, NULL);
}
int
SetReal(DataObj this, double value){
if (!this)
return 0;
return this->call(this, SET, REAL, (char *)(intptr_t)&value);
}
double
GetReal(DataObj this){
char * result;
if (!this)
return 0;
result = (char *)(intptr_t)this->call(this, GET, REAL, NULL);
return *(double *)(intptr_t)result ;
}
// TEST
void
DataTest (){
char * str_str;
char * str_int;
char * str_hex;
char * str_real;
int int_str;
int int_int;
int int_hex;
int int_real;
char * hex_str;
char * hex_int;
char * hex_hex;
char * hex_real;
double real_str;
double real_int;
double real_hex;
double real_real;
DataObj str_do = NewData(STRING);
DataObj int_do = NewData(INTEGER);
DataObj hex_do = NewData(HEX);
DataObj real_do = NewData(REAL);
//int i = 0;
SetStr(str_do, " 1000 test me ");
SetInt(int_do, 67676);
SetHex(hex_do, "BEEF");
SetReal(real_do, 12344.56 );
str_str = GetStr(str_do);
int_str = GetInt(str_do);
hex_str = GetHex(str_do);
real_str = GetReal(str_do);
str_int = GetStr(int_do);
int_int = GetInt(int_do);
hex_int = GetHex(int_do);
real_int = GetReal(int_do);
str_hex = GetStr(hex_do);
int_hex = GetInt(hex_do);
hex_hex = GetHex(hex_do);
real_hex = GetReal(hex_do);
str_real = GetStr(real_do);
int_real = GetInt(real_do);
hex_real = GetHex(real_do);
real_real = GetReal(real_do);
printf(" str\t\t\tint\t\t\thex\t\t\treal\n");
printf("str >%s<\t %s \t\t\t %s \t\t\t %s \n", str_str, str_int, str_hex, str_real);
printf("int %d \t\t\t>%d<\t\t\t %d \t\t\t %d \n", int_str, int_int, int_hex, int_real);
printf("hex %s \t\t\t %s \t\t\t>%s<\t\t\t %s \n", hex_str, hex_int, hex_hex, hex_real);
printf("real %e \t\t %e \t\t %e \t\t>%e<\n\n", real_str, real_int, real_hex, real_real);
DataObj long_do = NewData(LONG);
SetLong(long_do, 140224278132965);
printf("\nLong to string check This long %lu to string %s \n\n", GetLong(long_do), GetStr(long_do));
}