Panda3D
pandaFileStreamBuf.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file pandaFileStreamBuf.h
10  * @author drose
11  * @date 2008-09-08
12  */
13 
14 #ifndef PANDAFILESTREAMBUF_H
15 #define PANDAFILESTREAMBUF_H
16 
17 #include "dtoolbase.h"
18 
19 #ifdef USE_PANDAFILESTREAM
20 
21 #if defined(_WIN32)
22 #ifndef WIN32_LEAN_AND_MEAN
23 #define WIN32_LEAN_AND_MEAN 1
24 #endif
25 #include <windows.h>
26 #endif
27 
28 /**
29  * The streambuf object that implements pifstream and pofstream.
30  */
31 class EXPCL_DTOOL_DTOOLUTIL PandaFileStreamBuf : public std::streambuf {
32 public:
33  PandaFileStreamBuf();
34  virtual ~PandaFileStreamBuf();
35 
36  void open(const char *filename, std::ios::openmode mode);
37 #ifdef _WIN32
38  void attach(const char *filename, HANDLE handle, std::ios::openmode mode);
39 #else
40  void attach(const char *filename, int fd, std::ios::openmode mode);
41 #endif
42 
43  bool is_open() const;
44  void close();
45 
46  enum NewlineMode {
47  NM_native,
48  NM_binary,
49  NM_msdos,
50  NM_unix,
51  NM_mac,
52  };
53  static NewlineMode _newline_mode;
54 
55 protected:
56  virtual std::streampos seekoff(std::streamoff off, ios_seekdir dir, ios_openmode which);
57  virtual std::streampos seekpos(std::streampos pos, ios_openmode which);
58 
59  virtual int overflow(int c);
60  virtual int sync();
61  virtual int underflow();
62 
63 private:
64  size_t read_chars(char *start, size_t length);
65  size_t write_chars(const char *start, size_t length);
66 
67  size_t read_chars_raw(char *start, size_t length);
68  size_t write_chars_raw(const char *start, size_t length);
69 
70  size_t decode_newlines(char *dest, size_t dest_length,
71  const char *source, size_t source_length);
72 
73  size_t encode_newlines_msdos(char *dest, size_t dest_length,
74  const char *source, size_t source_length);
75  size_t encode_newlines_unix(char *dest, size_t dest_length,
76  const char *source, size_t source_length);
77  size_t encode_newlines_mac(char *dest, size_t dest_length,
78  const char *source, size_t source_length);
79 
80 private:
81  std::string _filename;
82  bool _is_open;
83  std::ios::openmode _open_mode;
84 
85  char _last_read_nl;
86 
87 #ifdef _WIN32
88  HANDLE _handle;
89 #else
90  int _fd; // Posix file descriptor
91 #endif // _WIN32
92 
93  char *_buffer;
94  std::streampos _ppos;
95  std::streampos _gpos;
96 };
97 
98 EXPCL_DTOOL_DTOOLUTIL std::ostream &
99 operator << (std::ostream &out, PandaFileStreamBuf::NewlineMode newline_mode);
100 
101 EXPCL_DTOOL_DTOOLUTIL std::istream &
102 operator >> (std::istream &in, PandaFileStreamBuf::NewlineMode &newline_mode);
103 
104 #endif // USE_PANDAFILESTREAM
105 
106 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.