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

Function luaL_loadfilex

Libs/sol/sol.hpp:3432–3493  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3430}
3431
3432COMPAT53_API int luaL_loadfilex(lua_State* L, const char* filename, const char* mode) {
3433 compat53_LoadF lf;
3434 int status, readstatus;
3435 int c;
3436 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
3437 if (filename == NULL) {
3438 lua_pushliteral(L, "=stdin");
3439 lf.f = stdin;
3440 }
3441 else {
3442 lua_pushfstring(L, "@%s", filename);
3443#if defined(_MSC_VER)
3444 /* This code is here to stop a deprecation error that stops builds
3445 * if a certain macro is defined. While normally not caring would
3446 * be best, some header-only libraries and builds can't afford to
3447 * dictate this to the user. A quick check shows that fopen_s this
3448 * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
3449 * possibly even before that so we don't need to do any version
3450 * number checks, since this has been there since forever. */
3451
3452 /* TO USER: if you want the behavior of typical fopen_s/fopen,
3453 * which does lock the file on VC++, define the macro used below to 0 */
3454#if COMPAT53_FOPEN_NO_LOCK
3455 lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
3456 if (lf.f == NULL)
3457 return compat53_errfile(L, "open", fnameindex);
3458#else /* use default locking version */
3459 if (fopen_s(&lf.f, filename, "r") != 0)
3460 return compat53_errfile(L, "open", fnameindex);
3461#endif /* Locking vs. No-locking fopen variants */
3462#else
3463 lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
3464 if (lf.f == NULL)
3465 return compat53_errfile(L, "open", fnameindex);
3466#endif
3467 }
3468 if (compat53_skipcomment(&lf, &c)) /* read initial portion */
3469 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
3470 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
3471#if defined(_MSC_VER)
3472 if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
3473 return compat53_errfile(L, "reopen", fnameindex);
3474#else
3475 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
3476 if (lf.f == NULL)
3477 return compat53_errfile(L, "reopen", fnameindex);
3478#endif
3479 compat53_skipcomment(&lf, &c); /* re-read initial portion */
3480 }
3481 if (c != EOF)
3482 lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
3483 status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
3484 readstatus = ferror(lf.f);
3485 if (filename)
3486 fclose(lf.f); /* close file (even in case of errors) */
3487 if (readstatus) {
3488 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
3489 return compat53_errfile(L, "read", fnameindex);

Callers 4

script_fileFunction · 0.85
do_fileMethod · 0.85
unsafe_script_fileMethod · 0.85
load_fileMethod · 0.85

Calls 4

compat53_errfileFunction · 0.85
fopen_sFunction · 0.85
compat53_skipcommentFunction · 0.85
lua_loadFunction · 0.85

Tested by

no test coverage detected