KiCad PCB EDA Suite
streamwrapper.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Cirilo Bernardo <[email protected]>
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef STREAMWRAPPER_H
26 #define STREAMWRAPPER_H
27 
28 #include <iostream>
29 
30 
31 #if defined( _WIN32 ) && defined( __GNUC__ )
32 #include <ext/stdio_filebuf.h>
33 
34 #define OSTREAM std::ostream
35 
36 #define OPEN_OSTREAM( var, name ) \
37  kicad::stream var##_BUF_; \
38  std::ostream& var = *var##_BUF_.Open( name, std::ios_base::out | std::ios_base::trunc \
39  | std::ios_base::binary )
40 
41 #define OPEN_ISTREAM( var, name ) \
42  kicad::stream var##_BUF_; \
43  std::istream& var = *var##_BUF_.Open( name, std::ios_base::in | std::ios_base::binary )
44 
45 #define OPEN_IOSTREAM( var, name ) \
46  kicad::stream var##_BUF_; \
47  std::iostream& var = *var##_BUF_.Open( name, std::ios_base::out | std::ios_base::in \
48  | std::ios_base::binary )
49 
50 #define CLOSE_STREAM( var ) var##_BUF_.Close()
51 
55 namespace kicad
56 {
60 class stream
61 {
62 private:
63  __gnu_cxx::stdio_filebuf<char>* m_buf;
64  std::iostream* m_stream;
65 
66 public:
67  stream();
68  virtual ~stream();
69 
70  std::iostream* Open( const char* aFileName, std::ios_base::openmode aMode );
71  void Close( void );
72 
73  std::iostream* GetStream( void );
74 };
75 } // namespace kicad
76 
77 
78 #elif defined( _MSC_VER ) // defined( _WIN32 ) && defined( __GNUC__ )
79 
80 #define OSTREAM std::ofstream
81 
82 #define OPEN_OSTREAM( var, name ) \
83  std::ofstream var; \
84  var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
85  std::ios_base::out | std::ios_base::trunc | std::ios_base::binary )
86 
87 #define OPEN_ISTREAM( var, name ) \
88  std::ifstream var; \
89  var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
90  std::ios_base::in | std::ios_base::binary )
91 
92 #define OPEN_IOSTREAM( var, name ) \
93  std::fstream var; \
94  var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
95  std::ios_base::out | std::ios_base::in | std::ios_base::binary )
96 
97 #define CLOSE_STREAM( var ) var.close()
98 
99 #else // defined( _WIN32 ) && defined( __GNUC__ )
100 
101 #define OSTREAM std::ofstream
102 
103 #define OPEN_OSTREAM( var, name ) \
104  std::ofstream var; \
105  var.open( name, std::ios_base::out | std::ios_base::trunc )
106 
107 #define OPEN_ISTREAM( var, name ) \
108  std::ifstream var; \
109  var.open( name, std::ios_base::in )
110 
111 #define OPEN_IOSTREAM( var, name ) \
112  std::fstream var; \
113  var.open( name, std::ios_base::out | std::ios_base::in )
114 
115 #define CLOSE_STREAM( var ) var.close()
116 
117 #endif // defined( _WIN32 ) && defined( __GNUC__ )
118 
119 #endif // STREAMWRAPPER_H