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