FStream.hxx.in 6.73 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 4 5
#ifndef @KWSYS_NAMESPACE@_FStream_hxx
#define @KWSYS_NAMESPACE@_FStream_hxx

Dāvis Mosāns's avatar
Dāvis Mosāns committed
6
#include <@KWSYS_NAMESPACE@/Configure.hxx>
7

8
#include <@KWSYS_NAMESPACE@/Encoding.hxx>
9 10

#include <fstream>
Dāvis Mosāns's avatar
Dāvis Mosāns committed
11
#if defined(_WIN32)
12 13 14
#if !defined(_MSC_VER) && @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
#include <ext/stdio_filebuf.h>
#endif
Dāvis Mosāns's avatar
Dāvis Mosāns committed
15
#endif
16

17 18 19 20 21 22 23 24
namespace @KWSYS_NAMESPACE@ {
#if defined(_WIN32) &&                                                        \
  (defined(_MSC_VER) || @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H)
#if defined(_NOEXCEPT)
#define @KWSYS_NAMESPACE@_FStream_NOEXCEPT _NOEXCEPT
#else
#define @KWSYS_NAMESPACE@_FStream_NOEXCEPT
#endif
Dāvis Mosāns's avatar
Dāvis Mosāns committed
25 26 27

#if defined(_MSC_VER)

28 29 30 31 32 33 34
template <typename CharType, typename Traits>
class basic_filebuf : public std::basic_filebuf<CharType, Traits>
{
#if _MSC_VER >= 1400
public:
  typedef std::basic_filebuf<CharType, Traits> my_base_type;
  basic_filebuf* open(char const* s, std::ios_base::openmode mode)
35
  {
36 37 38 39 40
    const std::wstring wstr = Encoding::ToWide(s);
    return static_cast<basic_filebuf*>(my_base_type::open(wstr.c_str(), mode));
  }
#endif
};
Dāvis Mosāns's avatar
Dāvis Mosāns committed
41 42 43

#else

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
inline std::wstring getcmode(const std::ios_base::openmode mode)
{
  std::wstring cmode;
  bool plus = false;
  if (mode & std::ios_base::app) {
    cmode += L"a";
    plus = mode & std::ios_base::in ? true : false;
  } else if (mode & std::ios_base::trunc ||
             (mode & std::ios_base::out && (mode & std::ios_base::in) == 0)) {
    cmode += L"w";
    plus = mode & std::ios_base::in ? true : false;
  } else {
    cmode += L"r";
    plus = mode & std::ios_base::out ? true : false;
  }
  if (plus) {
    cmode += L"+";
  }
  if (mode & std::ios_base::binary) {
    cmode += L"b";
  } else {
    cmode += L"t";
  }
  return cmode;
};
69

Dāvis Mosāns's avatar
Dāvis Mosāns committed
70 71
#endif

72 73 74 75
template <typename CharType, typename Traits = std::char_traits<CharType> >
class basic_efilebuf
{
public:
Dāvis Mosāns's avatar
Dāvis Mosāns committed
76
#if defined(_MSC_VER)
77
  typedef basic_filebuf<CharType, Traits> internal_buffer_type;
Dāvis Mosāns's avatar
Dāvis Mosāns committed
78
#else
79
  typedef __gnu_cxx::stdio_filebuf<CharType, Traits> internal_buffer_type;
Dāvis Mosāns's avatar
Dāvis Mosāns committed
80
#endif
81

82 83 84 85 86
  basic_efilebuf()
    : file_(0)
  {
    buf_ = 0;
  }
87

88 89 90 91 92
  bool _open(char const* file_name, std::ios_base::openmode mode)
  {
    if (is_open() || file_) {
      return false;
    }
Dāvis Mosāns's avatar
Dāvis Mosāns committed
93
#if defined(_MSC_VER)
94
    const bool success = buf_->open(file_name, mode) != 0;
Dāvis Mosāns's avatar
Dāvis Mosāns committed
95
#else
96 97 98 99 100 101 102
    const std::wstring wstr = Encoding::ToWide(file_name);
    bool success = false;
    std::wstring cmode = getcmode(mode);
    file_ = _wfopen(wstr.c_str(), cmode.c_str());
    if (file_) {
      if (buf_) {
        delete buf_;
103
      }
104 105 106 107 108 109
      buf_ = new internal_buffer_type(file_, mode);
      success = true;
    }
#endif
    return success;
  }
110

111 112 113 114 115 116 117
  bool is_open()
  {
    if (!buf_) {
      return false;
    }
    return buf_->is_open();
  }
118

119 120 121 122 123 124 125
  bool is_open() const
  {
    if (!buf_) {
      return false;
    }
    return buf_->is_open();
  }
126

127 128 129 130 131
  bool _close()
  {
    bool success = false;
    if (buf_) {
      success = buf_->close() != 0;
Dāvis Mosāns's avatar
Dāvis Mosāns committed
132
#if !defined(_MSC_VER)
133 134 135
      if (file_) {
        success = fclose(file_) == 0 ? success : false;
        file_ = 0;
136
      }
137 138 139 140
#endif
    }
    return success;
  }
141

142 143 144
  static void _set_state(bool success, std::basic_ios<CharType, Traits>* ios,
                         basic_efilebuf* efilebuf)
  {
Dāvis Mosāns's avatar
Dāvis Mosāns committed
145
#if !defined(_MSC_VER)
146
    ios->rdbuf(efilebuf->buf_);
147
#else
148
    static_cast<void>(efilebuf);
Dāvis Mosāns's avatar
Dāvis Mosāns committed
149
#endif
150 151 152 153 154 155 156 157 158 159 160 161 162
    if (!success) {
      ios->setstate(std::ios_base::failbit);
    } else {
      ios->clear();
    }
  }

