diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-01-22 15:46:55 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-01-22 15:46:55 -0200 |
commit | be48c4d91e1d5134c016ef50cec902c49d1a3d21 (patch) | |
tree | 45bd1939be0f99cb065399baeeff061b7ee1aa7e /iolib.c | |
parent | a19f9056f3194332b22fe9ae96cd7f24d39c7d82 (diff) | |
download | lua-be48c4d91e1d5134c016ef50cec902c49d1a3d21.tar.gz lua-be48c4d91e1d5134c016ef50cec902c49d1a3d21.tar.bz2 lua-be48c4d91e1d5134c016ef50cec902c49d1a3d21.zip |
add_char now is global.
time returns week-day.
Diffstat (limited to 'iolib.c')
-rw-r--r-- | iolib.c | 76 |
1 files changed, 36 insertions, 40 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** Input/output library to LUA | 3 | ** Input/output library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_iolib="$Id: iolib.c,v 1.29 1995/11/10 18:32:59 roberto Exp roberto $"; | 6 | char *rcs_iolib="$Id: iolib.c,v 1.31 1996/01/22 17:38:57 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <ctype.h> | 9 | #include <ctype.h> |
@@ -177,32 +177,6 @@ static char getformat (char *f, int *just, int *m, int *n) | |||
177 | } | 177 | } |
178 | 178 | ||
179 | 179 | ||
180 | static char *add_char (int c) | ||
181 | { | ||
182 | static char *buff = NULL; | ||
183 | static int max = 0; | ||
184 | static int n = 0; | ||
185 | if (n >= max) | ||
186 | { | ||
187 | if (max == 0) | ||
188 | { | ||
189 | max = 100; | ||
190 | buff = (char *)malloc(max); | ||
191 | } | ||
192 | else | ||
193 | { | ||
194 | max *= 2; | ||
195 | buff = (char *)realloc(buff, max); | ||
196 | } | ||
197 | if (buff == NULL) | ||
198 | lua_error("memory overflow"); | ||
199 | } | ||
200 | buff[n++] = c; | ||
201 | if (c == 0) | ||
202 | n = 0; /* prepare for next string */ | ||
203 | return buff; | ||
204 | } | ||
205 | |||
206 | /* | 180 | /* |
207 | ** Read a variable. On error put nil on stack. | 181 | ** Read a variable. On error put nil on stack. |
208 | ** LUA interface: | 182 | ** LUA interface: |
@@ -222,7 +196,7 @@ static int read_until_char (int del) | |||
222 | { | 196 | { |
223 | int c; | 197 | int c; |
224 | while((c = fgetc(in)) != EOF && c != del) | 198 | while((c = fgetc(in)) != EOF && c != del) |
225 | add_char(c); | 199 | luaI_addchar(c); |
226 | return c; | 200 | return c; |
227 | } | 201 | } |
228 | 202 | ||
@@ -230,7 +204,7 @@ static int read_until_blank (void) | |||
230 | { | 204 | { |
231 | int c; | 205 | int c; |
232 | while((c = fgetc(in)) != EOF && !isspace(c)) | 206 | while((c = fgetc(in)) != EOF && !isspace(c)) |
233 | add_char(c); | 207 | luaI_addchar(c); |
234 | return c; | 208 | return c; |
235 | } | 209 | } |
236 | 210 | ||
@@ -238,7 +212,7 @@ static void read_m (int m) | |||
238 | { | 212 | { |
239 | int c; | 213 | int c; |
240 | while (m-- && (c = fgetc(in)) != EOF) | 214 | while (m-- && (c = fgetc(in)) != EOF) |
241 | add_char(c); | 215 | luaI_addchar(c); |
242 | } | 216 | } |
243 | 217 | ||
244 | 218 | ||
@@ -256,21 +230,18 @@ static void read_free (void) | |||
256 | { /* string */ | 230 | { /* string */ |
257 | c = read_until_char(c); | 231 | c = read_until_char(c); |
258 | if (c == EOF) | 232 | if (c == EOF) |
259 | { | ||
260 | add_char(0); /* to be ready for next time */ | ||
261 | lua_pushnil(); | 233 | lua_pushnil(); |
262 | } | ||
263 | else | 234 | else |
264 | lua_pushstring(add_char(0)); | 235 | lua_pushstring(luaI_addchar(0)); |
265 | } | 236 | } |
266 | else | 237 | else |
267 | { | 238 | { |
268 | double d; | 239 | double d; |
269 | char dummy; | 240 | char dummy; |
270 | char *s; | 241 | char *s; |
271 | add_char(c); | 242 | luaI_addchar(c); |
272 | read_until_blank(); | 243 | read_until_blank(); |
273 | s = add_char(0); | 244 | s = luaI_addchar(0); |
274 | if (sscanf(s, "%lf %c", &d, &dummy) == 1) | 245 | if (sscanf(s, "%lf %c", &d, &dummy) == 1) |
275 | lua_pushnumber(d); | 246 | lua_pushnumber(d); |
276 | else | 247 | else |
@@ -281,6 +252,7 @@ static void read_free (void) | |||
281 | static void io_read (void) | 252 | static void io_read (void) |
282 | { | 253 | { |
283 | lua_Object o = lua_getparam (1); | 254 | lua_Object o = lua_getparam (1); |
255 | luaI_addchar(0); /* initialize buffer */ | ||
284 | if (o == LUA_NOOBJECT) /* free format */ | 256 | if (o == LUA_NOOBJECT) /* free format */ |
285 | read_free(); | 257 | read_free(); |
286 | else /* formatted */ | 258 | else /* formatted */ |
@@ -295,7 +267,7 @@ static void io_read (void) | |||
295 | read_until_blank(); | 267 | read_until_blank(); |
296 | else | 268 | else |
297 | read_m(m); | 269 | read_m(m); |
298 | s = add_char(0); | 270 | s = luaI_addchar(0); |
299 | if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0)) | 271 | if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0)) |
300 | lua_pushstring(s); | 272 | lua_pushstring(s); |
301 | else | 273 | else |
@@ -313,7 +285,7 @@ static void io_read (void) | |||
313 | else | 285 | else |
314 | { | 286 | { |
315 | read_m(m); | 287 | read_m(m); |
316 | result = sscanf(add_char(0), "%lf", &d); | 288 | result = sscanf(luaI_addchar(0), "%lf", &d); |
317 | } | 289 | } |
318 | if (result == 1) | 290 | if (result == 1) |
319 | lua_pushnumber(d); | 291 | lua_pushnumber(d); |
@@ -333,13 +305,14 @@ static void io_readuntil (void) | |||
333 | { | 305 | { |
334 | int del, c; | 306 | int del, c; |
335 | lua_Object p = lua_getparam(1); | 307 | lua_Object p = lua_getparam(1); |
308 | luaI_addchar(0); /* initialize buffer */ | ||
336 | if (p == LUA_NOOBJECT || lua_isnil(p)) | 309 | if (p == LUA_NOOBJECT || lua_isnil(p)) |
337 | del = EOF; | 310 | del = EOF; |
338 | else | 311 | else |
339 | del = *lua_check_string(1, "readuntil"); | 312 | del = *lua_check_string(1, "readuntil"); |
340 | c = read_until_char(del); | 313 | c = read_until_char(del); |
341 | if (c != EOF) ungetc(c,in); | 314 | if (c != EOF) ungetc(c,in); |
342 | lua_pushstring(add_char(0)); | 315 | lua_pushstring(luaI_addchar(0)); |
343 | } | 316 | } |
344 | 317 | ||
345 | 318 | ||
@@ -512,7 +485,7 @@ static void io_time (void) | |||
512 | } | 485 | } |
513 | 486 | ||
514 | /* | 487 | /* |
515 | ** Return date: dd, mm, yyyy | 488 | ** Return date: dd, mm, yyyy, weekday |
516 | */ | 489 | */ |
517 | static void io_date (void) | 490 | static void io_date (void) |
518 | { | 491 | { |
@@ -524,6 +497,7 @@ static void io_date (void) | |||
524 | lua_pushnumber(s->tm_mday); | 497 | lua_pushnumber(s->tm_mday); |
525 | lua_pushnumber(s->tm_mon+1); | 498 | lua_pushnumber(s->tm_mon+1); |
526 | lua_pushnumber(s->tm_year+1900); | 499 | lua_pushnumber(s->tm_year+1900); |
500 | lua_pushnumber(s->tm_wday+1); | ||
527 | } | 501 | } |
528 | 502 | ||
529 | /* | 503 | /* |
@@ -632,3 +606,25 @@ void iolib_open (void) | |||
632 | lua_setfallback("error", errorfb); | 606 | lua_setfallback("error", errorfb); |
633 | } | 607 | } |
634 | 608 | ||
609 | /* | ||
610 | ** Return user formatted time stamp | ||
611 | * | ||
612 | static void sys_localtime (void) | ||
613 | { | ||
614 | time_t t; | ||
615 | struct tm *tm; | ||
616 | lua_Object o = lua_getparam(1); | ||
617 | |||
618 | time(&t); tm = localtime(&t); | ||
619 | if (lua_isstring(o)) | ||
620 | { | ||
621 | char b[BUFSIZ]; | ||
622 | if (strftime(b,sizeof(b),lua_getstring(o),tm)==0) | ||
623 | lua_pushstring(ctime(&t)); | ||
624 | else | ||
625 | lua_pushstring(b); | ||
626 | } | ||
627 | else | ||
628 | lua_pushstring(ctime(&t)); | ||
629 | } | ||
630 | */ | ||