Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_OSTREAMS_H_ |
| 6 | #define V8_OSTREAMS_H_ |
| 7 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8 | #include <cstddef> |
| 9 | #include <cstdio> |
| 10 | #include <cstring> |
| 11 | #include <ostream> // NOLINT |
| 12 | #include <streambuf> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | |
| 14 | #include "include/v8config.h" |
| 15 | #include "src/base/macros.h" |
| 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 21 | class OFStreamBase : public std::streambuf { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 23 | explicit OFStreamBase(FILE* f); |
| 24 | virtual ~OFStreamBase(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | protected: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | FILE* const f_; |
| 28 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 29 | virtual int sync(); |
| 30 | virtual int_type overflow(int_type c); |
| 31 | virtual std::streamsize xsputn(const char* s, std::streamsize n); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | |
| 35 | // An output stream writing to a file. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | class OFStream : public std::ostream { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 37 | public: |
| 38 | explicit OFStream(FILE* f); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 39 | virtual ~OFStream(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 40 | |
| 41 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 42 | OFStreamBase buf_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | |
| 46 | // Wrappers to disambiguate uint16_t and uc16. |
| 47 | struct AsUC16 { |
| 48 | explicit AsUC16(uint16_t v) : value(v) {} |
| 49 | uint16_t value; |
| 50 | }; |
| 51 | |
| 52 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 53 | struct AsUC32 { |
| 54 | explicit AsUC32(int32_t v) : value(v) {} |
| 55 | int32_t value; |
| 56 | }; |
| 57 | |
| 58 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | struct AsReversiblyEscapedUC16 { |
| 60 | explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {} |
| 61 | uint16_t value; |
| 62 | }; |
| 63 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 64 | struct AsEscapedUC16ForJSON { |
| 65 | explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {} |
| 66 | uint16_t value; |
| 67 | }; |
| 68 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 69 | struct AsHex { |
| 70 | explicit AsHex(uint64_t v, uint8_t min_width = 0) |
| 71 | : value(v), min_width(min_width) {} |
| 72 | uint64_t value; |
| 73 | uint8_t min_width; |
| 74 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | |
| 76 | // Writes the given character to the output escaping everything outside of |
| 77 | // printable/space ASCII range. Additionally escapes '\' making escaping |
| 78 | // reversible. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 79 | std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | // Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '. |
| 82 | std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c); |
| 83 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | // Writes the given character to the output escaping everything outside |
| 85 | // of printable ASCII range. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 86 | std::ostream& operator<<(std::ostream& os, const AsUC16& c); |
| 87 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 88 | // Writes the given character to the output escaping everything outside |
| 89 | // of printable ASCII range. |
| 90 | std::ostream& operator<<(std::ostream& os, const AsUC32& c); |
| 91 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 92 | // Writes the given number to the output in hexadecimal notation. |
| 93 | std::ostream& operator<<(std::ostream& os, const AsHex& v); |
| 94 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 95 | } // namespace internal |
| 96 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | |
| 98 | #endif // V8_OSTREAMS_H_ |