zig/doc/docgen.zig
Andrew Kelley 4543413491 std.io: introduce buffered I/O and change API
I started working on #465 and made some corresponding std.io
API changes.

New structs:
 * std.io.FileInStream
 * std.io.FileOutStream
 * std.io.BufferedOutStream
 * std.io.BufferedInStream

Removed:
 * std.io.File.in_stream
 * std.io.File.out_stream

Now instead of &file.out_stream or &file.in_stream to get access to
the stream API for a file, you get it like this:

var file_in_stream = io.FileInStream.init(&file);
const in_stream = &file_in_stream.stream;

var file_out_stream = io.FileOutStream.init(&file);
const out_stream = &file_out_stream.stream;

This is evidence that we might not need any OOP features -
See #130.
2017-11-07 03:22:27 -05:00

64 lines
1.8 KiB
Zig

const std = @import("std");
const io = std.io;
const os = std.os;
pub fn main() -> %void {
// TODO use a more general purpose allocator here
var inc_allocator = %%std.heap.IncrementingAllocator.init(5 * 1024 * 1024);
defer inc_allocator.deinit();
const allocator = &inc_allocator.allocator;
var args_it = os.args();
if (!args_it.skip()) @panic("expected self arg");
const in_file_name = %%(args_it.next(allocator) ?? @panic("expected input arg"));
defer allocator.free(in_file_name);
const out_file_name = %%(args_it.next(allocator) ?? @panic("expected output arg"));
defer allocator.free(out_file_name);
var in_file = %%io.File.openRead(in_file_name, allocator);
defer in_file.close();
var out_file = %%io.File.openWrite(out_file_name, allocator);
defer out_file.close();
var file_in_stream = io.FileInStream.init(&in_file);
var buffered_in_stream = io.BufferedInStream.init(&file_in_stream.stream);
var file_out_stream = io.FileOutStream.init(&out_file);
var buffered_out_stream = io.BufferedOutStream.init(&file_out_stream.stream);
gen(&buffered_in_stream.stream, &buffered_out_stream.stream);
%%buffered_out_stream.flush();
}
const State = enum {
Start,
Derp,
};
// TODO look for code segments
fn gen(in: &io.InStream, out: &const io.OutStream) {
var state = State.Start;
while (true) {
const byte = in.readByte() %% |err| {
if (err == error.EndOfStream) {
return;
}
std.debug.panic("{}", err)
};
switch (state) {
State.Start => switch (byte) {
else => {
%%out.writeByte(byte);
},
},
State.Derp => unreachable,
}
}
}