2016-09-11 21:01:06 -07:00
|
|
|
const system = switch(@compileVar("os")) {
|
2016-12-17 21:25:26 -08:00
|
|
|
Os.linux => @import("linux.zig"),
|
|
|
|
Os.darwin => @import("darwin.zig"),
|
2016-09-11 21:01:06 -07:00
|
|
|
else => @compileError("Unsupported OS"),
|
|
|
|
};
|
|
|
|
|
2016-02-27 21:06:46 -08:00
|
|
|
const errno = @import("errno.zig");
|
|
|
|
const math = @import("math.zig");
|
2016-08-17 20:11:04 -07:00
|
|
|
const endian = @import("endian.zig");
|
|
|
|
const debug = @import("debug.zig");
|
|
|
|
const assert = debug.assert;
|
2016-09-13 23:44:31 -07:00
|
|
|
const os = @import("os.zig");
|
2016-09-22 23:00:23 -07:00
|
|
|
const mem = @import("mem.zig");
|
2015-12-14 23:07:51 -08:00
|
|
|
|
2016-01-22 22:24:09 -08:00
|
|
|
pub const stdin_fileno = 0;
|
|
|
|
pub const stdout_fileno = 1;
|
|
|
|
pub const stderr_fileno = 2;
|
2016-01-02 20:56:33 -08:00
|
|
|
|
2016-01-20 17:18:50 -08:00
|
|
|
pub var stdin = InStream {
|
|
|
|
.fd = stdin_fileno,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub var stdout = OutStream {
|
|
|
|
.fd = stdout_fileno,
|
2016-01-22 21:02:07 -08:00
|
|
|
.buffer = undefined,
|
2016-01-20 17:18:50 -08:00
|
|
|
.index = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub var stderr = OutStream {
|
|
|
|
.fd = stderr_fileno,
|
2016-01-22 21:02:07 -08:00
|
|
|
.buffer = undefined,
|
2016-01-20 17:18:50 -08:00
|
|
|
.index = 0,
|
|
|
|
};
|
|
|
|
|
2016-01-24 21:53:00 -08:00
|
|
|
/// The function received invalid input at runtime. An Invalid error means a
|
|
|
|
/// bug in the program that called the function.
|
2016-12-17 14:48:07 -08:00
|
|
|
error Invalid;
|
2016-01-24 21:53:00 -08:00
|
|
|
|
|
|
|
/// When an Unexpected error occurs, code that emitted the error likely needs
|
|
|
|
/// a patch to recognize the unexpected case so that it can handle it and emit
|
|
|
|
/// a more specific error.
|
2016-12-17 14:48:07 -08:00
|
|
|
error Unexpected;
|
|
|
|
|
|
|
|
error DiskQuota;
|
|
|
|
error FileTooBig;
|
|
|
|
error Io;
|
|
|
|
error NoSpaceLeft;
|
|
|
|
error BadPerm;
|
|
|
|
error PipeFail;
|
|
|
|
error BadFd;
|
|
|
|
error IsDir;
|
|
|
|
error NotDir;
|
|
|
|
error SymLinkLoop;
|
|
|
|
error ProcessFdQuotaExceeded;
|
|
|
|
error SystemFdQuotaExceeded;
|
|
|
|
error NameTooLong;
|
|
|
|
error NoDevice;
|
|
|
|
error PathNotFound;
|
|
|
|
error NoMem;
|
|
|
|
error Unseekable;
|
|
|
|
error Eof;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-01-22 22:24:09 -08:00
|
|
|
const buffer_size = 4 * 1024;
|
|
|
|
const max_u64_base10_digits = 20;
|
2016-01-27 22:28:43 -08:00
|
|
|
const max_f64_digits = 65;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-04-08 13:13:33 -07:00
|
|
|
pub const OpenRead = 0b0001;
|
|
|
|
pub const OpenWrite = 0b0010;
|
|
|
|
pub const OpenCreate = 0b0100;
|
|
|
|
pub const OpenTruncate = 0b1000;
|
|
|
|
|
2016-12-18 14:24:52 -08:00
|
|
|
pub const OutStream = struct {
|
2016-07-26 20:40:11 -07:00
|
|
|
fd: i32,
|
2016-01-20 17:18:50 -08:00
|
|
|
buffer: [buffer_size]u8,
|
2016-07-26 20:40:11 -07:00
|
|
|
index: usize,
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-09-13 23:44:31 -07:00
|
|
|
pub fn writeByte(self: &OutStream, b: u8) -> %void {
|
|
|
|
if (self.buffer.len == self.index) %return self.flush();
|
|
|
|
self.buffer[self.index] = b;
|
|
|
|
self.index += 1;
|
2016-07-26 18:13:22 -07:00
|
|
|
}
|
|
|
|
|
2017-01-23 22:58:20 -08:00
|
|
|
pub fn write(self: &OutStream, bytes: []const u8) -> %void {
|
2016-04-08 13:13:33 -07:00
|
|
|
var src_bytes_left = bytes.len;
|
2016-12-17 22:54:27 -08:00
|
|
|
var src_index: usize = 0;
|
2016-09-13 23:44:31 -07:00
|
|
|
const dest_space_left = self.buffer.len - self.index;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
|
|
|
while (src_bytes_left > 0) {
|
2016-09-09 05:58:39 -07:00
|
|
|
const copy_amt = math.min(dest_space_left, src_bytes_left);
|
2016-09-13 23:44:31 -07:00
|
|
|
@memcpy(&self.buffer[self.index], &bytes[src_index], copy_amt);
|
|
|
|
self.index += copy_amt;
|
|
|
|
if (self.index == self.buffer.len) {
|
|
|
|
%return self.flush();
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
src_bytes_left -= copy_amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:58:20 -08:00
|
|
|
const State = enum { // TODO put inside printf function and make sure the name and debug info is correct
|
|
|
|
Start,
|
|
|
|
OpenBrace,
|
|
|
|
CloseBrace,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Calls print and then flushes the buffer.
|
|
|
|
pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) -> %void {
|
|
|
|
comptime var start_index: usize = 0;
|
|
|
|
comptime var state = State.Start;
|
|
|
|
comptime var next_arg: usize = 0;
|
|
|
|
inline for (format) |c, i| {
|
|
|
|
switch (state) {
|
|
|
|
State.Start => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
if (start_index < i) %return self.write(format[start_index...i]);
|
|
|
|
state = State.OpenBrace;
|
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
if (start_index < i) %return self.write(format[start_index...i]);
|
|
|
|
state = State.CloseBrace;
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
},
|
|
|
|
State.OpenBrace => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i;
|
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
%return self.printValue(args[next_arg]);
|
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
else => @compileError("Unknown format character: " ++ c),
|
|
|
|
},
|
|
|
|
State.CloseBrace => switch (c) {
|
|
|
|
'}' => {
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i;
|
|
|
|
},
|
|
|
|
else => @compileError("Single '}' encountered in format string"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comptime {
|
|
|
|
if (args.len != next_arg) {
|
|
|
|
@compileError("Unused arguments");
|
|
|
|
}
|
|
|
|
if (state != State.Start) {
|
|
|
|
@compileError("Incomplete format string: " ++ format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline if (start_index < format.len) {
|
|
|
|
%return self.write(format[start_index...format.len]);
|
|
|
|
}
|
2016-09-13 23:44:31 -07:00
|
|
|
%return self.flush();
|
2016-01-24 21:53:00 -08:00
|
|
|
}
|
|
|
|
|
2017-01-23 22:58:20 -08:00
|
|
|
pub fn printValue(self: &OutStream, value: var) -> %void {
|
|
|
|
const T = @typeOf(value);
|
|
|
|
if (@isInteger(T)) {
|
|
|
|
return self.printInt(T, value);
|
|
|
|
} else if (@isFloat(T)) {
|
|
|
|
return self.printFloat(T, value);
|
|
|
|
} else if (@canImplicitCast([]const u8, value)) {
|
|
|
|
const casted_value = ([]const u8)(value);
|
|
|
|
return self.write(casted_value);
|
|
|
|
} else {
|
|
|
|
@compileError("Unable to print type '" ++ @typeName(T) ++ "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn printInt(self: &OutStream, comptime T: type, x: T) -> %void {
|
2016-08-16 22:59:32 -07:00
|
|
|
// TODO replace max_u64_base10_digits with math.log10(math.pow(2, @sizeOf(T)))
|
2016-09-13 23:44:31 -07:00
|
|
|
if (self.index + max_u64_base10_digits >= self.buffer.len) {
|
|
|
|
%return self.flush();
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
2016-09-13 23:44:31 -07:00
|
|
|
const amt_printed = bufPrintInt(T, self.buffer[self.index...], x);
|
|
|
|
self.index += amt_printed;
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
|
2016-09-13 23:44:31 -07:00
|
|
|
pub fn flush(self: &OutStream) -> %void {
|
2016-08-31 20:23:47 -07:00
|
|
|
while (true) {
|
2016-09-13 23:44:31 -07:00
|
|
|
const write_ret = system.write(self.fd, &self.buffer[0], self.index);
|
2016-09-11 21:01:06 -07:00
|
|
|
const write_err = system.getErrno(write_ret);
|
2016-08-31 20:23:47 -07:00
|
|
|
if (write_err > 0) {
|
|
|
|
return switch (write_err) {
|
|
|
|
errno.EINTR => continue,
|
2016-09-13 13:46:27 -07:00
|
|
|
errno.EINVAL => @unreachable(),
|
2016-08-31 20:23:47 -07:00
|
|
|
errno.EDQUOT => error.DiskQuota,
|
|
|
|
errno.EFBIG => error.FileTooBig,
|
|
|
|
errno.EIO => error.Io,
|
|
|
|
errno.ENOSPC => error.NoSpaceLeft,
|
|
|
|
errno.EPERM => error.BadPerm,
|
|
|
|
errno.EPIPE => error.PipeFail,
|
|
|
|
else => error.Unexpected,
|
|
|
|
}
|
2016-01-24 21:53:00 -08:00
|
|
|
}
|
2016-09-13 23:44:31 -07:00
|
|
|
self.index = 0;
|
2016-08-31 20:23:47 -07:00
|
|
|
return;
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-28 06:22:09 -08:00
|
|
|
|
2016-12-31 14:10:29 -08:00
|
|
|
pub fn close(self: &OutStream) {
|
2016-08-31 20:23:47 -07:00
|
|
|
while (true) {
|
2016-09-13 23:44:31 -07:00
|
|
|
const close_ret = system.close(self.fd);
|
2016-09-11 21:01:06 -07:00
|
|
|
const close_err = system.getErrno(close_ret);
|
2016-12-31 14:10:29 -08:00
|
|
|
if (close_err > 0 && close_err == errno.EINTR)
|
|
|
|
continue;
|
2016-08-31 20:23:47 -07:00
|
|
|
return;
|
2016-02-28 06:22:09 -08:00
|
|
|
}
|
|
|
|
}
|
2016-12-18 14:24:52 -08:00
|
|
|
};
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-08-17 20:11:04 -07:00
|
|
|
// TODO created a BufferedInStream struct and move some of this code there
|
|
|
|
// BufferedInStream API goes on top of minimal InStream API.
|
2016-12-18 14:24:52 -08:00
|
|
|
pub const InStream = struct {
|
2016-07-26 20:40:11 -07:00
|
|
|
fd: i32,
|
2016-08-17 20:11:04 -07:00
|
|
|
|
|
|
|
/// Call close to clean up.
|
|
|
|
pub fn open(is: &InStream, path: []const u8) -> %void {
|
2016-08-31 20:23:47 -07:00
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-08-31 20:23:47 -07:00
|
|
|
while (true) {
|
2016-09-11 21:01:06 -07:00
|
|
|
const result = system.open(path, system.O_LARGEFILE|system.O_RDONLY, 0);
|
|
|
|
const err = system.getErrno(result);
|
2016-08-31 20:23:47 -07:00
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
errno.EINTR => continue,
|
|
|
|
|
2016-09-13 13:46:27 -07:00
|
|
|
errno.EFAULT => @unreachable(),
|
|
|
|
errno.EINVAL => @unreachable(),
|
2016-08-31 20:23:47 -07:00
|
|
|
errno.EACCES => error.BadPerm,
|
|
|
|
errno.EFBIG, errno.EOVERFLOW => error.FileTooBig,
|
|
|
|
errno.EISDIR => error.IsDir,
|
|
|
|
errno.ELOOP => error.SymLinkLoop,
|
|
|
|
errno.EMFILE => error.ProcessFdQuotaExceeded,
|
|
|
|
errno.ENAMETOOLONG => error.NameTooLong,
|
|
|
|
errno.ENFILE => error.SystemFdQuotaExceeded,
|
|
|
|
errno.ENODEV => error.NoDevice,
|
|
|
|
errno.ENOENT => error.PathNotFound,
|
|
|
|
errno.ENOMEM => error.NoMem,
|
|
|
|
errno.ENOSPC => error.NoSpaceLeft,
|
|
|
|
errno.ENOTDIR => error.NotDir,
|
|
|
|
errno.EPERM => error.BadPerm,
|
|
|
|
else => error.Unexpected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is.fd = i32(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported OS"),
|
2016-04-08 13:13:33 -07:00
|
|
|
}
|
2016-09-11 21:01:06 -07:00
|
|
|
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
2016-02-28 06:22:09 -08:00
|
|
|
|
2016-09-08 11:21:42 -07:00
|
|
|
/// Upon success, the stream is in an uninitialized state. To continue using it,
|
|
|
|
/// you must use the open() function.
|
2016-02-28 06:22:09 -08:00
|
|
|
pub fn close(is: &InStream) -> %void {
|
2016-08-31 20:23:47 -07:00
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-08-31 20:23:47 -07:00
|
|
|
while (true) {
|
2016-09-11 21:01:06 -07:00
|
|
|
const close_ret = system.close(is.fd);
|
|
|
|
const close_err = system.getErrno(close_ret);
|
2016-08-31 20:23:47 -07:00
|
|
|
if (close_err > 0) {
|
|
|
|
return switch (close_err) {
|
|
|
|
errno.EINTR => continue,
|
|
|
|
|
|
|
|
errno.EIO => error.Io,
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
else => error.Unexpected,
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 11:21:42 -07:00
|
|
|
return;
|
2016-08-31 20:23:47 -07:00
|
|
|
}
|
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported OS"),
|
2016-02-28 06:22:09 -08:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 20:11:04 -07:00
|
|
|
|
|
|
|
/// Returns the number of bytes read. If the number read is smaller than buf.len, then
|
|
|
|
/// the stream reached End Of File.
|
|
|
|
pub fn read(is: &InStream, buf: []u8) -> %usize {
|
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-08-31 20:23:47 -07:00
|
|
|
var index: usize = 0;
|
|
|
|
while (index < buf.len) {
|
2016-09-11 21:01:06 -07:00
|
|
|
const amt_read = system.read(is.fd, &buf[index], buf.len - index);
|
|
|
|
const read_err = system.getErrno(amt_read);
|
2016-08-17 20:11:04 -07:00
|
|
|
if (read_err > 0) {
|
|
|
|
switch (read_err) {
|
|
|
|
errno.EINTR => continue,
|
2016-08-31 20:23:47 -07:00
|
|
|
|
2016-09-13 13:46:27 -07:00
|
|
|
errno.EINVAL => @unreachable(),
|
|
|
|
errno.EFAULT => @unreachable(),
|
2016-08-17 20:11:04 -07:00
|
|
|
errno.EBADF => return error.BadFd,
|
|
|
|
errno.EIO => return error.Io,
|
|
|
|
else => return error.Unexpected,
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 20:23:47 -07:00
|
|
|
if (amt_read == 0) return index;
|
|
|
|
index += amt_read;
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
2016-08-31 20:23:47 -07:00
|
|
|
return index;
|
2016-08-17 20:11:04 -07:00
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported OS"),
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readNoEof(is: &InStream, buf: []u8) -> %void {
|
|
|
|
const amt_read = %return is.read(buf);
|
|
|
|
if (amt_read < buf.len) return error.Eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readByte(is: &InStream) -> %u8 {
|
|
|
|
var result: [1]u8 = undefined;
|
|
|
|
%return is.readNoEof(result);
|
|
|
|
return result[0];
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn readIntLe(is: &InStream, comptime T: type) -> %T {
|
2016-08-17 20:11:04 -07:00
|
|
|
is.readInt(false, T)
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn readIntBe(is: &InStream, comptime T: type) -> %T {
|
2016-08-17 20:11:04 -07:00
|
|
|
is.readInt(true, T)
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn readInt(is: &InStream, is_be: bool, comptime T: type) -> %T {
|
2016-08-17 20:11:04 -07:00
|
|
|
var result: T = undefined;
|
|
|
|
const result_slice = ([]u8)((&result)[0...1]);
|
|
|
|
%return is.readNoEof(result_slice);
|
|
|
|
return endian.swapIf(!is_be, T, result);
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn readVarInt(is: &InStream, is_be: bool, comptime T: type, size: usize) -> %T {
|
2016-09-22 23:00:23 -07:00
|
|
|
assert(size <= @sizeOf(T));
|
|
|
|
assert(size <= 8);
|
|
|
|
var input_buf: [8]u8 = undefined;
|
|
|
|
const input_slice = input_buf[0...size];
|
|
|
|
%return is.readNoEof(input_slice);
|
|
|
|
return mem.sliceAsInt(input_slice, is_be, T);
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn seekForward(is: &InStream, amount: usize) -> %void {
|
2016-08-31 20:23:47 -07:00
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-09-11 21:01:06 -07:00
|
|
|
const result = system.lseek(is.fd, amount, system.SEEK_CUR);
|
|
|
|
const err = system.getErrno(result);
|
2016-08-31 20:23:47 -07:00
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.EINVAL => error.Unseekable,
|
|
|
|
errno.EOVERFLOW => error.Unseekable,
|
|
|
|
errno.ESPIPE => error.Unseekable,
|
|
|
|
errno.ENXIO => error.Unseekable,
|
|
|
|
else => error.Unexpected,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported OS"),
|
2016-08-31 20:23:47 -07:00
|
|
|
}
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn seekTo(is: &InStream, pos: usize) -> %void {
|
2016-08-31 20:23:47 -07:00
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-09-11 21:01:06 -07:00
|
|
|
const result = system.lseek(is.fd, pos, system.SEEK_SET);
|
|
|
|
const err = system.getErrno(result);
|
2016-08-31 20:23:47 -07:00
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.EINVAL => error.Unseekable,
|
|
|
|
errno.EOVERFLOW => error.Unseekable,
|
|
|
|
errno.ESPIPE => error.Unseekable,
|
|
|
|
errno.ENXIO => error.Unseekable,
|
|
|
|
else => error.Unexpected,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported OS"),
|
2016-08-31 20:23:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getPos(is: &InStream) -> %usize {
|
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-09-11 21:01:06 -07:00
|
|
|
const result = system.lseek(is.fd, 0, system.SEEK_CUR);
|
|
|
|
const err = system.getErrno(result);
|
2016-08-31 20:23:47 -07:00
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.EINVAL => error.Unseekable,
|
|
|
|
errno.EOVERFLOW => error.Unseekable,
|
|
|
|
errno.ESPIPE => error.Unseekable,
|
|
|
|
errno.ENXIO => error.Unseekable,
|
|
|
|
else => error.Unexpected,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported OS"),
|
2016-08-31 20:23:47 -07:00
|
|
|
}
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
|
|
|
|
2016-08-31 20:23:47 -07:00
|
|
|
pub fn getEndPos(is: &InStream) -> %usize {
|
2016-09-11 21:01:06 -07:00
|
|
|
var stat: system.stat = undefined;
|
|
|
|
const err = system.getErrno(system.fstat(is.fd, &stat));
|
2016-08-17 20:11:04 -07:00
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.ENOMEM => error.NoMem,
|
|
|
|
else => error.Unexpected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return usize(stat.size);
|
|
|
|
}
|
2016-12-18 14:24:52 -08:00
|
|
|
};
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn parseUnsigned(comptime T: type, buf: []u8, radix: u8) -> %T {
|
2016-05-13 11:11:55 -07:00
|
|
|
var x: T = 0;
|
2016-01-08 02:59:37 -08:00
|
|
|
|
2016-02-05 16:15:19 -08:00
|
|
|
for (buf) |c| {
|
2016-08-16 22:42:50 -07:00
|
|
|
const digit = %return charToDigit(c, radix);
|
|
|
|
x = %return math.mulOverflow(T, x, radix);
|
|
|
|
x = %return math.addOverflow(T, x, digit);
|
2016-01-07 02:23:38 -08:00
|
|
|
}
|
2016-01-08 02:59:37 -08:00
|
|
|
|
2016-01-25 12:53:40 -08:00
|
|
|
return x;
|
2016-01-07 02:23:38 -08:00
|
|
|
}
|
|
|
|
|
2016-12-17 14:48:07 -08:00
|
|
|
error InvalidChar;
|
2016-08-16 22:42:50 -07:00
|
|
|
fn charToDigit(c: u8, radix: u8) -> %u8 {
|
2017-01-22 22:19:03 -08:00
|
|
|
const value = switch (c) {
|
|
|
|
'0' ... '9' => c - '0',
|
|
|
|
'A' ... 'Z' => c - 'A' + 10,
|
|
|
|
'a' ... 'z' => c - 'a' + 10,
|
|
|
|
else => return error.InvalidChar,
|
2016-07-24 18:35:50 -07:00
|
|
|
};
|
2017-01-22 22:19:03 -08:00
|
|
|
|
|
|
|
if (value >= radix)
|
|
|
|
return error.InvalidChar;
|
|
|
|
|
|
|
|
return value;
|
2016-01-02 20:56:33 -08:00
|
|
|
}
|
2016-01-02 02:38:45 -08:00
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn bufPrintInt(comptime T: type, out_buf: []u8, x: T) -> usize {
|
2016-08-16 22:59:32 -07:00
|
|
|
if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn bufPrintSigned(comptime T: type, out_buf: []u8, x: T) -> usize {
|
2016-08-16 22:42:50 -07:00
|
|
|
const uint = @intType(false, T.bit_count);
|
2016-01-05 05:30:49 -08:00
|
|
|
if (x < 0) {
|
|
|
|
out_buf[0] = '-';
|
2016-08-16 22:42:50 -07:00
|
|
|
return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
|
2016-01-05 05:30:49 -08:00
|
|
|
} else {
|
2016-08-16 22:42:50 -07:00
|
|
|
return bufPrintUnsigned(uint, out_buf, uint(x));
|
2016-01-05 05:30:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn bufPrintUnsigned(comptime T: type, out_buf: []u8, x: T) -> usize {
|
2016-01-25 22:56:46 -08:00
|
|
|
var buf: [max_u64_base10_digits]u8 = undefined;
|
2016-01-02 02:38:45 -08:00
|
|
|
var a = x;
|
2016-07-26 20:40:11 -07:00
|
|
|
var index: usize = buf.len;
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const digit = a % 10;
|
|
|
|
index -= 1;
|
2016-01-14 01:52:33 -08:00
|
|
|
buf[index] = '0' + u8(digit);
|
2016-01-02 02:38:45 -08:00
|
|
|
a /= 10;
|
|
|
|
if (a == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:47:47 -08:00
|
|
|
const len = buf.len - index;
|
2016-01-02 02:38:45 -08:00
|
|
|
|
2016-01-20 17:18:50 -08:00
|
|
|
@memcpy(&out_buf[0], &buf[index], len);
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2016-01-24 21:53:00 -08:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn parseU64DigitTooBig() {
|
2016-12-31 14:10:29 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
parseUnsigned(u64, "123a", 10) %% |err| {
|
2016-02-09 08:51:25 -08:00
|
|
|
if (err == error.InvalidChar) return;
|
2016-09-13 13:46:27 -07:00
|
|
|
@unreachable();
|
2016-02-09 08:51:25 -08:00
|
|
|
};
|
2016-09-13 13:46:27 -07:00
|
|
|
@unreachable();
|
2016-02-09 08:51:25 -08:00
|
|
|
}
|
2016-08-17 20:11:04 -07:00
|
|
|
|
|
|
|
pub fn openSelfExe(stream: &InStream) -> %void {
|
|
|
|
switch (@compileVar("os")) {
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.linux => {
|
2016-08-17 20:11:04 -07:00
|
|
|
%return stream.open("/proc/self/exe");
|
|
|
|
},
|
2016-12-31 14:10:29 -08:00
|
|
|
Os.darwin => {
|
2016-09-13 23:44:31 -07:00
|
|
|
%%stderr.printf("TODO: openSelfExe on Darwin\n");
|
|
|
|
os.abort();
|
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported os"),
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
|
|
|
}
|