pandaFileStreamBuf.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef PANDAFILESTREAMBUF_H
  14. #define PANDAFILESTREAMBUF_H
  15. #include "dtoolbase.h"
  16. #ifdef USE_PANDAFILESTREAM
  17. #if defined(_WIN32)
  18. #ifndef WIN32_LEAN_AND_MEAN
  19. #define WIN32_LEAN_AND_MEAN 1
  20. #endif
  21. #include <windows.h>
  22. #endif
  23. /**
  24. * The streambuf object that implements pifstream and pofstream.
  25. */
  26. class EXPCL_DTOOL_DTOOLUTIL PandaFileStreamBuf : public std::streambuf {
  27. public:
  28. PandaFileStreamBuf();
  29. virtual ~PandaFileStreamBuf();
  30. void open(const char *filename, std::ios::openmode mode);
  31. #ifdef _WIN32
  32. void attach(const char *filename, HANDLE handle, std::ios::openmode mode);
  33. #else
  34. void attach(const char *filename, int fd, std::ios::openmode mode);
  35. #endif
  36. bool is_open() const;
  37. void close();
  38. enum NewlineMode {
  39. NM_native,
  40. NM_binary,
  41. NM_msdos,
  42. NM_unix,
  43. NM_mac,
  44. };
  45. static NewlineMode _newline_mode;
  46. protected:
  47. virtual std::streampos seekoff(std::streamoff off, ios_seekdir dir, ios_openmode which);
  48. virtual std::streampos seekpos(std::streampos pos, ios_openmode which);
  49. virtual int overflow(int c);
  50. virtual int sync();
  51. virtual int underflow();
  52. private:
  53. size_t read_chars(char *start, size_t length);
  54. size_t write_chars(const char *start, size_t length);
  55. size_t read_chars_raw(char *start, size_t length);
  56. size_t write_chars_raw(const char *start, size_t length);
  57. size_t decode_newlines(char *dest, size_t dest_length,
  58. const char *source, size_t source_length);
  59. size_t encode_newlines_msdos(char *dest, size_t dest_length,
  60. const char *source, size_t source_length);
  61. size_t encode_newlines_unix(char *dest, size_t dest_length,
  62. const char *source, size_t source_length);
  63. size_t encode_newlines_mac(char *dest, size_t dest_length,
  64. const char *source, size_t source_length);
  65. private:
  66. std::string _filename;
  67. bool _is_open;
  68. std::ios::openmode _open_mode;
  69. char _last_read_nl;
  70. #ifdef _WIN32
  71. HANDLE _handle;
  72. #else
  73. int _fd; // Posix file descriptor
  74. #endif // _WIN32
  75. char *_buffer;
  76. std::streampos _ppos;
  77. std::streampos _gpos;
  78. };
  79. EXPCL_DTOOL_DTOOLUTIL std::ostream &
  80. operator << (std::ostream &out, PandaFileStreamBuf::NewlineMode newline_mode);
  81. EXPCL_DTOOL_DTOOLUTIL std::istream &
  82. operator >> (std::istream &in, PandaFileStreamBuf::NewlineMode &newline_mode);
  83. #endif // USE_PANDAFILESTREAM
  84. #endif