add io_stream:seek

This commit is contained in:
Artem 2026-03-13 21:19:47 +03:00
parent 3c383a9277
commit 33f7815a21
5 changed files with 51 additions and 3 deletions

View file

@ -347,6 +347,41 @@ static int l_write_descriptor(lua::State* L) {
return 0;
}
static int l_seek_descriptor(lua::State* L) {
int descriptor = lua::tointeger(L, 1);
if (!scripting::descriptors_manager::has_descriptor(descriptor)) {
throw std::runtime_error("unknown descriptor");
}
std::string mode = lua::require_string(L, 2);
std::ios_base::seekdir dir;
switch (mode[0]) {
case 'b':
dir = std::ios_base::beg;
break;
case 'c':
dir = std::ios_base::cur;
break;
case 'e':
dir = std::ios_base::end;
break;
default:
throw std::runtime_error("invalid seek mode");
}
auto* stream = scripting::descriptors_manager::get_output(descriptor);
stream->seekp(lua::tointeger(L, 3), dir);
if (!stream->good()) {
throw std::runtime_error("failed to seek stream");
}
return 0;
}
static int l_flush_descriptor(lua::State* L) {
int descriptor = lua::tointeger(L, 1);
@ -406,9 +441,9 @@ const luaL_Reg filelib[] = {
{"__has_descriptor", lua::wrap<l_has_descriptor>},
{"__read_descriptor", lua::wrap<l_read_descriptor>},
{"__write_descriptor", lua::wrap<l_write_descriptor>},
{"__seek_descriptor", lua::wrap<l_seek_descriptor>},
{"__flush_descriptor", lua::wrap<l_flush_descriptor>},
{"__close_descriptor", lua::wrap<l_close_descriptor>},
{"__close_all_descriptors", lua::wrap<l_close_all_descriptors>},
{nullptr, nullptr}
};