FStream.hxx.in 7.5 KB
Newer Older
Kitware Robot's avatar
Kitware Robot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*============================================================================
  KWSys - Kitware System Library
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#ifndef @KWSYS_NAMESPACE@_FStream_hxx
#define @KWSYS_NAMESPACE@_FStream_hxx

Kitware Robot's avatar
Kitware Robot committed
15
#include <@KWSYS_NAMESPACE@/Configure.hxx>
Kitware Robot's avatar
Kitware Robot committed
16
#include <@KWSYS_NAMESPACE@/Encoding.hxx>
Kitware Robot's avatar
Kitware Robot committed
17
#include <fstream>
Kitware Robot's avatar
Kitware Robot committed
18
19
20
21
22
#if defined(_WIN32)
# if !defined(_MSC_VER) && @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
#  include <ext/stdio_filebuf.h>
# endif
#endif
Kitware Robot's avatar
Kitware Robot committed
23
24
25

namespace @KWSYS_NAMESPACE@
{
Kitware Robot's avatar
Kitware Robot committed
26
#if defined(_WIN32) && (defined(_MSC_VER) || @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H)
Kitware Robot's avatar
Kitware Robot committed
27
28
29
30
31
# if defined(_NOEXCEPT)
#  define @KWSYS_NAMESPACE@_FStream_NOEXCEPT _NOEXCEPT
# else
#  define @KWSYS_NAMESPACE@_FStream_NOEXCEPT
# endif
Kitware Robot's avatar
Kitware Robot committed
32
33
34

#if defined(_MSC_VER)

Kitware Robot's avatar
Kitware Robot committed
35
36
37
  template<typename CharType,typename Traits>
  class basic_filebuf : public std::basic_filebuf<CharType,Traits>
  {
Kitware Robot's avatar
Kitware Robot committed
38
# if _MSC_VER >= 1400
Kitware Robot's avatar
Kitware Robot committed
39
40
41
42
    public:
      typedef std::basic_filebuf<CharType,Traits> my_base_type;
      basic_filebuf *open(char const *s,std::ios_base::openmode mode)
      {
Kitware Robot's avatar
Kitware Robot committed
43
        const std::wstring wstr = Encoding::ToWide(s);
Kitware Robot's avatar
Kitware Robot committed
44
        return static_cast<basic_filebuf*>(
Kitware Robot's avatar
Kitware Robot committed
45
          my_base_type::open(wstr.c_str(), mode)
Kitware Robot's avatar
Kitware Robot committed
46
          );
Kitware Robot's avatar
Kitware Robot committed
47
      }
Kitware Robot's avatar
Kitware Robot committed
48
# endif
Kitware Robot's avatar
Kitware Robot committed
49
50
  };

Kitware Robot's avatar
Kitware Robot committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#else

  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;
  };

#endif

Kitware Robot's avatar
Kitware Robot committed
80
  template<typename CharType,typename Traits = std::char_traits<CharType> >
Kitware Robot's avatar
Kitware Robot committed
81
  class basic_efilebuf
Kitware Robot's avatar
Kitware Robot committed
82
  {
Kitware Robot's avatar
Kitware Robot committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    public:
#if defined(_MSC_VER)
      typedef basic_filebuf<CharType,Traits> internal_buffer_type;
#else
      typedef __gnu_cxx::stdio_filebuf<CharType,Traits> internal_buffer_type;
#endif

      basic_efilebuf() : file_(0)
      {
        buf_ = 0;
      }

      bool _open(char const *file_name,std::ios_base::openmode mode)
      {
        if (is_open() || file_) {
          return false;
        }
#if defined(_MSC_VER)
        const bool success = buf_->open(file_name,mode) != 0;
#else
        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_;
          }
          buf_ = new internal_buffer_type(file_, mode);
          success = true;
        }
#endif
        return success;
      }

      bool is_open()
      {
        if (!buf_) {
          return false;
        }
        return buf_->is_open();
      }

      bool is_open() const
      {
        if (!buf_) {
          return false;
        }
        return buf_->is_open();
      }

      bool _close()
      {
        bool success = false;
        if (buf_) {
          success = buf_->close() != 0;
#if !defined(_MSC_VER)
          if (file_) {
            success = fclose(file_) == 0 ? success : false;
            file_ = 0;
          }
#endif
        }
        return success;
      }

      static void _set_state(bool success, std::basic_ios<CharType,Traits> *ios, basic_efilebuf* efilebuf)
      {
#if !defined(_MSC_VER)
        ios->rdbuf(efilebuf->buf_);
#else
        static_cast<void>(efilebuf);
#endif
        if (!success) {
          ios->setstate(std::ios_base::failbit);
        } else {
          ios->clear();
        }
      }

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

    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>
{
  using basic_efilebuf<CharType,Traits>::is_open;

Kitware Robot's avatar
Kitware Robot committed
181
  public:
Kitware Robot's avatar
Kitware Robot committed
182
    typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type internal_buffer_type;
Kitware Robot's avatar
Kitware Robot committed
183
184
185
186
    typedef std::basic_istream<CharType,Traits> internal_stream_type;

    basic_ifstream() : internal_stream_type(new internal_buffer_type())
    {
Kitware Robot's avatar
Kitware Robot committed
187
      this->buf_ = static_cast<internal_buffer_type *>(internal_stream_type::rdbuf());
Kitware Robot's avatar
Kitware Robot committed
188
189
190
191
192
    }
    explicit basic_ifstream(char const *file_name,
                            std::ios_base::openmode mode = std::ios_base::in)
      : internal_stream_type(new internal_buffer_type())
    {
Kitware Robot's avatar
Kitware Robot committed
193
      this->buf_ = static_cast<internal_buffer_type *>(internal_stream_type::rdbuf());
Kitware Robot's avatar
Kitware Robot committed
194
195
      open(file_name,mode);
    }
Kitware Robot's avatar
Kitware Robot committed
196

Kitware Robot's avatar
Kitware Robot committed
197
198
    void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::in)
    {
Kitware Robot's avatar
Kitware Robot committed
199
200
      mode = mode | std::ios_base::in;
      this->_set_state(this->_open(file_name, mode), this, this);
Kitware Robot's avatar
Kitware Robot committed
201
    }
