Commit 17ffe166c2
Changed files (4)
std/io/c_out_stream.zig
@@ -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,
std/io_test.zig → std/io/test.zig
@@ -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));
+}
std/c.zig
@@ -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;
std/io.zig
@@ -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");
}
}