new function 'luaL_loadfilex'
This commit is contained in:
28
lauxlib.c
28
lauxlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.c,v 1.234 2011/07/25 17:18:49 roberto Exp roberto $
|
** $Id: lauxlib.c,v 1.235 2011/11/09 19:08:55 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -331,7 +331,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
|
|||||||
|
|
||||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
|
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
|
||||||
/* keep some extra space to run error routines, if needed */
|
/* keep some extra space to run error routines, if needed */
|
||||||
const int extra = 2 * LUA_MINSTACK;
|
const int extra = LUA_MINSTACK;
|
||||||
if (!lua_checkstack(L, space + extra)) {
|
if (!lua_checkstack(L, space + extra)) {
|
||||||
if (msg)
|
if (msg)
|
||||||
luaL_error(L, "stack overflow (%s)", msg);
|
luaL_error(L, "stack overflow (%s)", msg);
|
||||||
@@ -592,6 +592,16 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int checkmode (lua_State *L, const char *mode, const char *x) {
|
||||||
|
if (mode && strchr(mode, x[0]) == NULL) {
|
||||||
|
lua_pushfstring(L,
|
||||||
|
"attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
|
||||||
|
return LUA_ERRFILE;
|
||||||
|
}
|
||||||
|
else return LUA_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int skipBOM (LoadF *lf) {
|
static int skipBOM (LoadF *lf) {
|
||||||
const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
|
const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
|
||||||
int c;
|
int c;
|
||||||
@@ -624,7 +634,8 @@ static int skipcomment (LoadF *lf, int *cp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
||||||
|
const char *mode) {
|
||||||
LoadF lf;
|
LoadF lf;
|
||||||
int status, readstatus;
|
int status, readstatus;
|
||||||
int c;
|
int c;
|
||||||
@@ -640,14 +651,23 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
|||||||
}
|
}
|
||||||
if (skipcomment(&lf, &c)) /* read initial portion */
|
if (skipcomment(&lf, &c)) /* read initial portion */
|
||||||
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
|
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
|
||||||
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
|
if (c == LUA_SIGNATURE[0]) { /* binary file? */
|
||||||
|
if ((status = checkmode(L, mode, "binary")) != LUA_OK)
|
||||||
|
goto closefile;
|
||||||
|
if (filename) {
|
||||||
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
|
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
|
||||||
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
|
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
|
||||||
skipcomment(&lf, &c); /* re-read initial portion */
|
skipcomment(&lf, &c); /* re-read initial portion */
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else { /* text file */
|
||||||
|
if ((status = checkmode(L, mode, "text")) != LUA_OK)
|
||||||
|
goto closefile;
|
||||||
|
}
|
||||||
if (c != EOF)
|
if (c != EOF)
|
||||||
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
||||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
|
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
|
||||||
|
closefile:
|
||||||
readstatus = ferror(lf.f);
|
readstatus = ferror(lf.f);
|
||||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||||
if (readstatus) {
|
if (readstatus) {
|
||||||
|
|||||||
14
lauxlib.h
14
lauxlib.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.h,v 1.117 2011/06/16 14:10:12 roberto Exp roberto $
|
** $Id: lauxlib.h,v 1.118 2011/11/11 19:59:17 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -72,7 +72,11 @@ LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
|
|||||||
LUALIB_API int (luaL_ref) (lua_State *L, int t);
|
LUALIB_API int (luaL_ref) (lua_State *L, int t);
|
||||||
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
|
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
|
||||||
|
|
||||||
LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
|
LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
|
||||||
|
const char *mode);
|
||||||
|
|
||||||
|
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
|
||||||
|
|
||||||
LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
|
LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
|
||||||
const char *name);
|
const char *name);
|
||||||
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
||||||
@@ -172,17 +176,17 @@ LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
|
** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
|
||||||
** initial structure 'luaIO_Stream' (it may contain other fields
|
** initial structure 'luaL_Stream' (it may contain other fields
|
||||||
** after that initial structure).
|
** after that initial structure).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define LUA_FILEHANDLE "FILE*"
|
#define LUA_FILEHANDLE "FILE*"
|
||||||
|
|
||||||
|
|
||||||
typedef struct luaIO_Stream {
|
typedef struct luaL_Stream {
|
||||||
FILE *f; /* stream (NULL for incompletely created streams) */
|
FILE *f; /* stream (NULL for incompletely created streams) */
|
||||||
lua_CFunction closef; /* to close stream (NULL for closed streams) */
|
lua_CFunction closef; /* to close stream (NULL for closed streams) */
|
||||||
} luaIO_Stream;
|
} luaL_Stream;
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|||||||
21
lbaselib.c
21
lbaselib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lbaselib.c,v 1.267 2011/11/09 19:28:27 roberto Exp roberto $
|
** $Id: lbaselib.c,v 1.268 2011/11/09 19:38:00 roberto Exp roberto $
|
||||||
** Basic library
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -255,7 +255,14 @@ static int load_aux (lua_State *L, int status) {
|
|||||||
|
|
||||||
static int luaB_loadfile (lua_State *L) {
|
static int luaB_loadfile (lua_State *L) {
|
||||||
const char *fname = luaL_optstring(L, 1, NULL);
|
const char *fname = luaL_optstring(L, 1, NULL);
|
||||||
return load_aux(L, luaL_loadfile(L, fname));
|
const char *mode = luaL_optstring(L, 2, NULL);
|
||||||
|
int env = !lua_isnone(L, 3); /* 'env' parameter? */
|
||||||
|
int status = luaL_loadfilex(L, fname, mode);
|
||||||
|
if (status == LUA_OK && env) { /* 'env' parameter? */
|
||||||
|
lua_pushvalue(L, 3);
|
||||||
|
lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */
|
||||||
|
}
|
||||||
|
return load_aux(L, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -277,11 +284,11 @@ typedef struct {
|
|||||||
** pushed on the stack) in case of errors.
|
** pushed on the stack) in case of errors.
|
||||||
*/
|
*/
|
||||||
static const char *checkrights (lua_State *L, const char *mode, const char *s) {
|
static const char *checkrights (lua_State *L, const char *mode, const char *s) {
|
||||||
if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0])
|
const char *x = (*s == LUA_SIGNATURE[0]) ? "binary" : "text";
|
||||||
return lua_pushstring(L, "attempt to load a binary chunk");
|
if (strchr(mode, x[0]) == NULL)
|
||||||
if (strchr(mode, 't') == NULL && *s != LUA_SIGNATURE[0])
|
return lua_pushfstring(L,
|
||||||
return lua_pushstring(L, "attempt to load a text chunk");
|
"attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
|
||||||
return NULL; /* chunk in allowed format */
|
else return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user