| ... | ... | @@ -1,44 +0,0 @@ |
| 1 | | const std = @import("../std.zig"); |
| 2 | | const builtin = @import("builtin"); |
| 3 | | const io = std.io; |
| 4 | | const testing = std.testing; |
| 5 | | |
| 6 | | pub const CWriter = io.GenericWriter(*std.c.FILE, std.fs.File.WriteError, cWriterWrite); |
| 7 | | |
| 8 | | pub fn cWriter(c_file: *std.c.FILE) CWriter { |
| 9 | | return .{ .context = c_file }; |
| 10 | | } |
| 11 | | |
| 12 | | fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!usize { |
| 13 | | const amt_written = std.c.fwrite(bytes.ptr, 1, bytes.len, c_file); |
| 14 | | if (amt_written >= 0) return amt_written; |
| 15 | | switch (@as(std.c.E, @enumFromInt(std.c._errno().*))) { |
| 16 | | .SUCCESS => unreachable, |
| 17 | | .INVAL => unreachable, |
| 18 | | .FAULT => unreachable, |
| 19 | | .AGAIN => unreachable, // this is a blocking API |
| 20 | | .BADF => unreachable, // always a race condition |
| 21 | | .DESTADDRREQ => unreachable, // connect was never called |
| 22 | | .DQUOT => return error.DiskQuota, |
| 23 | | .FBIG => return error.FileTooBig, |
| 24 | | .IO => return error.InputOutput, |
| 25 | | .NOSPC => return error.NoSpaceLeft, |
| 26 | | .PERM => return error.PermissionDenied, |
| 27 | | .PIPE => return error.BrokenPipe, |
| 28 | | else => |err| return std.posix.unexpectedErrno(err), |
| 29 | | } |
| 30 | | } |
| 31 | | |
| 32 | | test cWriter { |
| 33 | | if (!builtin.link_libc or builtin.os.tag == .wasi) return error.SkipZigTest; |
| 34 | | |
| 35 | | const filename = "tmp_io_test_file.txt"; |
| 36 | | const out_file = std.c.fopen(filename, "w") orelse return error.UnableToOpenTestFile; |
| 37 | | defer { |
| 38 | | _ = std.c.fclose(out_file); |
| 39 | | std.fs.cwd().deleteFileZ(filename) catch {}; |
| 40 | | } |
| 41 | | |
| 42 | | const writer = cWriter(out_file); |
| 43 | | try writer.print("hi: {}\n", .{@as(i32, 123)}); |
| 44 | | } |