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 
     17 namespace v8 {
     18 namespace internal {
     19 
     20 
     21 class OFStreamBase : public std::streambuf {
     22  public:
     23   explicit OFStreamBase(FILE* f);
     24   virtual ~OFStreamBase();
     25 
     26  protected:
     27   FILE* const f_;
     28 
     29   virtual int sync();
     30   virtual int_type overflow(int_type c);
     31   virtual std::streamsize xsputn(const char* s, std::streamsize n);
     32 };
     33 
     34 
     35 // An output stream writing to a file.
     36 class OFStream : public std::ostream {
     37  public:
     38   explicit OFStream(FILE* f);
     39   virtual ~OFStream();
     40 
     41  private:
     42   OFStreamBase buf_;
     43 };
     44 
     45 
     46 // Wrappers to disambiguate uint16_t and uc16.
     47 struct AsUC16 {
     48   explicit AsUC16(uint16_t v) : value(v) {}
     49   uint16_t value;
     50 };
     51 
     52 
     53 struct AsReversiblyEscapedUC16 {
     54   explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
     55   uint16_t value;
     56 };
     57 
     58 struct AsEscapedUC16ForJSON {
     59   explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
     60   uint16_t value;
     61 };
     62 
     63 
     64 // Writes the given character to the output escaping everything outside of
     65 // printable/space ASCII range. Additionally escapes '\' making escaping
     66 // reversible.
     67 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
     68 
     69 // Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
     70 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c);
     71 
     72 // Writes the given character to the output escaping everything outside
     73 // of printable ASCII range.
     74 std::ostream& operator<<(std::ostream& os, const AsUC16& c);
     75 
     76 }  // namespace internal
     77 }  // namespace v8
     78 
     79 #endif  // V8_OSTREAMS_H_
     80