KiCad PCB EDA Suite
streamwrapper.cpp
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 #if !defined( WIN32 ) || !defined( __GNUC__ )
25  #error streamwrapper.cpp should not be included in this build
26 #endif
27 
28 #include "streamwrapper.h"
29 
30 #include <wx/string.h>
31 #include <iostream>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 
36 
37 kicad::stream::stream()
38 {
39  m_buf = NULL;
40  m_stream = NULL;
41 }
42 
43 
44 kicad::stream::~stream()
45 {
46  if( NULL != m_stream )
47  delete m_stream;
48 
49  if( NULL != m_buf )
50  {
51  m_buf->close(); // ensure file is closed regardless of m_buf's destructor
52  delete m_buf;
53  }
54 }
55 
56 
57 std::iostream* kicad::stream::Open( const char* aFileName, std::ios_base::openmode aMode )
58 {
59  if( NULL != m_stream )
60  {
61  delete m_stream;
62  m_stream = NULL;
63  }
64 
65  if( NULL != m_buf )
66  {
67  m_buf->close();
68  delete m_buf;
69  }
70 
71  int flags = 0;
72 
73  if( aMode & std::ios_base::app )
74  flags |= _O_APPEND;
75 
76  if( aMode & std::ios_base::out && aMode & std::ios_base::in )
77  flags |= _O_RDWR;
78  else if( aMode & std::ios_base::out )
79  flags |= _O_WRONLY;
80  else if( aMode & std::ios_base::in )
81  flags |= _O_RDONLY;
82 
83  if( aMode & std::ios_base::binary )
84  flags |= _O_BINARY;
85 
86  if( aMode & std::ios_base::out && aMode & std::ios_base::trunc
87  && !( aMode & std::ios_base::app ) && !( aMode & std::ios_base::ate ) )
88  flags |= _O_TRUNC;
89 
90  if( aMode & std::ios_base::out )
91  flags |= _O_CREAT;
92 
93  //int fd = open( "testfile.txt", flags, S_IRUSR | S_IWUSR );
94  wxString lstr( wxString::FromUTF8Unchecked( aFileName ) );
95  int fd = _wopen( lstr.wc_str(), flags, _S_IREAD | _S_IWRITE );
96 
97  if( fd >= 0 && aMode & std::ios_base::ate )
98  lseek( fd, 0, SEEK_END );
99 
100  // NOTE: _O_RDONLY in Windows, O_RDONLY in Linux
101  m_buf = new __gnu_cxx::stdio_filebuf<char>( fd, aMode );
102 
103  m_stream = new std::iostream( m_buf );
104 
105  return m_stream;
106 }
107 
108 
109 void kicad::stream::Close( void )
110 {
111  if( m_buf )
112  m_buf->close();
113 }
114 
115 
116 std::iostream* kicad::stream::GetStream( void )
117 {
118  return m_stream;
119 }