[vlc-commits] lua: libs: Add a basic directory listing functionnality
Hugo Beauzée-Luyssen
git at videolan.org
Thu Apr 12 13:01:16 CEST 2018
vlc/vlc-3.0 | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Fri Apr 6 18:20:16 2018 +0200| [27a51b07c5ce7eff29ae48134d0870405becf718] | committer: Hugo Beauzée-Luyssen
lua: libs: Add a basic directory listing functionnality
(cherry picked from commit c0b1fbb0b8c2e5f71a20cd3c6ea547c5d39708ca)
(cherry picked from commit dbafe08dfa0e88942aff92cf0e70793fe305bbe3)
(cherry picked from commit 4b4b2df3dff27cc16156f9b5070853f0b9d966fb)
Signed-off-by: Hugo Beauzée-Luyssen <hugo at beauzee.fr>
> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=27a51b07c5ce7eff29ae48134d0870405becf718
---
modules/lua/libs/io.c | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/modules/lua/libs/io.c b/modules/lua/libs/io.c
index 67b92f36b5..73911ce074 100644
--- a/modules/lua/libs/io.c
+++ b/modules/lua/libs/io.c
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <errno.h>
+#include <sys/stat.h>
#include <vlc_common.h>
#include <vlc_fs.h>
@@ -189,6 +190,28 @@ static int vlclua_io_open( lua_State *L )
return 1;
}
+static int vlclua_io_readdir( lua_State *L )
+{
+ if( lua_gettop( L ) < 1 )
+ return luaL_error( L, "Usage: vlc.io.opendir(name)" );
+ const char* psz_path = luaL_checkstring( L, 1 );
+ DIR* p_dir = vlc_opendir( psz_path );
+ if ( p_dir == NULL )
+ return 0;
+
+ lua_newtable( L );
+ const char* psz_entry;
+ int idx = 1;
+ while ( ( psz_entry = vlc_readdir( p_dir ) ) != NULL )
+ {
+ lua_pushstring( L, psz_entry );
+ lua_rawseti( L, -2, idx );
+ idx++;
+ }
+ closedir( p_dir );
+ return 1;
+}
+
static int vlclua_mkdir( lua_State *L )
{
if( lua_gettop( L ) < 2 ) return vlclua_error( L );
@@ -198,13 +221,16 @@ static int vlclua_mkdir( lua_State *L )
if ( !psz_dir || !psz_mode )
return vlclua_error( L );
int i_res = vlc_mkdir( psz_dir, strtoul( psz_mode, NULL, 0 ) );
- lua_pushboolean( L, i_res == 0 || errno == EEXIST );
- return 1;
+ int i_err = i_res != 0 ? errno : 0;
+ lua_pushinteger( L, i_res );
+ lua_pushinteger( L, i_err );
+ return 2;
}
static const luaL_Reg vlclua_io_reg[] = {
{ "mkdir", vlclua_mkdir },
{ "open", vlclua_io_open },
+ { "readdir", vlclua_io_readdir },
{ NULL, NULL }
};
More information about the vlc-commits
mailing list