15#include "physfsrwops.h"
88 return (
LuaFile_t*) lua_touserdata(L,ind);
101 luaL_typerror(L, ind, FILE_METATABLE);
115 luaL_getmetatable(L, FILE_METATABLE);
116 lua_setmetatable(L, -2);
130 if (lua_getmetatable(L,ind)==0)
132 lua_getfield(L, LUA_REGISTRYINDEX, FILE_METATABLE);
135 if (lua_rawequal(L, -1, -2))
151 if (lf->
rw != NULL) {
171 lua_pushboolean( L, (memcmp( f1, f2,
sizeof(
LuaFile_t) )==0) );
185 const char *str = luaL_checkstring(L,1);
186 memset( &lf, 0,
sizeof(lf) );
187 strncpy( lf.
path, str,
sizeof(lf.
path)-1 );
205 const char *mode = luaL_optstring(L,2,
"r");
208 if (strcmp(mode,
"w")==0)
209 lf->
rw = PHYSFSRWOPS_openWrite( lf->
path );
210 else if (strcmp(mode,
"a")==0)
211 lf->
rw = PHYSFSRWOPS_openAppend( lf->
path );
213 lf->
rw = PHYSFSRWOPS_openRead( lf->
path );
214 if (lf->
rw == NULL) {
215 lua_pushboolean(L,0);
216 lua_pushstring(L, SDL_GetError());
220 lf->size = (size_t)SDL_RWsize(lf->
rw);
222 lua_pushboolean(L,1);
236 if (lf->
rw != NULL) {
241 lua_pushboolean(L,1);
261 return NLUA_ERROR(L, _(
"file not open!"));
264 readlen = luaL_optinteger(L,2,SDL_RWsize(lf->
rw));
267 buf = malloc( readlen );
268 len = SDL_RWread( lf->
rw, buf, 1, readlen );
270 lua_pushlstring(L, buf, len);
271 lua_pushinteger(L,len);
287 size_t write, wrote, len;
292 return NLUA_ERROR(L, _(
"file not open!"));
294 buf = luaL_checklstring(L,2,&len);
295 write = luaL_optlong(L,3,len);
297 wrote = SDL_RWwrite( lf->
rw, buf, 1, write );
298 if (wrote != write) {
299 lua_pushboolean(L,0);
300 lua_pushstring(L, SDL_GetError());
304 lua_pushboolean(L,1);
319 size_t pos = luaL_checkinteger(L,2);
322 if (lf->
rw == NULL) {
323 lua_pushboolean(L,1);
327 ret = SDL_RWseek( lf->
rw, pos, RW_SEEK_SET );
329 lua_pushboolean(L, ret>=0);
343 lua_pushstring(L, lf->
path);
357 lua_pushlstring(L, &lf->mode, 1);
371 lua_pushinteger(L, lf->size);
385 lua_pushboolean(L, lf->
rw!=NULL);
398 const char *path = luaL_checkstring(L,1);
399 PHYSFS_Stat path_stat;
401 if (!PHYSFS_stat( path, &path_stat )) {
406 if (path_stat.filetype == PHYSFS_FILETYPE_REGULAR)
407 lua_pushstring(L,
"file");
408 else if (path_stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
409 lua_pushstring(L,
"directory");
424 const char *path = luaL_checkstring(L,1);
425 int ret = PHYSFS_mkdir( path );
426 lua_pushboolean(L,ret);
428 lua_pushstring(L, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) );
444 const char *path = luaL_checkstring(L,1);
447 items = PHYSFS_enumerateFiles( path );
449 return NLUA_ERROR(L,_(
"Directory '%s' enumerate error: %s"), path,
450 _(PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
451 for (
int i=0; items[i]!=NULL; i++) {
452 lua_pushstring(L,items[i]);
453 lua_rawseti(L,-2,i+1);
455 PHYSFS_freeList( items );
468 const char *path = luaL_checkstring(L,1);
469 int ret = PHYSFS_delete( path );
470 lua_pushboolean(L,ret);
472 lua_pushstring(L, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) );
Header file with generic functions and naev-specifics.
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_remove(lua_State *L)
Removes a file or directory.
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.