diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-09-26 01:54:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-26 01:54:45 -0400 |
| commit | 68bb3945708c43109c48bda3664176307d45b62c (patch) | |
| tree | afb9731e10cef9d192560b52cd9ae2cf179775c4 /lib/std/io/c_out_stream.zig | |
| parent | 6128bc728d1e1024a178c16c2149f5b1a167a013 (diff) | |
| parent | 4637e8f9699af9c3c6cf4df50ef5bb67c7a318a4 (diff) | |
| download | zig-68bb3945708c43109c48bda3664176307d45b62c.tar.gz zig-68bb3945708c43109c48bda3664176307d45b62c.zip | |
Merge pull request #3315 from ziglang/mv-std-lib
Move std/ to lib/std/
Diffstat (limited to 'lib/std/io/c_out_stream.zig')
| -rw-r--r-- | lib/std/io/c_out_stream.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/std/io/c_out_stream.zig b/lib/std/io/c_out_stream.zig new file mode 100644 index 0000000000..8b341e6937 --- /dev/null +++ b/lib/std/io/c_out_stream.zig @@ -0,0 +1,43 @@ +const std = @import("../std.zig"); +const os = std.os; +const OutStream = std.io.OutStream; +const builtin = @import("builtin"); + +/// TODO make a proposal to make `std.fs.File` use *FILE when linking libc and this just becomes +/// std.io.FileOutStream because std.fs.File.write would do this when linking +/// libc. +pub const COutStream = struct { + pub const Error = std.fs.File.WriteError; + pub const Stream = OutStream(Error); + + stream: Stream, + c_file: *std.c.FILE, + + pub fn init(c_file: *std.c.FILE) COutStream { + return COutStream{ + .c_file = c_file, + .stream = Stream{ .writeFn = writeFn }, + }; + } + + fn writeFn(out_stream: *Stream, bytes: []const u8) Error!void { + const self = @fieldParentPtr(COutStream, "stream", out_stream); + const amt_written = std.c.fwrite(bytes.ptr, 1, bytes.len, self.c_file); + if (amt_written == bytes.len) return; + switch (std.c._errno().*) { + 0 => unreachable, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => unreachable, // this is a blocking API + os.EBADF => unreachable, // always a race condition + os.EDESTADDRREQ => unreachable, // connect was never called + os.EDQUOT => return error.DiskQuota, + os.EFBIG => return error.FileTooBig, + os.EIO => return error.InputOutput, + os.ENOSPC => return error.NoSpaceLeft, + os.EPERM => return error.AccessDenied, + os.EPIPE => return error.BrokenPipe, + else => |err| return os.unexpectedErrno(@intCast(usize, err)), + } + } +}; |
