Panda3D
 All Classes Functions Variables Enumerations
pandaFileStreamBuf.h
00001 // Filename: pandaFileStreamBuf.h
00002 // Created by:  drose (08Sep08)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef PANDAFILESTREAMBUF_H
00016 #define PANDAFILESTREAMBUF_H
00017 
00018 #include "dtoolbase.h"
00019 
00020 #ifdef USE_PANDAFILESTREAM
00021 
00022 #if defined(_WIN32)
00023 #define WIN32_LEAN_AND_MEAN
00024 #include <windows.h>
00025 #endif
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //       Class : PandaFileStreamBuf
00029 // Description : The streambuf object that implements
00030 //               pifstream and pofstream.
00031 ////////////////////////////////////////////////////////////////////
00032 class EXPCL_DTOOL PandaFileStreamBuf : public streambuf {
00033 public:
00034   PandaFileStreamBuf();
00035   virtual ~PandaFileStreamBuf();
00036 
00037   void open(const char *filename, ios::openmode mode);
00038 #ifdef _WIN32
00039   void attach(const char *filename, HANDLE handle, ios::openmode mode);
00040 #else
00041   void attach(const char *filename, int fd, ios::openmode mode);
00042 #endif
00043 
00044   bool is_open() const;
00045   void close();
00046 
00047   enum NewlineMode {
00048     NM_native,
00049     NM_binary,
00050     NM_msdos,
00051     NM_unix,
00052     NM_mac,
00053   };
00054   static NewlineMode _newline_mode;
00055 
00056 protected:
00057   virtual streampos seekoff(streamoff off, ios_seekdir dir, ios_openmode which);
00058   virtual streampos seekpos(streampos pos, ios_openmode which);
00059 
00060   virtual int overflow(int c);
00061   virtual int sync();
00062   virtual int underflow();
00063 
00064 private:
00065   size_t read_chars(char *start, size_t length);
00066   size_t write_chars(const char *start, size_t length);
00067 
00068   size_t read_chars_raw(char *start, size_t length);
00069   size_t write_chars_raw(const char *start, size_t length);
00070 
00071   size_t decode_newlines(char *dest, size_t dest_length,
00072                          const char *source, size_t source_length);
00073 
00074   size_t encode_newlines_msdos(char *dest, size_t dest_length,
00075                                const char *source, size_t source_length);
00076   size_t encode_newlines_unix(char *dest, size_t dest_length,
00077                               const char *source, size_t source_length);
00078   size_t encode_newlines_mac(char *dest, size_t dest_length,
00079                              const char *source, size_t source_length);
00080 
00081 private:
00082   string _filename;
00083   bool _is_open;
00084   ios::openmode _open_mode;
00085 
00086   char _last_read_nl;
00087 
00088 #ifdef _WIN32
00089   HANDLE _handle;
00090 #else
00091   int _fd;  // Posix file descriptor
00092 #endif  // _WIN32
00093 
00094   char *_buffer;
00095   streampos _ppos;
00096   streampos _gpos;
00097 };
00098 
00099 EXPCL_DTOOL ostream &
00100 operator << (ostream &out, PandaFileStreamBuf::NewlineMode newline_mode);
00101 
00102 EXPCL_DTOOL istream &
00103 operator >> (istream &in, PandaFileStreamBuf::NewlineMode &newline_mode);
00104 
00105 #endif  // USE_PANDAFILESTREAM
00106 
00107 #endif
 All Classes Functions Variables Enumerations