stlab.adobe.com Adobe Systems Incorporated
istream.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_ISTREAM_HPP
10 #define ADOBE_ISTREAM_HPP
11 
12 #include <adobe/config.hpp>
13 
14 #include <ios>
15 #include <istream>
16 #include <stdexcept>
17 #include <vector>
18 
19 #include <adobe/name_fwd.hpp>
20 #include <adobe/istream_fwd.hpp>
21 
22 #include <boost/any.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/function.hpp>
25 
26 /*************************************************************************************************/
27 
28 namespace adobe {
29 
30 /*************************************************************************************************/
31 
32 #if 0
33 std::istream& getline(std::istream& is, std::string& str);
34 #endif
35 
36 /*************************************************************************************************/
37 
77 //***************************************************************************//
78 //***************************************************************************//
79 //***************************************************************************//
80 
133 //***************************************************************************//
134 //***************************************************************************//
135 //***************************************************************************//
136 
151 // line_position_t is used to remember a position on a particular line of a file.
152 
154 {
155 public:
157  typedef boost::shared_ptr<getline_proc_impl_t> getline_proc_t;
158 
159  // line_number starts at 1.
160  line_position_t( name_t file_path,
161  getline_proc_t getline_proc,
162  int line_number = 1,
163  std::streampos line_start = 0,
164  std::streampos position = -1);
165 
166  // This constructor is used with __FILE__ and __LINE__, line_index starts at 0
167  explicit line_position_t(const char*, int line_index = 0);
168 
169 #if !defined(ADOBE_NO_DOCUMENTATION)
170  line_position_t();
171 #endif // !defined(ADOBE_NO_DOCUMENTATION)
172 
173  const char* stream_name() const
174  { return file_name_m.c_str(); }
175 
176  std::string file_snippet() const
177  {
178  return getline_proc_m ?
179  (*getline_proc_m)(file_name_m, line_start_m) :
180  std::string();
181  }
182 
183  int line_number_m; // type int to match __LINE__ token
184  std::streampos line_start_m;
185  std::streampos position_m;
186 
187 #if !defined(ADOBE_NO_DOCUMENTATION)
188 private:
189  name_t file_name_m;
190  getline_proc_t getline_proc_m;
191 #endif // !defined(ADOBE_NO_DOCUMENTATION)
192 };
193 
194 /*************************************************************************************************/
195 
196 std::ostream& operator<<(std::ostream&, const line_position_t&);
197 
198 /*************************************************************************************************/
199 
200 class stream_error_t : public std::logic_error
201 {
202  public:
203  typedef std::vector<line_position_t> position_set_t;
204 
205  stream_error_t(const std::exception& base, const line_position_t& position) :
206  std::logic_error(base.what())
207  {
208  try {
209  const stream_error_t* error = dynamic_cast<const stream_error_t*>(&base);
210 
211  if (error) line_position_set_m = error->line_position_set_m;
212 
213  line_position_set_m.push_back(position);
214  } catch (...) { }
215  }
216 
217  stream_error_t(const char* what, const line_position_t& position) :
218  std::logic_error(what),
219  line_position_set_m(1, position)
220  { }
221 
222  stream_error_t(const std::string& what, const line_position_t& position) :
223  std::logic_error(what),
224  line_position_set_m(1, position)
225  { }
226 
228  { return line_position_set_m; }
229 
230 #if !defined(ADOBE_NO_DOCUMENTATION)
231  ~stream_error_t() throw()
232  { }
233 
234 private:
235  position_set_t line_position_set_m;
236 #endif // !defined(ADOBE_NO_DOCUMENTATION)
237 };
238 
239 /*************************************************************************************************/
240 
241 /*
242  REVISIT (sparent) : I'm not certain this code is correct when used with an istream iterator
243  which might do line ending conversions with operator <<. Read up on this.
244 */
245 
246 /*
247  REVISIT (sparent) : The interface to is_line_end should take an iterator pair and return
248  a pair (first, bool) - not work on a reference to first.
249 */
250 
251 template <typename I> // I models InputIterator
252 bool is_line_end(I& first, I last)
253 {
254  // Handle any type of line ending.
255 
256  typename std::iterator_traits<I>::value_type c(*first);
257 
258  if (c != '\n' && c != '\r') return false;
259 
260  ++first;
261 
262  if (c == '\r' && first != last && *first == '\n') ++first;
263 
264  return true;
265 }
266 
267 /*************************************************************************************************/
268 
269 template <typename I> // I models InputIterator
270 std::size_t is_line_end(I& first, I last, char c)
271 {
272  // Handle any type of line ending.
273 
274  if (c == '\n') return 1;
275 
276  if (c == '\r')
277  {
278  if (first != last && *first == '\n')
279  {
280  ++first;
281 
282  return 2;
283  }
284 
285  return 1;
286  }
287 
288  return 0;
289 }
290 
291 /*************************************************************************************************/
292 
293 template <typename I> // I models InputIterator
294 std::pair<I, std::string> get_line(I first, I last)
295 {
296  std::string result;
297 
298  while (first != last && !is_line_end(first, last))
299  {
300  result.append(1, *first);
301  ++ first;
302  }
303 
304  return std::make_pair(first, result);
305 }
306 
307 /*************************************************************************************************/
308 
309 } // namespace adobe
310 
311 /*************************************************************************************************/
312 
313 #endif
314 
315 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google