diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-26 19:23:15 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-26 19:23:15 -0300 |
commit | 264f8c5e7bd168de2f0ca07399e6fc70d5a820d3 (patch) | |
tree | 2cb28f94f0b7dd131ee16679df813a0b52a852c7 /iolib.c | |
parent | 9e9e2ea287a24871e6e35faef52ae236bf85f8ae (diff) | |
download | lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.tar.gz lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.tar.bz2 lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.zip |
new (internal?) functions to manipulate userdata
Diffstat (limited to 'iolib.c')
-rw-r--r-- | iolib.c | 47 |
1 files changed, 44 insertions, 3 deletions
@@ -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 | */ | ||
283 | static 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 | |||
293 | static 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 | |||
280 | static struct luaL_reg iolib[] = { | 319 | static 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 | ||