mirror of
https://git.do.srb2.org/STJr/SRB2.git
synced 2024-11-15 17:22:12 +00:00
Split open() into open() and openlocal()
open() for normal, consistency-friendly reading, openlocal() for writing and local reading.
This commit is contained in:
parent
e8760fe5dd
commit
3b22a84296
1 changed files with 62 additions and 60 deletions
|
@ -137,6 +137,14 @@ static int aux_close (lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_close (lua_State *L) {
|
||||||
|
if (lua_isnone(L, 1))
|
||||||
|
lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
|
||||||
|
tofile(L); /* make sure argument is a file */
|
||||||
|
return aux_close(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int io_gc (lua_State *L) {
|
static int io_gc (lua_State *L) {
|
||||||
FILE *f = *tofilep(L);
|
FILE *f = *tofilep(L);
|
||||||
/* ignore closed files */
|
/* ignore closed files */
|
||||||
|
@ -172,14 +180,11 @@ void MakePathDirs(char *path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int io_open (lua_State *L) {
|
static int CheckFileName(lua_State *L, const char *filename)
|
||||||
FILE **pf;
|
{
|
||||||
const char *filename = luaL_checkstring(L, 1);
|
int length = strlen(filename);
|
||||||
boolean pass = false;
|
boolean pass = false;
|
||||||
size_t i;
|
size_t i;
|
||||||
int length = strlen(filename);
|
|
||||||
const char *mode = luaL_optstring(L, 2, "r");
|
|
||||||
luafiletransfer_t *filetransfer;
|
|
||||||
|
|
||||||
if (strchr(filename, '\\'))
|
if (strchr(filename, '\\'))
|
||||||
{
|
{
|
||||||
|
@ -202,26 +207,41 @@ static int io_open (lua_State *L) {
|
||||||
return pushresult(L,0,filename);
|
return pushresult(L,0,filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
luaL_checktype(L, 4, LUA_TFUNCTION);
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int io_open (lua_State *L) {
|
||||||
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
|
const char *mode = luaL_optstring(L, 2, "r");
|
||||||
|
int checkresult;
|
||||||
|
|
||||||
|
checkresult = CheckFileName(L, filename);
|
||||||
|
if (checkresult)
|
||||||
|
return checkresult;
|
||||||
|
|
||||||
|
luaL_checktype(L, 3, LUA_TFUNCTION);
|
||||||
|
|
||||||
|
if (!(strchr(mode, 'r') || strchr(mode, '+')))
|
||||||
|
luaL_error(L, "open() is only for reading, use openlocal() for writing");
|
||||||
|
|
||||||
if (lua_isnil(L, 3) && (strchr(mode, 'r') || strchr(mode, '+'))) // Synched reading
|
|
||||||
{
|
|
||||||
AddLuaFileTransfer(filename, mode);
|
AddLuaFileTransfer(filename, mode);
|
||||||
|
|
||||||
/*pf = newfile(L);
|
|
||||||
*pf = fopen(realfilename, mode);
|
|
||||||
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;*/
|
|
||||||
}
|
|
||||||
else // Local I/O
|
|
||||||
{
|
|
||||||
char *realfilename = va("%s" PATHSEP "%s", luafiledir, filename);
|
|
||||||
player_t *player = *((player_t **)luaL_checkudata(L, 3, META_PLAYER));
|
|
||||||
|
|
||||||
if (!player)
|
|
||||||
return LUA_ErrInvalid(L, "player_t");
|
|
||||||
|
|
||||||
if (player != &players[consoleplayer])
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_openlocal (lua_State *L) {
|
||||||
|
FILE **pf;
|
||||||
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
|
const char *mode = luaL_optstring(L, 2, "r");
|
||||||
|
luafiletransfer_t *filetransfer;
|
||||||
|
int checkresult;
|
||||||
|
|
||||||
|
checkresult = CheckFileName(L, filename);
|
||||||
|
if (checkresult)
|
||||||
|
return checkresult;
|
||||||
|
|
||||||
|
char *realfilename = va("%s" PATHSEP "%s", luafiledir, filename);
|
||||||
|
|
||||||
if (client && strnicmp(filename, "shared/", strlen("shared/")))
|
if (client && strnicmp(filename, "shared/", strlen("shared/")))
|
||||||
I_Error("Access denied to %s\n"
|
I_Error("Access denied to %s\n"
|
||||||
|
@ -237,32 +257,10 @@ static int io_open (lua_State *L) {
|
||||||
|
|
||||||
MakePathDirs(realfilename);
|
MakePathDirs(realfilename);
|
||||||
|
|
||||||
// The callback is the last argument, no need to push it again
|
// Open and return the file
|
||||||
|
pf = newfile(L);
|
||||||
// Push the first argument (file handle) on the stack
|
*pf = fopen(realfilename, mode);
|
||||||
pf = newfile(gL); // Create and push the file handle
|
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
|
||||||
*pf = fopen(realfilename, mode); // Open the file
|
|
||||||
if (!*pf)
|
|
||||||
{
|
|
||||||
lua_pop(gL, 1);
|
|
||||||
lua_pushnil(gL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push the second argument (file name) on the stack
|
|
||||||
lua_pushstring(gL, filename);
|
|
||||||
|
|
||||||
// Call the callback
|
|
||||||
LUA_Call(gL, 2);
|
|
||||||
|
|
||||||
// Close the file
|
|
||||||
if (*pf)
|
|
||||||
{
|
|
||||||
fclose(*pf);
|
|
||||||
*pf = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0; // !!! Todo: error handling?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -335,7 +333,7 @@ void Got_LuaFile(UINT8 **cp, INT32 playernum)
|
||||||
void StoreLuaFileCallback(INT32 id)
|
void StoreLuaFileCallback(INT32 id)
|
||||||
{
|
{
|
||||||
lua_pushfstring(gL, FMT_FILECALLBACKID, id);
|
lua_pushfstring(gL, FMT_FILECALLBACKID, id);
|
||||||
lua_pushvalue(gL, 4); // Parameter 4 is the callback
|
lua_pushvalue(gL, 3); // Parameter 3 is the callback
|
||||||
lua_settable(gL, LUA_REGISTRYINDEX); // registry[callbackid] = callback
|
lua_settable(gL, LUA_REGISTRYINDEX); // registry[callbackid] = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,6 +345,7 @@ void RemoveLuaFileCallback(INT32 id)
|
||||||
lua_settable(gL, LUA_REGISTRYINDEX); // registry[callbackid] = nil
|
lua_settable(gL, LUA_REGISTRYINDEX); // registry[callbackid] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int io_tmpfile (lua_State *L) {
|
static int io_tmpfile (lua_State *L) {
|
||||||
FILE **pf = newfile(L);
|
FILE **pf = newfile(L);
|
||||||
*pf = tmpfile();
|
*pf = tmpfile();
|
||||||
|
@ -649,10 +648,12 @@ static int f_flush (lua_State *L) {
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg iolib[] = {
|
static const luaL_Reg iolib[] = {
|
||||||
|
{"close", io_close},
|
||||||
{"flush", io_flush},
|
{"flush", io_flush},
|
||||||
{"input", io_input},
|
{"input", io_input},
|
||||||
{"lines", io_lines},
|
{"lines", io_lines},
|
||||||
{"open", io_open},
|
{"open", io_open},
|
||||||
|
{"openlocal", io_openlocal},
|
||||||
{"output", io_output},
|
{"output", io_output},
|
||||||
{"read", io_read},
|
{"read", io_read},
|
||||||
{"tmpfile", io_tmpfile},
|
{"tmpfile", io_tmpfile},
|
||||||
|
@ -663,6 +664,7 @@ static const luaL_Reg iolib[] = {
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg flib[] = {
|
static const luaL_Reg flib[] = {
|
||||||
|
{"close", io_close},
|
||||||
{"flush", f_flush},
|
{"flush", f_flush},
|
||||||
{"lines", f_lines},
|
{"lines", f_lines},
|
||||||
{"read", f_read},
|
{"read", f_read},
|
||||||
|
|
Loading…
Reference in a new issue