Kitware Robot's avatar
Kitware Robot committed
202

Kitware Robot's avatar
Kitware Robot committed
203
204
    void close()
    {
Kitware Robot's avatar
Kitware Robot committed
205
      this->_set_state(this->_close(), this, this);
Kitware Robot's avatar
Kitware Robot committed
206
207
208
209
    }

    internal_buffer_type *rdbuf() const
    {
Kitware Robot's avatar
Kitware Robot committed
210
      return this->buf_;
Kitware Robot's avatar
Kitware Robot committed
211
212
    }

Kitware Robot's avatar
Kitware Robot committed
213
    ~basic_ifstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT
Kitware Robot's avatar
Kitware Robot committed
214
    {
Kitware Robot's avatar
Kitware Robot committed
215
      close();
Kitware Robot's avatar
Kitware Robot committed
216
217
218
219
    }
};

template<typename CharType,typename Traits = std::char_traits<CharType> >
Kitware Robot's avatar
Kitware Robot committed
220
221
class basic_ofstream : public std::basic_ostream<CharType,Traits>,
                       public basic_efilebuf<CharType,Traits>
Kitware Robot's avatar
Kitware Robot committed
222
{
Kitware Robot's avatar
Kitware Robot committed
223
224
  using basic_efilebuf<CharType,Traits>::is_open;

Kitware Robot's avatar
Kitware Robot committed
225
  public:
Kitware Robot's avatar
Kitware Robot committed
226
  typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type internal_buffer_type;
Kitware Robot's avatar
Kitware Robot committed
227
228
229
230
  typedef std::basic_ostream<CharType,Traits> internal_stream_type;

  basic_ofstream() : internal_stream_type(new internal_buffer_type())
  {
Kitware Robot's avatar
Kitware Robot committed
231
    this->buf_ = static_cast<internal_buffer_type *>(internal_stream_type::rdbuf());
Kitware Robot's avatar
Kitware Robot committed
232
233
234
235
  }
  explicit basic_ofstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out) :
  internal_stream_type(new internal_buffer_type())
  {
Kitware Robot's avatar
Kitware Robot committed
236
    this->buf_ = static_cast<internal_buffer_type *>(internal_stream_type::rdbuf());
Kitware Robot's avatar
Kitware Robot committed
237
238
239
240
    open(file_name,mode);
  }
  void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out)
  {
Kitware Robot's avatar
Kitware Robot committed
241
242
    mode = mode | std::ios_base::out;
    this->_set_state(this->_open(file_name, mode), this, this);
Kitware Robot's avatar
Kitware Robot committed
243
  }
Kitware Robot's avatar
Kitware Robot committed
244

Kitware Robot's avatar
Kitware Robot committed
245
246
  void close()
  {
Kitware Robot's avatar
Kitware Robot committed
247
    this->_set_state(this->_close(), this, this);
Kitware Robot's avatar
Kitware Robot committed
248
249
250
251
  }

  internal_buffer_type *rdbuf() const
  {
Kitware Robot's avatar
Kitware Robot committed
252
    return this->buf_;
Kitware Robot's avatar
Kitware Robot committed
253
  }
Kitware Robot's avatar
Kitware Robot committed
254

Kitware Robot's avatar
Kitware Robot committed
255
  ~basic_ofstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT
Kitware Robot's avatar
Kitware Robot committed
256
  {
Kitware Robot's avatar
Kitware Robot committed
257
    close();
Kitware Robot's avatar
Kitware Robot committed
258
259
260
261
262
263
  }
};

  typedef basic_ifstream<char> ifstream;
  typedef basic_ofstream<char> ofstream;

Kitware Robot's avatar
Kitware Robot committed
264
# undef @KWSYS_NAMESPACE@_FStream_NOEXCEPT
Kitware Robot's avatar
Kitware Robot committed
265
#else
Kitware Robot's avatar
Kitware Robot committed
266
267
  using std::ofstream;
  using std::ifstream;
Kitware Robot's avatar
Kitware Robot committed
268
269
#endif

Kitware Robot's avatar
Kitware Robot committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  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);
  }
Kitware Robot's avatar
Kitware Robot committed
288
289
290
}

#endif