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