-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_string.c
81 lines (69 loc) · 2.27 KB
/
data_string.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
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "data.h"
#include "util.h"
/* Release objects created in `NewStringData`*/
void CleanStringData(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
free(self->raw.stringVal);
self->raw.stringVal = NULL;
}
int StringDataCompareTo(Data *self, Data *rhs) {
LURE_ASSERT(self != NULL, "self must not be NULL");
LURE_ASSERT(rhs != NULL, "right hand side must not be NULL");
char *left = self->toString(self);
char *right = rhs->toString(rhs);
int cmp = strcmp(left, right);
free(left);
free(right);
return cmp;
}
/* (boo)aString is false only if aString is literally "false"
* (case insensitive) or it is empty. */
bool StringDataToBool(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
if (str_equal_ignore_case(self->raw.stringVal, "false")) {
return false;
}
if ((self->raw.stringVal == NULL) || strlen(self->raw.stringVal) == 0) {
return false;
}
return true;
}
int StringDataToInt(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
return atoi(self->getCStr(self));
}
double StringDataToDouble(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
return (double)self->raw.intVal;
}
char *StringDataToString(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
return str_concat(1, self->raw.stringVal);
}
char *StringDataGetCStr(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
return self->raw.stringVal;
}
Data *StringDataCopy(Data *self) {
LURE_ASSERT(self != NULL, "self must not be NULL");
return NewStringData(self->raw.stringVal);
}
/* Create a new Data that manages string and setup function ptrs accordingly. */
Data *NewStringData(const char *val) {
Data *ptr = (Data *)calloc(1, sizeof(Data));
ptr->rawType = RawDataString;
ptr->raw.stringVal = str_concat(1, val);
ptr->clean = CleanStringData;
ptr->compareTo = StringDataCompareTo;
ptr->toBoolean = StringDataToBool;
ptr->toDouble = StringDataToDouble;
ptr->toInt = StringDataToInt;
ptr->toString = StringDataToString;
ptr->getCStr = StringDataGetCStr;
ptr->copy = StringDataCopy;
ptr->intKey = NULL;
return ptr;
}