Skip to content

Commit

Permalink
AP_Scripting: added isdirectory()
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Dec 4, 2023
1 parent 423c8fd commit 240ede6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libraries/AP_Scripting/docs/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3062,6 +3062,11 @@ function scripting:restart_all() end
---@return table -- table of filenames
function dirlist(directoryname) end

-- return true if path is a directory
---@param path string
---@return result
function isdirectory(path) end

--desc
---@param filename string
function remove(filename) end
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ userdata uint32_t manual tofloat uint32_t_tofloat 0

global manual dirlist lua_dirlist 1
global manual remove lua_removefile 1
global manual isdirectory lua_isdirectory 1
global manual print lua_print 1

singleton mavlink depends HAL_GCS_ENABLED
Expand Down
19 changes: 19 additions & 0 deletions libraries/AP_Scripting/lua_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,25 @@ int lua_dirlist(lua_State *L) {
return 1; /* table is already on top */
}

/*
return true if path is a directory
*/
int lua_isdirectory(lua_State *L) {
binding_argcheck(L, 1);

const char *path = luaL_checkstring(L, 1);

struct stat st;
if (AP::FS().stat(path, &st) != 0) {
lua_pushnil(L); /* return nil and ... */
lua_pushstring(L, strerror(errno)); /* error message */
return 2;
}
bool ret = (st.st_mode & S_IFMT) == S_IFDIR;
lua_pushboolean(L, ret);
return 1;
}

/*
remove a file
*/
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/lua_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ int lua_get_CAN_device(lua_State *L);
int lua_get_CAN_device2(lua_State *L);
int lua_dirlist(lua_State *L);
int lua_removefile(lua_State *L);
int lua_isdirectory(lua_State *L);
int SRV_Channels_get_safety_state(lua_State *L);
int lua_get_PWMSource(lua_State *L);
int lua_get_SocketAPM(lua_State *L);
Expand Down

0 comments on commit 240ede6

Please sign in to comment.