OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
escape.hpp
Go to the documentation of this file.
1 #ifndef COMPONENTS_FILES_ESCAPE_HPP
2 #define COMPONENTS_FILES_ESCAPE_HPP
3 
4 #include <queue>
5 
7 
8 #include <boost/iostreams/filtering_stream.hpp>
9 #include <boost/filesystem/path.hpp>
10 #include <boost/program_options.hpp>
11 
15 namespace Files
16 {
21  {
22  static const int sEscape;
23  static const int sHashIdentifier;
24  static const int sEscapeIdentifier;
25 
27  virtual ~escape_hash_filter();
28 
29  template <typename Source> int get(Source & src);
30 
31  private:
32  std::queue<int> mNext;
33 
36  };
37 
38  template <typename Source>
39  int escape_hash_filter::get(Source & src)
40  {
41  if (mNext.empty())
42  {
43  int character = boost::iostreams::get(src);
44  if (character == boost::iostreams::WOULD_BLOCK)
45  {
46  mNext.push(character);
47  }
48  else if (character == EOF)
49  {
50  mSeenNonWhitespace = false;
51  mFinishLine = false;
52  mNext.push(character);
53  }
54  else if (character == '\n')
55  {
56  mSeenNonWhitespace = false;
57  mFinishLine = false;
58  mNext.push(character);
59  }
60  else if (mFinishLine)
61  {
62  mNext.push(character);
63  }
64  else if (character == '#')
65  {
67  {
68  mNext.push(sEscape);
69  mNext.push(sHashIdentifier);
70  }
71  else
72  {
73  //it's fine being interpreted by Boost as a comment, and so is anything afterwards
74  mNext.push(character);
75  mFinishLine = true;
76  }
77  }
78  else if (character == sEscape)
79  {
80  mNext.push(sEscape);
82  }
83  else
84  {
85  mNext.push(character);
86  }
87  if (!mSeenNonWhitespace && !isspace(character))
88  mSeenNonWhitespace = true;
89  }
90  int retval = mNext.front();
91  mNext.pop();
92  return retval;
93  }
94 
96  {
98  virtual ~unescape_hash_filter();
99 
100  template <typename Source> int get(Source & src);
101 
102  private:
104  };
105 
106  template <typename Source>
107  int unescape_hash_filter::get(Source & src)
108  {
109  int character;
110  if (!expectingIdentifier)
111  character = boost::iostreams::get(src);
112  else
113  {
114  character = escape_hash_filter::sEscape;
115  expectingIdentifier = false;
116  }
117  if (character == escape_hash_filter::sEscape)
118  {
119  int nextChar = boost::iostreams::get(src);
120  int intended;
122  intended = escape_hash_filter::sEscape;
123  else if (nextChar == escape_hash_filter::sHashIdentifier)
124  intended = '#';
125  else if (nextChar == boost::iostreams::WOULD_BLOCK)
126  {
127  expectingIdentifier = true;
128  intended = nextChar;
129  }
130  else
131  intended = '?';
132  return intended;
133  }
134  else
135  return character;
136  }
137 
142  {
143  private:
144  std::string mData;
145  public:
146  static std::string processString(const std::string & str);
147 
149  EscapeHashString(const std::string & str);
150  EscapeHashString(const std::string & str, size_t pos, size_t len = std::string::npos);
151  EscapeHashString(const char * s);
152  EscapeHashString(const char * s, size_t n);
153  EscapeHashString(size_t n, char c);
154  template <class InputIterator>
155  EscapeHashString(InputIterator first, InputIterator last);
156 
157  std::string toStdString() const;
158 
159  friend std::ostream & operator<< (std::ostream & os, const EscapeHashString & eHS);
160  };
161 
162  std::istream & operator>> (std::istream & is, EscapeHashString & eHS);
163 
165  {
166  std::vector<Files::EscapeHashString> mVector;
167 
169  virtual ~EscapeStringVector();
170 
171  std::vector<std::string> toStdStringVector() const;
172  };
173 
174  //boost program options validation
175 
176  void validate(boost::any &v, const std::vector<std::string> &tokens, Files::EscapeHashString * eHS, int a);
177 
178  void validate(boost::any &v, const std::vector<std::string> &tokens, EscapeStringVector *, int);
179 
180  struct EscapePath
181  {
182  boost::filesystem::path mPath;
183 
184  static PathContainer toPathContainer(const std::vector<EscapePath> & escapePathContainer);
185  };
186 
187  typedef std::vector<EscapePath> EscapePathContainer;
188 
189  std::istream & operator>> (std::istream & istream, EscapePath & escapePath);
190 } /* namespace Files */
191 #endif /* COMPONENTS_FILES_ESCAPE_HPP */
Definition: escape.hpp:141
escape_hash_filter()
Definition: escape.cpp:11
bool expectingIdentifier
Definition: escape.hpp:103
Definition: escape.hpp:95
unescape_hash_filter()
Definition: escape.cpp:19
static std::string processString(const std::string &str)
Definition: escape.cpp:27
int get(Source &src)
Definition: escape.hpp:107
EscapeStringVector()
Definition: escape.cpp:88
std::vector< EscapePath > EscapePathContainer
Definition: escape.hpp:187
virtual ~unescape_hash_filter()
Definition: escape.cpp:23
static const int sEscapeIdentifier
Definition: escape.hpp:24
bool mFinishLine
Definition: escape.hpp:35
bool mSeenNonWhitespace
Definition: escape.hpp:34
Definition: escape.hpp:180
friend std::ostream & operator<<(std::ostream &os, const EscapeHashString &eHS)
Definition: escape.cpp:82
static PathContainer toPathContainer(const std::vector< EscapePath > &escapePathContainer)
Definition: escape.cpp:127
virtual ~escape_hash_filter()
Definition: escape.cpp:15
std::string toStdString() const
Definition: escape.cpp:69
std::istream & operator>>(std::istream &is, EscapeHashString &eHS)
Definition: escape.cpp:74
Definition: escape.hpp:20
std::string mData
Definition: escape.hpp:144
virtual ~EscapeStringVector()
Definition: escape.cpp:92
static const int sHashIdentifier
Definition: escape.hpp:23
State & get()
Definition: state.cpp:664
static const int sEscape
Definition: escape.hpp:22
EscapeHashString()
Definition: escape.cpp:40
std::vector< std::string > toStdStringVector() const
Definition: escape.cpp:96
void validate(boost::any &v, const std::vector< std::string > &tokens, Files::EscapeHashString *eHS, int a)
Definition: escape.cpp:108
boost::filesystem::path mPath
Definition: escape.hpp:182
int get(Source &src)
Definition: escape.hpp:39
std::vector< boost::filesystem::path > PathContainer
Definition: gamesettings.hpp:14
std::queue< int > mNext
Definition: escape.hpp:32
std::vector< Files::EscapeHashString > mVector
Definition: escape.hpp:166
Definition: escape.hpp:164