-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_version.c
176 lines (157 loc) · 4.96 KB
/
data_version.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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "data.h"
#include "util.h"
typedef struct sem_ver {
unsigned major;
unsigned minor;
unsigned patch;
} sem_ver_t;
Data *NewVersionDataOfVersion(sem_ver_t *version);
bool parse_version(const char *vs, sem_ver_t *version);
/* Release objects created in `NewVersionData`*/
void CleanVersionData(Data *self) {
free(self->raw.genericVal);
self->raw.genericVal = NULL;
free(self->intKey);
self->intKey = NULL;
}
int version_cmp(sem_ver_t *lhsVer, sem_ver_t *rhsVer) {
if (lhsVer->major != rhsVer->major) {
return lhsVer->major - rhsVer->major;
}
if (lhsVer->minor != rhsVer->minor) {
return lhsVer->minor - rhsVer->minor;
}
if (lhsVer->patch != rhsVer->patch) {
return lhsVer->patch - rhsVer->patch;
}
return 0;
}
int VersionDataCompareTo(Data *self, Data *rhs) {
assert(self != NULL);
assert(rhs != NULL);
assert(self->rawType == RawDataCustom);
assert(rhs->rawType == RawDataCustom || rhs->rawType == RawDataString);
int cmp = 0;
sem_ver_t *lhsVer = (sem_ver_t *)self->raw.genericVal;
sem_ver_t *rhsVer = NULL;
if (rhs->rawType == RawDataString) {
rhsVer = (sem_ver_t *)calloc(1, sizeof(sem_ver_t));
bool ok = parse_version(rhs->getCStr(rhs), rhsVer);
if (!ok) {
LURE_ERROR("Unable to parse version: <%s>", rhs->getCStr(rhs));
}
cmp = version_cmp(lhsVer, rhsVer);
free(rhsVer);
} else if (rhs->rawType == RawDataCustom && str_equal_ignore_case(rhs->extKey, "semver")) {
rhsVer = (sem_ver_t *)rhs->raw.genericVal;
cmp = version_cmp(lhsVer, rhsVer);
}
return cmp;
}
/* false only verion is 0.0.0 */
bool VersionDataToBool(Data *self) {
assert(self != NULL);
sem_ver_t *ver = (sem_ver_t *)self->raw.genericVal;
return ver->major != 0 || ver->minor != 0 || ver->patch != 0;
}
/* return major value. */
int VersionDataToInt(Data *self) {
assert(self != NULL);
sem_ver_t *ver = (sem_ver_t *)self->raw.genericVal;
return ver->major;
}
double VersionDataToDouble(Data *self) {
assert(self != NULL);
return (double)self->raw.intVal;
}
char *VersionDataToString(Data *self) {
assert(self != NULL);
assert(self->raw.genericVal != NULL);
sem_ver_t *version = (sem_ver_t *)self->raw.genericVal;
char * num = (char *)calloc(64, sizeof(char));
snprintf(num, 63, "v%d.%d.%d", version->major, version->minor, version->patch);
num[63] = '\0';
return num;
}
char *VersionDataGetCstr(Data *self) {
return self->intKey;
}
/* parse string representation of a sematic version to version struct.
* a valid sem version string must start with "v" or "V" and should have
* only three version fields: major, minor and path.
* Examples: v3.2.1, v0.0.0, V123.3456.100001
*
* @return true if parse succeeded. Version values are undefined if parse
* failed.
*/
bool parse_version(const char *vs, sem_ver_t *version) {
assert(version != NULL);
assert(vs != NULL);
size_t ns = strlen(vs);
if (ns < 6) {
return false;
}
if (vs[0] != 'v' && vs[0] != 'V') {
return false;
}
int parsed = 0; // tracks how many int parsed
for(size_t i = 1; i < ns; i++) {
if (!isdigit(vs[i])) {
continue;
}
int v = atoi(&vs[i]);
switch (parsed)
{
case 0:
version->major = v;
break;
case 1:
version->minor = v;
break;
case 2:
version->patch = v;
break;
default:
break;
}
++parsed;
}
return true;
}
Data *VersionDataCopy(Data *self) {
assert(self->rawType == RawDataCustom);
assert(self->raw.genericVal != NULL);
sem_ver_t *version = (sem_ver_t *)calloc(1, sizeof(sem_ver_t));
sem_ver_t *srcVer = (sem_ver_t *)self->raw.genericVal;
version->major = srcVer->major;
version->minor = srcVer->minor;
version->patch = srcVer->patch;
return NewVersionDataOfVersion(version);
}
Data *NewVersionDataOfVersion(sem_ver_t *version) {
Data *ptr = (Data *)calloc(1, sizeof(Data));
ptr->extKey = "semver";
ptr->raw.genericVal = (void *)version;
ptr->rawType = RawDataCustom;
ptr->clean = CleanVersionData;
ptr->compareTo = VersionDataCompareTo;
ptr->toBoolean = VersionDataToBool;
ptr->toDouble = VersionDataToDouble;
ptr->toInt = VersionDataToInt;
ptr->toString = VersionDataToString;
ptr->getCStr = VersionDataGetCstr;
ptr->copy = VersionDataCopy;
ptr->intKey = VersionDataToString(ptr);
return ptr;
}
/* Create a new Data that manages integer and setup function ptrs accordingly. */
Data *NewVersionData(const char *val) {
sem_ver_t *version = (sem_ver_t *)malloc(sizeof(sem_ver_t));
parse_version(val, version);
return NewVersionDataOfVersion(version);
}