diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-18 17:35:49 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-18 17:35:49 -0300 |
commit | 60bdc370d90298723552b5c62e84052b1c06de91 (patch) | |
tree | db0be2648ba56ad472b96a13f6663009b8db1836 | |
parent | b9e31f73ee1acd556b00c6bc09b5d0be4eb3bb0f (diff) | |
download | lua-60bdc370d90298723552b5c62e84052b1c06de91.tar.gz lua-60bdc370d90298723552b5c62e84052b1c06de91.tar.bz2 lua-60bdc370d90298723552b5c62e84052b1c06de91.zip |
new functions lua_doFILE and lua_dobuffer to help users...
-rw-r--r-- | inout.c | 63 | ||||
-rw-r--r-- | inout.h | 6 |
2 files changed, 50 insertions, 19 deletions
@@ -5,7 +5,7 @@ | |||
5 | ** Also provides some predefined lua functions. | 5 | ** Also provides some predefined lua functions. |
6 | */ | 6 | */ |
7 | 7 | ||
8 | char *rcs_inout="$Id: inout.c,v 2.61 1997/06/16 16:50:22 roberto Exp roberto $"; | 8 | char *rcs_inout="$Id: inout.c,v 2.62 1997/06/17 18:44:31 roberto Exp roberto $"; |
9 | 9 | ||
10 | #include <stdio.h> | 10 | #include <stdio.h> |
11 | #include <string.h> | 11 | #include <string.h> |
@@ -38,6 +38,25 @@ char *luaI_typenames[] = { /* ORDER LUA_T */ | |||
38 | 38 | ||
39 | 39 | ||
40 | 40 | ||
41 | static void setparsedfile (char *name) | ||
42 | { | ||
43 | lua_parsedfile = luaI_createfixedstring(name)->str; | ||
44 | } | ||
45 | |||
46 | |||
47 | int lua_doFILE (FILE *f, int bin) | ||
48 | { | ||
49 | ZIO z; | ||
50 | luaz_Fopen(&z, f); | ||
51 | if (bin) | ||
52 | return luaI_undump(&z); | ||
53 | else { | ||
54 | lua_setinput(&z); | ||
55 | return lua_domain(); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | |||
41 | int lua_dofile (char *filename) | 60 | int lua_dofile (char *filename) |
42 | { | 61 | { |
43 | int status; | 62 | int status; |
@@ -45,26 +64,19 @@ int lua_dofile (char *filename) | |||
45 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); | 64 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); |
46 | if (f == NULL) | 65 | if (f == NULL) |
47 | return 2; | 66 | return 2; |
48 | lua_parsedfile = luaI_createfixedstring(filename?filename:"(stdin)")->str; | 67 | setparsedfile(filename?filename:"(stdin)"); |
49 | c = fgetc(f); | 68 | c = fgetc(f); |
50 | ungetc(c, f); | 69 | ungetc(c, f); |
51 | if (c == ID_CHUNK) { | 70 | if (c == ID_CHUNK) { |
52 | ZIO z; | ||
53 | f = freopen(filename, "rb", f); /* set binary mode */ | 71 | f = freopen(filename, "rb", f); /* set binary mode */ |
54 | zFopen(&z, f); | 72 | status = lua_doFILE(f, 1); |
55 | lua_setinput(&z); | ||
56 | status = luaI_undump(&z); | ||
57 | zclose(&z); | ||
58 | } | 73 | } |
59 | else { | 74 | else { |
60 | ZIO z; | ||
61 | if (c == '#') | 75 | if (c == '#') |
62 | while ((c=fgetc(f)) != '\n') /* skip first line */; | 76 | while ((c=fgetc(f)) != '\n') /* skip first line */; |
63 | zFopen(&z, f); | 77 | status = lua_doFILE(f, 0); |
64 | lua_setinput(&z); | ||
65 | status = lua_domain(); | ||
66 | zclose(&z); | ||
67 | } | 78 | } |
79 | fclose(f); | ||
68 | return status; | 80 | return status; |
69 | } | 81 | } |
70 | 82 | ||
@@ -72,16 +84,31 @@ int lua_dofile (char *filename) | |||
72 | 84 | ||
73 | #define SIZE_PREF 20 /* size of string prefix to appear in error messages */ | 85 | #define SIZE_PREF 20 /* size of string prefix to appear in error messages */ |
74 | 86 | ||
87 | |||
88 | int lua_dobuffer (char *buff, int size) | ||
89 | { | ||
90 | int status; | ||
91 | ZIO z; | ||
92 | setparsedfile("(buffer)"); | ||
93 | luaz_mopen(&z, buff, size); | ||
94 | status = luaI_undump(&z); | ||
95 | zclose(&z); | ||
96 | return status; | ||
97 | } | ||
98 | |||
99 | |||
75 | int lua_dostring (char *str) | 100 | int lua_dostring (char *str) |
76 | { | 101 | { |
77 | int status; | 102 | int status; |
78 | char buff[SIZE_PREF+25]; | 103 | char buff[SIZE_PREF+25]; |
104 | char *temp; | ||
79 | ZIO z; | 105 | ZIO z; |
80 | if (str == NULL) return 1; | 106 | if (str == NULL) return 1; |
81 | sprintf(buff, "(dostring) >> %.20s%s", str, | 107 | sprintf(buff, "(dostring) >> %.20s", str); |
82 | (strlen(str) > SIZE_PREF) ? "..." : ""); | 108 | temp = strchr(buff, '\n'); |
83 | lua_parsedfile = luaI_createfixedstring(buff)->str; | 109 | if (temp) *temp = 0; /* end string after first line */ |
84 | zsopen(&z, str); | 110 | setparsedfile(buff); |
111 | luaz_sopen(&z, str); | ||
85 | lua_setinput(&z); | 112 | lua_setinput(&z); |
86 | status = lua_domain(); | 113 | status = lua_domain(); |
87 | zclose(&z); | 114 | zclose(&z); |
@@ -251,7 +278,7 @@ static void luaI_call (void) | |||
251 | { | 278 | { |
252 | lua_Object f = lua_getparam(1); | 279 | lua_Object f = lua_getparam(1); |
253 | lua_Object arg = lua_getparam(2); | 280 | lua_Object arg = lua_getparam(2); |
254 | int withtable = (strcmp(luaL_opt_string(3, "plain"), "pack") == 0); | 281 | int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0); |
255 | int narg, i; | 282 | int narg, i; |
256 | luaL_arg_check(lua_isfunction(f), 1, "function expected"); | 283 | luaL_arg_check(lua_isfunction(f), 1, "function expected"); |
257 | luaL_arg_check(lua_istable(arg), 2, "table expected"); | 284 | luaL_arg_check(lua_istable(arg), 2, "table expected"); |
@@ -326,7 +353,7 @@ static struct { | |||
326 | } int_funcs[] = { | 353 | } int_funcs[] = { |
327 | {"assert", luaI_assert}, | 354 | {"assert", luaI_assert}, |
328 | {"call", luaI_call}, | 355 | {"call", luaI_call}, |
329 | {"callgc", luaI_collectgarbage}, | 356 | {"collectgarbage", luaI_collectgarbage}, |
330 | {"dofile", lua_internaldofile}, | 357 | {"dofile", lua_internaldofile}, |
331 | {"dostring", lua_internaldostring}, | 358 | {"dostring", lua_internaldostring}, |
332 | {"error", luaI_error}, | 359 | {"error", luaI_error}, |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: inout.h,v 1.17 1997/02/26 17:38:41 roberto Unstable roberto $ | 2 | ** $Id: inout.h,v 1.18 1997/06/16 16:50:22 roberto Exp roberto $ |
3 | */ | 3 | */ |
4 | 4 | ||
5 | 5 | ||
@@ -21,4 +21,8 @@ void lua_closestring (void); | |||
21 | 21 | ||
22 | void luaI_predefine (void); | 22 | void luaI_predefine (void); |
23 | 23 | ||
24 | int lua_dobuffer (char *buff, int size); | ||
25 | int lua_doFILE (FILE *f, int bin); | ||
26 | |||
27 | |||
24 | #endif | 28 | #endif |