This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathValue.cpp
More file actions
373 lines (297 loc) · 7.44 KB
/
Value.cpp
File metadata and controls
373 lines (297 loc) · 7.44 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
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include "Value.h"
#include "CString.h"
////////////////////////////////////////////////////////////////////////////////
struct Value
{
ValueType type;
uint32_t references;
union
{
bool boolean;
int64_t integer;
double real;
struct
{
char *chars;
uint32_t char_count;
} string;
struct
{
Value *next;
ValueRef string;
} name;
};
};
static Value *s_names = nil;
////////////////////////////////////////////////////////////////////////////////
ValueRef ValueRetain(ValueRef self)
{
if (self == nil)
return nil;
self -> references += 1;
return self;
}
void ValueRelease(ValueRef self)
{
if (self == nil)
return;
self -> references -= 1;
if (self -> references != 0)
return;
switch(self -> type)
{
case kValueTypeEmpty:
case kValueTypeBoolean:
case kValueTypeInteger:
case kValueTypeReal:
break;
case kValueTypeName:
if (s_names == self)
s_names = self -> name . next;
else
{
Value *t_name;
for(t_name = s_names; t_name != nil && t_name -> name . next != self; t_name = t_name -> name . next)
;
if (t_name != nil)
t_name -> name . next = self -> name . next;
}
ValueRelease(self -> name . string);
break;
case kValueTypeString:
MCMemoryDeleteArray(self -> string . chars);
break;
}
MCMemoryDelete(self);
}
// MERG-2013-06-14: [[ ExternalsApiV5 ]] Implement type checking methods.
bool ValueIsEmpty(ValueRef self)
{
if (self == nil)
return false;
return self->type == kValueTypeEmpty;
}
bool ValueIsBoolean(ValueRef self)
{
if (self == nil)
return false;
return self->type == kValueTypeBoolean;
}
bool ValueIsInteger(ValueRef self)
{
if (self == nil)
return false;
return self->type == kValueTypeInteger;
}
bool ValueIsReal(ValueRef self)
{
if (self == nil)
return false;
return self->type == kValueTypeReal;
}
bool ValueIsString(ValueRef self)
{
if (self == nil)
return false;
return self->type == kValueTypeString;
}
bool ValueIsName(ValueRef self)
{
if (self == nil)
return false;
return self->type == kValueTypeName;
}
////////////////////////////////////////////////////////////////////////////////
bool NameCreateWithNativeChars(const char *p_chars, uindex_t p_char_count, ValueRef& r_value)
{
bool t_success;
t_success = true;
for(Value *t_name = s_names; t_name != nil; t_name = t_name -> name . next)
if (StringEqualToChars(t_name -> name . string, p_chars, p_char_count))
{
t_name -> references += 1;
r_value = t_name;
return true;
}
Value *t_string;
t_string = nil;
if (t_success)
t_success = StringCreateWithNativeChars(p_chars, p_char_count, t_string);
Value *self;
self = nil;
if (t_success)
t_success = MCMemoryNew(self);
if (t_success)
{
self -> references = 1;
self -> type = kValueTypeName;
self -> name . string = t_string;
self -> name . next = s_names;
s_names = self;
r_value = self;
}
else
{
MCMemoryDelete(self);
ValueRelease(t_string);
}
return t_success;
}
bool NameEqual(NameRef p_left_name, NameRef p_right_name)
{
return p_left_name == p_right_name;
}
bool NameEqualCaseless(NameRef p_left_name, NameRef p_right_name)
{
return p_left_name == p_right_name || MCCStringEqualCaseless(StringGetCStringPtr(p_left_name -> name . string), StringGetCStringPtr(p_right_name -> name . string));
}
bool NameEqualToCString(NameRef self, const char *p_cstring)
{
return MCCStringEqual(StringGetCStringPtr(self -> name . string), p_cstring);
}
StringRef NameGetString(StringRef self)
{
return self -> name . string;
}
const char *NameGetCString(NameRef self)
{
return StringGetCStringPtr(self -> name . string);
}
////////////////////////////////////////////////////////////////////////////////
bool StringCreateWithNativeChars(const char *p_chars, uindex_t p_char_count, ValueRef& r_value)
{
bool t_success;
t_success = true;
Value *self;
self = nil;
if (t_success)
t_success = MCMemoryNew(self);
char *t_chars;
t_chars = nil;
if (t_success)
t_success = MCMemoryNewArray(p_char_count + 1, t_chars);
if (t_success)
{
MCMemoryCopy(t_chars, p_chars, p_char_count);
// MW-2012-09-04: [[ Bug ]] NUL terminator should be at char_count, not char_count+1.
t_chars[p_char_count] = '\0';
self -> references = 1;
self -> type = kValueTypeString;
self -> string . chars = t_chars;
self -> string . char_count = p_char_count;
r_value = self;
}
else
{
MCMemoryDeleteArray(t_chars);
MCMemoryDelete(self);
}
return t_success;
}
bool StringEqualCaseless(StringRef left, StringRef right)
{
if (left -> string . char_count != right -> string . char_count)
return false;
return MCCStringEqualCaseless(left -> string . chars, right -> string . chars);
}
bool StringEqualToChars(StringRef self, const char *p_chars, uindex_t p_char_count)
{
if (self -> string . char_count != p_char_count)
return false;
return MCCStringEqualSubstring(self -> string . chars, p_chars, p_char_count);
}
const char *StringGetCStringPtr(StringRef self)
{
return self -> string . chars;
}
////////////////////////////////////////////////////////////////////////////////
bool NumberCreateWithNativeChars(const char *p_chars, uindex_t p_char_count, ValueRef& r_value)
{
bool t_success;
t_success = true;
char *t_cstring;
t_cstring = nil;
if (t_success)
t_success = MCCStringCloneSubstring(p_chars, p_char_count, t_cstring);
ValueRef self;
self = nil;
if (t_success)
t_success = MCMemoryNew(self);
if (t_success)
{
self -> references = 1;
char *t_end;
#ifdef WIN32
self -> integer = _strtoui64(t_cstring, &t_end, 10);
#else
self -> integer = strtoull(t_cstring, &t_end, 10);
#endif
if (*t_end == '\0')
self -> type = kValueTypeInteger;
else
{
self -> type = kValueTypeReal;
self -> real = strtod(t_cstring, &t_end);
}
}
if (t_success)
r_value = self;
else
MCMemoryDelete(self);
MCCStringFree(t_cstring);
return t_success;
}
bool NumberIsInteger(NumberRef self)
{
return self -> type == kValueTypeInteger;
}
int64_t NumberGetInteger(NumberRef self)
{
return self -> integer;
}
double NumberGetReal(NumberRef self)
{
if (self -> type == kValueTypeInteger)
return (double)self -> integer;
return self -> real;
}
////////////////////////////////////////////////////////////////////////////////
// MERG-2013-06-14: [[ ExternalsApiV5 ]] Implement boolean ValueRef methods.
bool BooleanCreateWithBool(bool value, ValueRef& r_value)
{
bool t_success;
t_success = true;
Value *self;
self = nil;
if (t_success)
t_success = MCMemoryNew(self);
if (t_success)
{
self -> references = 1;
self -> type = kValueTypeBoolean;
self -> boolean = value;
r_value = self;
}
else
{
MCMemoryDelete(self);
}
return t_success;
}
bool BooleanGetBool(ValueRef self)
{
return self -> boolean;
}
////////////////////////////////////////////////////////////////////////////////