2017-10-31 01:47:55 -07:00
|
|
|
const std = @import("index.zig");
|
2017-05-01 10:12:38 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Os = builtin.Os;
|
2017-10-31 01:47:55 -07:00
|
|
|
const c = std.c;
|
2016-09-11 21:01:06 -07:00
|
|
|
|
2017-10-31 01:47:55 -07:00
|
|
|
const math = std.math;
|
|
|
|
const debug = std.debug;
|
2016-08-17 20:11:04 -07:00
|
|
|
const assert = debug.assert;
|
2017-10-31 01:47:55 -07:00
|
|
|
const os = std.os;
|
|
|
|
const mem = std.mem;
|
|
|
|
const Buffer = std.Buffer;
|
|
|
|
const fmt = std.fmt;
|
2018-02-10 17:55:13 -08:00
|
|
|
const File = std.os.File;
|
2015-12-14 23:07:51 -08:00
|
|
|
|
2017-06-04 07:08:55 -07:00
|
|
|
const is_posix = builtin.os != builtin.Os.windows;
|
|
|
|
const is_windows = builtin.os == builtin.Os.windows;
|
|
|
|
|
2018-02-01 07:23:25 -08:00
|
|
|
const GetStdIoErrs = os.WindowsGetStdHandleErrs;
|
|
|
|
|
|
|
|
pub fn getStdErr() GetStdIoErrs!File {
|
2017-12-21 21:50:30 -08:00
|
|
|
const handle = if (is_windows)
|
2018-02-10 17:55:13 -08:00
|
|
|
try os.windowsGetStdHandle(os.windows.STD_ERROR_HANDLE)
|
2017-12-21 21:50:30 -08:00
|
|
|
else if (is_posix)
|
2018-02-10 17:55:13 -08:00
|
|
|
os.posix.STDERR_FILENO
|
2017-12-21 21:50:30 -08:00
|
|
|
else
|
|
|
|
unreachable;
|
2017-10-31 01:47:55 -07:00
|
|
|
return File.openHandle(handle);
|
|
|
|
}
|
2017-04-24 09:01:19 -07:00
|
|
|
|
2018-02-01 07:23:25 -08:00
|
|
|
pub fn getStdOut() GetStdIoErrs!File {
|
2017-12-21 21:50:30 -08:00
|
|
|
const handle = if (is_windows)
|
2018-02-10 17:55:13 -08:00
|
|
|
try os.windowsGetStdHandle(os.windows.STD_OUTPUT_HANDLE)
|
2017-12-21 21:50:30 -08:00
|
|
|
else if (is_posix)
|
2018-02-10 17:55:13 -08:00
|
|
|
os.posix.STDOUT_FILENO
|
2017-12-21 21:50:30 -08:00
|
|
|
else
|
|
|
|
unreachable;
|
2017-10-31 01:47:55 -07:00
|
|
|
return File.openHandle(handle);
|
|
|
|
}
|
2017-06-04 07:08:55 -07:00
|
|
|
|
2018-02-01 07:23:25 -08:00
|
|
|
pub fn getStdIn() GetStdIoErrs!File {
|
2017-12-21 21:50:30 -08:00
|
|
|
const handle = if (is_windows)
|
2018-02-10 17:55:13 -08:00
|
|
|
try os.windowsGetStdHandle(os.windows.STD_INPUT_HANDLE)
|
2017-12-21 21:50:30 -08:00
|
|
|
else if (is_posix)
|
2018-02-10 17:55:13 -08:00
|
|
|
os.posix.STDIN_FILENO
|
2017-12-21 21:50:30 -08:00
|
|
|
else
|
|
|
|
unreachable;
|
2017-10-31 01:47:55 -07:00
|
|
|
return File.openHandle(handle);
|
|
|
|
}
|
2017-06-04 07:08:55 -07:00
|
|
|
|
2017-11-07 00:22:27 -08:00
|
|
|
/// Implementation of InStream trait for File
|
|
|
|
pub const FileInStream = struct {
|
|
|
|
file: &File,
|
2018-02-05 04:38:24 -08:00
|
|
|
stream: Stream,
|
|
|
|
|
|
|
|
pub const Error = @typeOf(File.read).ReturnType.ErrorSet;
|
|
|
|
pub const Stream = InStream(Error);
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn init(file: &File) FileInStream {
|
2017-11-07 00:22:27 -08:00
|
|
|
return FileInStream {
|
|
|
|
.file = file,
|
2018-02-05 04:38:24 -08:00
|
|
|
.stream = Stream {
|
2017-11-07 00:22:27 -08:00
|
|
|
.readFn = readFn,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
fn readFn(in_stream: &Stream, buffer: []u8) Error!usize {
|
2017-11-07 00:22:27 -08:00
|
|
|
const self = @fieldParentPtr(FileInStream, "stream", in_stream);
|
|
|
|
return self.file.read(buffer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Implementation of OutStream trait for File
|
|
|
|
pub const FileOutStream = struct {
|
|
|
|
file: &File,
|
2018-02-05 04:38:24 -08:00
|
|
|
stream: Stream,
|
|
|
|
|
|
|
|
pub const Error = File.WriteError;
|
|
|
|
pub const Stream = OutStream(Error);
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn init(file: &File) FileOutStream {
|
2017-11-07 00:22:27 -08:00
|
|
|
return FileOutStream {
|
|
|
|
.file = file,
|
2018-02-05 04:38:24 -08:00
|
|
|
.stream = Stream {
|
2017-11-07 00:22:27 -08:00
|
|
|
.writeFn = writeFn,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
|
2017-11-07 00:22:27 -08:00
|
|
|
const self = @fieldParentPtr(FileOutStream, "stream", out_stream);
|
|
|
|
return self.file.write(bytes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-10 17:55:13 -08:00
|
|
|
pub fn InStream(comptime ReadError: type) type {
|
2018-02-05 04:38:24 -08:00
|
|
|
return struct {
|
|
|
|
const Self = this;
|
2018-02-10 17:55:13 -08:00
|
|
|
pub const Error = ReadError;
|
2018-02-05 04:38:24 -08:00
|
|
|
|
|
|
|
/// Return the number of bytes read. If the number read is smaller than buf.len, it
|
|
|
|
/// means the stream reached the end. Reaching the end of a stream is not an error
|
|
|
|
/// condition.
|
|
|
|
readFn: fn(self: &Self, buffer: []u8) Error!usize,
|
|
|
|
|
|
|
|
/// Replaces `buffer` contents by reading from the stream until it is finished.
|
|
|
|
/// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and
|
|
|
|
/// the contents read from the stream are lost.
|
|
|
|
pub fn readAllBuffer(self: &Self, buffer: &Buffer, max_size: usize) !void {
|
|
|
|
try buffer.resize(0);
|
|
|
|
|
|
|
|
var actual_buf_len: usize = 0;
|
|
|
|
while (true) {
|
|
|
|
const dest_slice = buffer.toSlice()[actual_buf_len..];
|
|
|
|
const bytes_read = try self.readFn(self, dest_slice);
|
|
|
|
actual_buf_len += bytes_read;
|
|
|
|
|
|
|
|
if (bytes_read != dest_slice.len) {
|
|
|
|
buffer.shrink(actual_buf_len);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-28 00:07:11 -08:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
const new_buf_size = math.min(max_size, actual_buf_len + os.page_size);
|
|
|
|
if (new_buf_size == actual_buf_len)
|
|
|
|
return error.StreamTooLong;
|
|
|
|
try buffer.resize(new_buf_size);
|
|
|
|
}
|
2017-02-28 00:07:11 -08:00
|
|
|
}
|
2017-04-24 09:01:19 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Allocates enough memory to hold all the contents of the stream. If the allocated
|
|
|
|
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
|
|
|
|
/// Caller owns returned memory.
|
|
|
|
/// If this function returns an error, the contents from the stream read so far are lost.
|
|
|
|
pub fn readAllAlloc(self: &Self, allocator: &mem.Allocator, max_size: usize) ![]u8 {
|
|
|
|
var buf = Buffer.initNull(allocator);
|
|
|
|
defer buf.deinit();
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
try self.readAllBuffer(&buf, max_size);
|
|
|
|
return buf.toOwnedSlice();
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Replaces `buffer` contents by reading from the stream until `delimiter` is found.
|
|
|
|
/// Does not include the delimiter in the result.
|
|
|
|
/// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents
|
|
|
|
/// read from the stream so far are lost.
|
|
|
|
pub fn readUntilDelimiterBuffer(self: &Self, buffer: &Buffer, delimiter: u8, max_size: usize) !void {
|
2018-03-31 09:21:19 -07:00
|
|
|
try buffer.resize(0);
|
2017-09-10 08:10:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
while (true) {
|
|
|
|
var byte: u8 = try self.readByte();
|
2017-09-10 08:10:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
if (byte == delimiter) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-10 08:10:55 -07:00
|
|
|
|
2018-03-31 09:21:19 -07:00
|
|
|
if (buffer.len() == max_size) {
|
2018-02-05 04:38:24 -08:00
|
|
|
return error.StreamTooLong;
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-03-31 09:21:19 -07:00
|
|
|
try buffer.appendByte(byte);
|
2018-02-05 04:38:24 -08:00
|
|
|
}
|
2017-06-04 07:08:55 -07:00
|
|
|
}
|
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Allocates enough memory to read until `delimiter`. If the allocated
|
|
|
|
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
|
|
|
|
/// Caller owns returned memory.
|
|
|
|
/// If this function returns an error, the contents from the stream read so far are lost.
|
|
|
|
pub fn readUntilDelimiterAlloc(self: &Self, allocator: &mem.Allocator,
|
|
|
|
delimiter: u8, max_size: usize) ![]u8
|
|
|
|
{
|
|
|
|
var buf = Buffer.initNull(allocator);
|
|
|
|
defer buf.deinit();
|
|
|
|
|
2018-03-31 09:21:19 -07:00
|
|
|
try self.readUntilDelimiterBuffer(&buf, delimiter, max_size);
|
2018-02-05 04:38:24 -08:00
|
|
|
return buf.toOwnedSlice();
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Returns the number of bytes read. If the number read is smaller than buf.len, it
|
|
|
|
/// means the stream reached the end. Reaching the end of a stream is not an error
|
|
|
|
/// condition.
|
|
|
|
pub fn read(self: &Self, buffer: []u8) !usize {
|
|
|
|
return self.readFn(self, buffer);
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Same as `read` but end of stream returns `error.EndOfStream`.
|
|
|
|
pub fn readNoEof(self: &Self, buf: []u8) !void {
|
|
|
|
const amt_read = try self.read(buf);
|
|
|
|
if (amt_read < buf.len) return error.EndOfStream;
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Reads 1 byte from the stream or returns `error.EndOfStream`.
|
|
|
|
pub fn readByte(self: &Self) !u8 {
|
|
|
|
var result: [1]u8 = undefined;
|
|
|
|
try self.readNoEof(result[0..]);
|
|
|
|
return result[0];
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
/// Same as `readByte` except the returned byte is signed.
|
|
|
|
pub fn readByteSigned(self: &Self) !i8 {
|
|
|
|
return @bitCast(i8, try self.readByte());
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn readIntLe(self: &Self, comptime T: type) !T {
|
|
|
|
return self.readInt(builtin.Endian.Little, T);
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn readIntBe(self: &Self, comptime T: type) !T {
|
|
|
|
return self.readInt(builtin.Endian.Big, T);
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn readInt(self: &Self, endian: builtin.Endian, comptime T: type) !T {
|
|
|
|
var bytes: [@sizeOf(T)]u8 = undefined;
|
|
|
|
try self.readNoEof(bytes[0..]);
|
|
|
|
return mem.readInt(bytes, T, endian);
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn readVarInt(self: &Self, endian: builtin.Endian, comptime T: type, size: usize) !T {
|
|
|
|
assert(size <= @sizeOf(T));
|
|
|
|
assert(size <= 8);
|
|
|
|
var input_buf: [8]u8 = undefined;
|
|
|
|
const input_slice = input_buf[0..size];
|
|
|
|
try self.readNoEof(input_slice);
|
|
|
|
return mem.readInt(input_slice, T, endian);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-10 17:55:13 -08:00
|
|
|
pub fn OutStream(comptime WriteError: type) type {
|
2018-02-05 04:38:24 -08:00
|
|
|
return struct {
|
|
|
|
const Self = this;
|
2018-02-10 17:55:13 -08:00
|
|
|
pub const Error = WriteError;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
writeFn: fn(self: &Self, bytes: []const u8) Error!void,
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn print(self: &Self, comptime format: []const u8, args: ...) !void {
|
2018-02-09 10:49:58 -08:00
|
|
|
return std.fmt.format(self, Error, self.writeFn, format, args);
|
2018-02-05 04:38:24 -08:00
|
|
|
}
|
2017-04-03 01:58:19 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn write(self: &Self, bytes: []const u8) !void {
|
|
|
|
return self.writeFn(self, bytes);
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn writeByte(self: &Self, byte: u8) !void {
|
|
|
|
const slice = (&byte)[0..1];
|
|
|
|
return self.writeFn(self, slice);
|
|
|
|
}
|
2017-12-11 06:21:06 -08:00
|
|
|
|
2018-02-05 04:38:24 -08:00
|
|
|
pub fn writeByteNTimes(self: &Self, byte: u8, n: usize) !void {
|
|
|
|
const slice = (&byte)[0..1];
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < n) : (i += 1) {
|
|
|
|
try self.writeFn(self, slice);
|
|
|
|
}
|
2017-12-11 06:21:06 -08:00
|
|
|
}
|
2018-02-05 04:38:24 -08:00
|
|
|
};
|
|
|
|
}
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-09 15:27:50 -08:00
|
|
|
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
|
|
|
|
pub fn writeFile(allocator: &mem.Allocator, path: []const u8, data: []const u8) !void {
|
|
|
|
var file = try File.openWrite(allocator, path);
|
2017-11-07 00:22:27 -08:00
|
|
|
defer file.close();
|
2018-01-07 13:51:46 -08:00
|
|
|
try file.write(data);
|
2017-11-07 00:22:27 -08:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:05:27 -08:00
|
|
|
/// On success, caller owns returned buffer.
|
2018-02-09 15:27:50 -08:00
|
|
|
pub fn readFileAlloc(allocator: &mem.Allocator, path: []const u8) ![]u8 {
|
|
|
|
var file = try File.openRead(allocator, path);
|
2017-12-04 19:05:27 -08:00
|
|
|
defer file.close();
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
const size = try file.getEndPos();
|
2018-02-10 11:52:39 -08:00
|
|
|
const buf = try allocator.alloc(u8, size);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(buf);
|
2017-12-04 19:05:27 -08:00
|
|
|
|
|
|
|
var adapter = FileInStream.init(&file);
|
2018-01-07 13:51:46 -08:00
|
|
|
try adapter.stream.readNoEof(buf[0..size]);
|
2017-12-04 19:05:27 -08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub fn BufferedInStream(comptime Error: type) type {
|
|
|
|
return BufferedInStreamCustom(os.page_size, Error);
|
|
|
|
}
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) type {
|
2017-11-07 00:22:27 -08:00
|
|
|
return struct {
|
|
|
|
const Self = this;
|
2018-02-05 15:09:13 -08:00
|
|
|
const Stream = InStream(Error);
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub stream: Stream,
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
unbuffered_in_stream: &Stream,
|
2017-11-07 00:22:27 -08:00
|
|
|
|
|
|
|
buffer: [buffer_size]u8,
|
|
|
|
start_index: usize,
|
|
|
|
end_index: usize,
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub fn init(unbuffered_in_stream: &Stream) Self {
|
2017-11-07 00:22:27 -08:00
|
|
|
return Self {
|
|
|
|
.unbuffered_in_stream = unbuffered_in_stream,
|
|
|
|
.buffer = undefined,
|
|
|
|
|
|
|
|
// Initialize these two fields to buffer_size so that
|
|
|
|
// in `readFn` we treat the state as being able to read
|
|
|
|
// more from the unbuffered stream. If we set them to 0
|
|
|
|
// and 0, the code would think we already hit EOF.
|
|
|
|
.start_index = buffer_size,
|
|
|
|
.end_index = buffer_size,
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
.stream = Stream {
|
2017-11-07 00:22:27 -08:00
|
|
|
.readFn = readFn,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
fn readFn(in_stream: &Stream, dest: []u8) !usize {
|
2017-11-07 00:22:27 -08:00
|
|
|
const self = @fieldParentPtr(Self, "stream", in_stream);
|
|
|
|
|
|
|
|
var dest_index: usize = 0;
|
|
|
|
while (true) {
|
|
|
|
const dest_space = dest.len - dest_index;
|
|
|
|
if (dest_space == 0) {
|
|
|
|
return dest_index;
|
|
|
|
}
|
|
|
|
const amt_buffered = self.end_index - self.start_index;
|
|
|
|
if (amt_buffered == 0) {
|
|
|
|
assert(self.end_index <= buffer_size);
|
|
|
|
if (self.end_index == buffer_size) {
|
|
|
|
// we can read more data from the unbuffered stream
|
|
|
|
if (dest_space < buffer_size) {
|
|
|
|
self.start_index = 0;
|
2018-01-07 13:51:46 -08:00
|
|
|
self.end_index = try self.unbuffered_in_stream.read(self.buffer[0..]);
|
2017-11-07 00:22:27 -08:00
|
|
|
} else {
|
|
|
|
// asking for so much data that buffering is actually less efficient.
|
|
|
|
// forward the request directly to the unbuffered stream
|
2018-01-07 13:51:46 -08:00
|
|
|
const amt_read = try self.unbuffered_in_stream.read(dest[dest_index..]);
|
2017-11-07 00:22:27 -08:00
|
|
|
return dest_index + amt_read;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// reading from the unbuffered stream returned less than we asked for
|
|
|
|
// so we cannot read any more data.
|
|
|
|
return dest_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const copy_amount = math.min(dest_space, amt_buffered);
|
|
|
|
const copy_end_index = self.start_index + copy_amount;
|
|
|
|
mem.copy(u8, dest[dest_index..], self.buffer[self.start_index..copy_end_index]);
|
|
|
|
self.start_index = copy_end_index;
|
|
|
|
dest_index += copy_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub fn BufferedOutStream(comptime Error: type) type {
|
|
|
|
return BufferedOutStreamCustom(os.page_size, Error);
|
|
|
|
}
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-10 17:55:13 -08:00
|
|
|
pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamError: type) type {
|
2017-11-07 00:22:27 -08:00
|
|
|
return struct {
|
|
|
|
const Self = this;
|
2018-02-10 17:55:13 -08:00
|
|
|
pub const Stream = OutStream(Error);
|
|
|
|
pub const Error = OutStreamError;
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub stream: Stream,
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
unbuffered_out_stream: &Stream,
|
2017-11-07 00:22:27 -08:00
|
|
|
|
|
|
|
buffer: [buffer_size]u8,
|
|
|
|
index: usize,
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
pub fn init(unbuffered_out_stream: &Stream) Self {
|
2017-11-07 00:22:27 -08:00
|
|
|
return Self {
|
|
|
|
.unbuffered_out_stream = unbuffered_out_stream,
|
|
|
|
.buffer = undefined,
|
|
|
|
.index = 0,
|
2018-02-05 15:09:13 -08:00
|
|
|
.stream = Stream {
|
2017-11-07 00:22:27 -08:00
|
|
|
.writeFn = writeFn,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn flush(self: &Self) !void {
|
2018-01-07 13:51:46 -08:00
|
|
|
try self.unbuffered_out_stream.write(self.buffer[0..self.index]);
|
2017-11-07 00:22:27 -08:00
|
|
|
self.index = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:09:13 -08:00
|
|
|
fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
|
2017-11-07 00:22:27 -08:00
|
|
|
const self = @fieldParentPtr(Self, "stream", out_stream);
|
|
|
|
|
|
|
|
if (bytes.len >= self.buffer.len) {
|
2018-01-07 13:51:46 -08:00
|
|
|
try self.flush();
|
2017-11-07 00:22:27 -08:00
|
|
|
return self.unbuffered_out_stream.write(bytes);
|
|
|
|
}
|
|
|
|
var src_index: usize = 0;
|
|
|
|
|
|
|
|
while (src_index < bytes.len) {
|
|
|
|
const dest_space_left = self.buffer.len - self.index;
|
|
|
|
const copy_amt = math.min(dest_space_left, bytes.len - src_index);
|
|
|
|
mem.copy(u8, self.buffer[self.index..], bytes[src_index..src_index + copy_amt]);
|
|
|
|
self.index += copy_amt;
|
|
|
|
assert(self.index <= self.buffer.len);
|
|
|
|
if (self.index == self.buffer.len) {
|
2018-01-07 13:51:46 -08:00
|
|
|
try self.flush();
|
2017-11-07 00:22:27 -08:00
|
|
|
}
|
|
|
|
src_index += copy_amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-12-10 18:26:28 -08:00
|
|
|
|
|
|
|
/// Implementation of OutStream trait for Buffer
|
|
|
|
pub const BufferOutStream = struct {
|
|
|
|
buffer: &Buffer,
|
2018-02-05 15:09:13 -08:00
|
|
|
stream: Stream,
|
|
|
|
|
|
|
|
pub const Error = error{OutOfMemory};
|
|
|
|
pub const Stream = OutStream(Error);
|
2017-12-10 18:26:28 -08:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn init(buffer: &Buffer) BufferOutStream {
|
2017-12-10 18:26:28 -08:00
|
|
|
return BufferOutStream {
|
|
|
|
.buffer = buffer,
|
2018-02-07 23:08:45 -08:00
|
|
|
.stream = Stream {
|
2017-12-10 18:26:28 -08:00
|
|
|
.writeFn = writeFn,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:08:45 -08:00
|
|
|
fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
|
2017-12-10 18:26:28 -08:00
|
|
|
const self = @fieldParentPtr(BufferOutStream, "stream", out_stream);
|
|
|
|
return self.buffer.append(bytes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-10 17:55:13 -08:00
|
|
|
|
|
|
|
pub const BufferedAtomicFile = struct {
|
|
|
|
atomic_file: os.AtomicFile,
|
|
|
|
file_stream: FileOutStream,
|
|
|
|
buffered_stream: BufferedOutStream(FileOutStream.Error),
|
|
|
|
|
|
|
|
pub fn create(allocator: &mem.Allocator, dest_path: []const u8) !&BufferedAtomicFile {
|
|
|
|
// TODO with well defined copy elision we don't need this allocation
|
|
|
|
var self = try allocator.create(BufferedAtomicFile);
|
|
|
|
errdefer allocator.destroy(self);
|
|
|
|
|
|
|
|
*self = BufferedAtomicFile {
|
|
|
|
.atomic_file = undefined,
|
|
|
|
.file_stream = undefined,
|
|
|
|
.buffered_stream = undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.atomic_file = try os.AtomicFile.init(allocator, dest_path, os.default_file_mode);
|
|
|
|
errdefer self.atomic_file.deinit();
|
|
|
|
|
|
|
|
self.file_stream = FileOutStream.init(&self.atomic_file.file);
|
|
|
|
self.buffered_stream = BufferedOutStream(FileOutStream.Error).init(&self.file_stream.stream);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// always call destroy, even after successful finish()
|
|
|
|
pub fn destroy(self: &BufferedAtomicFile) void {
|
|
|
|
const allocator = self.atomic_file.allocator;
|
|
|
|
self.atomic_file.deinit();
|
|
|
|
allocator.destroy(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(self: &BufferedAtomicFile) !void {
|
|
|
|
try self.buffered_stream.flush();
|
|
|
|
try self.atomic_file.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stream(self: &BufferedAtomicFile) &OutStream(FileOutStream.Error) {
|
|
|
|
return &self.buffered_stream.stream;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test "import io tests" {
|
|
|
|
comptime {
|
|
|
|
_ = @import("io_test.zig");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 08:34:31 -07:00
|
|
|
pub fn readLine(buf: []u8) !usize {
|
|
|
|
var stdin = getStdIn() catch return error.StdInUnavailable;
|
|
|
|
var adapter = FileInStream.init(&stdin);
|
|
|
|
var stream = &adapter.stream;
|
|
|
|
var index: usize = 0;
|
|
|
|
while (true) {
|
|
|
|
const byte = stream.readByte() catch return error.EndOfFile;
|
|
|
|
switch (byte) {
|
2018-04-09 18:14:55 -07:00
|
|
|
'\r' => {
|
|
|
|
// trash the following \n
|
|
|
|
_ = stream.readByte() catch return error.EndOfFile;
|
|
|
|
return index;
|
|
|
|
},
|
2018-04-02 08:34:31 -07:00
|
|
|
'\n' => return index,
|
|
|
|
else => {
|
|
|
|
if (index == buf.len) return error.InputTooLong;
|
|
|
|
buf[index] = byte;
|
|
|
|
index += 1;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|