diff options
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 37 |
1 files changed, 9 insertions, 28 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.235 2011/11/09 19:08:55 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.236 2011/11/14 17:10:24 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -592,16 +592,6 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { | |||
592 | } | 592 | } |
593 | 593 | ||
594 | 594 | ||
595 | static int checkmode (lua_State *L, const char *mode, const char *x) { | ||
596 | if (mode && strchr(mode, x[0]) == NULL) { | ||
597 | lua_pushfstring(L, | ||
598 | "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); | ||
599 | return LUA_ERRFILE; | ||
600 | } | ||
601 | else return LUA_OK; | ||
602 | } | ||
603 | |||
604 | |||
605 | static int skipBOM (LoadF *lf) { | 595 | static int skipBOM (LoadF *lf) { |
606 | const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ | 596 | const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ |
607 | int c; | 597 | int c; |
@@ -651,23 +641,14 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, | |||
651 | } | 641 | } |
652 | if (skipcomment(&lf, &c)) /* read initial portion */ | 642 | if (skipcomment(&lf, &c)) /* read initial portion */ |
653 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ | 643 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ |
654 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ | 644 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
655 | if ((status = checkmode(L, mode, "binary")) != LUA_OK) | 645 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
656 | goto closefile; | 646 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
657 | if (filename) { | 647 | skipcomment(&lf, &c); /* re-read initial portion */ |
658 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ | ||
659 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); | ||
660 | skipcomment(&lf, &c); /* re-read initial portion */ | ||
661 | } | ||
662 | } | ||
663 | else { /* text file */ | ||
664 | if ((status = checkmode(L, mode, "text")) != LUA_OK) | ||
665 | goto closefile; | ||
666 | } | 648 | } |
667 | if (c != EOF) | 649 | if (c != EOF) |
668 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ | 650 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
669 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); | 651 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
670 | closefile: | ||
671 | readstatus = ferror(lf.f); | 652 | readstatus = ferror(lf.f); |
672 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ | 653 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
673 | if (readstatus) { | 654 | if (readstatus) { |
@@ -695,12 +676,12 @@ static const char *getS (lua_State *L, void *ud, size_t *size) { | |||
695 | } | 676 | } |
696 | 677 | ||
697 | 678 | ||
698 | LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, | 679 | LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, |
699 | const char *name) { | 680 | const char *name, const char *mode) { |
700 | LoadS ls; | 681 | LoadS ls; |
701 | ls.s = buff; | 682 | ls.s = buff; |
702 | ls.size = size; | 683 | ls.size = size; |
703 | return lua_load(L, getS, &ls, name); | 684 | return lua_load(L, getS, &ls, name, mode); |
704 | } | 685 | } |
705 | 686 | ||
706 | 687 | ||