aboutsummaryrefslogtreecommitdiff
path: root/iolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'iolib.c')
-rw-r--r--iolib.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/iolib.c b/iolib.c
index 05747d37..0dfb89c8 100644
--- a/iolib.c
+++ b/iolib.c
@@ -116,7 +116,7 @@ static void io_read (void)
116 char *p = luaL_opt_string(1, "[^\n]*{\n}", "read"); 116 char *p = luaL_opt_string(1, "[^\n]*{\n}", "read");
117 int inskip = 0; /* to control {skips} */ 117 int inskip = 0; /* to control {skips} */
118 int c = NEED_OTHER; 118 int c = NEED_OTHER;
119 luaI_addchar(0); 119 luaI_emptybuff();
120 while (*p) { 120 while (*p) {
121 if (*p == '{') { 121 if (*p == '{') {
122 inskip++; 122 inskip++;
@@ -129,10 +129,10 @@ static void io_read (void)
129 p++; 129 p++;
130 } 130 }
131 else { 131 else {
132 char *ep = item_end(p); /* get what is next */ 132 char *ep = luaL_item_end(p); /* get what is next */
133 int m; /* match result */ 133 int m; /* match result */
134 if (c == NEED_OTHER) c = getc(lua_infile); 134 if (c == NEED_OTHER) c = getc(lua_infile);
135 m = (c == EOF) ? 0 : singlematch((char)c, p); 135 m = (c == EOF) ? 0 : luaL_singlematch((char)c, p);
136 if (m) { 136 if (m) {
137 if (inskip == 0) luaI_addchar(c); 137 if (inskip == 0) luaI_addchar(c);
138 c = NEED_OTHER; 138 c = NEED_OTHER;
@@ -277,6 +277,45 @@ static void errorfb (void)
277} 277}
278 278
279 279
280/* --------------------------------------------
281* functions to manipulate userdata
282*/
283static void getbyte (void)
284{
285 lua_Object ud = lua_getparam(1);
286 int i = luaL_check_number(2, "getbyte")-1;
287 luaL_arg_check(lua_isuserdata(ud), "getbyte", 1, "userdata expected");
288 luaL_arg_check(0 <= i && i < lua_getbindatasize(ud), "getbyte", 2,
289 "out of range");
290 lua_pushnumber(*(((char *)lua_getbinarydata(ud))+i));
291}
292
293static void createuserdata (void)
294{
295 lua_Object t = lua_getparam(1);
296 int tag = luaL_opt_number(2, 0, "createud");
297 int i;
298 luaI_emptybuff();
299 luaL_arg_check(lua_istable(t), "createud", 1, "table expected");
300 for (i=0; ; i++) {
301 lua_Object o;
302 lua_beginblock();
303 lua_pushobject(t);
304 lua_pushnumber(i+1);
305 o = lua_basicindex();
306 if (lua_isnil(o)) {
307 lua_endblock();
308 break;
309 }
310 luaL_arg_check(lua_isnumber(o), "createud", 1,
311 "table values must be numbers");
312 luaI_addchar(lua_getnumber(o));
313 lua_endblock();
314 }
315 lua_pushbinarydata(luaI_addchar(0), i, tag);
316}
317
318
280static struct luaL_reg iolib[] = { 319static struct luaL_reg iolib[] = {
281{"readfrom", io_readfrom}, 320{"readfrom", io_readfrom},
282{"writeto", io_writeto}, 321{"writeto", io_writeto},
@@ -291,6 +330,8 @@ static struct luaL_reg iolib[] = {
291{"date", io_date}, 330{"date", io_date},
292{"exit", io_exit}, 331{"exit", io_exit},
293{"debug", io_debug}, 332{"debug", io_debug},
333{"getbyte", getbyte},
334{"createud", createuserdata},
294{"print_stack", errorfb} 335{"print_stack", errorfb}
295}; 336};
296 337