1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 * Robin J. Maxwell 11-22-96 8 * Fredrik Roubert <roubert@google.com> 2010-07-23 9 * Matt Austern <austern@google.com> 2010-07-23 10 */ 11 12 #ifndef _PRSTRMS_H 13 #define _PRSTRMS_H 14 15 #include <cstddef> 16 #include <istream> 17 #include <ostream> 18 #include <streambuf> 19 20 #include "prio.h" 21 22 #ifdef _MSC_VER 23 // http://support.microsoft.com/kb/q168958/ 24 class PR_IMPLEMENT(std::_Mutex); 25 class PR_IMPLEMENT(std::ios_base); 26 #endif 27 PRfilebuf()28 29 class PR_IMPLEMENT(PRfilebuf): public std::streambuf 30 { 31 public: 32 PRfilebuf(); 33 PRfilebuf(PRFileDesc *fd); 34 PRfilebuf(PRFileDesc *fd, char_type *ptr, std::streamsize len); 35 virtual ~PRfilebuf(); 36 37 bool is_open() const { 38 return _fd != NULL; 39 } 40 41 PRfilebuf *open( 42 const char *name, 43 std::ios_base::openmode flags, 44 PRIntn mode); 45 PRfilebuf *attach(PRFileDesc *fd); 46 PRfilebuf *close(); 47 48 protected: 49 virtual std::streambuf *setbuf(char_type *ptr, std::streamsize len); 50 virtual pos_type seekoff( 51 off_type offset, 52 std::ios_base::seekdir dir, 53 std::ios_base::openmode flags); 54 virtual pos_type seekpos( 55 pos_type pos, 56 std::ios_base::openmode flags) { 57 return seekoff(pos, std::ios_base::beg, flags); 58 } 59 virtual int sync(); 60 virtual int_type underflow(); 61 virtual int_type overflow(int_type c = traits_type::eof()); 62 63 // TODO: Override pbackfail(), showmanyc(), uflow(), xsgetn(), and xsputn(). 64 65 private: 66 bool allocate(); 67 void setb(char_type *buf_base, char_type *buf_end, bool user_buf); 68 69 PRFileDesc *_fd; 70 bool _opened; 71 bool _allocated; 72 bool _unbuffered; 73 bool _user_buf; 74 char_type *_buf_base; 75 char_type *_buf_end; 76 }; 77 78 79 class PR_IMPLEMENT(PRifstream): public std::istream 80 { 81 public: 82 PRifstream(); 83 PRifstream(PRFileDesc *fd); 84 PRifstream(PRFileDesc *fd, char_type *ptr, std::streamsize len); 85 PRifstream(const char *name, openmode flags = in, PRIntn mode = 0); 86 virtual ~PRifstream(); 87 88 PRfilebuf *rdbuf() const { 89 return &_filebuf; 90 } 91 bool is_open() const { 92 return _filebuf.is_open(); 93 } 94 95 void open(const char *name, openmode flags = in, PRIntn mode = 0); 96 void attach(PRFileDesc *fd); 97 void close(); 98 99 private: 100 mutable PRfilebuf _filebuf; 101 }; 102 103 104 class PR_IMPLEMENT(PRofstream): public std::ostream 105 { 106 public: 107 PRofstream(); 108 PRofstream(PRFileDesc *fd); 109 PRofstream(PRFileDesc *fd, char_type *ptr, std::streamsize len); 110 PRofstream(const char *name, openmode flags = out, PRIntn mode = 0); 111 virtual ~PRofstream(); 112 113 PRfilebuf *rdbuf() const { 114 return &_filebuf; 115 } 116 bool is_open() const { 117 return _filebuf.is_open(); 118 } 119 120 void open(const char *name, openmode flags = out, PRIntn mode = 0); 121 void attach(PRFileDesc *fd); 122 void close(); 123 124 private: 125 mutable PRfilebuf _filebuf; 126 }; attach(PRFileDesc * fd)127 128 129 class PR_IMPLEMENT(PRfstream): public std::iostream 130 { 131 public: 132 PRfstream(); 133 PRfstream(PRFileDesc *fd); 134 PRfstream(PRFileDesc *fd, char_type *ptr, std::streamsize len); 135 PRfstream(const char *name, openmode flags = in | out, PRIntn mode = 0); 136 virtual ~PRfstream(); 137 138 PRfilebuf *rdbuf() const { 139 return &_filebuf; 140 } 141 bool is_open() const { 142 return _filebuf.is_open(); 143 } 144 145 void open(const char *name, openmode flags = in | out, PRIntn mode = 0); 146 void attach(PRFileDesc *fd); 147 void close(); 148 149 private: 150 mutable PRfilebuf _filebuf; 151 }; 152 153 154 #endif /* _PRSTRMS_H */ 155