MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / luaL_loadfilex

Function luaL_loadfilex

extlibs/sol3/include/sol/sol.hpp:2913–2971  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2911}
2912
2913COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode) {
2914 compat53_LoadF lf;
2915 int status, readstatus;
2916 int c;
2917 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
2918 if (filename == NULL) {
2919 lua_pushliteral(L, "=stdin");
2920 lf.f = stdin;
2921 }
2922 else {
2923 lua_pushfstring(L, "@%s", filename);
2924#if defined(_MSC_VER)
2925 /* This code is here to stop a deprecation error that stops builds
2926 * if a certain macro is defined. While normally not caring would
2927 * be best, some header-only libraries and builds can't afford to
2928 * dictate this to the user. A quick check shows that fopen_s this
2929 * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
2930 * possibly even before that so we don't need to do any version
2931 * number checks, since this has been there since forever. */
2932
2933 /* TO USER: if you want the behavior of typical fopen_s/fopen,
2934 * which does lock the file on VC++, define the macro used below to 0 */
2935#if COMPAT53_FOPEN_NO_LOCK
2936 lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
2937 if (lf.f == NULL)
2938 return compat53_errfile(L, "open", fnameindex);
2939#else /* use default locking version */
2940 if (fopen_s(&lf.f, filename, "r") != 0)
2941 return compat53_errfile(L, "open", fnameindex);
2942#endif /* Locking vs. No-locking fopen variants */
2943#else
2944 lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
2945 if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex);
2946#endif
2947 }
2948 if (compat53_skipcomment(&lf, &c)) /* read initial portion */
2949 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
2950 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
2951#if defined(_MSC_VER)
2952 if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
2953 return compat53_errfile(L, "reopen", fnameindex);
2954#else
2955 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
2956 if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex);
2957#endif
2958 compat53_skipcomment(&lf, &c); /* re-read initial portion */
2959 }
2960 if (c != EOF)
2961 lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
2962 status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
2963 readstatus = ferror(lf.f);
2964 if (filename) fclose(lf.f); /* close file (even in case of errors) */
2965 if (readstatus) {
2966 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
2967 return compat53_errfile(L, "read", fnameindex);
2968 }
2969 lua_remove(L, fnameindex);
2970 return status;

Callers 4

script_fileFunction · 0.70
do_fileMethod · 0.70
unsafe_script_fileMethod · 0.70
load_fileMethod · 0.70

Calls 7

lua_gettopFunction · 0.85
lua_pushfstringFunction · 0.85
compat53_errfileFunction · 0.85
fopen_sFunction · 0.85
compat53_skipcommentFunction · 0.85
lua_settopFunction · 0.85
lua_loadFunction · 0.70

Tested by

no test coverage detected