uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StringReader.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_CORE_STRINGREADER_H
29 #define U_CORE_STRINGREADER_H
30 
31 #include <istream>
32 #include <algorithm>
33 
34 #include <boost/lexical_cast.hpp>
35 #include <boost/algorithm/string.hpp>
36 
37 
38 // RIPENSARE COMPLETAMENTE !!!
39 
40 namespace uLib {
41 
42 struct StringReader {
43 
44  std::istream & m_is;
45 
46  StringReader(std::istream &is) : m_is(is) {}
47 
48  void ReadString(const char *str) {
49  // // clean compared string //
50  // char chars[] = " \t\n";
51  // for (unsigned int i = 0; i < strlen(chars); ++i)
52  // { cmps.erase (std::remove(str.begin(), str.end(), chars[i]), str.end()); }
53 
54  // SISTEMARE !!!
55 
56  int pos = 0;
57  char c;
58  while( pos < strlen(str) ) {
59  while( IsEscape(str[pos])) pos++;
60  if ( pos >= strlen(str) ) break;
61  while( IsEscape(c = m_is.get()) );
62  if (c == str[pos] ) pos++;
63  else pos = 0;
64  }
65  }
66 
67  static inline bool IsEscape(char c){
68  const char *escapes = " \r\n\t";
69  for ( int i = 0 ; i < 4 ; ++i) {
70  if(c == escapes[i]) return true;
71  }
72  return false;
73  }
74 
75 };
76 
77 template <typename T>
78 inline StringReader & operator >> (StringReader &isr, T &t) {
79  isr.m_is >> t;
80  return isr;
81 }
82 
83 inline StringReader & operator >> (StringReader &isr, const char *str) {
84  isr.ReadString(str);
85  return isr;
86 }
87 
88 
89 
90 } // uLib
91 
92 
93 #endif // STRINGREADER_H