blob: 56787f7c126a2d1ff0be295652b247354816170c [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Bernierd0a1eb72015-03-24 16:35:39 -04008#include <cstddef>
9#include <cstdio>
10#include <cstring>
11#include <ostream> // NOLINT
12#include <streambuf>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14#include "include/v8config.h"
15#include "src/base/macros.h"
16
17namespace v8 {
18namespace internal {
19
Emily Bernierd0a1eb72015-03-24 16:35:39 -040020class OFStreamBase : public std::streambuf {
21 protected:
22 explicit OFStreamBase(FILE* f);
23 virtual ~OFStreamBase();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
Emily Bernierd0a1eb72015-03-24 16:35:39 -040025 int_type sync() FINAL;
26 int_type overflow(int_type c) FINAL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027
28 private:
29 FILE* const f_;
30
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031 DISALLOW_COPY_AND_ASSIGN(OFStreamBase);
32};
33
34
35// An output stream writing to a file.
36class OFStream FINAL : private virtual OFStreamBase, public std::ostream {
37 public:
38 explicit OFStream(FILE* f);
39 ~OFStream();
40
41 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 DISALLOW_COPY_AND_ASSIGN(OFStream);
43};
44
45
46// Wrappers to disambiguate uint16_t and uc16.
47struct AsUC16 {
48 explicit AsUC16(uint16_t v) : value(v) {}
49 uint16_t value;
50};
51
52
53struct AsReversiblyEscapedUC16 {
54 explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
55 uint16_t value;
56};
57
58
59// Writes the given character to the output escaping everything outside of
60// printable/space ASCII range. Additionally escapes '\' making escaping
61// reversible.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063
64// Writes the given character to the output escaping everything outside
65// of printable ASCII range.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066std::ostream& operator<<(std::ostream& os, const AsUC16& c);
67
68} // namespace internal
69} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070
71#endif // V8_OSTREAMS_H_