ostreams.h 3.34 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OSTREAMS_H_
#define V8_OSTREAMS_H_

8 9 10 11 12
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <ostream>  // NOLINT
#include <streambuf>
13 14 15

#include "include/v8config.h"
#include "src/base/macros.h"
16
#include "src/globals.h"
17 18 19 20

namespace v8 {
namespace internal {

svenpanne's avatar
svenpanne committed
21

22
class OFStreamBase : public std::streambuf {
svenpanne's avatar
svenpanne committed
23
 public:
24 25
  explicit OFStreamBase(FILE* f);
  virtual ~OFStreamBase();
26

svenpanne's avatar
svenpanne committed
27
 protected:
28
  FILE* const f_;
29

svenpanne's avatar
svenpanne committed
30 31 32
  virtual int sync();
  virtual int_type overflow(int_type c);
  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:
svenpanne's avatar
svenpanne committed
43
  OFStreamBase buf_;
44 45
};

46

47
// Wrappers to disambiguate uint16_t and uc16.
48 49 50 51 52 53
struct AsUC16 {
  explicit AsUC16(uint16_t v) : value(v) {}
  uint16_t value;
};


54 55 56 57 58 59
struct AsUC32 {
  explicit AsUC32(int32_t v) : value(v) {}
  int32_t value;
};


60 61 62 63 64
struct AsReversiblyEscapedUC16 {
  explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
  uint16_t value;
};

danno's avatar
danno committed
65 66 67 68 69
struct AsEscapedUC16ForJSON {
  explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
  uint16_t value;
};

70 71 72
// Output the given value as hex, with a minimum width and optional prefix (0x).
// E.g. AsHex(23, 3, true) produces "0x017". Produces an empty string if both
// {min_width} and the value are 0.
73
struct AsHex {
74 75
  explicit AsHex(uint64_t v, uint8_t min_width = 1, bool with_prefix = false)
      : value(v), min_width(min_width), with_prefix(with_prefix) {}
76 77
  uint64_t value;
  uint8_t min_width;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  bool with_prefix;
};

// Output the given value as hex, separated in individual bytes.
// E.g. AsHexBytes(0x231712, 4) produces "12 17 23 00" if output as little
// endian (default), and "00 23 17 12" as big endian. Produces an empty string
// if both {min_bytes} and the value are 0.
struct AsHexBytes {
  enum ByteOrder { kLittleEndian, kBigEndian };
  explicit AsHexBytes(uint64_t v, uint8_t min_bytes = 1,
                      ByteOrder byte_order = kLittleEndian)
      : value(v), min_bytes(min_bytes), byte_order(byte_order) {}
  uint64_t value;
  uint8_t min_bytes;
  ByteOrder byte_order;
93
};
94

95 96
// Writes the given character to the output escaping everything outside of
// printable/space ASCII range. Additionally escapes '\' making escaping
97
// reversible.
98
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
99

danno's avatar
danno committed
100
// Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
101 102
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const AsEscapedUC16ForJSON& c);
danno's avatar
danno committed
103

104 105
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
106 107
std::ostream& operator<<(std::ostream& os, const AsUC16& c);

108 109 110 111
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
std::ostream& operator<<(std::ostream& os, const AsUC32& c);

112 113 114
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v);
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const AsHexBytes& v);
115

116 117
}  // namespace internal
}  // namespace v8
118 119

#endif  // V8_OSTREAMS_H_