add preliminary windows support to std.io.COutStream

This commit is contained in:
Andrew Kelley 2019-04-25 00:24:25 -04:00
parent e1bf74fca3
commit 17ffe166c2
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
4 changed files with 20 additions and 2 deletions

View File

@ -13,6 +13,8 @@ pub use switch (builtin.os) {
// TODO https://github.com/ziglang/zig/issues/265 on this whole file
pub const FILE = @OpaqueType();
pub extern "c" fn fopen(filename: [*]const u8, modes: [*]const u8) ?*FILE;
pub extern "c" fn fclose(stream: *FILE) c_int;
pub extern "c" fn fwrite(ptr: [*]const u8, size_of_type: usize, item_count: usize, stream: *FILE) usize;
pub extern "c" fn fread(ptr: [*]u8, size_of_type: usize, item_count: usize, stream: *FILE) usize;

View File

@ -1092,6 +1092,7 @@ test "io.readLineSliceFrom" {
pub const Packing = enum {
/// Pack data to byte alignment
Byte,
/// Pack data to bit alignment
Bit,
};
@ -1454,6 +1455,6 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
test "import io tests" {
comptime {
_ = @import("io_test.zig");
_ = @import("io/test.zig");
}
}

View File

@ -24,6 +24,10 @@ pub const COutStream = struct {
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;
// TODO errno on windows. should we have a posix layer for windows?
if (builtin.os == .windows) {
return error.InputOutput;
}
const errno = std.c._errno().*;
switch (errno) {
0 => unreachable,

View File

@ -1,4 +1,4 @@
const std = @import("std.zig");
const std = @import("../std.zig");
const io = std.io;
const meta = std.meta;
const trait = std.trait;
@ -589,3 +589,14 @@ test "Deserializer bad data" {
try testBadData(.Big, .Bit);
try testBadData(.Little, .Bit);
}
test "c out stream" {
if (!builtin.link_libc) return error.SkipZigTest;
const filename = c"tmp_io_test_file.txt";
const out_file = std.c.fopen(filename, c"w") orelse return error.UnableToOpenTestFile;
defer std.os.deleteFileC(filename) catch {};
const out_stream = &io.COutStream.init(out_file).stream;
try out_stream.print("hi: {}\n", i32(123));
}