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
28
PR_IMPLEMENT(PRfilebuf)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 { return _fd != NULL; }
38
39 PRfilebuf *open(
40 const char *name,
41 std::ios_base::openmode flags,
42 PRIntn mode);
43 PRfilebuf *attach(PRFileDesc *fd);
44 PRfilebuf *close();
45
46 protected:
47 virtual std::streambuf *setbuf(char_type *ptr, std::streamsize len);
48 virtual pos_type seekoff(
49 off_type offset,
50 std::ios_base::seekdir dir,
51 std::ios_base::openmode flags);
52 virtual pos_type seekpos(
53 pos_type pos,
54 std::ios_base::openmode flags) {
55 return seekoff(pos, std::ios_base::beg, flags);
56 }
57 virtual int sync();
58 virtual int_type underflow();
59 virtual int_type overflow(int_type c = traits_type::eof());
60
61 // TODO: Override pbackfail(), showmanyc(), uflow(), xsgetn(), and xsputn().
62
63 private:
64 bool allocate();
65 void setb(char_type *buf_base, char_type *buf_end, bool user_buf);
66
67 PRFileDesc *_fd;
68 bool _opened;
69 bool _allocated;
70 bool _unbuffered;
71 bool _user_buf;
72 char_type *_buf_base;
73 char_type *_buf_end;
74 };
75
76
PR_IMPLEMENT(PRifstream)77 class PR_IMPLEMENT(PRifstream): public std::istream
78 {
79 public:
80 PRifstream();
81 PRifstream(PRFileDesc *fd);
82 PRifstream(PRFileDesc *fd, char_type *ptr, std::streamsize len);
83 PRifstream(const char *name, openmode flags = in, PRIntn mode = 0);
84 virtual ~PRifstream();
85
86 PRfilebuf *rdbuf() const { return &_filebuf; }
87 bool is_open() const { return _filebuf.is_open(); }
88
89 void open(const char *name, openmode flags = in, PRIntn mode = 0);
90 void attach(PRFileDesc *fd);
91 void close();
92
93 private:
94 mutable PRfilebuf _filebuf;
95 };
96
97
PR_IMPLEMENT(PRofstream)98 class PR_IMPLEMENT(PRofstream): public std::ostream
99 {
100 public:
101 PRofstream();
102 PRofstream(PRFileDesc *fd);
103 PRofstream(PRFileDesc *fd, char_type *ptr, std::streamsize len);
104 PRofstream(const char *name, openmode flags = out, PRIntn mode = 0);
105 virtual ~PRofstream();
106
107 PRfilebuf *rdbuf() const { return &_filebuf; }
108 bool is_open() const { return _filebuf.is_open(); }
109
110 void open(const char *name, openmode flags = out, PRIntn mode = 0);
111 void attach(PRFileDesc *fd);
112 void close();
113
114 private:
115 mutable PRfilebuf _filebuf;
116 };
117
118
PR_IMPLEMENT(PRfstream)119 class PR_IMPLEMENT(PRfstream): public std::iostream
120 {
121 public:
122 PRfstream();
123 PRfstream(PRFileDesc *fd);
124 PRfstream(PRFileDesc *fd, char_type *ptr, std::streamsize len);
125 PRfstream(const char *name, openmode flags = in | out, PRIntn mode = 0);
126 virtual ~PRfstream();
127
128 PRfilebuf *rdbuf() const { return &_filebuf; }
129 bool is_open() const { return _filebuf.is_open(); }
130
131 void open(const char *name, openmode flags = in | out, PRIntn mode = 0);
132 void attach(PRFileDesc *fd);
133 void close();
134
135 private:
136 mutable PRfilebuf _filebuf;
137 };
138
139
140 #endif /* _PRSTRMS_H */
141