Skip to content

Commit 698e87a

Browse files
committed
Move Lua setup code into its own file
1 parent cdcaa99 commit 698e87a

4 files changed

Lines changed: 62 additions & 19 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ if (WITH_LUA)
5858
flex-lua-table.cpp
5959
flex-write.cpp
6060
geom-transform.cpp
61+
lua-setup.cpp
6162
lua-utils.cpp
6263
output-flex.cpp
6364
tagtransform-lua.cpp

src/lua-setup.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2023 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include "lua-setup.hpp"
11+
#include "lua-utils.hpp"
12+
#include "version.hpp"
13+
14+
#include <lua.hpp>
15+
16+
#include <boost/filesystem.hpp>
17+
18+
void setup_lua_environment(lua_State *lua_state, std::string const &filename,
19+
bool append_mode)
20+
{
21+
// Set up global lua libs
22+
luaL_openlibs(lua_state);
23+
24+
// Set up global "osm2pgsql" object
25+
lua_newtable(lua_state);
26+
lua_pushvalue(lua_state, -1);
27+
lua_setglobal(lua_state, "osm2pgsql");
28+
29+
luaX_add_table_str(lua_state, "version", get_osm2pgsql_short_version());
30+
31+
std::string dir_path =
32+
boost::filesystem::path{filename}.parent_path().string();
33+
if (!dir_path.empty()) {
34+
dir_path += boost::filesystem::path::preferred_separator;
35+
}
36+
luaX_add_table_str(lua_state, "config_dir", dir_path.c_str());
37+
38+
luaX_add_table_str(lua_state, "mode", append_mode ? "append" : "create");
39+
}

src/lua-setup.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef OSM2PGSQL_LUA_CONFIG_HPP
2+
#define OSM2PGSQL_LUA_CONFIG_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2023 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include <string>
14+
15+
struct lua_State;
16+
17+
void setup_lua_environment(lua_State *lua_state, std::string const &filename,
18+
bool append_mode);
19+
20+
#endif // OSM2PGSQL_LUA_CONFIG_HPP

src/output-flex.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "geom-transform.hpp"
2424
#include "logging.hpp"
2525
#include "lua-init.hpp"
26+
#include "lua-setup.hpp"
2627
#include "lua-utils.hpp"
2728
#include "middle.hpp"
2829
#include "options.hpp"
@@ -44,8 +45,6 @@ extern "C"
4445
#include <lualib.h>
4546
}
4647

47-
#include <boost/filesystem.hpp>
48-
4948
#include <cassert>
5049
#include <cstdlib>
5150
#include <cstring>
@@ -1439,32 +1438,16 @@ void output_flex_t::init_lua(std::string const &filename)
14391438
m_lua_state.reset(luaL_newstate(),
14401439
[](lua_State *state) { lua_close(state); });
14411440

1442-
// Set up global lua libs
1443-
luaL_openlibs(lua_state());
1444-
1445-
// Set up global "osm2pgsql" object
1446-
lua_newtable(lua_state());
1441+
setup_lua_environment(lua_state(), filename, get_options()->append);
14471442

1448-
luaX_add_table_str(lua_state(), "version", get_osm2pgsql_short_version());
1449-
luaX_add_table_str(lua_state(), "mode",
1450-
get_options()->append ? "append" : "create");
14511443
luaX_add_table_int(lua_state(), "stage", 1);
14521444

1453-
std::string dir_path =
1454-
boost::filesystem::path{filename}.parent_path().string();
1455-
if (!dir_path.empty()) {
1456-
dir_path += boost::filesystem::path::preferred_separator;
1457-
}
1458-
luaX_add_table_str(lua_state(), "config_dir", dir_path.c_str());
1459-
14601445
luaX_add_table_func(lua_state(), "define_table",
14611446
lua_trampoline_app_define_table);
14621447

14631448
luaX_add_table_func(lua_state(), "define_expire_output",
14641449
lua_trampoline_app_define_expire_output);
14651450

1466-
lua_setglobal(lua_state(), "osm2pgsql");
1467-
14681451
init_table_class(lua_state());
14691452
init_expire_output_class(lua_state());
14701453

0 commit comments

Comments
 (0)