new (internal?) functions to manipulate userdata

This commit is contained in:
Roberto Ierusalimschy 1997-03-26 19:23:15 -03:00
parent 9e9e2ea287
commit 264f8c5e7b
3 changed files with 72 additions and 26 deletions

47
iolib.c
View File

@ -116,7 +116,7 @@ static void io_read (void)
char *p = luaL_opt_string(1, "[^\n]*{\n}", "read");
int inskip = 0; /* to control {skips} */
int c = NEED_OTHER;
luaI_addchar(0);
luaI_emptybuff();
while (*p) {
if (*p == '{') {
inskip++;
@ -129,10 +129,10 @@ static void io_read (void)
p++;
}
else {
char *ep = item_end(p); /* get what is next */
char *ep = luaL_item_end(p); /* get what is next */
int m; /* match result */
if (c == NEED_OTHER) c = getc(lua_infile);
m = (c == EOF) ? 0 : singlematch((char)c, p);
m = (c == EOF) ? 0 : luaL_singlematch((char)c, p);
if (m) {
if (inskip == 0) luaI_addchar(c);
c = NEED_OTHER;
@ -277,6 +277,45 @@ static void errorfb (void)
}
/* --------------------------------------------
* functions to manipulate userdata
*/
static void getbyte (void)
{
lua_Object ud = lua_getparam(1);
int i = luaL_check_number(2, "getbyte")-1;
luaL_arg_check(lua_isuserdata(ud), "getbyte", 1, "userdata expected");
luaL_arg_check(0 <= i && i < lua_getbindatasize(ud), "getbyte", 2,
"out of range");
lua_pushnumber(*(((char *)lua_getbinarydata(ud))+i));
}
static void createuserdata (void)
{
lua_Object t = lua_getparam(1);
int tag = luaL_opt_number(2, 0, "createud");
int i;
luaI_emptybuff();
luaL_arg_check(lua_istable(t), "createud", 1, "table expected");
for (i=0; ; i++) {
lua_Object o;
lua_beginblock();
lua_pushobject(t);
lua_pushnumber(i+1);
o = lua_basicindex();
if (lua_isnil(o)) {
lua_endblock();
break;
}
luaL_arg_check(lua_isnumber(o), "createud", 1,
"table values must be numbers");
luaI_addchar(lua_getnumber(o));
lua_endblock();
}
lua_pushbinarydata(luaI_addchar(0), i, tag);
}
static struct luaL_reg iolib[] = {
{"readfrom", io_readfrom},
{"writeto", io_writeto},
@ -291,6 +330,8 @@ static struct luaL_reg iolib[] = {
{"date", io_date},
{"exit", io_exit},
{"debug", io_debug},
{"getbyte", getbyte},
{"createud", createuserdata},
{"print_stack", errorfb}
};

View File

@ -2,7 +2,7 @@
** Libraries to be used in LUA programs
** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio
** $Id: lualib.h,v 1.11 1997/03/17 17:01:10 roberto Exp roberto $
** $Id: lualib.h,v 1.12 1997/03/18 15:30:50 roberto Exp roberto $
*/
#ifndef lualib_h
@ -19,10 +19,11 @@ void mathlib_open (void);
/* auxiliar functions (private) */
char *luaI_addchar (int c);
void luaI_emptybuff (void);
void luaI_addquoted (char *s);
char *item_end (char *p);
int singlematch (int c, char *p);
char *luaL_item_end (char *p);
int luaL_singlematch (int c, char *p);
#endif

View File

@ -3,7 +3,7 @@
** String library to LUA
*/
char *rcs_strlib="$Id: strlib.c,v 1.36 1997/03/17 17:01:10 roberto Exp roberto $";
char *rcs_strlib="$Id: strlib.c,v 1.37 1997/03/18 15:30:50 roberto Exp roberto $";
#include <string.h>
#include <stdio.h>
@ -24,7 +24,7 @@ struct lbuff {
static struct lbuff lbuffer = {NULL, 0, 0};
static char *lua_strbuffer (unsigned long size)
static char *luaL_strbuffer (unsigned long size)
{
if (size > lbuffer.max) {
/* ANSI "realloc" doesn't need this test, but some machines (Sun!)
@ -39,20 +39,24 @@ static char *lua_strbuffer (unsigned long size)
static char *openspace (unsigned long size)
{
char *buff = lua_strbuffer(lbuffer.size+size);
char *buff = luaL_strbuffer(lbuffer.size+size);
return buff+lbuffer.size;
}
char *luaI_addchar (int c)
{
if (lbuffer.size >= lbuffer.max)
lua_strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2);
luaL_strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2);
lbuffer.b[lbuffer.size++] = c;
if (c == 0)
lbuffer.size = 0; /* prepare for next string */
return lbuffer.b;
}
void luaI_emptybuff (void)
{
lbuffer.size = 0; /* prepare for next string */
}
static void addnchar (char *s, int n)
{
char *b = openspace(n);
@ -75,7 +79,7 @@ static void str_tok (void)
lua_Object t = lua_createtable();
int i = 1;
/* As strtok changes s1, and s1 is "constant", make a copy of it */
s1 = strcpy(lua_strbuffer(strlen(s1+1)), s1);
s1 = strcpy(luaL_strbuffer(strlen(s1+1)), s1);
while ((s1 = strtok(s1, del)) != NULL) {
lua_pushobject(t);
lua_pushnumber(i++);
@ -105,7 +109,7 @@ static void str_sub (void)
long start = (long)luaL_check_number(2, "strsub");
long end = (long)luaL_opt_number(3, strlen(s), "strsub");
if (1 <= start && start <= end && end <= strlen(s)) {
luaI_addchar(0);
luaI_emptybuff();
addnchar(s+start-1, end-start+1);
lua_pushstring(luaI_addchar(0));
}
@ -118,7 +122,7 @@ static void str_sub (void)
static void str_lower (void)
{
char *s = luaL_check_string(1, "strlower");
luaI_addchar(0);
luaI_emptybuff();
while (*s)
luaI_addchar(tolower((unsigned char)*s++));
lua_pushstring(luaI_addchar(0));
@ -130,7 +134,7 @@ static void str_lower (void)
static void str_upper (void)
{
char *s = luaL_check_string(1, "strupper");
luaI_addchar(0);
luaI_emptybuff();
while (*s)
luaI_addchar(toupper((unsigned char)*s++));
lua_pushstring(luaI_addchar(0));
@ -140,7 +144,7 @@ static void str_rep (void)
{
char *s = luaL_check_string(1, "strrep");
int n = (int)luaL_check_number(2, "strrep");
luaI_addchar(0);
luaI_emptybuff();
while (n-- > 0)
addstr(s);
lua_pushstring(luaI_addchar(0));
@ -168,7 +172,7 @@ static char *bracket_end (char *p)
return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']');
}
char *item_end (char *p)
char *luaL_item_end (char *p)
{
switch (*p++) {
case '\0': return p-1;
@ -202,7 +206,7 @@ static int matchclass (int c, int cl)
return (islower((unsigned char)cl) ? res : !res);
}
int singlematch (int c, char *p)
int luaL_singlematch (int c, char *p)
{
if (c == 0) return 0;
switch (*p) {
@ -324,8 +328,8 @@ static char *match (char *s, char *p, int level)
}
else goto dflt;
default: dflt: { /* it is a pattern item */
int m = singlematch(*s, p);
char *ep = item_end(p); /* get what is next */
int m = luaL_singlematch(*s, p);
char *ep = luaL_item_end(p); /* get what is next */
switch (*ep) {
case '*': { /* repetition */
char *res;
@ -427,7 +431,7 @@ static void str_gsub (void)
int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub");
int anchor = (*p == '^') ? (p++, 1) : 0;
int n = 0;
luaI_addchar(0);
luaI_emptybuff();
while (n < max_s) {
char *e = match(src, p, 0);
if (e) {
@ -450,10 +454,10 @@ static void str_set (void)
{
char *item = luaL_check_string(1, "strset");
int i;
luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format");
luaI_addchar(0);
luaL_arg_check(*luaL_item_end(item) == 0, "strset", 1, "wrong format");
luaI_emptybuff();
for (i=1; i<256; i++) /* 0 cannot be part of a set */
if (singlematch(i, item))
if (luaL_singlematch(i, item))
luaI_addchar(i);
lua_pushstring(luaI_addchar(0));
}
@ -476,7 +480,7 @@ static void str_format (void)
{
int arg = 1;
char *strfrmt = luaL_check_string(arg++, "format");
luaI_addchar(0); /* initialize */
luaI_emptybuff(); /* initialize */
while (*strfrmt) {
if (*strfrmt != '%')
luaI_addchar(*strfrmt++);