2015-01-27 11:44:46 -05:00
|
|
|
#ifndef LUA_UTIL_HPP
|
|
|
|
#define LUA_UTIL_HPP
|
|
|
|
|
2020-11-26 10:21:39 -05:00
|
|
|
extern "C"
|
|
|
|
{
|
2015-01-27 11:44:46 -05:00
|
|
|
#include <lauxlib.h>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <lua.h>
|
2015-01-27 11:44:46 -05:00
|
|
|
#include <lualib.h>
|
|
|
|
}
|
|
|
|
|
2024-06-20 15:44:28 -04:00
|
|
|
#include <filesystem>
|
2015-01-27 11:44:46 -05:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2022-12-11 04:10:26 -05:00
|
|
|
namespace osrm::util
|
2016-01-05 10:51:13 -05:00
|
|
|
{
|
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
// Add the folder contain the script to the lua load path, so script can easily require() other lua
|
|
|
|
// scripts inside that folder, or subfolders.
|
|
|
|
// See http://lua-users.org/wiki/PackagePath for details on the package.path syntax.
|
|
|
|
inline void luaAddScriptFolderToLoadPath(lua_State *lua_state, const char *file_name)
|
|
|
|
{
|
2024-06-20 15:44:28 -04:00
|
|
|
std::filesystem::path profile_path = std::filesystem::canonical(file_name);
|
2016-05-05 05:19:27 -04:00
|
|
|
std::string folder = profile_path.parent_path().generic_string();
|
2015-05-15 09:30:41 -04:00
|
|
|
const std::string lua_code = "package.path = \"" + folder + "/?.lua;\" .. package.path";
|
2015-01-27 11:44:46 -05:00
|
|
|
luaL_dostring(lua_state, lua_code.c_str());
|
|
|
|
}
|
2022-12-20 12:00:11 -05:00
|
|
|
} // namespace osrm::util
|
2016-01-05 10:51:13 -05:00
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
#endif // LUA_UTIL_HPP
|