15#include "physfsrwops.h"
86 return (
LuaFile_t*) lua_touserdata(L,ind);
99 luaL_typerror(L, ind, FILE_METATABLE);
113 luaL_getmetatable(L, FILE_METATABLE);
114 lua_setmetatable(L, -2);
128 if (lua_getmetatable(L,ind)==0)
130 lua_getfield(L, LUA_REGISTRYINDEX, FILE_METATABLE);
133 if (lua_rawequal(L, -1, -2))
149 if (lf->
rw != NULL) {
169 lua_pushboolean( L, (memcmp( f1, f2,
sizeof(
LuaFile_t) )==0) );
183 const char *str = luaL_checkstring(L,1);
184 strncpy( lf.
path, str,
sizeof(lf.
path)-1 );
203 const char *mode = luaL_optstring(L,2,
"r");
206 if (strcmp(mode,
"w")==0)
207 lf->
rw = PHYSFSRWOPS_openWrite( lf->
path );
208 else if (strcmp(mode,
"a")==0)
209 lf->
rw = PHYSFSRWOPS_openAppend( lf->
path );
211 lf->
rw = PHYSFSRWOPS_openRead( lf->
path );
212 if (lf->
rw == NULL) {
213 lua_pushboolean(L,0);
214 lua_pushstring(L, SDL_GetError());
218 lf->size = (size_t)SDL_RWsize(lf->
rw);
220 lua_pushboolean(L,1);
234 if (lf->
rw != NULL) {
239 lua_pushboolean(L,1);
259 NLUA_ERROR(L, _(
"file not open!"));
262 readlen = luaL_optinteger(L,2,SDL_RWsize(lf->
rw));
265 buf = malloc( readlen );
266 len = SDL_RWread( lf->
rw, buf, 1, readlen );
268 lua_pushlstring(L, buf, len);
269 lua_pushinteger(L,len);
285 size_t write, wrote, len;
290 NLUA_ERROR(L, _(
"file not open!"));
292 buf = luaL_checklstring(L,2,&len);
293 write = luaL_optlong(L,3,len);
295 wrote = SDL_RWwrite( lf->
rw, buf, 1, write );
296 if (wrote != write) {
297 lua_pushboolean(L,0);
298 lua_pushstring(L, SDL_GetError());
302 lua_pushboolean(L,1);
317 size_t pos = luaL_checkinteger(L,2);
320 if (lf->
rw == NULL) {
321 lua_pushboolean(L,1);
325 ret = SDL_RWseek( lf->
rw, pos, RW_SEEK_SET );
327 lua_pushboolean(L, ret>=0);
341 lua_pushstring(L, lf->
path);
355 lua_pushlstring(L, &lf->mode, 1);
369 lua_pushinteger(L, lf->size);
383 lua_pushboolean(L, lf->
rw!=NULL);
396 const char *path = luaL_checkstring(L,1);
397 PHYSFS_Stat path_stat;
399 if (!PHYSFS_stat( path, &path_stat )) {
404 if (path_stat.filetype == PHYSFS_FILETYPE_REGULAR)
405 lua_pushstring(L,
"file");
406 else if (path_stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
407 lua_pushstring(L,
"directory");
422 const char *path = luaL_checkstring(L,1);
423 int ret = PHYSFS_mkdir( path );
424 lua_pushboolean(L,ret==0);
438 const char *path = luaL_checkstring(L,1);
441 items = PHYSFS_enumerateFiles( path );
443 NLUA_ERROR(L,_(
"Directory '%s' enumerate error: %s"), path,
444 _(PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
445 for (
int i=0; items[i]!=NULL; i++) {
446 lua_pushstring(L,items[i]);
447 lua_rawseti(L,-2,i+1);
449 PHYSFS_freeList( items );
Header file with generic functions and naev-specifics.
static char buf[NEWS_MAX_LENGTH]
static int fileL_close(lua_State *L)
Closes a file.
static const luaL_Reg fileL_methods[]
static int fileL_seek(lua_State *L)
Seeks in an open file.
static int fileL_write(lua_State *L)
Reads from an open file.
LuaFile_t * lua_tofile(lua_State *L, int ind)
Lua bindings to interact with files.
static int fileL_size(lua_State *L)
Gets the size of a file (must be open).
static int fileL_enumerate(lua_State *L)
Returns a list of files and subdirectories of a directory.
static int fileL_new(lua_State *L)
Opens a new file.
int lua_isfile(lua_State *L, int ind)
Checks to see if ind is a file.
LuaFile_t * luaL_checkfile(lua_State *L, int ind)
Gets file at index or raises error if there is no file at index.
LuaFile_t * lua_pushfile(lua_State *L, LuaFile_t file)
Pushes a file on the stack.
static int fileL_eq(lua_State *L)
Compares two files to see if they are the same.
static int fileL_isopen(lua_State *L)
Checks to see if a file is open.
static int fileL_gc(lua_State *L)
Frees a file.
static int fileL_mkdir(lua_State *L)
Makes a directory.
static int fileL_open(lua_State *L)
Opens a File object.
static int fileL_read(lua_State *L)
Reads from an open file.
static int fileL_name(lua_State *L)
Gets the name of a file object.
int nlua_loadFile(nlua_env env)
Loads the file library.
static int fileL_mode(lua_State *L)
Gets the mode a file is currently in.
static int fileL_filetype(lua_State *L)
Checks to see the filetype of a path.