-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
zen_time.c
304 lines (278 loc) · 5.89 KB
/
zen_time.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
/*
* This file is part of zenroom
*
* Copyright (C) 2017-2021 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3.0
*
* 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 Affero General Public License for more details.
*
* Along with this program you should have received a copy of the
* GNU Affero General Public License v3.0
* If not, see http://www.gnu.org/licenses/agpl.txt
*
*/
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <zen_error.h>
#include <lua_functions.h>
#include <amcl.h>
#include <zenroom.h>
#include <zen_octet.h>
#include <zen_memory.h>
#include <zen_time.h>
int _string_from_time(char dest[1024], ztime_t src) {
char *format = "%d";
size_t ubufsz = snprintf(dest, 1024, format, src);
if(ubufsz >= 1024) {
return -1;
}
register int bufsz = (int)ubufsz;
return bufsz;
}
ztime_t *time_new(lua_State *L) {
ztime_t *number = (ztime_t *)lua_newuserdata(L, sizeof(ztime_t));
if(!number) {
zerror(L, "Error allocating a new int in %s", __func__);
return NULL;
}
*number = 0;
luaL_getmetatable(L, "zenroom.time");
lua_setmetatable(L, -2);
return number;
}
static void time_free(lua_State *L, ztime_t *f) {
Z(L);
if(f) {
free(f);
Z->memcount_times--;
}
}
ztime_t* time_arg(lua_State *L, int n) {
Z(L);
ztime_t *result = (ztime_t*)malloc(sizeof(ztime_t));
if(result == NULL) {
return NULL;
}
void *ud = luaL_testudata(L, n, "zenroom.time");
if(ud) {
*result = *(ztime_t*)ud;
Z->memcount_times++;
return result;
}
octet *o = o_arg(L, n);
if(o) {
char *pEnd = NULL;
*result = strtol(o->val, &pEnd, 10);
if(*pEnd) {
free(result);
result = NULL;
}
o_free(L, o);
}
if(result) Z->memcount_times++;
return result;
}
octet *new_octet_from_time(lua_State *L, ztime_t t) {
octet *o;
o = o_alloc(L, sizeof(ztime_t));
// TODO: check endianness
memcpy(o->val, &t, sizeof(ztime_t));
o->len = sizeof(ztime_t);
return o;
}
/***
Create a new float number. If an argument is present,
import it as @{OCTET} and initialise it with its value.
@param[opt] octet value
@return a new float number
@function T.new(octet)
*/
static int newtime(lua_State *L) {
BEGIN();
if(lua_isstring(L, 1)) {
const char* arg = lua_tostring(L, 1);
ztime_t *tm = time_new(L);
if(!tm) {
zerror(L, "Could not create time object");
return 0;
}
char *pEnd;
*tm = strtol(arg, &pEnd, 10);
if(*pEnd) {
zerror(L, "Could not parse unix timestamp %s", arg);
return 0;
}
return 1;
}
// number argument, import
if(lua_isnumber(L, 1)) {
lua_Number number = lua_tonumber(L, 1);
ztime_t *tm = time_new(L);
if(!tm) {
lerror(L, "Could not create time object");
return 0;
}
*tm = (int)number;
END(1);
}
// octet argument, import
char *failed_msg = NULL;
octet *o = o_arg(L, 1);
if(!o) {
failed_msg = "Could not allocate octet";
goto end;
}
char *pEnd = NULL;
ztime_t* tm = time_new(L);
if(!tm) {
failed_msg = "Could not create float number";
goto end;
}
*tm = strtol(o->val, &pEnd, 10);
if(*pEnd) {
failed_msg = "Could not parse float number";
goto end;
}
end:
o_free(L, o);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/*static int is_time(lua_State *L) {
BEGIN();
int result = 0;
if(lua_isnumber(L, 1)) {
result = 1;
} else if(lua_isstring(L, 1)) {
const char* arg = lua_tostring(L, 1);
float *flt = float_new(L);
if(!flt) {
THROW("Could not create float number");
}
char *pEnd;
*flt = strtof(arg, &pEnd);
result = (*pEnd == '\0');
}
lua_pushboolean(L, result);
END(1);
}*/
static int time_to_octet(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *o = NULL;
ztime_t *c = time_arg(L,1);
if(!c) {
failed_msg = "Could not read float input";
goto end;
}
o = new_octet_from_time(L, *c);
if(o == NULL) {
failed_msg = "Could not create octet";
goto end;
}
o_dup(L, o);
end:
time_free(L,c);
o_free(L, o);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
static int time_eq(lua_State *L) {
BEGIN();
ztime_t *a,*b;
a = time_arg(L,1);
b = time_arg(L,2);
if(a && b) {
lua_pushboolean(L, *a == *b);
}
time_free(L,a);
time_free(L,b);
if(!a || !b) {
THROW("Could not allocate float number");
}
END(1);
}
static int time_lt(lua_State *L) {
BEGIN();
ztime_t *a = time_arg(L,1);
ztime_t *b = time_arg(L,2);
if(a && b) {
lua_pushboolean(L, *a < *b);
}
time_free(L,a);
time_free(L,b);
if(!a || !b) {
THROW("Could not allocate time number");
}
END(1);
}
// TODO: could be wrong due to equality
static int time_lte(lua_State *L) {
BEGIN();
ztime_t *a = time_arg(L,1);
ztime_t *b = time_arg(L,2);
if(a && b) {
lua_pushboolean(L, *a <= *b);
}
time_free(L,a);
time_free(L,b);
if(!a || !b) {
THROW("Could not allocate time number");
}
END(1);
}
static int time_to_string(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ztime_t* c = time_arg(L,1);
if(c == NULL) {
failed_msg = "Could not read float";
goto end;
}
char dest[1024];
int bufsz = _string_from_time(dest, *c);
if(bufsz < 0) {
failed_msg = "Output size too big";
goto end;
}
lua_pushstring(L, dest);
end:
time_free(L,c);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
int luaopen_time(lua_State *L) {
(void)L;
const struct luaL_Reg time_class[] = {
{"new", newtime},
{"to_octet", time_to_octet},
//{"is_time", is_time},
{NULL, NULL}
};
const struct luaL_Reg time_methods[] = {
{"octet", time_to_octet},
{"__tostring", time_to_string},
{"__eq", time_eq},
{"__lt", time_lt},
{"__lte", time_lte},
{NULL, NULL}
};
zen_add_class(L, "time", time_class, time_methods);
return 1;
}