2017-11-07 08:22:27 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const io = std.io;
|
|
|
|
const os = std.os;
|
|
|
|
|
|
|
|
pub fn main() -> %void {
|
|
|
|
// TODO use a more general purpose allocator here
|
2018-01-09 05:07:01 +00:00
|
|
|
var inc_allocator = try std.heap.IncrementingAllocator.init(5 * 1024 * 1024);
|
2017-11-07 08:22:27 +00:00
|
|
|
defer inc_allocator.deinit();
|
|
|
|
const allocator = &inc_allocator.allocator;
|
|
|
|
|
|
|
|
var args_it = os.args();
|
|
|
|
|
|
|
|
if (!args_it.skip()) @panic("expected self arg");
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
const in_file_name = try (args_it.next(allocator) ?? @panic("expected input arg"));
|
2017-11-07 08:22:27 +00:00
|
|
|
defer allocator.free(in_file_name);
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
const out_file_name = try (args_it.next(allocator) ?? @panic("expected output arg"));
|
2017-11-07 08:22:27 +00:00
|
|
|
defer allocator.free(out_file_name);
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
var in_file = try io.File.openRead(in_file_name, allocator);
|
2017-11-07 08:22:27 +00:00
|
|
|
defer in_file.close();
|
|
|
|
|
2018-01-09 05:07:01 +00:00
|
|
|
var out_file = try io.File.openWrite(out_file_name, allocator);
|
2017-11-07 08:22:27 +00:00
|
|
|
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);
|
2018-01-09 05:07:01 +00:00
|
|
|
try buffered_out_stream.flush();
|
2017-11-07 08:22:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const State = enum {
|
|
|
|
Start,
|
|
|
|
Derp,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO look for code segments
|
|
|
|
|
2017-12-14 02:53:52 +00:00
|
|
|
fn gen(in: &io.InStream, out: &io.OutStream) {
|
2017-11-07 08:22:27 +00:00
|
|
|
var state = State.Start;
|
|
|
|
while (true) {
|
2018-01-07 22:28:20 +00:00
|
|
|
const byte = in.readByte() catch |err| {
|
2017-11-07 08:22:27 +00:00
|
|
|
if (err == error.EndOfStream) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-22 05:50:30 +00:00
|
|
|
std.debug.panic("{}", err);
|
2017-11-07 08:22:27 +00:00
|
|
|
};
|
|
|
|
switch (state) {
|
|
|
|
State.Start => switch (byte) {
|
|
|
|
else => {
|
2018-01-09 05:07:01 +00:00
|
|
|
out.writeByte(byte) catch unreachable;
|
2017-11-07 08:22:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
State.Derp => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|