1 /* 2 Gordon's Text-Utilities Library 3 Copyright (C) 2009-2013 Assaf Gordon (assafgordon@gmail.com) 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU Affero General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/> 17 */ 18 #ifndef __TEXT_FILE_READER__ 19 #define __TEXT_FILE_READER__ 20 21 #include <string> 22 #include <istream> 23 #include <sstream> 24 25 class TextLineReader 26 { 27 private: 28 std::istream& input_stream ; 29 size_t current_line_number; 30 std::string current_line_string ; 31 std::istringstream current_line_stream ; 32 bool unget_line_active ; 33 34 TextLineReader(const TextLineReader&); 35 TextLineReader& operator=(const TextLineReader&); 36 37 public: 38 TextLineReader(std::istream& _input_stream) ; 39 line_number()40 size_t line_number() const { return current_line_number ; } 41 42 bool next_line() ; 43 unget_line(const std::string & line)44 void unget_line ( const std::string& line ) 45 { 46 unget_line_active = true ; 47 current_line_string = line ; 48 } unget_current_line()49 void unget_current_line () { unget_line_active = true; } 50 51 //explicit conversions line_string()52 const std::string& line_string() const { return current_line_string; } line_stream()53 std::istringstream& line_stream() { return current_line_stream; } 54 55 //implicit conversions 56 operator const std::string& () const { return line_string() ; } string()57 operator std::string() const { return line_string(); } 58 operator std::istream& () { return line_stream(); } 59 60 }; 61 62 #endif 63 64