Line data Source code
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 :
8 : #include <cstddef>
9 : #include <cstdio>
10 : #include <cstring>
11 : #include <ostream> // NOLINT
12 : #include <streambuf>
13 :
14 : #include "include/v8config.h"
15 : #include "src/base/macros.h"
16 : #include "src/globals.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 :
22 : class OFStreamBase : public std::streambuf {
23 : public:
24 : explicit OFStreamBase(FILE* f);
25 : virtual ~OFStreamBase();
26 :
27 : protected:
28 : FILE* const f_;
29 :
30 : virtual int sync();
31 : virtual int_type overflow(int_type c);
32 : virtual std::streamsize xsputn(const char* s, std::streamsize n);
33 : };
34 :
35 :
36 : // An output stream writing to a file.
37 : class V8_EXPORT_PRIVATE OFStream : public std::ostream {
38 : public:
39 : explicit OFStream(FILE* f);
40 : virtual ~OFStream();
41 :
42 : private:
43 : OFStreamBase buf_;
44 : };
45 :
46 :
47 : // Wrappers to disambiguate uint16_t and uc16.
48 : struct AsUC16 {
49 4416 : explicit AsUC16(uint16_t v) : value(v) {}
50 : uint16_t value;
51 : };
52 :
53 :
54 : struct AsUC32 {
55 1182 : explicit AsUC32(int32_t v) : value(v) {}
56 : int32_t value;
57 : };
58 :
59 :
60 : struct AsReversiblyEscapedUC16 {
61 0 : explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
62 : uint16_t value;
63 : };
64 :
65 : struct AsEscapedUC16ForJSON {
66 6582 : explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
67 : uint16_t value;
68 : };
69 :
70 : // Output the given value as hex, with a minimum width and optional prefix (0x).
71 : // E.g. AsHex(23, 3, true) produces "0x017". Produces an empty string if both
72 : // {min_width} and the value are 0.
73 : struct AsHex {
74 : explicit AsHex(uint64_t v, uint8_t min_width = 1, bool with_prefix = false)
75 53 : : value(v), min_width(min_width), with_prefix(with_prefix) {}
76 : uint64_t value;
77 : uint8_t min_width;
78 : bool with_prefix;
79 : };
80 :
81 : // Output the given value as hex, separated in individual bytes.
82 : // E.g. AsHexBytes(0x231712, 4) produces "12 17 23 00" if output as little
83 : // endian (default), and "00 23 17 12" as big endian. Produces an empty string
84 : // if both {min_bytes} and the value are 0.
85 : struct AsHexBytes {
86 : enum ByteOrder { kLittleEndian, kBigEndian };
87 : explicit AsHexBytes(uint64_t v, uint8_t min_bytes = 1,
88 : ByteOrder byte_order = kLittleEndian)
89 18 : : value(v), min_bytes(min_bytes), byte_order(byte_order) {}
90 : uint64_t value;
91 : uint8_t min_bytes;
92 : ByteOrder byte_order;
93 : };
94 :
95 : // Writes the given character to the output escaping everything outside of
96 : // printable/space ASCII range. Additionally escapes '\' making escaping
97 : // reversible.
98 : std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
99 :
100 : // Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
101 : V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
102 : const AsEscapedUC16ForJSON& c);
103 :
104 : // Writes the given character to the output escaping everything outside
105 : // of printable ASCII range.
106 : std::ostream& operator<<(std::ostream& os, const AsUC16& c);
107 :
108 : // Writes the given character to the output escaping everything outside
109 : // of printable ASCII range.
110 : std::ostream& operator<<(std::ostream& os, const AsUC32& c);
111 :
112 : V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v);
113 : V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
114 : const AsHexBytes& v);
115 :
116 : } // namespace internal
117 : } // namespace v8
118 :
119 : #endif // V8_OSTREAMS_H_
|