├── LICENSE ├── kattio.pl ├── Kattio.cs └── Kattio.java /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2011-2015 Kattis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /kattio.pl: -------------------------------------------------------------------------------- 1 | % Prolog predicates for tokenized reading from stdin. 2 | % 3 | % Provides the following predicates: 4 | % read_string(S): reads a string token 5 | % read_int(I): reads an integer token 6 | % read_atom(A): reads an atom 7 | % 8 | % For all three predicates, the result is unified with end_of_file 9 | % if the end of the input stream was reached. 10 | % 11 | 12 | :- module(kattio, [read_string/1, read_int/1, read_atom/1]). 13 | 14 | read_string(S) :- 15 | read_token_codes(S). 16 | 17 | read_int(I) :- 18 | read_token_codes(Codes), 19 | (Codes == end_of_file -> I = Codes ; number_codes(I, Codes)). 20 | 21 | read_atom(A) :- 22 | read_token_codes(Codes), 23 | (Codes == end_of_file -> A = Codes ; atom_codes(A, Codes)). 24 | 25 | 26 | % Internal predicate for getting the next token 27 | 28 | read_token_codes(end_of_file) :- 29 | peek_code(end_of_file), !. 30 | 31 | read_token_codes(Codes) :- 32 | peek_code(C), 33 | \+ code_type(C, space), !, 34 | read_token_codes_helper(Codes). 35 | 36 | read_token_codes(T) :- 37 | get_char(_), !, 38 | read_token_codes(T). 39 | 40 | 41 | read_token_codes_helper([C0|C]) :- 42 | peek_code(C0), 43 | \+ code_type(C0, space), !, 44 | get_code(C0), 45 | read_token_codes_helper(C). 46 | 47 | read_token_codes_helper([]). 48 | -------------------------------------------------------------------------------- /Kattio.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace Kattis.IO 5 | { 6 | public class NoMoreTokensException : Exception 7 | { 8 | } 9 | 10 | public class Tokenizer 11 | { 12 | string[] tokens = new string[0]; 13 | private int pos; 14 | StreamReader reader; 15 | 16 | public Tokenizer(Stream inStream) 17 | { 18 | var bs = new BufferedStream(inStream); 19 | reader = new StreamReader(bs); 20 | } 21 | 22 | public Tokenizer() : this(Console.OpenStandardInput()) 23 | { 24 | // Nothing more to do 25 | } 26 | 27 | private string PeekNext() 28 | { 29 | if (pos < 0) 30 | // pos < 0 indicates that there are no more tokens 31 | return null; 32 | if (pos < tokens.Length) 33 | { 34 | if (tokens[pos].Length == 0) 35 | { 36 | ++pos; 37 | return PeekNext(); 38 | } 39 | return tokens[pos]; 40 | } 41 | string line = reader.ReadLine(); 42 | if (line == null) 43 | { 44 | // There is no more data to read 45 | pos = -1; 46 | return null; 47 | } 48 | // Split the line that was read on white space characters 49 | tokens = line.Split(null); 50 | pos = 0; 51 | return PeekNext(); 52 | } 53 | 54 | public bool HasNext() 55 | { 56 | return (PeekNext() != null); 57 | } 58 | 59 | public string Next() 60 | { 61 | string next = PeekNext(); 62 | if (next == null) 63 | throw new NoMoreTokensException(); 64 | ++pos; 65 | return next; 66 | } 67 | } 68 | 69 | public class Scanner : Tokenizer 70 | { 71 | public int NextInt() 72 | { 73 | return int.Parse(Next()); 74 | } 75 | 76 | public long NextLong() 77 | { 78 | return long.Parse(Next()); 79 | } 80 | 81 | public float NextFloat() 82 | { 83 | return float.Parse(Next()); 84 | } 85 | 86 | public double NextDouble() 87 | { 88 | return double.Parse(Next()); 89 | } 90 | } 91 | 92 | public class BufferedStdoutWriter : StreamWriter 93 | { 94 | public BufferedStdoutWriter() : base(new BufferedStream(Console.OpenStandardOutput())) 95 | { 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Kattio.java: -------------------------------------------------------------------------------- 1 | /** Simple yet moderately fast I/O routines. 2 | * 3 | * Example usage: 4 | * 5 | * Kattio io = new Kattio(System.in, System.out); 6 | * 7 | * while (io.hasMoreTokens()) { 8 | * int n = io.getInt(); 9 | * double d = io.getDouble(); 10 | * double ans = d*n; 11 | * 12 | * io.println("Answer: " + ans); 13 | * } 14 | * 15 | * io.close(); 16 | * 17 | * 18 | * Some notes: 19 | * 20 | * - When done, you should always do io.close() or io.flush() on the 21 | * Kattio-instance, otherwise, you may lose output. 22 | * 23 | * - The getInt(), getDouble(), and getLong() methods will throw an 24 | * exception if there is no more data in the input, so it is generally 25 | * a good idea to use hasMoreTokens() to check for end-of-file. 26 | * 27 | * @author: Kattis 28 | */ 29 | 30 | import java.util.StringTokenizer; 31 | import java.io.BufferedReader; 32 | import java.io.BufferedOutputStream; 33 | import java.io.IOException; 34 | import java.io.InputStream; 35 | import java.io.InputStreamReader; 36 | import java.io.PrintWriter; 37 | import java.io.OutputStream; 38 | 39 | class Kattio extends PrintWriter { 40 | public Kattio(InputStream i) { 41 | super(new BufferedOutputStream(System.out)); 42 | this.r = new BufferedReader(new InputStreamReader(i)); 43 | } 44 | 45 | public Kattio(InputStream i, OutputStream o) { 46 | super(new BufferedOutputStream(o)); 47 | this.r = new BufferedReader(new InputStreamReader(i)); 48 | } 49 | 50 | public boolean hasMoreTokens() { 51 | return this.peekToken() != null; 52 | } 53 | 54 | public int getInt() { 55 | return Integer.parseInt(this.nextToken()); 56 | } 57 | 58 | public double getDouble() { 59 | return Double.parseDouble(this.nextToken()); 60 | } 61 | 62 | public long getLong() { 63 | return Long.parseLong(this.nextToken()); 64 | } 65 | 66 | public String getWord() { 67 | return this.nextToken(); 68 | } 69 | 70 | private BufferedReader r; 71 | private String line; 72 | private StringTokenizer st; 73 | private String token; 74 | 75 | private String peekToken() { 76 | if (this.token == null) { 77 | try { 78 | while (this.st == null || !this.st.hasMoreTokens()) { 79 | this.line = this.r.readLine(); 80 | if (this.line == null) { 81 | return null; 82 | } 83 | this.st = new StringTokenizer(this.line); 84 | } 85 | this.token = this.st.nextToken(); 86 | } catch (IOException e) {} 87 | } 88 | 89 | return this.token; 90 | } 91 | 92 | private String nextToken() { 93 | String ans = this.peekToken(); 94 | this.token = null; 95 | return ans; 96 | } 97 | } 98 | --------------------------------------------------------------------------------