  ~basic_efilebuf()
  {
    if (buf_) {
      delete buf_;
    }
  }
163

164 165 166 167 168 169 170 171
protected:
  internal_buffer_type* buf_;
  FILE* file_;
};

template <typename CharType, typename Traits = std::char_traits<CharType> >
class basic_ifstream : public std::basic_istream<CharType, Traits>,
                       public basic_efilebuf<CharType, Traits>
172
{
173
  using basic_efilebuf<CharType, Traits>::is_open;
174

175 176 177 178
public:
  typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type
    internal_buffer_type;
  typedef std::basic_istream<CharType, Traits> internal_stream_type;
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193
  basic_ifstream()
    : internal_stream_type(new internal_buffer_type())
  {
    this->buf_ =
      static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
  }
  explicit basic_ifstream(char const* file_name,
                          std::ios_base::openmode mode = std::ios_base::in)
    : internal_stream_type(new internal_buffer_type())
  {
    this->buf_ =
      static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
    open(file_name, mode);
  }
194

195 196 197 198 199 200
  void open(char const* file_name,
            std::ios_base::openmode mode = std::ios_base::in)
  {
    mode = mode | std::ios_base::in;
    this->_set_state(this->_open(file_name, mode), this, this);
  }
201

202
  void close() { this->_set_state(this->_close(), this, this); }
203

204
  internal_buffer_type* rdbuf() const { return this->buf_; }
205

206
  ~basic_ifstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); }
207 208
};

209 210 211
template <typename CharType, typename Traits = std::char_traits<CharType> >
class basic_ofstream : public std::basic_ostream<CharType, Traits>,
                       public basic_efilebuf<CharType, Traits>
212
{
213
  using basic_efilebuf<CharType, Traits>::is_open;
214

215 216 217 218
public:
  typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type
    internal_buffer_type;
  typedef std::basic_ostream<CharType, Traits> internal_stream_type;
219

220 221
  basic_ofstream()
    : internal_stream_type(new internal_buffer_type())
222
  {
223 224
    this->buf_ =
      static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
225
  }
226 227 228
  explicit basic_ofstream(char const* file_name,
                          std::ios_base::openmode mode = std::ios_base::out)
    : internal_stream_type(new internal_buffer_type())
229
  {
230 231 232
    this->buf_ =
      static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
    open(file_name, mode);
233
  }
234 235
  void open(char const* file_name,
            std::ios_base::openmode mode = std::ios_base::out)
236
  {
237
    mode = mode | std::ios_base::out;
Dāvis Mosāns's avatar
Dāvis Mosāns committed
238
    this->_set_state(this->_open(file_name, mode), this, this);
239
  }
240

241
  void close() { this->_set_state(this->_close(), this, this); }
242

243
  internal_buffer_type* rdbuf() const { return this->buf_; }
244

245
  ~basic_ofstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); }
246 247
};

248 249
typedef basic_ifstream<char> ifstream;
typedef basic_ofstream<char> ofstream;
250

251
#undef @KWSYS_NAMESPACE@_FStream_NOEXCEPT
252
#else
253 254
using std::ofstream;
using std::ifstream;
255 256
#endif

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
namespace FStream {
enum BOM
{
  BOM_None,
  BOM_UTF8,
  BOM_UTF16BE,
  BOM_UTF16LE,
  BOM_UTF32BE,
  BOM_UTF32LE
};

// Read a BOM, if one exists.
// If a BOM exists, the stream is advanced to after the BOM.
// This function requires a seekable stream (but not a relative
// seekable stream).
BOM ReadBOM(std::istream& in);
}
274 275 276
}

#endif