1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::io;
use std::io::Read;
/**
Types of valid readers for binput.

* binput::Stdin uses input from stdin
* binput::Str(s) uses input from string s(mainly for testing purposes)

*/
pub enum Readers {
    Stdin,
    Str(String),
}

/**
Bin is a struct to store information about the reader and has the input functions.
*/
pub struct Bin {
    reader: Readers,
    line_number: usize,
}

impl Bin {
    pub fn bin(reader: Readers) -> Bin {
        Bin {reader: reader, line_number: 0}
    }
    fn get_line(&mut self) -> String {
        let mut input = String::new();
        match self.reader {
            Readers::Stdin => {
                io::stdin().read_line(&mut input)
                    .expect("Failed to read line");
            },
            Readers::Str(ref s) => {
                let mut tmp = String::new();
                s.as_bytes().read_to_string(&mut tmp)
                    .expect("Failed to read line");
                input = String::from(tmp.lines().nth(self.line_number).unwrap());
            },
        };
        self.line_number += 1;
        input
    }
    /**
    Returns a String containing next line of stdin.
    # Remarks
    This function will panic if if fails to read a line.
    */
    pub fn read_line(&mut self) -> String {
        self.get_line()
    }

    /**
    Returns a (isize) int containing next line of stdin.
    # Remarks
    This function will panic if if fails to read a line, or if it fails to translate the next line to an integer.
    This function will remove whitespace according to trim()
    */
    pub fn read_int(&mut self) -> isize {
        let input = self.read_line();
        input.trim().parse().unwrap()
    }
}