stream.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include "yaml-cpp/mark.h"
  9. #include <cstddef>
  10. #include <deque>
  11. #include <ios>
  12. #include <iostream>
  13. #include <set>
  14. #include <string>
  15. namespace YAML {
  16. class StreamCharSource;
  17. class Stream {
  18. public:
  19. friend class StreamCharSource;
  20. Stream(std::istream& input);
  21. Stream(const Stream&) = delete;
  22. Stream(Stream&&) = delete;
  23. Stream& operator=(const Stream&) = delete;
  24. Stream& operator=(Stream&&) = delete;
  25. ~Stream();
  26. operator bool() const;
  27. bool operator!() const { return !static_cast<bool>(*this); }
  28. char peek() const;
  29. char get();
  30. std::string get(int n);
  31. void eat(int n = 1);
  32. static char eof() { return 0x04; }
  33. const Mark mark() const { return m_mark; }
  34. int pos() const { return m_mark.pos; }
  35. int line() const { return m_mark.line; }
  36. int column() const { return m_mark.column; }
  37. void ResetColumn() { m_mark.column = 0; }
  38. private:
  39. enum CharacterSet { utf8, utf16le, utf16be, utf32le, utf32be };
  40. std::istream& m_input;
  41. Mark m_mark;
  42. CharacterSet m_charSet;
  43. mutable std::deque<char> m_readahead;
  44. unsigned char* const m_pPrefetched;
  45. mutable size_t m_nPrefetchedAvailable;
  46. mutable size_t m_nPrefetchedUsed;
  47. void AdvanceCurrent();
  48. char CharAt(size_t i) const;
  49. bool ReadAheadTo(size_t i) const;
  50. bool _ReadAheadTo(size_t i) const;
  51. void StreamInUtf8() const;
  52. void StreamInUtf16() const;
  53. void StreamInUtf32() const;
  54. unsigned char GetNextByte() const;
  55. };
  56. // CharAt
  57. // . Unchecked access
  58. inline char Stream::CharAt(size_t i) const { return m_readahead[i]; }
  59. inline bool Stream::ReadAheadTo(size_t i) const {
  60. if (m_readahead.size() > i)
  61. return true;
  62. return _ReadAheadTo(i);
  63. }
  64. } // namespace YAML
  65. #endif // STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66