tzt2tzt/src/main.zig

94 lines
2.3 KiB
Zig

const std = @import("std");
const diccionario = [_][]const u8{
"e_/¯¯-____",
"a___/_//",
"r__//¯_/",
"d/¯_/¯__",
"l/_/ _//",
"u___///¯",
"v/¯/¯-__",
"x/¯¯--_/",
"n¯//_-/",
"i_/-/¯¯",
"c¯¯/__-",
"t-/__/¯",
"z//_/__",
"o__-//",
"s-//-_",
"p///_-",
"b__//¯",
"q--_//",
"h/¯-¯/",
"f_¯/__",
"g/ /_",
"y_//",
"ñ//-",
"k--/",
"m/_",
"w-",
"j/",
};
pub fn txt2castellano(
allocator: *std.mem.Allocator,
stdin: std.fs.File.Reader,
stdout: std.fs.File,
) !void {
const input = try stdin.readAllAlloc(allocator, 1024 * 1024);
defer allocator.free(input);
var ventana: []u8 = input[0..];
ventanaLoop: while (ventana.len != 0) {
for (diccionario) |letra| {
if (std.mem.startsWith(u8, ventana, letra[1..])) {
try stdout.writeAll(letra[0..1]);
ventana = ventana[letra[1..].len..];
continue :ventanaLoop;
}
}
try stdout.writeAll(ventana[0..1]);
ventana = ventana[1..];
}
}
pub fn castellano2txt(stdin: std.fs.File.Reader, stdout: std.fs.File) !void {
stdin: while (stdin.readByte() catch null) |byte| {
for (diccionario) |letra| {
if (letra[0] == byte) {
try stdout.writeAll(letra[1..]);
continue :stdin;
}
}
try stdout.writeAll(&[_]u8{byte});
}
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = &gpa.allocator;
defer _ = gpa.deinit();
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut();
const stderr = std.io.getStdErr();
var args = std.process.args();
if (!args.skip()) unreachable;
if (args.next(allocator)) |arg_union| {
const arg = try arg_union;
if (std.mem.eql(u8, arg, "castellano")) {
try castellano2txt(stdin, stdout);
return;
} else if (std.mem.eql(u8, arg, "txt")) {
try txt2castellano(allocator, stdin, stdout);
return;
}
}
try stderr.writeAll(
\\tzt2tzt {castellano|txt} < {archivo de texto}
\\Castellano si tenés castellano y querés traducir a txt, y viceversa
\\
);
}