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  int mPrevious;
34 
37  };
38 
39  template <typename Source>
40  int escape_hash_filter::get(Source & src)
41  {
42  if (mNext.empty())
43  {
44  int character = boost::iostreams::get(src);
45  bool record = true;
46  if (character == boost::iostreams::WOULD_BLOCK)
47  {
48  mNext.push(character);
49  record = false;
50  }
51  else if (character == EOF)
52  {
53  mSeenNonWhitespace = false;
54  mFinishLine = false;
55  mNext.push(character);
56  }
57  else if (character == '\n')
58  {
59  mSeenNonWhitespace = false;
60  mFinishLine = false;
61  mNext.push(character);
62  }
63  else if (mFinishLine)
64  {
65  mNext.push(character);
66  }
67  else if (character == '#')
68  {
70  {
71  mNext.push(sEscape);
72  mNext.push(sHashIdentifier);
73  }
74  else
75  {
76  //it's fine being interpreted by Boost as a comment, and so is anything afterwards
77  mNext.push(character);
78  mFinishLine = true;
79  }
80  }
81  else if (mPrevious == sEscape)
82  {
83  mNext.push(sEscape);
85  }
86  else
87  {
88  mNext.push(character);
89  }
90  if (!mSeenNonWhitespace && !isspace(character))
91  mSeenNonWhitespace = true;
92  if (record)
93  mPrevious = character;
94  }
95  int retval = mNext.front();
96  mNext.pop();
97  return retval;
98  }
99 
101  {
103  virtual ~unescape_hash_filter();
104 
105  template <typename Source> int get(Source & src);
106 
107  private:
109  };
110 
111  template <typename Source>
112  int unescape_hash_filter::get(Source & src)
113  {
114  int character;
115  if (!expectingIdentifier)
116  character = boost::iostreams::get(src);
117  else
118  {
119  character = escape_hash_filter::sEscape;
120  expectingIdentifier = false;
121  }
122  if (character == escape_hash_filter::sEscape)
123  {
124  int nextChar = boost::iostreams::get(src);
125  int intended;
127  intended = escape_hash_filter::sEscape;
128  else if (nextChar == escape_hash_filter::sHashIdentifier)
129  intended = '#';
130  else if (nextChar == boost::iostreams::WOULD_BLOCK)
131  {
132  expectingIdentifier = true;
133  intended = nextChar;
134  }
135  else
136  intended = '?';
137  return intended;
138  }
139  else
140  return character;
141  }
142 
147  {
148  private:
149  std::string mData;
150  public:
151  static std::string processString(const std::string & str);
152 
154  EscapeHashString(const std::string & str);
155  EscapeHashString(const std::string & str, size_t pos, size_t len = std::string::npos);
156  EscapeHashString(const char * s);
157  EscapeHashString(const char * s, size_t n);
158  EscapeHashString(size_t n, char c);
159  template <class InputIterator>
160  EscapeHashString(InputIterator first, InputIterator last);
161 
162  std::string toStdString() const;
163 
164  friend std::ostream & operator<< (std::ostream & os, const EscapeHashString & eHS);
165  };
166 
167  std::istream & operator>> (std::istream & is, EscapeHashString & eHS);
168 
170  {
171  std::vector<Files::EscapeHashString> mVector;
172 
174  virtual ~EscapeStringVector();
175 
176  std::vector<std::string> toStdStringVector() const;
177  };
178 
179  //boost program options validation
180 
181  void validate(boost::any &v, const std::vector<std::string> &tokens, Files::EscapeHashString * eHS, int a);
182 
183  void validate(boost::any &v, const std::vector<std::string> &tokens, EscapeStringVector *, int);
184 
185  struct EscapePath
186  {
187  boost::filesystem::path mPath;
188 
189  static PathContainer toPathContainer(const std::vector<EscapePath> & escapePathContainer);
190  };
191 
192  typedef std::vector<EscapePath> EscapePathContainer;
193 
194  std::istream & operator>> (std::istream & istream, EscapePath & escapePath);
195 } /* namespace Files */
196 #endif /* COMPONENTS_FILES_ESCAPE_HPP */
Definition: escape.hpp:146
escape_hash_filter()
Definition: escape.cpp:12
bool expectingIdentifier
Definition: escape.hpp:108
Definition: escape.hpp:100
unescape_hash_filter()
Definition: escape.cpp:20
static std::string processString(const std::string &str)
Definition: escape.cpp:28
int get(Source &src)
Definition: escape.hpp:112
EscapeStringVector()
Definition: escape.cpp:83
int mPrevious
Definition: escape.hpp:33
std::vector< EscapePath > EscapePathContainer
Definition: escape.hpp:192
virtual ~unescape_hash_filter()
Definition: escape.cpp:24
static const int sEscapeIdentifier
Definition: escape.hpp:24
bool mFinishLine
Definition: escape.hpp:36
bool mSeenNonWhitespace
Definition: escape.hpp:35
Definition: escape.hpp:185
friend std::ostream & operator<<(std::ostream &os, const EscapeHashString &eHS)
Definition: escape.cpp:77
static PathContainer toPathContainer(const std::vector< EscapePath > &escapePathContainer)
Definition: escape.cpp:122
virtual ~escape_hash_filter()
Definition: escape.cpp:16
std::string toStdString() const
Definition: escape.cpp:64
std::istream & operator>>(std::istream &is, EscapeHashString &eHS)
Definition: escape.cpp:69
Definition: escape.hpp:20
std::string mData
Definition: escape.hpp:149
virtual ~EscapeStringVector()
Definition: escape.cpp:87
static const int sHashIdentifier
Definition: escape.hpp:23
State & get()
Definition: state.cpp:585
static const int sEscape
Definition: escape.hpp:22
EscapeHashString()
Definition: escape.cpp:35
std::vector< std::string > toStdStringVector() const
Definition: escape.cpp:91
void validate(boost::any &v, const std::vector< std::string > &tokens, Files::EscapeHashString *eHS, int a)
Definition: escape.cpp:103
boost::filesystem::path mPath
Definition: escape.hpp:187
int get(Source &src)
Definition: escape.hpp:40
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:171
Definition: escape.hpp:169