Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fstream.hpp « nowide « boost « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0824a51b512dd839a6f9ae91ad941f4047a563c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
//  Copyright (c) 2012 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_NOWIDE_FSTREAM_INCLUDED_HPP
#define BOOST_NOWIDE_FSTREAM_INCLUDED_HPP

//#include <iosfwd>
#include <boost/config.hpp>
#include <boost/nowide/convert.hpp>
#include <boost/scoped_ptr.hpp>
#include <fstream>
#include <memory>
#include <boost/nowide/filebuf.hpp>

namespace boost {
///
/// \brief This namespace includes implementation of the standard library functios
/// such that they accept UTF-8 strings on Windows. On other platforms it is just an alias
/// of std namespace (i.e. not on Windows)
///
namespace nowide {
#if !defined(BOOST_WINDOWS)  && !defined(BOOST_NOWIDE_FSTREAM_TESTS) && !defined(BOOST_NOWIDE_DOXYGEN)

    using std::basic_ifstream;
    using std::basic_ofstream;
    using std::basic_fstream;
    using std::ifstream;
    using std::ofstream;
    using std::fstream;

#else
    ///
    /// \brief Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows
    ///
    template<typename CharType,typename Traits = std::char_traits<CharType> >
    class basic_ifstream : public std::basic_istream<CharType,Traits>
    {
    public:
        typedef basic_filebuf<CharType,Traits> internal_buffer_type;
        typedef std::basic_istream<CharType,Traits> internal_stream_type;

        basic_ifstream() : 
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
        }
        
        explicit basic_ifstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::in) : 
            internal_stream_type(0) 
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
            open(file_name,mode);
        }

        explicit basic_ifstream(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::in) : 
            internal_stream_type(0) 
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
            open(file_name,mode);
        }

        
        void open(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::in)
        {
            open(file_name.c_str(),mode);
        }
        void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::in)
        {
            if(!buf_->open(file_name,mode | std::ios_base::in)) {
                this->setstate(std::ios_base::failbit);
            }
            else {
                this->clear();
            }
        }
        bool is_open()
        {
            return buf_->is_open();
        }
        bool is_open() const
        {
            return buf_->is_open();
        }
        void close()
        {
            if(!buf_->close())
                this->setstate(std::ios_base::failbit);
            else
                this->clear();
        }

        internal_buffer_type *rdbuf() const
        {
            return buf_.get();
        }
        ~basic_ifstream()
        {
            buf_->close();
        }
            
    private:
        boost::scoped_ptr<internal_buffer_type> buf_;
    };

    ///
    /// \brief Same as std::basic_ofstream<char> but accepts UTF-8 strings under Windows
    ///

    template<typename CharType,typename Traits = std::char_traits<CharType> >
    class basic_ofstream : public std::basic_ostream<CharType,Traits>
    {
    public:
        typedef basic_filebuf<CharType,Traits> internal_buffer_type;
        typedef std::basic_ostream<CharType,Traits> internal_stream_type;

        basic_ofstream() : 
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
        }
        explicit basic_ofstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out) :
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
            open(file_name,mode);
        }
        explicit basic_ofstream(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out) :
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
            open(file_name,mode);
        }
        void open(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out)
        {
            open(file_name.c_str(),mode);
        }
        void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out)
        {
            if(!buf_->open(file_name,mode | std::ios_base::out)) {
                this->setstate(std::ios_base::failbit);
            }
            else {
                this->clear();
            }
        }
        bool is_open()
        {
            return buf_->is_open();
        }
        bool is_open() const
        {
            return buf_->is_open();
        }
        void close()
        {
            if(!buf_->close())
                this->setstate(std::ios_base::failbit);
            else
                this->clear();
        }

        internal_buffer_type *rdbuf() const
        {
            return buf_.get();
        }
        ~basic_ofstream()
        {
            buf_->close();
        }
            
    private:
        boost::scoped_ptr<internal_buffer_type> buf_;
    };

    ///
    /// \brief Same as std::basic_fstream<char> but accepts UTF-8 strings under Windows
    ///

    template<typename CharType,typename Traits = std::char_traits<CharType> >
    class basic_fstream : public std::basic_iostream<CharType,Traits>
    {
    public:
        typedef basic_filebuf<CharType,Traits> internal_buffer_type;
        typedef std::basic_iostream<CharType,Traits> internal_stream_type;

        basic_fstream() : 
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
        }
        explicit basic_fstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::in) :
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
            open(file_name,mode);
        }
        explicit basic_fstream(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::in) :
            internal_stream_type(0)
        {
            buf_.reset(new internal_buffer_type());
            std::ios::rdbuf(buf_.get());
            open(file_name,mode);
        }
        void open(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::out)
        {
            open(file_name.c_str(),mode);
        }
        void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::out)
        {
            if(!buf_->open(file_name,mode)) {
                this->setstate(std::ios_base::failbit);
            }
            else {
                this->clear();
            }
        }
        bool is_open()
        {
            return buf_->is_open();
        }
        bool is_open() const
        {
            return buf_->is_open();
        }
        void close()
        {
            if(!buf_->close())
                this->setstate(std::ios_base::failbit);
            else
                this->clear();
        }

        internal_buffer_type *rdbuf() const
        {
            return buf_.get();
        }
        ~basic_fstream()
        {
            buf_->close();
        }
            
    private:
        boost::scoped_ptr<internal_buffer_type> buf_;
    };


    ///
    /// \brief Same as std::filebuf but accepts UTF-8 strings under Windows
    ///
    typedef basic_filebuf<char> filebuf;
    ///
    /// Same as std::ifstream but accepts UTF-8 strings under Windows
    ///
    typedef basic_ifstream<char> ifstream;
    ///
    /// Same as std::ofstream but accepts UTF-8 strings under Windows
    ///
    typedef basic_ofstream<char> ofstream;
    ///
    /// Same as std::fstream but accepts UTF-8 strings under Windows
    ///
    typedef basic_fstream<char> fstream;

#endif
} // nowide
} // namespace boost



#endif
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4