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