From e561b67a4f184a29efae1cf290e6f9ace3151248 Mon Sep 17 00:00:00 2001 From: "Tim \"S.D.Eagle\" Zeitz" Date: Tue, 16 Oct 2018 18:22:06 +0200 Subject: [PATCH] translate encode_vector --- src/bin/encode_vector.rs | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/bin/encode_vector.rs diff --git a/src/bin/encode_vector.rs b/src/bin/encode_vector.rs new file mode 100644 index 0000000..ebd1c73 --- /dev/null +++ b/src/bin/encode_vector.rs @@ -0,0 +1,82 @@ +extern crate stud_rust_base; +use stud_rust_base::io::*; +use std::env; + +fn main() { + let mut args = env::args(); + args.next(); + + match &args.collect::>()[..] { + [data_type, output] => { + match data_type.as_ref() { + "i8" => parse_input::().write_to(output).expect("Failed to write to output"), + "u8" => parse_input::().write_to(output).expect("Failed to write to output"), + "i16" => parse_input::().write_to(output).expect("Failed to write to output"), + "u16" => parse_input::().write_to(output).expect("Failed to write to output"), + "i32" => parse_input::().write_to(output).expect("Failed to write to output"), + "u32" => parse_input::().write_to(output).expect("Failed to write to output"), + "i64" => parse_input::().write_to(output).expect("Failed to write to output"), + "u64" => parse_input::().write_to(output).expect("Failed to write to output"), + "f32" => parse_input::().write_to(output).expect("Failed to write to output"), + "f64" => parse_input::().write_to(output).expect("Failed to write to output"), + "int8" => parse_input::().write_to(output).expect("Failed to write to output"), + "uint8" => parse_input::().write_to(output).expect("Failed to write to output"), + "int16" => parse_input::().write_to(output).expect("Failed to write to output"), + "uint16" => parse_input::().write_to(output).expect("Failed to write to output"), + "int32" => parse_input::().write_to(output).expect("Failed to write to output"), + "uint32" => parse_input::().write_to(output).expect("Failed to write to output"), + "int64" => parse_input::().write_to(output).expect("Failed to write to output"), + "uint64" => parse_input::().write_to(output).expect("Failed to write to output"), + "float32" => parse_input::().write_to(output).expect("Failed to write to output"), + "float64" => parse_input::().write_to(output).expect("Failed to write to output"), + _ => { + print_usage(); + panic!("Unknown data_type {}", data_type); + } + }; + }, + _ => { + print_usage(); + panic!("Invalid input") + }, + } +} + +fn print_usage() { + eprintln!("Usage: encode_vector data_type output_vector_file + +Reads textual data from the standard input and writes it in a binary format to output_vector_file. The input data should be one data element per line. The data is only written once an end of file is encountered on the input. data_type can be one of +* i8 +* u8 +* i16 +* u16 +* i32 +* u32 +* i64 +* u64 +* f32 +* f64 + +"); +} + +use std::{ + str::FromStr, + fmt::Debug, +}; + +fn parse_input() -> Vec where + T: FromStr, + ::Err: Debug +{ + use std::io::{BufRead, stdin}; + + let mut values = Vec::new(); + + let stdin = stdin(); + for line in stdin.lock().lines() { + values.push(line.unwrap().parse::().unwrap()) + } + + values +} -- 2.34.1