1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 12:53:51 +00:00
openmw-tes3mp/apps/openmw-mp/Script/luaUtils.cpp

31 lines
802 B
C++

//
// Created by koncord on 03.08.17.
//
#include <boost/tokenizer.hpp>
#include "luaUtils.hpp"
#include "LuaState.hpp"
sol::table str_split(const std::string &str, const sol::object &sepObj, sol::this_state state)
{
sol::state_view lua(state);
sol::table tbl = lua.create_table();
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(sepObj.valid() ? sepObj.as<const char *>() : ":");
tokenizer tokens(str, sep);
for (const auto &token : tokens)
tbl.add(token);
return tbl;
}
void LuaUtils::Init(LuaState &lua)
{
sol::table strTable = (*lua.getState())["string"];
if (!strTable.valid())
throw std::runtime_error("string library not initialized");
strTable.set_function("split", &str_split);
}