blob: a7a67f5d2f06002fc9899b7c391476fd16022932 [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#include "src/ostreams.h"
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_OS_WIN
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#if _MSC_VER < 1900
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#define snprintf sprintf_s
10#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012
13namespace v8 {
14namespace internal {
15
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
17
18
19OFStreamBase::~OFStreamBase() {}
20
21
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022int OFStreamBase::sync() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023 std::fflush(f_);
24 return 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025}
26
27
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
29 return (c != EOF) ? std::fputc(c, f_) : c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030}
31
32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
34 return static_cast<std::streamsize>(
35 std::fwrite(s, 1, static_cast<size_t>(n), f_));
36}
37
38
39OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040040 DCHECK_NOT_NULL(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 rdbuf(&buf_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042}
43
44
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045OFStream::~OFStream() {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046
47
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049
50// Locale-independent predicates.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040051bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
52bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
53bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054
55
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 char buf[10];
58 const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
59 snprintf(buf, sizeof(buf), format, c);
60 return os << buf;
61}
62
Emily Bernierd0a1eb72015-03-24 16:35:39 -040063} // namespace
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065
66std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 return PrintUC16(os, c.value, IsOK);
68}
69
70
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
72 if (c.value == '\n') return os << "\\n";
73 if (c.value == '\r') return os << "\\r";
74 if (c.value == '\t') return os << "\\t";
75 if (c.value == '\"') return os << "\\\"";
76 return PrintUC16(os, c.value, IsOK);
77}
78
79
Emily Bernierd0a1eb72015-03-24 16:35:39 -040080std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 return PrintUC16(os, c.value, IsPrint);
82}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040083
84} // namespace internal
85} // namespace v8