28 #define METEO_VM2_SOURCE_FILTER \
29 "return function(q, l) " \
31 " for idx,item in pairs(l) do " \
33 " for k, v in pairs(q) do " \
34 " if (item[k] ~= v) then " \
40 " table.insert(res,idx) " \
51 const char* s = ::getenv(
"METEO_VM2_SOURCE");
53 return METEO_VM2_DEFAULT_SOURCE;
59 CoreSource::CoreSource(
const std::string& path, lua_State* L) : path(path), L(L), stations_ref(LUA_NOREF), variables_ref(LUA_NOREF), filter_ref(LUA_NOREF) {
60 if (luaL_dofile(L, path.c_str()) != 0) {
61 std::string msg = lua_tostring(L, -1);
63 throw std::runtime_error(
"Lua error while loading " + path +
": " + msg);
66 if (!lua_istable(L, -1))
67 throw std::runtime_error(
"Source file " + path +
" doesn't return a table");
69 lua_pushstring(L,
"stations");
71 stations_ref = luaL_ref(L, LUA_REGISTRYINDEX);
72 lua_pushstring(L,
"variables");
74 variables_ref = luaL_ref(L, LUA_REGISTRYINDEX);
76 if (luaL_dostring(L, METEO_VM2_SOURCE_FILTER) != 0) {
77 std::string msg = lua_tostring(L, -1);
79 throw std::runtime_error(
"Lua error while compiling filter: " + msg);
81 filter_ref = luaL_ref(L, LUA_REGISTRYINDEX);
86 luaL_unref(L, LUA_REGISTRYINDEX, stations_ref);
87 luaL_unref(L, LUA_REGISTRYINDEX, variables_ref);
88 luaL_unref(L, LUA_REGISTRYINDEX, filter_ref);
92 lua_rawgeti(L, LUA_REGISTRYINDEX, stations_ref);
93 lua_pushinteger(L,
id);
98 lua_rawgeti(L, LUA_REGISTRYINDEX, variables_ref);
99 lua_pushinteger(L,
id);
105 std::vector<int> res;
107 lua_rawgeti(L, LUA_REGISTRYINDEX, filter_ref);
108 lua_pushvalue(L, idx);
109 lua_rawgeti(L, LUA_REGISTRYINDEX, stations_ref);
110 if (lua_pcall(L, 2, 1, 0) != 0) {
111 std::string msg = lua_tostring(L, -1);
113 throw std::runtime_error(
"Lua error while filtering stations: " + msg);
116 while (lua_next(L, -2)) {
117 res.push_back(lua_tointeger(L, -1));
124 std::vector<int> res;
126 lua_rawgeti(L, LUA_REGISTRYINDEX, filter_ref);
127 lua_pushvalue(L, idx);
128 lua_rawgeti(L, LUA_REGISTRYINDEX, variables_ref);
129 if (lua_pcall(L, 2, 1, 0) != 0) {
130 std::string msg = lua_tostring(L, -1);
132 throw std::runtime_error(
"Lua error while filtering variables: " + msg);
135 while (lua_next(L, -2)) {
136 res.push_back(lua_tointeger(L, -1));
143 Source* Source::instance = NULL;
147 instance =
new Source(source::path());
152 Source::Source(
const std::string& path) {
void lua_push_station(int id)
std::vector< int > lua_find_stations(int idx)
std::vector< int > lua_find_variables(int idx)
void lua_push_variable(int id)
Reader for VM2 attributes file.
~CoreSource()
Unload the attributes.
std::vector< int > lua_find_variables(int idx)
List of station id matching the table at the given index.
void lua_push_variable(int id)
Push on top of the stack the variable attributes (or nil if not found)
CoreSource(const std::string &path, lua_State *L)
Load the attributes file path in Lua VM L.
std::vector< int > lua_find_stations(int idx)
List of station id matching the table at the given index.
void lua_push_station(int id)
Push on top of the stack the station attributes (or nil if not found)