TextReader.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef TEXTREADER_H_
32 #define TEXTREADER_H_
33 
34 #include <iostream>
35 #include <sstream>
36 #include <list>
37 #include <string>
38 
39 
40 
41 namespace drain
42 {
43 
45 
49 class TextReader {
50 
51 public:
52 
54 
59  static inline
60  std::string scanSegment(std::istream & istr, const std::string & endChars){
61  std::stringstream sstr;
62  scanSegment(istr, endChars, sstr);
63  return sstr.str();
64  }
65 
67 
70  static
71  char scanSegment(std::istream & istr, const std::string & endChars, std::ostream & ostr);
72 
76  template <class T>
77  static
78  char scanSegmentToValue(std::istream & istr, const std::string & endChars, T & dst);
79 
81 
84  static
85  char skipChars(std::istream & istr, const std::string skipChars); // = " \t\n\r"
86 
90  static inline
91  char skipWhiteSpace(std::istream & istr){
92  return skipChars(istr, " \t\n\r");
93  }
94 
95 };
96 
97 // Specified implementation
98 template <>
99 char TextReader::scanSegmentToValue(std::istream & istr, const std::string & endChars, std::string & dst);
100 
101 // Default implementation for basic (numeric) types
102 template <class T>
103 char TextReader::scanSegmentToValue(std::istream & istr, const std::string & endChars, T & dst){
104  std::stringstream sstr;
105  char result = scanSegment(istr, endChars, sstr);
106  sstr >> dst;
107  return result;
108 }
109 
110 
111 
112 } // ::drain
113 
114 #endif
Utility for scanning text segments.
Definition: TextReader.h:49
static char skipChars(std::istream &istr, const std::string skipChars)
Read stream until a char not in skipChars or end-of-file is encountered.
Definition: TextReader.cpp:53
static char skipWhiteSpace(std::istream &istr)
Definition: TextReader.h:91
static char scanSegmentToValue(std::istream &istr, const std::string &endChars, T &dst)
Definition: TextReader.h:103
static std::string scanSegment(std::istream &istr, const std::string &endChars)
Read input stream until any char in endChars is encountered. The end char will not be included,...
Definition: TextReader.h:60
Definition: DataSelector.cpp:1277