Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Input.h
1 /*
2  * Input class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2006-09, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_IO_INPUT_H
22 #define ELM_IO_INPUT_H
23 
24 #include <elm/enum_info.h>
25 #include <elm/meta.h>
26 #include <elm/types.h>
27 #include <elm/string/String.h>
28 #include <elm/string/CString.h>
29 #include <elm/io/InStream.h>
30 
31 namespace elm { namespace io {
32 
33 // Input class
34 class Input {
35 public:
36  Input(void);
38  inline InStream& stream(void) const { return *strm; };
39  inline void setStream(InStream& stream) { strm = &stream; buf = -1; };
40  inline bool ended() const { return state & ENDED; }
41  inline bool failed() const { return state & FAILED; }
42  inline bool error() const { return state & IO_ERROR; }
43  inline bool ok() const { return state == 0; }
44  inline void resetState() { state &= ~(FAILED | IO_ERROR); }
45 
46  bool scanBool(void);
47  char scanChar(void);
48  t::uint32 scanULong(int base = 0);
49  t::int32 scanLong(int base = 0);
50  t::uint64 scanULLong(int base = 0);
51  t::int64 scanLLong(int base = 0);
52  double scanDouble(void);
53  String scanWord(void);
54  String scanLine(void);
55  void swallow(char chr);
56  void swallow(CString str);
57  void swallow(const String& str);
58  void swallowBlank(void);
59 
60  inline Input& operator>>(bool& value) { value = scanBool(); return *this; };
61  inline Input& operator>>(char& value) { value = scanChar(); return *this; };
62  inline Input& operator>>(unsigned char& value) { value = scanULong(); return *this; };
63  inline Input& operator>>(short& value) { value = scanLong(); return *this; };
64  inline Input& operator>>(unsigned short& value) { value = scanULong(); return *this; };
65  inline Input& operator>>(int& value) { value = scanLong(); return *this; };
66  inline Input& operator>>(unsigned int& value) { value = scanULong(); return *this; };
67 # ifndef __LP64__
68  inline Input& operator>>(signed long& value) { value = scanLong(); return *this; };
69  inline Input& operator>>(unsigned long& value) { value = scanULong(); return *this; };
70 # else
71  inline Input& operator>>(signed long& value) { value = scanLLong(); return *this; };
72  inline Input& operator>>(unsigned long& value) { value = scanULLong(); return *this; };
73 # endif
74  inline Input& operator>>(signed long long& value) { value = scanLLong(); return *this; };
75  inline Input& operator>>(unsigned long long& value) { value= scanULLong(); return *this; };
76  inline Input& operator>>(float& value) { value = scanDouble(); return *this; };
77  inline Input& operator>>(double& value) { value = scanDouble(); return *this; };
78  inline Input& operator>>(String& value) { value = scanLine(); return *this; };
79 
80  inline Input& operator>>(const string& text) { swallow(text); return *this; }
81  inline Input& operator>>(cstring text) { swallow(text); return *this; }
82  inline Input& operator>>(const char *text) { swallow(cstring(text)); return *this; }
83 
84  template <class T> struct def_scanner { static inline void scan(Input& in, T& v) { unsupported(); } };
85  template <class T> struct enum_scanner { static inline void scan(Input& in, T& v) { string s; in >> s; v = enum_info<T>::fromString(s); } };
86  template <class T> Input& operator>>(T& v)
87  { _if<type_info<T>::is_defined_enum, enum_scanner<T>, def_scanner<T> >::scan(*this, v); return *this; }
88 
89  class LineIter: public PreIterator<LineIter, string> {
90  public:
91  inline LineIter(Input& input, bool end = false): in(input), e(end) { if(!e) next(); }
92  inline bool ended() const { return e; }
93  inline string item() const { return l; }
94  inline void next() { if(in.ended()) e = true; else l = in.scanLine(); }
95  inline bool equals(const LineIter& i) const { return &in == &i.in && e == i.e; }
96  private:
97  Input& in;
98  bool e;
99  string l;
100  };
101 
102  class LineRange {
103  public:
104  inline LineRange(Input& in): i(in) { }
105  inline LineIter begin() { return LineIter(i); }
106  inline LineIter end() { return LineIter(i, true); }
107  private:
108  Input& i;
109  };
110  inline LineRange lines() { return LineRange(*this); }
111 
112 
113 private:
114  [[noreturn]] static void unsupported(void);
115  InStream *strm;
116  t::int16 buf;
117  t::uint16 state;
118  int get();
119  int skip();
120  void back(int chr);
121  static const t::uint16
122  ENDED = 0x01,
123  FAILED = 0x02,
124  IO_ERROR = 0x04;
125 };
126 
127 } } // elm::io
128 
129 #endif // ELM_IO_INPUT_H
elm::io::Input::scanLong
t::int32 scanLong(int base=0)
Definition: io_Input.cpp:317
elm::io::Input::LineIter
Definition: Input.h:89
elm::io::Input::scanLLong
t::int64 scanLLong(int base=0)
Definition: io_Input.cpp:406
elm::io::Input::scanChar
char scanChar(void)
Definition: io_Input.cpp:223
elm::io::Input::scanULLong
t::uint64 scanULLong(int base=0)
Definition: io_Input.cpp:350
elm::io::Input::LineRange::begin
LineIter begin()
Definition: Input.h:105
elm::io::Input::operator>>
Input & operator>>(unsigned long &value)
Definition: Input.h:69
elm::_if
Definition: meta.h:43
elm::io::Input::operator>>
Input & operator>>(T &v)
Definition: Input.h:86
elm::t::int32
int int32
Definition: arch.h:30
elm::t::uint16
unsigned short uint16
Definition: arch.h:29
elm::io::Input::scanLine
String scanLine(void)
Definition: io_Input.cpp:528
elm::io::Input::LineIter::item
string item() const
Definition: Input.h:93
elm::io::Input::scanULong
t::uint32 scanULong(int base=0)
Definition: io_Input.cpp:263
elm::io::base
IntFormat base(int base, IntFormat fmt)
Definition: Output.h:256
elm::io::Input::operator>>
Input & operator>>(short &value)
Definition: Input.h:63
elm::io::Input::operator>>
Input & operator>>(char &value)
Definition: Input.h:61
elm::io::Input::LineIter::next
void next()
Definition: Input.h:94
elm::t::int64
long int64
Definition: arch.h:32
elm::cstring
CString cstring
Definition: CString.h:62
elm::io::Input::operator>>
Input & operator>>(signed long &value)
Definition: Input.h:68
elm::io::Input::operator>>
Input & operator>>(unsigned long long &value)
Definition: Input.h:75
value
elm::io::Input::swallowBlank
void swallowBlank(void)
Definition: io_Input.cpp:586
elm::io::Input::error
bool error() const
Definition: Input.h:42
elm::CString
Definition: CString.h:17
elm::io::Input::operator>>
Input & operator>>(const char *text)
Definition: Input.h:82
elm::io::Input::setStream
void setStream(InStream &stream)
Definition: Input.h:39
elm::io::Input::operator>>
Input & operator>>(signed long long &value)
Definition: Input.h:74
elm::io::Input::def_scanner::scan
static void scan(Input &in, T &v)
Definition: Input.h:84
elm::t::int16
short int16
Definition: arch.h:28
elm::io::Input::failed
bool failed() const
Definition: Input.h:41
elm::io::Input::enum_scanner
Definition: Input.h:85
elm::enum_info::fromString
static T fromString(const string &name)
Definition: enum_info.h:47
elm::io::Input::resetState
void resetState()
Definition: Input.h:44
elm::io::Input::scanDouble
double scanDouble(void)
Definition: io_Input.cpp:436
elm::io::Input::operator>>
Input & operator>>(double &value)
Definition: Input.h:77
elm
Definition: adapter.h:26
elm::io::Input::LineIter::equals
bool equals(const LineIter &i) const
Definition: Input.h:95
elm::io::Input::ok
bool ok() const
Definition: Input.h:43
elm::t::uint64
unsigned long uint64
Definition: arch.h:33
elm::io::Input::ended
bool ended() const
Definition: Input.h:40
elm::io::Input::LineIter::ended
bool ended() const
Definition: Input.h:92
elm::io::Input::scanBool
bool scanBool(void)
Definition: io_Input.cpp:180
elm::io::Input::Input
Input(void)
Definition: io_Input.cpp:50
elm::io::Input::enum_scanner::scan
static void scan(Input &in, T &v)
Definition: Input.h:85
elm::io::Input::operator>>
Input & operator>>(unsigned short &value)
Definition: Input.h:64
elm::io::Input
Definition: Input.h:34
elm::io::Input::operator>>
Input & operator>>(String &value)
Definition: Input.h:78
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::io::Input::swallow
void swallow(char chr)
Definition: io_Input.cpp:549
elm::io::Input::operator>>
Input & operator>>(const string &text)
Definition: Input.h:80
elm::io::Input::LineIter::LineIter
LineIter(Input &input, bool end=false)
Definition: Input.h:91
elm::io::Input::scanWord
String scanWord(void)
Definition: io_Input.cpp:506
elm::String
Definition: String.h:30
elm::io::Input::LineRange::LineRange
LineRange(Input &in)
Definition: Input.h:104
elm::io::Input::LineRange::end
LineIter end()
Definition: Input.h:106
elm::io::Input::operator>>
Input & operator>>(cstring text)
Definition: Input.h:81
elm::io::Input::operator>>
Input & operator>>(int &value)
Definition: Input.h:65
elm::str
string str(const char *s)
Definition: String.h:150
elm::io::Input::LineRange
Definition: Input.h:102
elm::io::in
sys::SystemInStream & in
Definition: system_SystemIO.cpp:116
elm::io::Input::operator>>
Input & operator>>(unsigned char &value)
Definition: Input.h:62
elm::PreIterator
Definition: iter.h:28
elm::io::Input::lines
LineRange lines()
Definition: Input.h:110
elm::io::Input::operator>>
Input & operator>>(bool &value)
Definition: Input.h:60
elm::io::Input::stream
InStream & stream(void) const
Definition: Input.h:38
elm::io::Input::operator>>
Input & operator>>(float &value)
Definition: Input.h:76
elm::io::Input::operator>>
Input & operator>>(unsigned int &value)
Definition: Input.h:66
elm::io::InStream
Definition: InStream.h:29
elm::io::Input::def_scanner
Definition: Input.h:84