File: streamwrapper.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (120 lines) | stat: -rw-r--r-- 2,991 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file streamwrapper.cpp
 */

#if !defined( _WIN32 ) || !defined( __GNUC__ )
    #error streamwrapper.cpp should not be included in this build
#endif

#include "streamwrapper.h"

#include <wx/string.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


kicad::stream::stream()
{
    m_buf = nullptr;
    m_stream = nullptr;
}


kicad::stream::~stream()
{
    if( nullptr != m_stream )
        delete m_stream;

    if( nullptr != m_buf )
    {
        m_buf->close(); // ensure file is closed regardless of m_buf's destructor
        delete m_buf;
    }
}


std::iostream* kicad::stream::Open( const char* aFileName, std::ios_base::openmode aMode )
{
    if( nullptr != m_stream )
    {
        delete m_stream;
        m_stream = nullptr;
    }

    if( nullptr != m_buf )
    {
        m_buf->close();
        delete m_buf;
    }

    int flags = 0;

    if( aMode & std::ios_base::app )
        flags |= _O_APPEND;

    if( aMode & std::ios_base::out && aMode & std::ios_base::in )
        flags |= _O_RDWR;
    else if( aMode & std::ios_base::out )
        flags |= _O_WRONLY;
    else if( aMode & std::ios_base::in )
        flags |= _O_RDONLY;

    if( aMode & std::ios_base::binary )
        flags |= _O_BINARY;

    if( aMode & std::ios_base::out && aMode & std::ios_base::trunc
        && !( aMode & std::ios_base::app ) && !( aMode & std::ios_base::ate ) )
        flags |= _O_TRUNC;

    if( aMode & std::ios_base::out )
        flags |= _O_CREAT;

    //int fd = open( "testfile.txt", flags, S_IRUSR | S_IWUSR );
    wxString lstr( wxString::FromUTF8Unchecked( aFileName ) );
    int fd = _wopen( lstr.wc_str(), flags, _S_IREAD | _S_IWRITE );

    if( fd >= 0 && aMode & std::ios_base::ate )
        lseek( fd, 0, SEEK_END );

    // NOTE: _O_RDONLY in Windows, O_RDONLY in Linux
    m_buf = new __gnu_cxx::stdio_filebuf<char>( fd, aMode );

    m_stream = new std::iostream( m_buf );

    return m_stream;
}


void kicad::stream::Close( void )
{
    if( m_buf )
        m_buf->close();
}


std::iostream* kicad::stream::GetStream( void )
{
    return m_stream;
}