lua

A copy of the Lua development repository
Log | Files | Refs | README

commit d7d7b477bb4e09fc69e31ccf21106a2c07fa441d
parent dc6d0dcc09126e70a4f6b36130752d0bd19aaf50
Author: Waldemar Celes <celes@tecgraf.puc-rio.br>
Date:   Fri,  7 Oct 1994 19:46:46 -0300

consertar bug no formato real com zero casas decimais e
implementar a funcao readuntil.

Diffstat:
Miolib.c | 39++++++++++++++++++++++++++++++++++++---
1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/iolib.c b/iolib.c @@ -3,7 +3,7 @@ ** Input/output library to LUA */ -char *rcs_iolib="$Id: iolib.c,v 1.8 1994/08/17 22:34:20 roberto Exp celes $"; +char *rcs_iolib="$Id: iolib.c,v 1.9 1994/09/12 19:24:31 celes Exp celes $"; #include <stdlib.h> #include <string.h> @@ -315,6 +315,38 @@ static void io_read (void) /* +** Read characters until a given one. The delimiter is not read. +*/ +static void io_readuntil (void) +{ + int n=255,m=0; + char c,d; + char *s; + lua_Object lo = lua_getparam(1); + if (!lua_isstring(lo)) + d = EOF; + else + d = *lua_getstring(lo); + + s = calloc(n+1, sizeof(char)); + while((c = fgetc(in)) != EOF && c != d) + { + if (m==n) + { + n *= 2; + s = realloc(s, (n+1)*sizeof(char)); + } + s[m++] = c; + } + if (c != EOF) ungetc(c,in); + s[m] = 0; + lua_pushstring(s); + free(s); +} + + + +/* ** Write a variable. On error put 0 on stack, otherwise put 1. ** LUA interface: ** status = write (variable [,format]) @@ -360,8 +392,8 @@ static char *buildformat (char *e, lua_Object o) sprintf(f,"%%"); if (j == '<' || j == '|') sprintf(strchr(f,0),"-"); - if (m != 0) sprintf(strchr(f,0),"%d", m); - if (n != 0) sprintf(strchr(f,0),".%d", n); + if (m > 0) sprintf(strchr(f,0),"%d", m); + if (n >= 0) sprintf(strchr(f,0),".%d", n); switch (t) { case 'i': case 'I': t = 'd'; @@ -536,6 +568,7 @@ void iolib_open (void) lua_register ("writeto", io_writeto); lua_register ("appendto", io_appendto); lua_register ("read", io_read); + lua_register ("readuntil",io_readuntil); lua_register ("write", io_write); lua_register ("execute", io_execute); lua_register ("remove", io_remove);