ostreams.h 1.82 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 39
  explicit OFStream(FILE* f);
  ~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
struct AsReversiblyEscapedUC16 {
  explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
  uint16_t value;
};

danno's avatar
danno committed
58 59 60 61 62
struct AsEscapedUC16ForJSON {
  explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
  uint16_t value;
};

63

64 65
// Writes the given character to the output escaping everything outside of
// printable/space ASCII range. Additionally escapes '\' making escaping
66
// reversible.
67
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
68

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

72 73
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
74 75 76 77
std::ostream& operator<<(std::ostream& os, const AsUC16& c);

}  // namespace internal
}  // namespace v8
78 79

#endif  // V8_OSTREAMS_H_