diff options
-rw-r--r-- | iolib.c | 39 |
1 files changed, 36 insertions, 3 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.8 1994/08/17 22:34:20 roberto Exp celes $"; | 6 | char *rcs_iolib="$Id: iolib.c,v 1.9 1994/09/12 19:24:31 celes Exp celes $"; |
7 | 7 | ||
8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
9 | #include <string.h> | 9 | #include <string.h> |
@@ -315,6 +315,38 @@ static void io_read (void) | |||
315 | 315 | ||
316 | 316 | ||
317 | /* | 317 | /* |
318 | ** Read characters until a given one. The delimiter is not read. | ||
319 | */ | ||
320 | static void io_readuntil (void) | ||
321 | { | ||
322 | int n=255,m=0; | ||
323 | char c,d; | ||
324 | char *s; | ||
325 | lua_Object lo = lua_getparam(1); | ||
326 | if (!lua_isstring(lo)) | ||
327 | d = EOF; | ||
328 | else | ||
329 | d = *lua_getstring(lo); | ||
330 | |||
331 | s = calloc(n+1, sizeof(char)); | ||
332 | while((c = fgetc(in)) != EOF && c != d) | ||
333 | { | ||
334 | if (m==n) | ||
335 | { | ||
336 | n *= 2; | ||
337 | s = realloc(s, (n+1)*sizeof(char)); | ||
338 | } | ||
339 | s[m++] = c; | ||
340 | } | ||
341 | if (c != EOF) ungetc(c,in); | ||
342 | s[m] = 0; | ||
343 | lua_pushstring(s); | ||
344 | free(s); | ||
345 | } | ||
346 | |||
347 | |||
348 | |||
349 | /* | ||
318 | ** Write a variable. On error put 0 on stack, otherwise put 1. | 350 | ** Write a variable. On error put 0 on stack, otherwise put 1. |
319 | ** LUA interface: | 351 | ** LUA interface: |
320 | ** status = write (variable [,format]) | 352 | ** status = write (variable [,format]) |
@@ -360,8 +392,8 @@ static char *buildformat (char *e, lua_Object o) | |||
360 | 392 | ||
361 | sprintf(f,"%%"); | 393 | sprintf(f,"%%"); |
362 | if (j == '<' || j == '|') sprintf(strchr(f,0),"-"); | 394 | if (j == '<' || j == '|') sprintf(strchr(f,0),"-"); |
363 | if (m != 0) sprintf(strchr(f,0),"%d", m); | 395 | if (m > 0) sprintf(strchr(f,0),"%d", m); |
364 | if (n != 0) sprintf(strchr(f,0),".%d", n); | 396 | if (n >= 0) sprintf(strchr(f,0),".%d", n); |
365 | switch (t) | 397 | switch (t) |
366 | { | 398 | { |
367 | case 'i': case 'I': t = 'd'; | 399 | case 'i': case 'I': t = 'd'; |
@@ -536,6 +568,7 @@ void iolib_open (void) | |||
536 | lua_register ("writeto", io_writeto); | 568 | lua_register ("writeto", io_writeto); |
537 | lua_register ("appendto", io_appendto); | 569 | lua_register ("appendto", io_appendto); |
538 | lua_register ("read", io_read); | 570 | lua_register ("read", io_read); |
571 | lua_register ("readuntil",io_readuntil); | ||
539 | lua_register ("write", io_write); | 572 | lua_register ("write", io_write); |
540 | lua_register ("execute", io_execute); | 573 | lua_register ("execute", io_execute); |
541 | lua_register ("remove", io_remove); | 574 | lua_register ("remove", io_remove); |