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 
20 
21 #ifndef STREAMWRAPPER_H
22 #define STREAMWRAPPER_H
23 
24 #include <iostream>
25 
26 
27 #if defined( WIN32 ) && defined( __GNUC__ )
28  #include <ext/stdio_filebuf.h>
29 
30  #define OSTREAM std::ostream
31 
32  #define OPEN_OSTREAM( var, name ) \
33  kicad::stream var ## _BUF_; \
34  std::ostream& var = *var ## _BUF_.Open( name, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary )
35 
36  #define OPEN_ISTREAM( var, name ) \
37  kicad::stream var ## _BUF_; \
38  std::istream& var = *var ## _BUF_.Open( name, std::ios_base::in | std::ios_base::binary )
39 
40  #define OPEN_IOSTREAM( var, name ) \
41  kicad::stream var ## _BUF_; \
42  std::iostream& var = *var ## _BUF_.Open( name, std::ios_base::out | std::ios_base::in | std::ios_base::binary )
43 
44  #define CLOSE_STREAM( var ) var ## _BUF_.Close()
45 
46  namespace kicad
47  {
48  class stream
49  {
50  private:
51  __gnu_cxx::stdio_filebuf<char>* m_buf;
52  std::iostream* m_stream;
53 
54  public:
55  stream();
56  virtual ~stream();
57 
58  std::iostream* Open( const char* aFileName, std::ios_base::openmode aMode );
59  void Close( void );
60 
61  std::iostream* GetStream( void );
62  };
63  }
64 
65 
66 #elif defined( _MSC_VER ) // defined( WIN32 ) && defined( __GNUC__ )
67 
68  #define OSTREAM std::ofstream
69 
70  #define OPEN_OSTREAM( var, name ) \
71  std::ofstream var; \
72  var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
73  std::ios_base::out | std::ios_base::trunc | std::ios_base::binary )
74 
75  #define OPEN_ISTREAM( var, name ) \
76  std::ifstream var; \
77  var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
78  std::ios_base::in | std::ios_base::binary )
79 
80  #define OPEN_IOSTREAM( var, name ) \
81  std::fstream var; \
82  var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
83  std::ios_base::out | std::ios_base::in | std::ios_base::binary )
84 
85  #define CLOSE_STREAM( var ) var.close()
86 
87 #else // defined( WIN32 ) && defined( __GNUC__ )
88 
89  #define OSTREAM std::ofstream
90 
91  #define OPEN_OSTREAM( var, name ) \
92  std::ofstream var; \
93  var.open( name, std::ios_base::out | std::ios_base::trunc )
94 
95  #define OPEN_ISTREAM( var, name ) \
96  std::ifstream var; \
97  var.open( name, std::ios_base::in )
98 
99  #define OPEN_IOSTREAM( var, name ) \
100  std::fstream var; \
101  var.open( name, std::ios_base::out | std::ios_base::in )
102 
103  #define CLOSE_STREAM( var ) var.close()
104 
105 #endif // defined( WIN32 ) && defined( __GNUC__ )
106 
107 #endif // STREAMWRAPPER_H