KiCad PCB EDA Suite
Loading...
Searching...
No Matches
model_import_internal.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
19 */
20
21#ifndef MODEL_IMPORT_INTERNAL_H
22#define MODEL_IMPORT_INTERNAL_H
23
25
26#include <cstddef>
27#include <cstdint>
28#include <exception>
29#include <string>
30
31#include <wx/filefn.h>
32#include <wx/string.h>
33
34#ifdef _WIN32
35#include <clocale>
36#include <cstdlib>
37#else
38#include <locale.h>
39#endif
40
41wxString CreateModelImportTempFileName( const wxString& aPrefix );
42
43
48{
49 wxString path;
50
52 {
53 if( !path.empty() )
54 wxRemoveFile( path );
55 }
56};
57
58
65
69MODEL_IMPORT_GZIP_RESULT DecompressBoundedModel( const char* aData, std::size_t aSize,
70 std::uint64_t aMaxExpandedBytes, std::string& aExpanded );
71
72
74{
76 std::string diagnostic;
77
78 void Set( S3D::MODEL_IMPORT_ERROR aError, const char* aDiagnostic )
79 {
80 error = aError;
81 diagnostic = aDiagnostic;
82 }
83};
84
85
86class MODEL_IMPORT_CANCELED : public std::exception
87{
88public:
89 const char* what() const noexcept override { return "Model import canceled"; }
90};
91
92
94{
95public:
98
99#ifdef _WIN32
101 {
102 m_previousMode = _configthreadlocale( _ENABLE_PER_THREAD_LOCALE );
103 const char* current = std::setlocale( LC_NUMERIC, nullptr );
104
105 if( current )
106 m_previousLocale = current;
107
108 std::setlocale( LC_NUMERIC, "C" );
109 }
110
112 {
113 if( !m_previousLocale.empty() )
114 std::setlocale( LC_NUMERIC, m_previousLocale.c_str() );
115
116 if( m_previousMode > 0 )
117 _configthreadlocale( m_previousMode );
118 }
119
120private:
121 int m_previousMode = _DISABLE_PER_THREAD_LOCALE;
122 std::string m_previousLocale;
123#else
125 {
126 locale_t current = uselocale( static_cast<locale_t>( 0 ) );
127 locale_t base = duplocale( current );
128
129 if( !base )
130 return;
131
132 m_locale = newlocale( LC_NUMERIC_MASK, "C", base );
133
134 if( !m_locale )
135 {
136 freelocale( base );
137 return;
138 }
139
140 m_previous = uselocale( m_locale );
141 }
142
144 {
145 if( m_previous )
146 uselocale( m_previous );
147
148 if( m_locale )
149 freelocale( m_locale );
150 }
151
152private:
153 locale_t m_locale = nullptr;
154 locale_t m_previous = nullptr;
155#endif
156};
157
158#endif
const char * what() const noexcept override
MODEL_IMPORT_NUMERIC_LOCALE(const MODEL_IMPORT_NUMERIC_LOCALE &)=delete
MODEL_IMPORT_NUMERIC_LOCALE & operator=(const MODEL_IMPORT_NUMERIC_LOCALE &)=delete
#define OK
MODEL_IMPORT_GZIP_RESULT
wxString CreateModelImportTempFileName(const wxString &aPrefix)
MODEL_IMPORT_GZIP_RESULT DecompressBoundedModel(const char *aData, std::size_t aSize, std::uint64_t aMaxExpandedBytes, std::string &aExpanded)
Expand a gzip stream into aExpanded, refusing anything over aMaxExpandedBytes.
MODEL_IMPORT_ERROR
S3D::MODEL_IMPORT_ERROR error
void Set(S3D::MODEL_IMPORT_ERROR aError, const char *aDiagnostic)
Remove the scratch file an importer expanded a compressed model into.