aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-26 19:23:15 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-26 19:23:15 -0300
commit264f8c5e7bd168de2f0ca07399e6fc70d5a820d3 (patch)
tree2cb28f94f0b7dd131ee16679df813a0b52a852c7
parent9e9e2ea287a24871e6e35faef52ae236bf85f8ae (diff)
downloadlua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.tar.gz
lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.tar.bz2
lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.zip
new (internal?) functions to manipulate userdata
-rw-r--r--iolib.c47
-rw-r--r--lualib.h7
-rw-r--r--strlib.c44
3 files changed, 72 insertions, 26 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
diff --git a/lualib.h b/lualib.h
index 09af12fb..03c7734d 100644
--- a/lualib.h
+++ b/lualib.h
@@ -2,7 +2,7 @@
2** Libraries to be used in LUA programs 2** Libraries to be used in LUA programs
3** Grupo de Tecnologia em Computacao Grafica 3** Grupo de Tecnologia em Computacao Grafica
4** TeCGraf - PUC-Rio 4** TeCGraf - PUC-Rio
5** $Id: lualib.h,v 1.11 1997/03/17 17:01:10 roberto Exp roberto $ 5** $Id: lualib.h,v 1.12 1997/03/18 15:30:50 roberto Exp roberto $
6*/ 6*/
7 7
8#ifndef lualib_h 8#ifndef lualib_h
@@ -19,10 +19,11 @@ void mathlib_open (void);
19/* auxiliar functions (private) */ 19/* auxiliar functions (private) */
20 20
21char *luaI_addchar (int c); 21char *luaI_addchar (int c);
22void luaI_emptybuff (void);
22void luaI_addquoted (char *s); 23void luaI_addquoted (char *s);
23 24
24char *item_end (char *p); 25char *luaL_item_end (char *p);
25int singlematch (int c, char *p); 26int luaL_singlematch (int c, char *p);
26 27
27#endif 28#endif
28 29
diff --git a/strlib.c b/strlib.c
index 122a4fb1..8d9c00d8 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,7 +3,7 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.36 1997/03/17 17:01:10 roberto Exp roberto $"; 6char *rcs_strlib="$Id: strlib.c,v 1.37 1997/03/18 15:30:50 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -24,7 +24,7 @@ struct lbuff {
24static struct lbuff lbuffer = {NULL, 0, 0}; 24static struct lbuff lbuffer = {NULL, 0, 0};
25 25
26 26
27static char *lua_strbuffer (unsigned long size) 27static char *luaL_strbuffer (unsigned long size)
28{ 28{
29 if (size > lbuffer.max) { 29 if (size > lbuffer.max) {
30 /* ANSI "realloc" doesn't need this test, but some machines (Sun!) 30 /* ANSI "realloc" doesn't need this test, but some machines (Sun!)
@@ -39,20 +39,24 @@ static char *lua_strbuffer (unsigned long size)
39 39
40static char *openspace (unsigned long size) 40static char *openspace (unsigned long size)
41{ 41{
42 char *buff = lua_strbuffer(lbuffer.size+size); 42 char *buff = luaL_strbuffer(lbuffer.size+size);
43 return buff+lbuffer.size; 43 return buff+lbuffer.size;
44} 44}
45 45
46char *luaI_addchar (int c) 46char *luaI_addchar (int c)
47{ 47{
48 if (lbuffer.size >= lbuffer.max) 48 if (lbuffer.size >= lbuffer.max)
49 lua_strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2); 49 luaL_strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2);
50 lbuffer.b[lbuffer.size++] = c; 50 lbuffer.b[lbuffer.size++] = c;
51 if (c == 0)
52 lbuffer.size = 0; /* prepare for next string */
53 return lbuffer.b; 51 return lbuffer.b;
54} 52}
55 53
54void luaI_emptybuff (void)
55{
56 lbuffer.size = 0; /* prepare for next string */
57}
58
59
56static void addnchar (char *s, int n) 60static void addnchar (char *s, int n)
57{ 61{
58 char *b = openspace(n); 62 char *b = openspace(n);
@@ -75,7 +79,7 @@ static void str_tok (void)
75 lua_Object t = lua_createtable(); 79 lua_Object t = lua_createtable();
76 int i = 1; 80 int i = 1;
77 /* As strtok changes s1, and s1 is "constant", make a copy of it */ 81 /* As strtok changes s1, and s1 is "constant", make a copy of it */
78 s1 = strcpy(lua_strbuffer(strlen(s1+1)), s1); 82 s1 = strcpy(luaL_strbuffer(strlen(s1+1)), s1);
79 while ((s1 = strtok(s1, del)) != NULL) { 83 while ((s1 = strtok(s1, del)) != NULL) {
80 lua_pushobject(t); 84 lua_pushobject(t);
81 lua_pushnumber(i++); 85 lua_pushnumber(i++);
@@ -105,7 +109,7 @@ static void str_sub (void)
105 long start = (long)luaL_check_number(2, "strsub"); 109 long start = (long)luaL_check_number(2, "strsub");
106 long end = (long)luaL_opt_number(3, strlen(s), "strsub"); 110 long end = (long)luaL_opt_number(3, strlen(s), "strsub");
107 if (1 <= start && start <= end && end <= strlen(s)) { 111 if (1 <= start && start <= end && end <= strlen(s)) {
108 luaI_addchar(0); 112 luaI_emptybuff();
109 addnchar(s+start-1, end-start+1); 113 addnchar(s+start-1, end-start+1);
110 lua_pushstring(luaI_addchar(0)); 114 lua_pushstring(luaI_addchar(0));
111 } 115 }
@@ -118,7 +122,7 @@ static void str_sub (void)
118static void str_lower (void) 122static void str_lower (void)
119{ 123{
120 char *s = luaL_check_string(1, "strlower"); 124 char *s = luaL_check_string(1, "strlower");
121 luaI_addchar(0); 125 luaI_emptybuff();
122 while (*s) 126 while (*s)
123 luaI_addchar(tolower((unsigned char)*s++)); 127 luaI_addchar(tolower((unsigned char)*s++));
124 lua_pushstring(luaI_addchar(0)); 128 lua_pushstring(luaI_addchar(0));
@@ -130,7 +134,7 @@ static void str_lower (void)
130static void str_upper (void) 134static void str_upper (void)
131{ 135{
132 char *s = luaL_check_string(1, "strupper"); 136 char *s = luaL_check_string(1, "strupper");
133 luaI_addchar(0); 137 luaI_emptybuff();
134 while (*s) 138 while (*s)
135 luaI_addchar(toupper((unsigned char)*s++)); 139 luaI_addchar(toupper((unsigned char)*s++));
136 lua_pushstring(luaI_addchar(0)); 140 lua_pushstring(luaI_addchar(0));
@@ -140,7 +144,7 @@ static void str_rep (void)
140{ 144{
141 char *s = luaL_check_string(1, "strrep"); 145 char *s = luaL_check_string(1, "strrep");
142 int n = (int)luaL_check_number(2, "strrep"); 146 int n = (int)luaL_check_number(2, "strrep");
143 luaI_addchar(0); 147 luaI_emptybuff();
144 while (n-- > 0) 148 while (n-- > 0)
145 addstr(s); 149 addstr(s);
146 lua_pushstring(luaI_addchar(0)); 150 lua_pushstring(luaI_addchar(0));
@@ -168,7 +172,7 @@ static char *bracket_end (char *p)
168 return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']'); 172 return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']');
169} 173}
170 174
171char *item_end (char *p) 175char *luaL_item_end (char *p)
172{ 176{
173 switch (*p++) { 177 switch (*p++) {
174 case '\0': return p-1; 178 case '\0': return p-1;
@@ -202,7 +206,7 @@ static int matchclass (int c, int cl)
202 return (islower((unsigned char)cl) ? res : !res); 206 return (islower((unsigned char)cl) ? res : !res);
203} 207}
204 208
205int singlematch (int c, char *p) 209int luaL_singlematch (int c, char *p)
206{ 210{
207 if (c == 0) return 0; 211 if (c == 0) return 0;
208 switch (*p) { 212 switch (*p) {
@@ -324,8 +328,8 @@ static char *match (char *s, char *p, int level)
324 } 328 }
325 else goto dflt; 329 else goto dflt;
326 default: dflt: { /* it is a pattern item */ 330 default: dflt: { /* it is a pattern item */
327 int m = singlematch(*s, p); 331 int m = luaL_singlematch(*s, p);
328 char *ep = item_end(p); /* get what is next */ 332 char *ep = luaL_item_end(p); /* get what is next */
329 switch (*ep) { 333 switch (*ep) {
330 case '*': { /* repetition */ 334 case '*': { /* repetition */
331 char *res; 335 char *res;
@@ -427,7 +431,7 @@ static void str_gsub (void)
427 int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub"); 431 int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub");
428 int anchor = (*p == '^') ? (p++, 1) : 0; 432 int anchor = (*p == '^') ? (p++, 1) : 0;
429 int n = 0; 433 int n = 0;
430 luaI_addchar(0); 434 luaI_emptybuff();
431 while (n < max_s) { 435 while (n < max_s) {
432 char *e = match(src, p, 0); 436 char *e = match(src, p, 0);
433 if (e) { 437 if (e) {
@@ -450,10 +454,10 @@ static void str_set (void)
450{ 454{
451 char *item = luaL_check_string(1, "strset"); 455 char *item = luaL_check_string(1, "strset");
452 int i; 456 int i;
453 luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format"); 457 luaL_arg_check(*luaL_item_end(item) == 0, "strset", 1, "wrong format");
454 luaI_addchar(0); 458 luaI_emptybuff();
455 for (i=1; i<256; i++) /* 0 cannot be part of a set */ 459 for (i=1; i<256; i++) /* 0 cannot be part of a set */
456 if (singlematch(i, item)) 460 if (luaL_singlematch(i, item))
457 luaI_addchar(i); 461 luaI_addchar(i);
458 lua_pushstring(luaI_addchar(0)); 462 lua_pushstring(luaI_addchar(0));
459} 463}
@@ -476,7 +480,7 @@ static void str_format (void)
476{ 480{
477 int arg = 1; 481 int arg = 1;
478 char *strfrmt = luaL_check_string(arg++, "format"); 482 char *strfrmt = luaL_check_string(arg++, "format");
479 luaI_addchar(0); /* initialize */ 483 luaI_emptybuff(); /* initialize */
480 while (*strfrmt) { 484 while (*strfrmt) {
481 if (*strfrmt != '%') 485 if (*strfrmt != '%')
482 luaI_addchar(*strfrmt++); 486 luaI_addchar(*strfrmt++);