aboutsummaryrefslogtreecommitdiff
path: root/std/io.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-11-10 16:45:01 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-11-10 16:45:01 -0500
commitdf89291d1ca04a5891dd48ea5f6d1a99b6006bcb (patch)
tree83970de0fdaa7383c11f64efceff7f88d6fd9cd9 /std/io.zig
parente9d7623e1f0300b1b652373f2e0e7b605eaf13d1 (diff)
parent019f18058bb74816f8754de63a219347597e06da (diff)
downloadzig-df89291d1ca04a5891dd48ea5f6d1a99b6006bcb.tar.gz
zig-df89291d1ca04a5891dd48ea5f6d1a99b6006bcb.zip
Merge remote-tracking branch 'origin/master' into llvm6
Diffstat (limited to 'std/io.zig')
-rw-r--r--std/io.zig39
1 files changed, 30 insertions, 9 deletions
diff --git a/std/io.zig b/std/io.zig
index dea327cf16..d570927488 100644
--- a/std/io.zig
+++ b/std/io.zig
@@ -20,6 +20,12 @@ const fmt = std.fmt;
const is_posix = builtin.os != builtin.Os.windows;
const is_windows = builtin.os == builtin.Os.windows;
+test "import io tests" {
+ comptime {
+ _ = @import("io_test.zig");
+ }
+}
+
/// The function received invalid input at runtime. An Invalid error means a
/// bug in the program that called the function.
error Invalid;
@@ -247,17 +253,32 @@ pub const File = struct {
}
pub fn getEndPos(self: &File) -> %usize {
- var stat: system.Stat = undefined;
- const err = system.getErrno(system.fstat(self.handle, &stat));
- if (err > 0) {
- return switch (err) {
- system.EBADF => error.BadFd,
- system.ENOMEM => error.OutOfMemory,
- else => os.unexpectedErrorPosix(err),
+ if (is_posix) {
+ var stat: system.Stat = undefined;
+ const err = system.getErrno(system.fstat(self.handle, &stat));
+ if (err > 0) {
+ return switch (err) {
+ system.EBADF => error.BadFd,
+ system.ENOMEM => error.OutOfMemory,
+ else => os.unexpectedErrorPosix(err),
+ }
}
- }
- return usize(stat.size);
+ return usize(stat.size);
+ } else if (is_windows) {
+ var file_size: system.LARGE_INTEGER = undefined;
+ if (system.GetFileSizeEx(self.handle, &file_size) == 0) {
+ const err = system.GetLastError();
+ return switch (err) {
+ else => os.unexpectedErrorWindows(err),
+ };
+ }
+ if (file_size < 0)
+ return error.Overflow;
+ return math.cast(usize, u64(file_size));
+ } else {
+ unreachable;
+ }
}
pub fn read(self: &File, buffer: []u8) -> %usize {