-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.c
202 lines (172 loc) · 5.59 KB
/
value.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
/*
* Copyright (C) 2014 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "value.h"
#include "util.h"
#include "status.h"
#define VALUE_STR_CONVERT_SIZE 100
const char *va_get_value_type(Value value)
{
return va_value_type_string(value.type);
}
const char *va_value_type_string(ValueType value_type)
{
switch (value_type) {
case VAL_TYPE_BOOL:
return "Boolean";
case VAL_TYPE_INT:
return "Integer";
case VAL_TYPE_FLOAT:
return "Float";
case VAL_TYPE_STR:
return "String";
case VAL_TYPE_REGEX:
return "Regex";
case VAL_TYPE_SHELL_COMMAND:
return "Shell Command";
default:
break;
}
assert(!"Invalid value type");
return "";
}
const char *va_multi_value_type_string(ValueType value_types)
{
static char value_type_str[1024] = { 0 };
size_t offset = 0;
if (value_types & VAL_TYPE_BOOL) {
offset += snprintf(value_type_str + offset,
sizeof(value_type_str) - offset,
"%s%s", (offset > 0 ? " or " : ""), "Boolean");
}
if (value_types & VAL_TYPE_INT) {
offset += snprintf(value_type_str + offset,
sizeof(value_type_str) - offset,
"%s%s", (offset > 0 ? " or " : ""), "Integer");
}
if (value_types & VAL_TYPE_FLOAT) {
offset += snprintf(value_type_str + offset,
sizeof(value_type_str) - offset,
"%s%s", (offset > 0 ? " or " : ""), "Float");
}
if (value_types & VAL_TYPE_STR) {
offset += snprintf(value_type_str + offset,
sizeof(value_type_str) - offset,
"%s%s", (offset > 0 ? " or " : ""), "String");
}
if (value_types & VAL_TYPE_REGEX) {
offset += snprintf(value_type_str + offset,
sizeof(value_type_str) - offset,
"%s%s", (offset > 0 ? " or " : ""), "Regex");
}
if (value_types & VAL_TYPE_SHELL_COMMAND) {
offset += snprintf(value_type_str + offset,
sizeof(value_type_str) - offset,
"%s%s", (offset > 0 ? " or " : ""),
"Shell Command");
}
assert(offset > 0);
return value_type_str;
}
Status va_deep_copy_value(Value value, Value *new_val)
{
if (!STR_BASED_VAL(value)) {
*new_val = value;
return STATUS_SUCCESS;
}
const char *curr_val = va_str_val(value);
if (curr_val == NULL) {
*new_val = value;
return STATUS_SUCCESS;
}
char *str_val = strdup(curr_val);
if (str_val == NULL) {
return OUT_OF_MEMORY("Unable to copy value");
}
if (value.type == VAL_TYPE_STR) {
*new_val = STR_VAL(str_val);
} else if (value.type == VAL_TYPE_REGEX) {
*new_val = REGEX_VAL(str_val, RVAL(value).modifiers);
} else if (value.type == VAL_TYPE_SHELL_COMMAND) {
*new_val = CMD_VAL(str_val);
}
return STATUS_SUCCESS;
}
char *va_to_string(Value value)
{
switch (value.type) {
case VAL_TYPE_STR:
return strdup(SVAL(value));
case VAL_TYPE_BOOL:
return strdup(IVAL(value) ? "true" : "false");
case VAL_TYPE_INT:
{
char *num_str = malloc(VALUE_STR_CONVERT_SIZE);
if (num_str == NULL) {
return NULL;
}
snprintf(num_str, VALUE_STR_CONVERT_SIZE, "%ld", IVAL(value));
return num_str;
}
case VAL_TYPE_FLOAT:
{
char *num_str = malloc(VALUE_STR_CONVERT_SIZE);
if (num_str == NULL) {
return NULL;
}
snprintf(num_str, VALUE_STR_CONVERT_SIZE, "%f", FVAL(value));
return num_str;
}
case VAL_TYPE_REGEX:
return strdup(RVAL(value).regex_pattern);
case VAL_TYPE_SHELL_COMMAND:
return strdup(CVAL(value));
default:
assert(!"Invalid value type");
break;
}
return NULL;
}
const char *va_str_val(Value value)
{
if (value.type == VAL_TYPE_STR) {
return SVAL(value);
} else if (value.type == VAL_TYPE_REGEX) {
return RVAL(value).regex_pattern;
} else if (value.type == VAL_TYPE_SHELL_COMMAND) {
return CVAL(value);
}
assert(!"Invalid value type");
return NULL;
}
void va_free_value(Value value)
{
if (!STR_BASED_VAL(value)) {
return;
}
if (value.type == VAL_TYPE_STR) {
free(SVAL(value));
} else if (value.type == VAL_TYPE_REGEX) {
free(RVAL(value).regex_pattern);
} else if (value.type == VAL_TYPE_SHELL_COMMAND) {
free(CVAL(value));
}
}