13#include "physfsrwops.h"
85 return (
LuaFile_t *)lua_touserdata( L, ind );
98 luaL_typerror( L, ind, FILE_METATABLE );
112 luaL_getmetatable( L, FILE_METATABLE );
113 lua_setmetatable( L, -2 );
127 if ( lua_getmetatable( L, ind ) == 0 )
129 lua_getfield( L, LUA_REGISTRYINDEX, FILE_METATABLE );
132 if ( lua_rawequal( L, -1, -2 ) )
148 if ( lf->
rw != NULL ) {
149 SDL_RWclose( lf->
rw );
168 lua_pushboolean( L, ( memcmp( f1, f2,
sizeof(
LuaFile_t ) ) == 0 ) );
182 const char *str = luaL_checkstring( L, 1 );
183 memset( &lf, 0,
sizeof( lf ) );
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 ) {
235 SDL_RWclose( lf->
rw );
239 lua_pushboolean( L, 1 );
258 if ( lf->
rw == NULL )
259 return 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;
288 if ( lf->
rw == NULL )
289 return NLUA_ERROR( L, _(
"file not open!" ) );
291 buf = luaL_checklstring( L, 2, &len );
292 write = luaL_optlong( L, 3, len );
294 wrote = SDL_RWwrite( lf->
rw, buf, 1, write );
295 if ( wrote != write ) {
296 lua_pushboolean( L, 0 );
297 lua_pushstring( L, SDL_GetError() );
301 lua_pushboolean( L, 1 );
316 size_t pos = luaL_checkinteger( L, 2 );
319 if ( lf->
rw == NULL ) {
320 lua_pushboolean( L, 1 );
324 ret = SDL_RWseek( lf->
rw, pos, RW_SEEK_SET );
326 lua_pushboolean( L, ret >= 0 );
340 lua_pushstring( L, lf->
path );
354 lua_pushlstring( L, &lf->mode, 1 );
368 lua_pushinteger( L, lf->size );
382 lua_pushboolean( L, lf->
rw != NULL );
395 const char *path = luaL_checkstring( L, 1 );
396 PHYSFS_Stat path_stat;
398 if ( !PHYSFS_stat( path, &path_stat ) ) {
403 if ( path_stat.filetype == PHYSFS_FILETYPE_REGULAR )
404 lua_pushstring( L,
"file" );
405 else if ( path_stat.filetype == PHYSFS_FILETYPE_DIRECTORY )
406 lua_pushstring( L,
"directory" );
421 const char *path = luaL_checkstring( L, 1 );
422 int ret = PHYSFS_mkdir( path );
423 lua_pushboolean( L, ret );
425 lua_pushstring( L, PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) );
442 const char *path = luaL_checkstring( L, 1 );
445 items = PHYSFS_enumerateFiles( path );
448 L, _(
"Directory '%s' enumerate error: %s" ), path,
449 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
450 for (
int i = 0; items[i] != NULL; i++ ) {
451 lua_pushstring( L, items[i] );
452 lua_rawseti( L, -2, i + 1 );
454 PHYSFS_freeList( items );
467 const char *path = luaL_checkstring( L, 1 );
468 int ret = PHYSFS_delete( path );
469 lua_pushboolean( L, ret );
471 lua_pushstring( L, PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) );
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.