prstrms.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * Robin J. Maxwell 11-22-96
  7. * Fredrik Roubert <roubert@google.com> 2010-07-23
  8. * Matt Austern <austern@google.com> 2010-07-23
  9. */
  10. #ifndef _PRSTRMS_H
  11. #define _PRSTRMS_H
  12. #include <cstddef>
  13. #include <istream>
  14. #include <ostream>
  15. #include <streambuf>
  16. #include "prio.h"
  17. #ifdef _MSC_VER
  18. // http://support.microsoft.com/kb/q168958/
  19. class PR_IMPLEMENT(std::_Mutex);
  20. class PR_IMPLEMENT(std::ios_base);
  21. #endif
  22. class PR_IMPLEMENT(PRfilebuf): public std::streambuf
  23. {
  24. public:
  25. PRfilebuf();
  26. PRfilebuf(PRFileDesc *fd);
  27. PRfilebuf(PRFileDesc *fd, char_type *ptr, std::streamsize len);
  28. virtual ~PRfilebuf();
  29. bool is_open() const {
  30. return _fd != NULL;
  31. }
  32. PRfilebuf *open(
  33. const char *name,
  34. std::ios_base::openmode flags,
  35. PRIntn mode);
  36. PRfilebuf *attach(PRFileDesc *fd);
  37. PRfilebuf *close();
  38. protected:
  39. virtual std::streambuf *setbuf(char_type *ptr, std::streamsize len);
  40. virtual pos_type seekoff(
  41. off_type offset,
  42. std::ios_base::seekdir dir,
  43. std::ios_base::openmode flags);
  44. virtual pos_type seekpos(
  45. pos_type pos,
  46. std::ios_base::openmode flags) {
  47. return seekoff(pos, std::ios_base::beg, flags);
  48. }
  49. virtual int sync();
  50. virtual int_type underflow();
  51. virtual int_type overflow(int_type c = traits_type::eof());
  52. // TODO: Override pbackfail(), showmanyc(), uflow(), xsgetn(), and xsputn().
  53. private:
  54. bool allocate();
  55. void setb(char_type *buf_base, char_type *buf_end, bool user_buf);
  56. PRFileDesc *_fd;
  57. bool _opened;
  58. bool _allocated;
  59. bool _unbuffered;
  60. bool _user_buf;
  61. char_type *_buf_base;
  62. char_type *_buf_end;
  63. };
  64. class PR_IMPLEMENT(PRifstream): public std::istream
  65. {
  66. public:
  67. PRifstream();
  68. PRifstream(PRFileDesc *fd);
  69. PRifstream(PRFileDesc *fd, char_type *ptr, std::streamsize len);
  70. PRifstream(const char *name, openmode flags = in, PRIntn mode = 0);
  71. virtual ~PRifstream();
  72. PRfilebuf *rdbuf() const {
  73. return &_filebuf;
  74. }
  75. bool is_open() const {
  76. return _filebuf.is_open();
  77. }
  78. void open(const char *name, openmode flags = in, PRIntn mode = 0);
  79. void attach(PRFileDesc *fd);
  80. void close();
  81. private:
  82. mutable PRfilebuf _filebuf;
  83. };
  84. class PR_IMPLEMENT(PRofstream): public std::ostream
  85. {
  86. public:
  87. PRofstream();
  88. PRofstream(PRFileDesc *fd);
  89. PRofstream(PRFileDesc *fd, char_type *ptr, std::streamsize len);
  90. PRofstream(const char *name, openmode flags = out, PRIntn mode = 0);
  91. virtual ~PRofstream();
  92. PRfilebuf *rdbuf() const {
  93. return &_filebuf;
  94. }
  95. bool is_open() const {
  96. return _filebuf.is_open();
  97. }
  98. void open(const char *name, openmode flags = out, PRIntn mode = 0);
  99. void attach(PRFileDesc *fd);
  100. void close();
  101. private:
  102. mutable PRfilebuf _filebuf;
  103. };
  104. class PR_IMPLEMENT(PRfstream): public std::iostream
  105. {
  106. public:
  107. PRfstream();
  108. PRfstream(PRFileDesc *fd);
  109. PRfstream(PRFileDesc *fd, char_type *ptr, std::streamsize len);
  110. PRfstream(const char *name, openmode flags = in | out, PRIntn mode = 0);
  111. virtual ~PRfstream();
  112. PRfilebuf *rdbuf() const {
  113. return &_filebuf;
  114. }
  115. bool is_open() const {
  116. return _filebuf.is_open();
  117. }
  118. void open(const char *name, openmode flags = in | out, PRIntn mode = 0);
  119. void attach(PRFileDesc *fd);
  120. void close();
  121. private:
  122. mutable PRfilebuf _filebuf;
  123. };
  124. #endif /* _PRSTRMS_H */