| ... | ... | @@ -61,18 +61,21 @@ pub fn getStdIn() GetStdIoErrs!File { |
| 61 | 61 | /// Implementation of InStream trait for File |
| 62 | 62 | pub const FileInStream = struct { |
| 63 | 63 | file: &File, |
| 64 | | stream: InStream, |
| 64 | stream: Stream, |
| 65 | |
| 66 | pub const Error = @typeOf(File.read).ReturnType.ErrorSet; |
| 67 | pub const Stream = InStream(Error); |
| 65 | 68 | |
| 66 | 69 | pub fn init(file: &File) FileInStream { |
| 67 | 70 | return FileInStream { |
| 68 | 71 | .file = file, |
| 69 | | .stream = InStream { |
| 72 | .stream = Stream { |
| 70 | 73 | .readFn = readFn, |
| 71 | 74 | }, |
| 72 | 75 | }; |
| 73 | 76 | } |
| 74 | 77 | |
| 75 | | fn readFn(in_stream: &InStream, buffer: []u8) !usize { |
| 78 | fn readFn(in_stream: &Stream, buffer: []u8) Error!usize { |
| 76 | 79 | const self = @fieldParentPtr(FileInStream, "stream", in_stream); |
| 77 | 80 | return self.file.read(buffer); |
| 78 | 81 | } |
| ... | ... | @@ -81,18 +84,21 @@ pub const FileInStream = struct { |
| 81 | 84 | /// Implementation of OutStream trait for File |
| 82 | 85 | pub const FileOutStream = struct { |
| 83 | 86 | file: &File, |
| 84 | | stream: OutStream, |
| 87 | stream: Stream, |
| 88 | |
| 89 | pub const Error = File.WriteError; |
| 90 | pub const Stream = OutStream(Error); |
| 85 | 91 | |
| 86 | 92 | pub fn init(file: &File) FileOutStream { |
| 87 | 93 | return FileOutStream { |
| 88 | 94 | .file = file, |
| 89 | | .stream = OutStream { |
| 95 | .stream = Stream { |
| 90 | 96 | .writeFn = writeFn, |
| 91 | 97 | }, |
| 92 | 98 | }; |
| 93 | 99 | } |
| 94 | 100 | |
| 95 | | fn writeFn(out_stream: &OutStream, bytes: []const u8) !void { |
| 101 | fn writeFn(out_stream: &Stream, bytes: []const u8) !void { |
| 96 | 102 | const self = @fieldParentPtr(FileOutStream, "stream", out_stream); |
| 97 | 103 | return self.file.write(bytes); |
| 98 | 104 | } |
| ... | ... | @@ -298,6 +304,8 @@ pub const File = struct { |
| 298 | 304 | } |
| 299 | 305 | } |
| 300 | 306 | |
| 307 | pub const ReadError = error {}; |
| 308 | |
| 301 | 309 | pub fn read(self: &File, buffer: []u8) !usize { |
| 302 | 310 | if (is_posix) { |
| 303 | 311 | var index: usize = 0; |
| ... | ... | @@ -340,7 +348,7 @@ pub const File = struct { |
| 340 | 348 | } |
| 341 | 349 | } |
| 342 | 350 | |
| 343 | | const WriteError = os.WindowsWriteError || os.PosixWriteError; |
| 351 | pub const WriteError = os.WindowsWriteError || os.PosixWriteError; |
| 344 | 352 | |
| 345 | 353 | fn write(self: &File, bytes: []const u8) WriteError!void { |
| 346 | 354 | if (is_posix) { |
| ... | ... | @@ -353,161 +361,165 @@ pub const File = struct { |
| 353 | 361 | } |
| 354 | 362 | }; |
| 355 | 363 | |
| 356 | | pub const InStream = struct { |
| 357 | | // TODO allow specifying the error set |
| 358 | | /// Return the number of bytes read. If the number read is smaller than buf.len, it |
| 359 | | /// means the stream reached the end. Reaching the end of a stream is not an error |
| 360 | | /// condition. |
| 361 | | readFn: fn(self: &InStream, buffer: []u8) error!usize, |
| 362 | | |
| 363 | | /// Replaces `buffer` contents by reading from the stream until it is finished. |
| 364 | | /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and |
| 365 | | /// the contents read from the stream are lost. |
| 366 | | pub fn readAllBuffer(self: &InStream, buffer: &Buffer, max_size: usize) !void { |
| 367 | | try buffer.resize(0); |
| 368 | | |
| 369 | | var actual_buf_len: usize = 0; |
| 370 | | while (true) { |
| 371 | | const dest_slice = buffer.toSlice()[actual_buf_len..]; |
| 372 | | const bytes_read = try self.readFn(self, dest_slice); |
| 373 | | actual_buf_len += bytes_read; |
| 374 | | |
| 375 | | if (bytes_read != dest_slice.len) { |
| 376 | | buffer.shrink(actual_buf_len); |
| 377 | | return; |
| 378 | | } |
| 379 | | |
| 380 | | const new_buf_size = math.min(max_size, actual_buf_len + os.page_size); |
| 381 | | if (new_buf_size == actual_buf_len) |
| 382 | | return error.StreamTooLong; |
| 383 | | try buffer.resize(new_buf_size); |
| 384 | | } |
| 385 | | } |
| 364 | pub fn InStream(comptime Error: type) type { |
| 365 | return struct { |
| 366 | const Self = this; |
| 386 | 367 | |
| 387 | | /// Allocates enough memory to hold all the contents of the stream. If the allocated |
| 388 | | /// memory would be greater than `max_size`, returns `error.StreamTooLong`. |
| 389 | | /// Caller owns returned memory. |
| 390 | | /// If this function returns an error, the contents from the stream read so far are lost. |
| 391 | | pub fn readAllAlloc(self: &InStream, allocator: &mem.Allocator, max_size: usize) ![]u8 { |
| 392 | | var buf = Buffer.initNull(allocator); |
| 393 | | defer buf.deinit(); |
| 368 | /// Return the number of bytes read. If the number read is smaller than buf.len, it |
| 369 | /// means the stream reached the end. Reaching the end of a stream is not an error |
| 370 | /// condition. |
| 371 | readFn: fn(self: &Self, buffer: []u8) Error!usize, |
| 394 | 372 | |
| 395 | | try self.readAllBuffer(&buf, max_size); |
| 396 | | return buf.toOwnedSlice(); |
| 397 | | } |
| 373 | /// Replaces `buffer` contents by reading from the stream until it is finished. |
| 374 | /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and |
| 375 | /// the contents read from the stream are lost. |
| 376 | pub fn readAllBuffer(self: &Self, buffer: &Buffer, max_size: usize) !void { |
| 377 | try buffer.resize(0); |
| 398 | 378 | |
| 399 | | /// Replaces `buffer` contents by reading from the stream until `delimiter` is found. |
| 400 | | /// Does not include the delimiter in the result. |
| 401 | | /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents |
| 402 | | /// read from the stream so far are lost. |
| 403 | | pub fn readUntilDelimiterBuffer(self: &InStream, buffer: &Buffer, delimiter: u8, max_size: usize) !void { |
| 404 | | try buf.resize(0); |
| 379 | var actual_buf_len: usize = 0; |
| 380 | while (true) { |
| 381 | const dest_slice = buffer.toSlice()[actual_buf_len..]; |
| 382 | const bytes_read = try self.readFn(self, dest_slice); |
| 383 | actual_buf_len += bytes_read; |
| 405 | 384 | |
| 406 | | while (true) { |
| 407 | | var byte: u8 = try self.readByte(); |
| 385 | if (bytes_read != dest_slice.len) { |
| 386 | buffer.shrink(actual_buf_len); |
| 387 | return; |
| 388 | } |
| 408 | 389 | |
| 409 | | if (byte == delimiter) { |
| 410 | | return; |
| 390 | const new_buf_size = math.min(max_size, actual_buf_len + os.page_size); |
| 391 | if (new_buf_size == actual_buf_len) |
| 392 | return error.StreamTooLong; |
| 393 | try buffer.resize(new_buf_size); |
| 411 | 394 | } |
| 395 | } |
| 412 | 396 | |
| 413 | | if (buf.len() == max_size) { |
| 414 | | return error.StreamTooLong; |
| 415 | | } |
| 397 | /// Allocates enough memory to hold all the contents of the stream. If the allocated |
| 398 | /// memory would be greater than `max_size`, returns `error.StreamTooLong`. |
| 399 | /// Caller owns returned memory. |
| 400 | /// If this function returns an error, the contents from the stream read so far are lost. |
| 401 | pub fn readAllAlloc(self: &Self, allocator: &mem.Allocator, max_size: usize) ![]u8 { |
| 402 | var buf = Buffer.initNull(allocator); |
| 403 | defer buf.deinit(); |
| 416 | 404 | |
| 417 | | try buf.appendByte(byte); |
| 405 | try self.readAllBuffer(&buf, max_size); |
| 406 | return buf.toOwnedSlice(); |
| 418 | 407 | } |
| 419 | | } |
| 420 | 408 | |
| 421 | | /// Allocates enough memory to read until `delimiter`. If the allocated |
| 422 | | /// memory would be greater than `max_size`, returns `error.StreamTooLong`. |
| 423 | | /// Caller owns returned memory. |
| 424 | | /// If this function returns an error, the contents from the stream read so far are lost. |
| 425 | | pub fn readUntilDelimiterAlloc(self: &InStream, allocator: &mem.Allocator, |
| 426 | | delimiter: u8, max_size: usize) ![]u8 |
| 427 | | { |
| 428 | | var buf = Buffer.initNull(allocator); |
| 429 | | defer buf.deinit(); |
| 430 | | |
| 431 | | try self.readUntilDelimiterBuffer(self, &buf, delimiter, max_size); |
| 432 | | return buf.toOwnedSlice(); |
| 433 | | } |
| 409 | /// Replaces `buffer` contents by reading from the stream until `delimiter` is found. |
| 410 | /// Does not include the delimiter in the result. |
| 411 | /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents |
| 412 | /// read from the stream so far are lost. |
| 413 | pub fn readUntilDelimiterBuffer(self: &Self, buffer: &Buffer, delimiter: u8, max_size: usize) !void { |
| 414 | try buf.resize(0); |
| 434 | 415 | |
| 435 | | /// Returns the number of bytes read. If the number read is smaller than buf.len, it |
| 436 | | /// means the stream reached the end. Reaching the end of a stream is not an error |
| 437 | | /// condition. |
| 438 | | pub fn read(self: &InStream, buffer: []u8) !usize { |
| 439 | | return self.readFn(self, buffer); |
| 440 | | } |
| 416 | while (true) { |
| 417 | var byte: u8 = try self.readByte(); |
| 441 | 418 | |
| 442 | | /// Same as `read` but end of stream returns `error.EndOfStream`. |
| 443 | | pub fn readNoEof(self: &InStream, buf: []u8) !void { |
| 444 | | const amt_read = try self.read(buf); |
| 445 | | if (amt_read < buf.len) return error.EndOfStream; |
| 446 | | } |
| 419 | if (byte == delimiter) { |
| 420 | return; |
| 421 | } |
| 447 | 422 | |
| 448 | | /// Reads 1 byte from the stream or returns `error.EndOfStream`. |
| 449 | | pub fn readByte(self: &InStream) !u8 { |
| 450 | | var result: [1]u8 = undefined; |
| 451 | | try self.readNoEof(result[0..]); |
| 452 | | return result[0]; |
| 453 | | } |
| 423 | if (buf.len() == max_size) { |
| 424 | return error.StreamTooLong; |
| 425 | } |
| 454 | 426 | |
| 455 | | /// Same as `readByte` except the returned byte is signed. |
| 456 | | pub fn readByteSigned(self: &InStream) !i8 { |
| 457 | | return @bitCast(i8, try self.readByte()); |
| 458 | | } |
| 427 | try buf.appendByte(byte); |
| 428 | } |
| 429 | } |
| 459 | 430 | |
| 460 | | pub fn readIntLe(self: &InStream, comptime T: type) !T { |
| 461 | | return self.readInt(builtin.Endian.Little, T); |
| 462 | | } |
| 431 | /// Allocates enough memory to read until `delimiter`. If the allocated |
| 432 | /// memory would be greater than `max_size`, returns `error.StreamTooLong`. |
| 433 | /// Caller owns returned memory. |
| 434 | /// If this function returns an error, the contents from the stream read so far are lost. |
| 435 | pub fn readUntilDelimiterAlloc(self: &Self, allocator: &mem.Allocator, |
| 436 | delimiter: u8, max_size: usize) ![]u8 |
| 437 | { |
| 438 | var buf = Buffer.initNull(allocator); |
| 439 | defer buf.deinit(); |
| 440 | |
| 441 | try self.readUntilDelimiterBuffer(self, &buf, delimiter, max_size); |
| 442 | return buf.toOwnedSlice(); |
| 443 | } |
| 463 | 444 | |
| 464 | | pub fn readIntBe(self: &InStream, comptime T: type) !T { |
| 465 | | return self.readInt(builtin.Endian.Big, T); |
| 466 | | } |
| 445 | /// Returns the number of bytes read. If the number read is smaller than buf.len, it |
| 446 | /// means the stream reached the end. Reaching the end of a stream is not an error |
| 447 | /// condition. |
| 448 | pub fn read(self: &Self, buffer: []u8) !usize { |
| 449 | return self.readFn(self, buffer); |
| 450 | } |
| 467 | 451 | |
| 468 | | pub fn readInt(self: &InStream, endian: builtin.Endian, comptime T: type) !T { |
| 469 | | var bytes: [@sizeOf(T)]u8 = undefined; |
| 470 | | try self.readNoEof(bytes[0..]); |
| 471 | | return mem.readInt(bytes, T, endian); |
| 472 | | } |
| 452 | /// Same as `read` but end of stream returns `error.EndOfStream`. |
| 453 | pub fn readNoEof(self: &Self, buf: []u8) !void { |
| 454 | const amt_read = try self.read(buf); |
| 455 | if (amt_read < buf.len) return error.EndOfStream; |
| 456 | } |
| 473 | 457 | |
| 474 | | pub fn readVarInt(self: &InStream, endian: builtin.Endian, comptime T: type, size: usize) !T { |
| 475 | | assert(size <= @sizeOf(T)); |
| 476 | | assert(size <= 8); |
| 477 | | var input_buf: [8]u8 = undefined; |
| 478 | | const input_slice = input_buf[0..size]; |
| 479 | | try self.readNoEof(input_slice); |
| 480 | | return mem.readInt(input_slice, T, endian); |
| 481 | | } |
| 458 | /// Reads 1 byte from the stream or returns `error.EndOfStream`. |
| 459 | pub fn readByte(self: &Self) !u8 { |
| 460 | var result: [1]u8 = undefined; |
| 461 | try self.readNoEof(result[0..]); |
| 462 | return result[0]; |
| 463 | } |
| 482 | 464 | |
| 465 | /// Same as `readByte` except the returned byte is signed. |
| 466 | pub fn readByteSigned(self: &Self) !i8 { |
| 467 | return @bitCast(i8, try self.readByte()); |
| 468 | } |
| 483 | 469 | |
| 484 | | }; |
| 470 | pub fn readIntLe(self: &Self, comptime T: type) !T { |
| 471 | return self.readInt(builtin.Endian.Little, T); |
| 472 | } |
| 485 | 473 | |
| 486 | | pub const OutStream = struct { |
| 487 | | // TODO allow specifying the error set |
| 488 | | writeFn: fn(self: &OutStream, bytes: []const u8) error!void, |
| 474 | pub fn readIntBe(self: &Self, comptime T: type) !T { |
| 475 | return self.readInt(builtin.Endian.Big, T); |
| 476 | } |
| 489 | 477 | |
| 490 | | pub fn print(self: &OutStream, comptime format: []const u8, args: ...) !void { |
| 491 | | return std.fmt.format(self, error, self.writeFn, format, args); |
| 492 | | } |
| 478 | pub fn readInt(self: &Self, endian: builtin.Endian, comptime T: type) !T { |
| 479 | var bytes: [@sizeOf(T)]u8 = undefined; |
| 480 | try self.readNoEof(bytes[0..]); |
| 481 | return mem.readInt(bytes, T, endian); |
| 482 | } |
| 493 | 483 | |
| 494 | | pub fn write(self: &OutStream, bytes: []const u8) !void { |
| 495 | | return self.writeFn(self, bytes); |
| 496 | | } |
| 484 | pub fn readVarInt(self: &Self, endian: builtin.Endian, comptime T: type, size: usize) !T { |
| 485 | assert(size <= @sizeOf(T)); |
| 486 | assert(size <= 8); |
| 487 | var input_buf: [8]u8 = undefined; |
| 488 | const input_slice = input_buf[0..size]; |
| 489 | try self.readNoEof(input_slice); |
| 490 | return mem.readInt(input_slice, T, endian); |
| 491 | } |
| 492 | }; |
| 493 | } |
| 497 | 494 | |
| 498 | | pub fn writeByte(self: &OutStream, byte: u8) !void { |
| 499 | | const slice = (&byte)[0..1]; |
| 500 | | return self.writeFn(self, slice); |
| 501 | | } |
| 495 | pub fn OutStream(comptime Error: type) type { |
| 496 | return struct { |
| 497 | const Self = this; |
| 498 | |
| 499 | writeFn: fn(self: &Self, bytes: []const u8) Error!void, |
| 502 | 500 | |
| 503 | | pub fn writeByteNTimes(self: &OutStream, byte: u8, n: usize) !void { |
| 504 | | const slice = (&byte)[0..1]; |
| 505 | | var i: usize = 0; |
| 506 | | while (i < n) : (i += 1) { |
| 507 | | try self.writeFn(self, slice); |
| 501 | pub fn print(self: &Self, comptime format: []const u8, args: ...) !void { |
| 502 | return std.fmt.format(self, error, self.writeFn, format, args); |
| 508 | 503 | } |
| 509 | | } |
| 510 | | }; |
| 504 | |
| 505 | pub fn write(self: &Self, bytes: []const u8) !void { |
| 506 | return self.writeFn(self, bytes); |
| 507 | } |
| 508 | |
| 509 | pub fn writeByte(self: &Self, byte: u8) !void { |
| 510 | const slice = (&byte)[0..1]; |
| 511 | return self.writeFn(self, slice); |
| 512 | } |
| 513 | |
| 514 | pub fn writeByteNTimes(self: &Self, byte: u8, n: usize) !void { |
| 515 | const slice = (&byte)[0..1]; |
| 516 | var i: usize = 0; |
| 517 | while (i < n) : (i += 1) { |
| 518 | try self.writeFn(self, slice); |
| 519 | } |
| 520 | } |
| 521 | }; |
| 522 | } |
| 511 | 523 | |
| 512 | 524 | /// `path` may need to be copied in memory to add a null terminating byte. In this case |
| 513 | 525 | /// a fixed size buffer of size `std.os.max_noalloc_path_len` is an attempted solution. If the fixed |