mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-12 06:21:50 +00:00
commit
e6f0bb38f4
6 changed files with 63 additions and 3 deletions
|
|
@ -137,6 +137,18 @@ io_stream:write_line(string)
|
||||||
io_stream:read_fully(
|
io_stream:read_fully(
|
||||||
[опционально] useTable: boolean
|
[опционально] useTable: boolean
|
||||||
) -> Bytearray | table<number> | table<string> | string
|
) -> Bytearray | table<number> | table<string> | string
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Устанавливает позицию в потоке
|
||||||
|
Режимы:
|
||||||
|
b - Задаёт позицию относительно начало файла
|
||||||
|
c - Задаёт позицию относительно текущей позиции
|
||||||
|
e - Задаёт позицию относительно конца файла
|
||||||
|
--]]
|
||||||
|
io_stream:seek(
|
||||||
|
mode: string
|
||||||
|
offset: number
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Методы Buffered-режима
|
## Методы Buffered-режима
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ local io_stream = require "core:io_stream"
|
||||||
local lib = {
|
local lib = {
|
||||||
read = file.__read_descriptor,
|
read = file.__read_descriptor,
|
||||||
write = file.__write_descriptor,
|
write = file.__write_descriptor,
|
||||||
|
seek = file.__seek_descriptor,
|
||||||
flush = file.__flush_descriptor,
|
flush = file.__flush_descriptor,
|
||||||
is_alive = file.__has_descriptor,
|
is_alive = file.__has_descriptor,
|
||||||
close = file.__close_descriptor
|
close = file.__close_descriptor
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,10 @@ function lib.write(fd, bytearray)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function lib.seek(fd, mode, offset)
|
||||||
|
error("cannot seek the named pipe")
|
||||||
|
end
|
||||||
|
|
||||||
function lib.flush(fd)
|
function lib.flush(fd)
|
||||||
-- no flush on unix
|
-- no flush on unix
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ function lib.read(handle, len)
|
||||||
|
|
||||||
if not has_data then
|
if not has_data then
|
||||||
return out
|
return out
|
||||||
elseif hasData == -1 then
|
elseif has_data == -1 then
|
||||||
error("failed to read from named pipe: "..tostring(C.GetLastError()))
|
error("failed to read from named pipe: "..tostring(C.GetLastError()))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -84,8 +84,8 @@ end
|
||||||
|
|
||||||
function lib.write(handle, bytearray)
|
function lib.write(handle, bytearray)
|
||||||
local len = #bytearray
|
local len = #bytearray
|
||||||
|
|
||||||
local buffer = FFI.new("uint8_t[?]", len)
|
local buffer = FFI.new("uint8_t[?]", len)
|
||||||
|
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
buffer[i-1] = bytearray[i]
|
buffer[i-1] = bytearray[i]
|
||||||
end
|
end
|
||||||
|
|
@ -97,6 +97,10 @@ function lib.write(handle, bytearray)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function lib.seek(handle, mode, offset)
|
||||||
|
error("cannot seek the named pipe")
|
||||||
|
end
|
||||||
|
|
||||||
function lib.flush(handle)
|
function lib.flush(handle)
|
||||||
C.FlushFileBuffers(handle)
|
C.FlushFileBuffers(handle)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -369,6 +369,10 @@ function io_stream:write(arg, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function io_stream:seek(mode, offset)
|
||||||
|
self.ioLib.seek(self.descriptor, mode, offset)
|
||||||
|
end
|
||||||
|
|
||||||
function io_stream:is_alive()
|
function io_stream:is_alive()
|
||||||
return self.ioLib.is_alive(self.descriptor)
|
return self.ioLib.is_alive(self.descriptor)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -347,6 +347,41 @@ static int l_write_descriptor(lua::State* L) {
|
||||||
return 0;
|
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) {
|
static int l_flush_descriptor(lua::State* L) {
|
||||||
int descriptor = lua::tointeger(L, 1);
|
int descriptor = lua::tointeger(L, 1);
|
||||||
|
|
||||||
|
|
@ -406,9 +441,9 @@ const luaL_Reg filelib[] = {
|
||||||
{"__has_descriptor", lua::wrap<l_has_descriptor>},
|
{"__has_descriptor", lua::wrap<l_has_descriptor>},
|
||||||
{"__read_descriptor", lua::wrap<l_read_descriptor>},
|
{"__read_descriptor", lua::wrap<l_read_descriptor>},
|
||||||
{"__write_descriptor", lua::wrap<l_write_descriptor>},
|
{"__write_descriptor", lua::wrap<l_write_descriptor>},
|
||||||
|
{"__seek_descriptor", lua::wrap<l_seek_descriptor>},
|
||||||
{"__flush_descriptor", lua::wrap<l_flush_descriptor>},
|
{"__flush_descriptor", lua::wrap<l_flush_descriptor>},
|
||||||
{"__close_descriptor", lua::wrap<l_close_descriptor>},
|
{"__close_descriptor", lua::wrap<l_close_descriptor>},
|
||||||
{"__close_all_descriptors", lua::wrap<l_close_all_descriptors>},
|
{"__close_all_descriptors", lua::wrap<l_close_all_descriptors>},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue