KiCad PCB EDA Suite
Loading...
Searching...
No Matches
xnode.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) 2010 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <fmt/format.h>
26
27#include "xnode.h"
28
29#include <richio.h>
30#include <string_utils.h>
32
33
34XATTR::XATTR( const wxString& aName, const VALUE_TYPE& aValue ) :
35 wxXmlAttribute( aName, wxEmptyString ),
36 m_originalValue( aValue )
37{
38 std::visit( [&]<typename T0>( T0&& arg )
39 {
40 using T = std::decay_t<T0>;
41
42 if constexpr( std::is_same_v<T, int> )
43 {
44 wxXmlAttribute::SetValue( wxString::Format( "%d", arg ) );
45 }
46 else if constexpr( std::is_same_v<T, double> )
47 {
48 std::string buf;
49
50 if( arg != 0.0 && std::fabs( arg ) <= 0.0001 )
51 {
52 buf = fmt::format( "{:.16f}", arg );
53
54 // remove trailing zeros (and the decimal marker if needed)
55 while( !buf.empty() && buf[buf.size() - 1] == '0' )
56 {
57 buf.pop_back();
58 }
59
60 // if the value was really small
61 // we may have just stripped all the zeros after the decimal
62 if( buf[buf.size() - 1] == '.' )
63 {
64 buf.pop_back();
65 }
66 }
67 else
68 {
69 buf = fmt::format( "{:.10g}", arg );
70 }
71
72 wxXmlAttribute::SetValue( buf );
73 }
74 else if constexpr( std::is_same_v<T, wxString> )
75 {
76 wxXmlAttribute::SetValue( arg );
77 }
78 else
79 {
80 static_assert( false, "Missing type handling in XNODE::FormatContents" );
81 }
82 }, aValue );
83}
84
85
86void XNODE::AddBool( const wxString& aKey, bool aValue )
87{
88 AddAttribute( aKey, aValue ? wxT( "yes" ) : wxT( "no" ) );
89}
90
91
92void XNODE::AddAttribute( const wxString& aName, const wxString& aValue )
93{
94 XATTR* attr = new XATTR( aName, aValue );
95 wxXmlNode::AddAttribute( attr );
96}
97
98
99void XNODE::AddAttribute( const wxString& aName, int aValue )
100{
101 XATTR* attr = new XATTR( aName, aValue );
102 wxXmlNode::AddAttribute( attr );
103}
104
105
106void XNODE::AddAttribute( const wxString& aName, double aValue )
107{
108 XATTR* attr = new XATTR( aName, aValue );
109 wxXmlNode::AddAttribute( attr );
110}
111
112
114{
115 switch( GetType() )
116 {
117 case wxXML_ELEMENT_NODE:
118 out->Print( "(%s", TO_UTF8( GetName() ) );
119 FormatContents( out );
120
121 if( GetNext() )
122 out->Print( 0, ")\n" );
123 else
124 out->Print( 0, ")" );
125
126 break;
127
128 default:
129 FormatContents( out );
130 }
131}
132
133
135{
136 // output attributes first if they exist
137 for( wxXmlAttribute* attr = GetAttributes(); attr; attr = attr->GetNext() )
138 {
139 bool quote = true;
140
141 if( auto xa = dynamic_cast<XATTR*>( attr ) )
142 {
143 XATTR::VALUE_TYPE value = xa->GetValue();
144
145 std::visit( [&quote]<typename T0>( T0&& )
146 {
147 using T = std::decay_t<T0>;
148
149 if constexpr( std::is_same_v<T, int> || std::is_same_v<T, double> )
150 {
151 quote = false;
152 }
153 }, value );
154 }
155
156 out->Print( 0, " (%s %s)",
157 TO_UTF8( attr->GetName() ),
158 quote ? out->Quotew( attr->GetValue() ).c_str() : TO_UTF8( attr->GetValue() ) );
159 }
160
161 // we only expect to have used one of two types here:
162 switch( GetType() )
163 {
164 case wxXML_ELEMENT_NODE:
165 for( XNODE* child = GetChildren(); child; child = child->GetNext() )
166 {
167 if( child->GetType() != wxXML_TEXT_NODE )
168 {
169 if( child == GetChildren() )
170 out->Print( 0, "\n" );
171
172 child->Format( out );
173 }
174 else
175 {
176 child->Format( out );
177 }
178 }
179
180 break;
181
182 case wxXML_TEXT_NODE:
183 out->Print( 0, " %s", out->Quotew( GetContent() ).c_str() );
184 break;
185
186 default:
187 ; // not supported
188 }
189}
190
191
192wxString XNODE::Format() const
193{
194 STRING_FORMATTER formatter;
195 Format( &formatter );
197 return formatter.GetString();
198}
199
200// EOF
An interface used to output 8 bit text in a convenient way.
Definition richio.h:323
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:550
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:465
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:450
std::string & MutableString()
Definition richio.h:478
const std::string & GetString()
Definition richio.h:473
An extension of wxXmlAttribute that stores a variant type rather than just a string.
Definition xnode.h:50
std::variant< wxString, int, double > VALUE_TYPE
Definition xnode.h:52
VALUE_TYPE m_originalValue
Definition xnode.h:64
XATTR()
Definition xnode.h:54
wxString Format() const
Definition xnode.cpp:192
XNODE * GetChildren() const
Definition xnode.h:101
XNODE * GetNext() const
Definition xnode.h:106
XNODE()
Definition xnode.h:73
void FormatContents(OUTPUTFORMATTER *out) const
Write the contents of object as UTF8 out to an OUTPUTFORMATTER as an S-expression.
Definition xnode.cpp:134
void AddAttribute(const wxString &aName, const wxString &aValue) override
Definition xnode.cpp:92
void AddBool(const wxString &aKey, bool aValue)
Definition xnode.cpp:86
void Prettify(std::string &aSource, FORMAT_MODE aMode)
Pretty-prints s-expression text according to KiCad format rules.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.