Home | History | Annotate | Download | only in src
      1 // 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 
      8 #include <cstddef>
      9 #include <cstdio>
     10 #include <cstring>
     11 #include <ostream>  // NOLINT
     12 #include <streambuf>
     13 
     14 #include "include/v8config.h"
     15 #include "src/base/macros.h"
     16 #include "src/globals.h"
     17 
     18 namespace v8 {
     19 namespace internal {
     20 
     21 
     22 class OFStreamBase : public std::streambuf {
     23  public:
     24   explicit OFStreamBase(FILE* f);
     25   virtual ~OFStreamBase();
     26 
     27  protected:
     28   FILE* const f_;
     29 
     30   virtual int sync();
     31   virtual int_type overflow(int_type c);
     32   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:
     43   OFStreamBase buf_;
     44 };
     45 
     46 
     47 // Wrappers to disambiguate uint16_t and uc16.
     48 struct AsUC16 {
     49   explicit AsUC16(uint16_t v) : value(v) {}
     50   uint16_t value;
     51 };
     52 
     53 
     54 struct AsUC32 {
     55   explicit AsUC32(int32_t v) : value(v) {}
     56   int32_t value;
     57 };
     58 
     59 
     60 struct AsReversiblyEscapedUC16 {
     61   explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
     62   uint16_t value;
     63 };
     64 
     65 struct AsEscapedUC16ForJSON {
     66   explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
     67   uint16_t value;
     68 };
     69 
     70 struct AsHex {
     71   explicit AsHex(uint64_t v, uint8_t min_width = 0)
     72       : value(v), min_width(min_width) {}
     73   uint64_t value;
     74   uint8_t min_width;
     75 };
     76 
     77 // Writes the given character to the output escaping everything outside of
     78 // printable/space ASCII range. Additionally escapes '\' making escaping
     79 // reversible.
     80 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
     81 
     82 // Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
     83 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
     84                                            const AsEscapedUC16ForJSON& c);
     85 
     86 // Writes the given character to the output escaping everything outside
     87 // of printable ASCII range.
     88 std::ostream& operator<<(std::ostream& os, const AsUC16& c);
     89 
     90 // Writes the given character to the output escaping everything outside
     91 // of printable ASCII range.
     92 std::ostream& operator<<(std::ostream& os, const AsUC32& c);
     93 
     94 // Writes the given number to the output in hexadecimal notation.
     95 std::ostream& operator<<(std::ostream& os, const AsHex& v);
     96 
     97 }  // namespace internal
     98 }  // namespace v8
     99 
    100 #endif  // V8_OSTREAMS_H_
    101