From d7d7b477bb4e09fc69e31ccf21106a2c07fa441d Mon Sep 17 00:00:00 2001 From: Waldemar Celes Date: Fri, 7 Oct 1994 19:47:05 -0300 Subject: consertar bug no formato real com zero casas decimais e implementar a funcao readuntil. --- iolib.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'iolib.c') diff --git a/iolib.c b/iolib.c index 52285d3d..94b2028b 100644 --- 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 #include @@ -314,6 +314,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: @@ -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); -- cgit v1.2.3-55-g6feb