KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_io_utils.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 The KiCad Developers, see AUTHORS.txt for contributors.
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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23
24#include <wx/buffer.h>
25#include <wx/ffile.h>
26
27// For some reason wxWidgets is built with wxUSE_BASE64 unset so expose the wxWidgets
28// base64 code.
29#define wxUSE_BASE64 1
30#include <wx/base64.h>
31
32#include <fmt/format.h>
33
34#include <eda_item.h>
35#include <kiid.h>
36#include <richio.h>
37#include <string_utils.h>
38
39namespace KICAD_FORMAT {
40
41void FormatBool( OUTPUTFORMATTER* aOut, const wxString& aKey, bool aValue )
42{
43 aOut->Print( "(%ls %s)", aKey.wc_str(), aValue ? "yes" : "no" );
44}
45
46void FormatOptBool( OUTPUTFORMATTER* aOut, const wxString& aKey, std::optional<bool> aValue )
47{
48 if( aValue.has_value() )
49 FormatBool( aOut, aKey, aValue.value() );
50 else
51 aOut->Print( "(%ls none)", aKey.wc_str() );
52}
53
54
55void FormatUuid( OUTPUTFORMATTER* aOut, const KIID& aUuid )
56{
57 aOut->Print( "(uuid %s)", aOut->Quotew( aUuid.AsString() ).c_str() );
58}
59
60
62{
63 if( !aItem.HasCustomProperties() )
64 return;
65
66 for( const auto& [key, value] : aItem.GetCustomProperties() )
67 aOut->Print( "(custom_property %s %s)", aOut->Quotew( key ).c_str(), aOut->Quotew( value ).c_str() );
68}
69
70
71void FormatStreamData( OUTPUTFORMATTER& aOut, const wxStreamBuffer& aStream )
72{
73 aOut.Print( "(data" );
74
75 const wxString out = wxBase64Encode( aStream.GetBufferStart(), aStream.GetBufferSize() );
76
77 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
78 // so use it in a vein attempt to be standard like.
79 static constexpr unsigned MIME_BASE64_LENGTH = 76;
80
81 size_t first = 0;
82
83 while( first < out.Length() )
84 {
85 aOut.Print( "\n\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
86 first += MIME_BASE64_LENGTH;
87 }
88
89 aOut.Print( ")" ); // Closes data token.
90}
91
92
93/*
94 * Formatting rules:
95 * - All extra (non-indentation) whitespace is trimmed
96 * - Indentation is one tab
97 * - Starting a new list (open paren) starts a new line with one deeper indentation
98 * - Lists with no inner lists go on a single line
99 * - End of multi-line lists (close paren) goes on a single line at same indentation as its start
100 *
101 * For example:
102 * (first
103 * (second
104 * (third list)
105 * (another list)
106 * )
107 * (fifth)
108 * (sixth thing with lots of tokens
109 * (and a sub list)
110 * )
111 * )
112 */
113void Prettify( std::string& aSource, FORMAT_MODE aMode )
114{
115 // Configuration
116 const char quoteChar = '"';
117 const char indentChar = '\t';
118 const int indentSize = 1;
119
120 // In order to visually compress PCB files, it is helpful to special-case long lists of (xy ...)
121 // lists, which we allow to exist on a single line until we reach column 99.
122 const int xySpecialCaseColumnLimit = 99;
123
124 // If whitespace occurs inside a list after this threshold, it will be converted into a newline
125 // and the indentation will be increased. This is mainly used for image and group objects,
126 // which contain potentially long sets of string tokens within a single list.
127 const int consecutiveTokenWrapThreshold = 72;
128
129 const bool textSpecialCase = aMode == FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
130 const bool libSpecialCase = aMode == FORMAT_MODE::LIBRARY_TABLE;
131
132 std::string formatted;
133 formatted.reserve( aSource.length() );
134
135 auto cursor = aSource.begin();
136 auto seek = cursor;
137
138 int listDepth = 0;
139 int libDepth = 0;
140 char lastNonWhitespace = 0;
141 bool inQuote = false;
142 bool hasInsertedSpace = false;
143 bool inMultiLineList = false;
144 bool inXY = false;
145 bool inShortForm = false;
146 bool inLibRow = false;
147 int shortFormDepth = 0;
148 int column = 0;
149 int backslashCount = 0; // Count of successive backslash read since any other char
150
151 auto isWhitespace = []( const char aChar )
152 {
153 return ( aChar == ' ' || aChar == '\t' || aChar == '\n' || aChar == '\r' );
154 };
155
156 auto nextNonWhitespace =
157 [&]( std::string::iterator aIt )
158 {
159 seek = aIt;
160
161 while( seek != aSource.end() && isWhitespace( *seek ) )
162 seek++;
163
164 if( seek == aSource.end() )
165 return (char)0;
166
167 return *seek;
168 };
169
170 auto isXY =
171 [&]( std::string::iterator aIt )
172 {
173 seek = aIt;
174
175 if( ++seek == aSource.end() || *seek != 'x' )
176 return false;
177
178 if( ++seek == aSource.end() || *seek != 'y' )
179 return false;
180
181 if( ++seek == aSource.end() || *seek != ' ' )
182 return false;
183
184 return true;
185 };
186
187 auto isShortForm =
188 [&]( std::string::iterator aIt )
189 {
190 seek = aIt;
191 std::string token;
192
193 while( ++seek != aSource.end() && isalpha( *seek ) )
194 token += *seek;
195
196 return token == "font" || token == "stroke" || token == "fill" || token == "teardrop"
197 || token == "offset" || token == "rotate" || token == "scale";
198 };
199
200 auto isLib =
201 [&]( std::string::iterator aIt )
202 {
203 seek = aIt;
204 std::string token;
205
206 while( ++seek != aSource.end() && isalpha( *seek ) )
207 token += *seek;
208
209 return token == "lib";
210 };
211
212 while( cursor != aSource.end() )
213 {
214 // A comment line is not s-expression syntax and must keep its spacing, quotes and parens
215 if( listDepth == 0 && *cursor == '#' && ( formatted.empty() || formatted.back() == '\n' ) )
216 {
217 auto lineEnd = std::find( cursor, aSource.end(), '\n' );
218
219 formatted.append( cursor, lineEnd );
220 formatted.push_back( '\n' );
221
222 if( lineEnd == aSource.end() )
223 break;
224
225 cursor = std::next( lineEnd );
226 continue;
227 }
228
229 char next = nextNonWhitespace( cursor );
230
231 if( isWhitespace( *cursor ) && !inQuote )
232 {
233 if( !hasInsertedSpace // Only permit one space between chars
234 && listDepth > 0 // Do not permit spaces in outer list
235 && lastNonWhitespace != '(' // Remove extra space after start of list
236 && next != ')' // Remove extra space before end of list
237 && next != '(' ) // Remove extra space before newline
238 {
239 if( inXY || column < consecutiveTokenWrapThreshold )
240 {
241 // Note that we only insert spaces here, no matter what kind of whitespace is
242 // in the input. Newlines will be inserted as needed by the logic below.
243 formatted.push_back( ' ' );
244 column++;
245 }
246 else if( inShortForm || inLibRow )
247 {
248 formatted.push_back( ' ' );
249 }
250 else
251 {
252 formatted += fmt::format( "\n{}",
253 std::string( listDepth * indentSize, indentChar ) );
254 column = listDepth * indentSize;
255 inMultiLineList = true;
256 }
257
258 hasInsertedSpace = true;
259 }
260 }
261 else
262 {
263 hasInsertedSpace = false;
264
265 if( *cursor == '(' && !inQuote )
266 {
267 bool currentIsXY = isXY( cursor );
268 bool currentIsShortForm = textSpecialCase && isShortForm( cursor );
269 bool currentIsLib = libSpecialCase && isLib( cursor );
270
271 if( formatted.empty() || formatted.back() == '\n' )
272 {
273 formatted.push_back( '(' );
274 column++;
275 }
276 else if( inXY && currentIsXY && column < xySpecialCaseColumnLimit )
277 {
278 // List-of-points special case
279 formatted += " (";
280 column += 2;
281 }
282 else if( inShortForm || inLibRow )
283 {
284 formatted += " (";
285 column += 2;
286 }
287 else
288 {
289 formatted += fmt::format( "\n{}(",
290 std::string( listDepth * indentSize, indentChar ) );
291 column = listDepth * indentSize + 1;
292 }
293
294 inXY = currentIsXY;
295
296 if( currentIsShortForm )
297 {
298 inShortForm = true;
299 shortFormDepth = listDepth;
300 }
301 else if( currentIsLib )
302 {
303 inLibRow = true;
304 libDepth = listDepth;
305 }
306
307 listDepth++;
308 }
309 else if( *cursor == ')' && !inQuote )
310 {
311 if( listDepth > 0 )
312 listDepth--;
313
314 if( inShortForm )
315 {
316 formatted.push_back( ')' );
317 column++;
318 }
319 else if( inLibRow && listDepth == libDepth )
320 {
321 formatted.push_back( ')' );
322 inLibRow = false;
323 }
324 else if( lastNonWhitespace == ')' || inMultiLineList )
325 {
326 formatted += fmt::format( "\n{})",
327 std::string( listDepth * indentSize, indentChar ) );
328 column = listDepth * indentSize + 1;
329 inMultiLineList = false;
330 }
331 else
332 {
333 formatted.push_back( ')' );
334 column++;
335 }
336
337 if( shortFormDepth == listDepth )
338 {
339 inShortForm = false;
340 shortFormDepth = 0;
341 }
342 }
343 else
344 {
345 // The output formatter escapes double-quotes (like \")
346 // But a corner case is a sequence like \\"
347 // therefore a '\' is attached to a '"' if a odd number of '\' is detected
348 if( *cursor == '\\' )
349 backslashCount++;
350 else if( *cursor == quoteChar && ( backslashCount & 1 ) == 0 )
351 inQuote = !inQuote;
352
353 if( *cursor != '\\' )
354 backslashCount = 0;
355
356 formatted.push_back( *cursor );
357 column++;
358 }
359
360 lastNonWhitespace = *cursor;
361 }
362
363 ++cursor;
364 }
365
366 // newline required at end of line / file for POSIX compliance. Keeps git diffs clean.
367 formatted += '\n';
368
369 aSource = std::move( formatted );
370}
371
372} // namespace KICAD_FORMAT
373
374
375bool LoadFileToMemory( const wxString& aFileName, wxMemoryBuffer& aBuffer )
376{
377 wxFFile file( aFileName, wxS( "rb" ) );
378
379 if( !file.IsOpened() )
380 return false;
381
382 wxFileOffset size = file.Length();
383
384 if( size <= 0 )
385 return false;
386
387 void* data = aBuffer.GetWriteBuf( size );
388
389 if( file.Read( data, size ) != static_cast<size_t>( size ) )
390 {
391 aBuffer.UngetWriteBuf( 0 );
392 return false;
393 }
394
395 aBuffer.UngetWriteBuf( size );
396 return true;
397}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
bool HasCustomProperties() const
Definition eda_item.h:269
const std::map< wxString, wxString > & GetCustomProperties() const
Custom user-defined string key/value properties attached to this item.
Definition eda_item.h:242
Definition kiid.h:46
wxString AsString() const
Definition kiid.cpp:264
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:505
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:432
bool LoadFileToMemory(const wxString &aFileName, wxMemoryBuffer &aBuffer)
Load the contents of a file into a memory buffer.
void FormatCustomProperties(OUTPUTFORMATTER *aOut, const EDA_ITEM &aItem)
Writes the item's custom properties as a series of (custom_property "key" "value")
void FormatOptBool(OUTPUTFORMATTER *aOut, const wxString &aKey, std::optional< bool > aValue)
Writes an optional boolean to the formatter.
void Prettify(std::string &aSource, FORMAT_MODE aMode)
Pretty-prints s-expression text according to KiCad format rules.
void FormatUuid(OUTPUTFORMATTER *aOut, const KIID &aUuid)
void FormatStreamData(OUTPUTFORMATTER &aOut, const wxStreamBuffer &aStream)
Write binary data to the formatter as base 64 encoded string.
void FormatBool(OUTPUTFORMATTER *aOut, const wxString &aKey, bool aValue)
Writes a boolean to the formatter, in the style (aKey [yes|no])
CITER next(CITER it)
Definition ptree.cpp:120
#define MIME_BASE64_LENGTH
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
static wxString formatted(LIB_SYMBOL &aSymbol)
Serialize one symbol the way the library writer would.