KiCad PCB EDA Suite
Loading...
Searching...
No Matches
richio.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) 2007-2011 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
26#include <cstdarg>
27#include <config.h> // HAVE_FGETC_NOLOCK
28
29#include <kiplatform/io.h>
30#include <core/ignore.h>
31#include <richio.h>
32#include <errno.h>
33#include <advanced_config.h>
35
36#include <wx/translation.h>
37#include <wx/ffile.h>
38
39
40// Fall back to getc() when getc_unlocked() is not available on the target platform.
41#if !defined( HAVE_FGETC_NOLOCK )
42#ifdef _MSC_VER
43
44// getc is not a macro on windows and adds a tiny overhead for the indirection to eventually
45// calling fgetc
46#define getc_unlocked _fgetc_nolock
47#else
48#define getc_unlocked getc
49#endif
50#endif
51
52
53static int vprint( std::string* result, const char* format, va_list ap )
54{
55 va_list tmp;
56 va_copy( tmp, ap );
57 size_t len = vsnprintf( nullptr, 0, format, tmp );
58 va_end( tmp );
59
60 // Resize the output to hold the required data
61 size_t size = result->size();
62 result->resize( size + len );
63
64 // Now do the actual printing
65 len = vsnprintf( result->data() + size, len + 1, format, ap );
66
67 return len;
68}
69
70
71int StrPrintf( std::string* result, const char* format, ... )
72{
73 va_list args;
74
75 va_start( args, format );
76 int ret = vprint( result, format, args );
77 va_end( args );
78
79 return ret;
80}
81
82
83std::string StrPrintf( const char* format, ... )
84{
85 std::string ret;
86 va_list args;
87
88 va_start( args, format );
89 ignore_unused( vprint( &ret, format, args ) );
90 va_end( args );
91
92 return ret;
93}
94
95
96wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType )
97{
98 // Check the path exists as a file first
99 // the IsOpened check would be logical, but on linux you can fopen (in read mode) a directory
100 // And then everything else in here will barf
101 if( !wxFileExists( aFilePath ) )
102 THROW_IO_ERROR( wxString::Format( _( "File '%s' does not exist." ), aFilePath ) );
103
104 wxString contents;
105 wxFFile ff( aFilePath );
106
107 if( !ff.IsOpened() )
108 THROW_IO_ERROR( wxString::Format( _( "Cannot open file '%s'." ), aFilePath ) );
109
110 // Try to determine encoding
111 char bytes[2]{ 0 };
112 ff.Read( bytes, 2 );
113 bool utf16le = bytes[1] == 0;
114
115 ff.Seek( 0 );
116
117 if( utf16le )
118 ff.ReadAll( &contents, wxMBConvUTF16LE() );
119 else
120 ff.ReadAll( &contents, wxMBConvUTF8() );
121
122 if( contents.empty() )
123 {
124 ff.Seek( 0 );
125 ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
126 }
127
128 if( contents.empty() )
129 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), aFilePath ) );
130
131 // I'm not sure what the source of this style of line-endings is, but it can be
132 // found in some Fairchild Semiconductor SPICE files.
133 contents.Replace( wxS( "\r\r\n" ), wxS( "\n" ) );
134
135 return contents;
136}
137
138
139//-----<LINE_READER>------------------------------------------------------
140
141LINE_READER::LINE_READER( unsigned aMaxLineLength ) :
142 m_length( 0 ), m_lineNum( 0 ), m_line( nullptr ),
143 m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
144{
145 if( aMaxLineLength != 0 )
146 {
147 // start at the INITIAL size, expand as needed up to the MAX size in maxLineLength
149
150 // but never go above user's aMaxLineLength, and leave space for trailing nul
151 if( m_capacity > aMaxLineLength+1 )
152 m_capacity = aMaxLineLength+1;
153
154 // Be sure there is room for a null EOL char, so reserve at least capacity+1 bytes
155 // to ensure capacity line length and avoid corner cases
156 // Use capacity+5 to cover and corner case
157 m_line = new char[m_capacity+5];
158
159 m_line[0] = '\0';
160 }
161}
162
163
165{
166 delete[] m_line;
167}
168
169
170void LINE_READER::expandCapacity( unsigned aNewsize )
171{
172 // m_length can equal maxLineLength and nothing breaks, there's room for
173 // the terminating nul. cannot go over this.
174 if( aNewsize > m_maxLineLength+1 )
175 aNewsize = m_maxLineLength+1;
176
177 if( aNewsize > m_capacity )
178 {
179 m_capacity = aNewsize;
180
181 // resize the buffer, and copy the original data
182 // Be sure there is room for the null EOL char, so reserve capacity+1 bytes
183 // to ensure capacity line length. Use capacity+5 to cover and corner case
184 char* bigger = new char[m_capacity+5];
185
186 wxASSERT( m_capacity >= m_length+1 );
187
188 memcpy( bigger, m_line, m_length );
189 bigger[m_length] = 0;
190
191 delete[] m_line;
192 m_line = bigger;
193 }
194}
195
196
197FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber,
198 unsigned aMaxLineLength ):
199 LINE_READER( aMaxLineLength ), m_iOwn( true )
200{
201 m_fp = KIPLATFORM::IO::SeqFOpen( aFileName, wxT( "rt" ) );
202
203 if( !m_fp )
204 {
205 wxString msg = wxString::Format( _( "Unable to open %s for reading." ),
206 aFileName.GetData() );
207 THROW_IO_ERROR( msg );
208 }
209
210 m_source = aFileName;
211 m_lineNum = aStartingLineNumber;
212}
213
214
215FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
216 bool doOwn,
217 unsigned aStartingLineNumber,
218 unsigned aMaxLineLength ) :
219 LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
220{
221 m_source = aFileName;
222 m_lineNum = aStartingLineNumber;
223}
224
225
227{
228 if( m_iOwn && m_fp )
229 fclose( m_fp );
230}
231
232
234{
235 fseek( m_fp, 0, SEEK_END );
236 long int fileLength = ftell( m_fp );
237 rewind( m_fp );
238
239 return fileLength;
240}
241
242
244{
245 return ftell( m_fp );
246}
247
248
250{
251 m_length = 0;
252
253 for( ;; )
254 {
256 THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
257
258 if( m_length >= m_capacity )
260
261 // faster, POSIX compatible fgetc(), no locking.
262 int cc = getc_unlocked( m_fp );
263
264 if( cc == EOF )
265 break;
266
267 m_line[ m_length++ ] = (char) cc;
268
269 if( cc == '\n' )
270 break;
271 }
272
273 m_line[ m_length ] = 0;
274
275 // m_lineNum is incremented even if there was no line read, because this
276 // leads to better error reporting when we hit an end of file.
277 ++m_lineNum;
278
279 return m_length ? m_line : nullptr;
280}
281
282
283STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ):
285 m_lines( aString ), m_ndx( 0 )
286{
287 // Clipboard text should be nice and _use multiple lines_ so that
288 // we can report _line number_ oriented error messages when parsing.
289 m_source = aSource;
290}
291
292
295 m_lines( aStartingPoint.m_lines ),
296 m_ndx( aStartingPoint.m_ndx )
297{
298 // since we are keeping the same "source" name, for error reporting purposes
299 // we need to have the same notion of line number and offset.
300
301 m_source = aStartingPoint.m_source;
302 m_lineNum = aStartingPoint.m_lineNum;
303}
304
305
307{
308 size_t nlOffset = m_lines.find( '\n', m_ndx );
309 unsigned new_length;
310
311 if( nlOffset == std::string::npos )
312 new_length = m_lines.length() - m_ndx;
313 else
314 new_length = nlOffset - m_ndx + 1; // include the newline, so +1
315
316 if( new_length )
317 {
318 if( new_length >= m_maxLineLength )
319 THROW_IO_ERROR( _("Line length exceeded") );
320
321 if( new_length+1 > m_capacity ) // +1 for terminating nul
322 expandCapacity( new_length+1 );
323
324 wxASSERT( m_ndx + new_length <= m_lines.length() );
325
326 memcpy( m_line, &m_lines[m_ndx], new_length );
327 m_ndx += new_length;
328 }
329
330 m_length = new_length;
331 ++m_lineNum; // this gets incremented even if no bytes were read
332 m_line[m_length] = 0;
333
334 return m_length ? m_line : nullptr;
335}
336
337
339 const wxString& aSource ) :
341 m_stream( aStream )
342{
343 m_source = aSource;
344}
345
346
348{
349 m_length = 0;
350
351 for( ;; )
352 {
354 THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
355
356 if( m_length + 1 > m_capacity )
358
359 // this read may fail, docs say to test LastRead() before trusting cc.
360 char cc = m_stream->GetC();
361
362 if( !m_stream->LastRead() )
363 break;
364
365 m_line[ m_length++ ] = cc;
366
367 if( cc == '\n' )
368 break;
369 }
370
371 m_line[ m_length ] = 0;
372
373 // m_lineNum is incremented even if there was no line read, because this
374 // leads to better error reporting when we hit an end of file.
375 ++m_lineNum;
376
377 return m_length ? m_line : nullptr;
378}
379
380
381//-----<OUTPUTFORMATTER>----------------------------------------------------
382
383// factor out a common GetQuoteChar
384
385const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote_char )
386{
387 // Include '#' so a symbol is not confused with a comment. We intend
388 // to wrap any symbol starting with a '#'.
389 // Our LEXER class handles comments, and comments appear to be an extension
390 // to the SPECCTRA DSN specification.
391 if( *wrapee == '#' )
392 return quote_char;
393
394 if( strlen( wrapee ) == 0 )
395 return quote_char;
396
397 bool isFirst = true;
398
399 for( ; *wrapee; ++wrapee, isFirst = false )
400 {
401 static const char quoteThese[] = "\t ()"
402 "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
403 "{}" // guessing that these are problems too
404 ;
405
406 // if the string to be wrapped (wrapee) has a delimiter in it,
407 // return the quote_char so caller wraps the wrapee.
408 if( strchr( quoteThese, *wrapee ) )
409 return quote_char;
410
411 if( !isFirst && '-' == *wrapee )
412 return quote_char;
413 }
414
415 return ""; // caller does not need to wrap, can use an unwrapped string.
416}
417
418
419const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee ) const
420{
421 return GetQuoteChar( wrapee, quoteChar );
422}
423
424
425int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap )
426{
427 // This function can call vsnprintf twice.
428 // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
429 // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
430 // see: www.cplusplus.com/reference/cstdio/vsnprintf
431 // we make a copy of va_list ap for the second call, if happens
432 va_list tmp;
433 va_copy( tmp, ap );
434 int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
435
436 if( ret >= (int) m_buffer.size() )
437 {
438 m_buffer.resize( ret + 1000 );
439 ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
440 }
441
442 va_end( tmp ); // Release the temporary va_list, initialised from ap
443
444 if( ret > 0 )
445 write( &m_buffer[0], ret );
446
447 return ret;
448}
449
450
451int OUTPUTFORMATTER::sprint( const char* fmt, ... )
452{
453 va_list args;
454
455 va_start( args, fmt );
456 int ret = vprint( fmt, args );
457 va_end( args );
458
459 return ret;
460}
461
462
463int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... )
464{
465#define NESTWIDTH 2
466
467 va_list args;
468
469 va_start( args, fmt );
470
471 int result = 0;
472 int total = 0;
473
474 for( int i = 0; i < nestLevel; ++i )
475 {
476 // no error checking needed, an exception indicates an error.
477 result = sprint( "%*c", NESTWIDTH, ' ' );
478
479 total += result;
480 }
481
482 // no error checking needed, an exception indicates an error.
483 result = vprint( fmt, args );
484
485 va_end( args );
486
487 total += result;
488 return total;
489}
490
491
492int OUTPUTFORMATTER::Print( const char* fmt, ... )
493{
494 va_list args;
495
496 va_start( args, fmt );
497
498 int result = 0;
499
500 // no error checking needed, an exception indicates an error.
501 result = vprint( fmt, args );
502
503 va_end( args );
504
505 return result;
506}
507
508
509std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) const
510{
511 std::string ret;
512
513 ret.reserve( aWrapee.size() * 2 + 2 );
514
515 ret += '"';
516
517 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
518 {
519 switch( *it )
520 {
521 case '\n':
522 ret += '\\';
523 ret += 'n';
524 break;
525 case '\r':
526 ret += '\\';
527 ret += 'r';
528 break;
529 case '\\':
530 ret += '\\';
531 ret += '\\';
532 break;
533 case '"':
534 ret += '\\';
535 ret += '"';
536 break;
537 default:
538 ret += *it;
539 }
540 }
541
542 ret += '"';
543
544 return ret;
545}
546
547
548std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) const
549{
550 // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
551 // The non-virtual function calls the virtual workhorse function, and if
552 // a different quoting or escaping strategy is desired from the standard,
553 // a derived class can overload Quotes() above, but
554 // should never be a reason to overload this Quotew() here.
555 return Quotes( (const char*) aWrapee.utf8_str() );
556}
557
558
559//-----<STRING_FORMATTER>----------------------------------------------------
560
561void STRING_FORMATTER::write( const char* aOutBuf, int aCount )
562{
563 m_mystring.append( aOutBuf, aCount );
564}
565
566
568{
569 std::string copy = m_mystring;
570
571 m_mystring.clear();
572
573 for( std::string::iterator i = copy.begin(); i != copy.end(); ++i )
574 {
575 if( !isspace( *i ) && *i != ')' && *i != '(' && *i != '"' )
576 {
577 m_mystring += *i;
578 }
579 }
580}
581
582
583FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode,
584 char aQuoteChar ):
585 OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
586 m_filename( aFileName )
587{
588 m_fp = wxFopen( aFileName, aMode );
589
590 if( !m_fp )
591 THROW_IO_ERROR( strerror( errno ) );
592}
593
594
596{
597 if( m_fp )
598 fclose( m_fp );
599}
600
601
602void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
603{
604 if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
605 THROW_IO_ERROR( strerror( errno ) );
606}
607
608
610 const wxChar* aMode,
611 char aQuoteChar ) :
612 OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar )
613{
614 m_fp = wxFopen( aFileName, aMode );
615
616 if( !m_fp )
617 THROW_IO_ERROR( strerror( errno ) );
618}
619
620
622{
623 try
624 {
626 }
627 catch( ... )
628 {}
629}
630
631
633{
634 if( !m_fp )
635 return false;
636
638
639 if( fwrite( m_buf.c_str(), m_buf.length(), 1, m_fp ) != 1 )
640 THROW_IO_ERROR( strerror( errno ) );
641
642 fclose( m_fp );
643 m_fp = nullptr;
644
645 return true;
646}
647
648
649void PRETTIFIED_FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
650{
651 m_buf.append( aOutBuf, aCount );
652}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
~FILE_LINE_READER()
May or may not close the open file, depending on doOwn in constructor.
Definition: richio.cpp:226
FILE_LINE_READER(const wxString &aFileName, unsigned aStartingLineNumber=0, unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Take aFileName and the size of the desired line buffer and opens the file and assumes the obligation ...
Definition: richio.cpp:197
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:245
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:249
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:244
long int FileLength()
Definition: richio.cpp:233
long int CurPos()
Definition: richio.cpp:243
FILE * m_fp
takes ownership
Definition: richio.h:510
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition: richio.cpp:583
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:602
wxInputStream * m_stream
The input stream to read. No ownership of this pointer.
Definition: richio.h:300
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition: richio.cpp:338
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:347
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
LINE_READER(unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Build a line reader and fixes the length of the maximum supported line length to aMaxLineLength.
Definition: richio.cpp:141
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:173
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:167
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:171
void expandCapacity(unsigned aNewsize)
Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assu...
Definition: richio.cpp:170
char * m_line
the read line of UTF8 text
Definition: richio.h:170
unsigned m_lineNum
Definition: richio.h:168
virtual ~LINE_READER()
Definition: richio.cpp:164
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:175
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
virtual void write(const char *aOutBuf, int aCount)=0
Should be coded in the interface implementation (derived) classes.
int sprint(const char *fmt,...)
Definition: richio.cpp:451
std::vector< char > m_buffer
Definition: richio.h:434
char quoteChar[2]
Definition: richio.h:435
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:425
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:548
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:463
static const char * GetQuoteChar(const char *wrapee, const char *quote_char)
Perform quote character need determination according to the Specctra DSN specification.
Definition: richio.cpp:385
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:509
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:649
PRETTIFIED_FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition: richio.cpp:609
bool Finish() override
Performs prettification and writes the stored buffer to the file.
Definition: richio.cpp:632
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:561
void StripUseless()
Removes whitespace, '(', and ')' from the string.
Definition: richio.cpp:567
std::string m_mystring
Definition: richio.h:481
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
std::string m_lines
Definition: richio.h:255
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition: richio.cpp:283
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:306
#define _(s)
void ignore_unused(const T &)
Definition: ignore.h:24
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
void Prettify(std::string &aSource, bool aCompactSave)
FILE * SeqFOpen(const wxString &aPath, const wxString &mode)
Opens the file like fopen but sets flags (if available) for sequential read hinting.
Definition: unix/io.cpp:31
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:53
#define NESTWIDTH
#define getc_unlocked
Definition: richio.cpp:48
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:71
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition: richio.cpp:96
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:304
#define LINE_READER_LINE_INITIAL_SIZE
Definition: richio.h:86
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:85