ostreams.h 2.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 16 17 18 19

#include "include/v8config.h"
#include "src/base/macros.h"

namespace v8 {
namespace internal {

svenpanne's avatar
svenpanne committed
20

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

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

svenpanne's avatar
svenpanne committed
29 30 31
  virtual int sync();
  virtual int_type overflow(int_type c);
  virtual std::streamsize xsputn(const char* s, std::streamsize n);
32 33 34 35
};


// An output stream writing to a file.
svenpanne's avatar
svenpanne committed
36
class OFStream : public std::ostream {
37
 public:
38
  explicit OFStream(FILE* f);
39
  virtual ~OFStream();
40 41

 private:
svenpanne's avatar
svenpanne committed
42
  OFStreamBase buf_;
43 44
};

45

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


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


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

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

69 70 71 72 73 74
struct AsHex {
  explicit AsHex(uint64_t v, uint8_t min_width = 0)
      : value(v), min_width(min_width) {}
  uint64_t value;
  uint8_t min_width;
};
75

76 77
// Writes the given character to the output escaping everything outside of
// printable/space ASCII range. Additionally escapes '\' making escaping
78
// reversible.
79
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
80

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

84 85
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
86 87
std::ostream& operator<<(std::ostream& os, const AsUC16& c);

88 89 90 91
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
std::ostream& operator<<(std::ostream& os, const AsUC32& c);

92 93 94
// Writes the given number to the output in hexadecimal notation.
std::ostream& operator<<(std::ostream& os, const AsHex& v);

95 96
}  // namespace internal
}  // namespace v8
97 98

#endif  // V8_OSTREAMS_H_