You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

652 lines
16 KiB

15 years ago
16 years ago
  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 <dick@softplc.com>
  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. #include <cstdarg>
  25. #include <config.h> // HAVE_FGETC_NOLOCK
  26. #include <kiplatform/io.h>
  27. #include <core/ignore.h>
  28. #include <richio.h>
  29. #include <errno.h>
  30. #include <advanced_config.h>
  31. #include <io/kicad/kicad_io_utils.h>
  32. #include <wx/translation.h>
  33. #include <wx/ffile.h>
  34. // Fall back to getc() when getc_unlocked() is not available on the target platform.
  35. #if !defined( HAVE_FGETC_NOLOCK )
  36. #ifdef _MSC_VER
  37. // getc is not a macro on windows and adds a tiny overhead for the indirection to eventually
  38. // calling fgetc
  39. #define getc_unlocked _fgetc_nolock
  40. #else
  41. #define getc_unlocked getc
  42. #endif
  43. #endif
  44. static int vprint( std::string* result, const char* format, va_list ap )
  45. {
  46. va_list tmp;
  47. va_copy( tmp, ap );
  48. size_t len = vsnprintf( nullptr, 0, format, tmp );
  49. va_end( tmp );
  50. // Resize the output to hold the required data
  51. size_t size = result->size();
  52. result->resize( size + len );
  53. // Now do the actual printing
  54. len = vsnprintf( result->data() + size, len + 1, format, ap );
  55. return len;
  56. }
  57. int StrPrintf( std::string* result, const char* format, ... )
  58. {
  59. va_list args;
  60. va_start( args, format );
  61. int ret = vprint( result, format, args );
  62. va_end( args );
  63. return ret;
  64. }
  65. std::string StrPrintf( const char* format, ... )
  66. {
  67. std::string ret;
  68. va_list args;
  69. va_start( args, format );
  70. ignore_unused( vprint( &ret, format, args ) );
  71. va_end( args );
  72. return ret;
  73. }
  74. wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType )
  75. {
  76. // Check the path exists as a file first
  77. // the IsOpened check would be logical, but on linux you can fopen (in read mode) a directory
  78. // And then everything else in here will barf
  79. if( !wxFileExists( aFilePath ) )
  80. THROW_IO_ERROR( wxString::Format( _( "File '%s' does not exist." ), aFilePath ) );
  81. wxString contents;
  82. wxFFile ff( aFilePath );
  83. if( !ff.IsOpened() )
  84. THROW_IO_ERROR( wxString::Format( _( "Cannot open file '%s'." ), aFilePath ) );
  85. // Try to determine encoding
  86. char bytes[2]{ 0 };
  87. ff.Read( bytes, 2 );
  88. bool utf16le = bytes[1] == 0;
  89. ff.Seek( 0 );
  90. if( utf16le )
  91. ff.ReadAll( &contents, wxMBConvUTF16LE() );
  92. else
  93. ff.ReadAll( &contents, wxMBConvUTF8() );
  94. if( contents.empty() )
  95. {
  96. ff.Seek( 0 );
  97. ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
  98. }
  99. if( contents.empty() )
  100. THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), aFilePath ) );
  101. // I'm not sure what the source of this style of line-endings is, but it can be
  102. // found in some Fairchild Semiconductor SPICE files.
  103. contents.Replace( wxS( "\r\r\n" ), wxS( "\n" ) );
  104. return contents;
  105. }
  106. //-----<LINE_READER>------------------------------------------------------
  107. LINE_READER::LINE_READER( unsigned aMaxLineLength ) :
  108. m_length( 0 ), m_lineNum( 0 ), m_line( nullptr ),
  109. m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
  110. {
  111. if( aMaxLineLength != 0 )
  112. {
  113. // start at the INITIAL size, expand as needed up to the MAX size in maxLineLength
  114. m_capacity = LINE_READER_LINE_INITIAL_SIZE;
  115. // but never go above user's aMaxLineLength, and leave space for trailing nul
  116. if( m_capacity > aMaxLineLength+1 )
  117. m_capacity = aMaxLineLength+1;
  118. // Be sure there is room for a null EOL char, so reserve at least capacity+1 bytes
  119. // to ensure capacity line length and avoid corner cases
  120. // Use capacity+5 to cover and corner case
  121. m_line = new char[m_capacity+5];
  122. m_line[0] = '\0';
  123. }
  124. }
  125. LINE_READER::~LINE_READER()
  126. {
  127. delete[] m_line;
  128. }
  129. void LINE_READER::expandCapacity( unsigned aNewsize )
  130. {
  131. // m_length can equal maxLineLength and nothing breaks, there's room for
  132. // the terminating nul. cannot go over this.
  133. if( aNewsize > m_maxLineLength+1 )
  134. aNewsize = m_maxLineLength+1;
  135. if( aNewsize > m_capacity )
  136. {
  137. m_capacity = aNewsize;
  138. // resize the buffer, and copy the original data
  139. // Be sure there is room for the null EOL char, so reserve capacity+1 bytes
  140. // to ensure capacity line length. Use capacity+5 to cover and corner case
  141. char* bigger = new char[m_capacity+5];
  142. wxASSERT( m_capacity >= m_length+1 );
  143. memcpy( bigger, m_line, m_length );
  144. bigger[m_length] = 0;
  145. delete[] m_line;
  146. m_line = bigger;
  147. }
  148. }
  149. FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber,
  150. unsigned aMaxLineLength ):
  151. LINE_READER( aMaxLineLength ), m_iOwn( true )
  152. {
  153. m_fp = KIPLATFORM::IO::SeqFOpen( aFileName, wxT( "rt" ) );
  154. if( !m_fp )
  155. {
  156. wxString msg = wxString::Format( _( "Unable to open %s for reading." ),
  157. aFileName.GetData() );
  158. THROW_IO_ERROR( msg );
  159. }
  160. m_source = aFileName;
  161. m_lineNum = aStartingLineNumber;
  162. }
  163. FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
  164. bool doOwn,
  165. unsigned aStartingLineNumber,
  166. unsigned aMaxLineLength ) :
  167. LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
  168. {
  169. m_source = aFileName;
  170. m_lineNum = aStartingLineNumber;
  171. }
  172. FILE_LINE_READER::~FILE_LINE_READER()
  173. {
  174. if( m_iOwn && m_fp )
  175. fclose( m_fp );
  176. }
  177. long int FILE_LINE_READER::FileLength()
  178. {
  179. fseek( m_fp, 0, SEEK_END );
  180. long int fileLength = ftell( m_fp );
  181. rewind( m_fp );
  182. return fileLength;
  183. }
  184. long int FILE_LINE_READER::CurPos()
  185. {
  186. return ftell( m_fp );
  187. }
  188. char* FILE_LINE_READER::ReadLine()
  189. {
  190. m_length = 0;
  191. for( ;; )
  192. {
  193. if( m_length >= m_maxLineLength )
  194. THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
  195. if( m_length >= m_capacity )
  196. expandCapacity( m_capacity * 2 );
  197. // faster, POSIX compatible fgetc(), no locking.
  198. int cc = getc_unlocked( m_fp );
  199. if( cc == EOF )
  200. break;
  201. m_line[ m_length++ ] = (char) cc;
  202. if( cc == '\n' )
  203. break;
  204. }
  205. m_line[ m_length ] = 0;
  206. // m_lineNum is incremented even if there was no line read, because this
  207. // leads to better error reporting when we hit an end of file.
  208. ++m_lineNum;
  209. return m_length ? m_line : nullptr;
  210. }
  211. STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ):
  212. LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
  213. m_lines( aString ), m_ndx( 0 )
  214. {
  215. // Clipboard text should be nice and _use multiple lines_ so that
  216. // we can report _line number_ oriented error messages when parsing.
  217. m_source = aSource;
  218. }
  219. STRING_LINE_READER::STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint ):
  220. LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
  221. m_lines( aStartingPoint.m_lines ),
  222. m_ndx( aStartingPoint.m_ndx )
  223. {
  224. // since we are keeping the same "source" name, for error reporting purposes
  225. // we need to have the same notion of line number and offset.
  226. m_source = aStartingPoint.m_source;
  227. m_lineNum = aStartingPoint.m_lineNum;
  228. }
  229. char* STRING_LINE_READER::ReadLine()
  230. {
  231. size_t nlOffset = m_lines.find( '\n', m_ndx );
  232. unsigned new_length;
  233. if( nlOffset == std::string::npos )
  234. new_length = m_lines.length() - m_ndx;
  235. else
  236. new_length = nlOffset - m_ndx + 1; // include the newline, so +1
  237. if( new_length )
  238. {
  239. if( new_length >= m_maxLineLength )
  240. THROW_IO_ERROR( _("Line length exceeded") );
  241. if( new_length+1 > m_capacity ) // +1 for terminating nul
  242. expandCapacity( new_length+1 );
  243. wxASSERT( m_ndx + new_length <= m_lines.length() );
  244. memcpy( m_line, &m_lines[m_ndx], new_length );
  245. m_ndx += new_length;
  246. }
  247. m_length = new_length;
  248. ++m_lineNum; // this gets incremented even if no bytes were read
  249. m_line[m_length] = 0;
  250. return m_length ? m_line : nullptr;
  251. }
  252. INPUTSTREAM_LINE_READER::INPUTSTREAM_LINE_READER( wxInputStream* aStream,
  253. const wxString& aSource ) :
  254. LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
  255. m_stream( aStream )
  256. {
  257. m_source = aSource;
  258. }
  259. char* INPUTSTREAM_LINE_READER::ReadLine()
  260. {
  261. m_length = 0;
  262. for( ;; )
  263. {
  264. if( m_length >= m_maxLineLength )
  265. THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
  266. if( m_length + 1 > m_capacity )
  267. expandCapacity( m_capacity * 2 );
  268. // this read may fail, docs say to test LastRead() before trusting cc.
  269. char cc = m_stream->GetC();
  270. if( !m_stream->LastRead() )
  271. break;
  272. m_line[ m_length++ ] = cc;
  273. if( cc == '\n' )
  274. break;
  275. }
  276. m_line[ m_length ] = 0;
  277. // m_lineNum is incremented even if there was no line read, because this
  278. // leads to better error reporting when we hit an end of file.
  279. ++m_lineNum;
  280. return m_length ? m_line : nullptr;
  281. }
  282. //-----<OUTPUTFORMATTER>----------------------------------------------------
  283. // factor out a common GetQuoteChar
  284. const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote_char )
  285. {
  286. // Include '#' so a symbol is not confused with a comment. We intend
  287. // to wrap any symbol starting with a '#'.
  288. // Our LEXER class handles comments, and comments appear to be an extension
  289. // to the SPECCTRA DSN specification.
  290. if( *wrapee == '#' )
  291. return quote_char;
  292. if( strlen( wrapee ) == 0 )
  293. return quote_char;
  294. bool isFirst = true;
  295. for( ; *wrapee; ++wrapee, isFirst = false )
  296. {
  297. static const char quoteThese[] = "\t ()"
  298. "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
  299. "{}" // guessing that these are problems too
  300. ;
  301. // if the string to be wrapped (wrapee) has a delimiter in it,
  302. // return the quote_char so caller wraps the wrapee.
  303. if( strchr( quoteThese, *wrapee ) )
  304. return quote_char;
  305. if( !isFirst && '-' == *wrapee )
  306. return quote_char;
  307. }
  308. return ""; // caller does not need to wrap, can use an unwrapped string.
  309. }
  310. const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee ) const
  311. {
  312. return GetQuoteChar( wrapee, quoteChar );
  313. }
  314. int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap )
  315. {
  316. // This function can call vsnprintf twice.
  317. // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
  318. // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
  319. // see: www.cplusplus.com/reference/cstdio/vsnprintf
  320. // we make a copy of va_list ap for the second call, if happens
  321. va_list tmp;
  322. va_copy( tmp, ap );
  323. int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
  324. if( ret >= (int) m_buffer.size() )
  325. {
  326. m_buffer.resize( ret + 1000 );
  327. ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
  328. }
  329. va_end( tmp ); // Release the temporary va_list, initialised from ap
  330. if( ret > 0 )
  331. write( &m_buffer[0], ret );
  332. return ret;
  333. }
  334. int OUTPUTFORMATTER::sprint( const char* fmt, ... )
  335. {
  336. va_list args;
  337. va_start( args, fmt );
  338. int ret = vprint( fmt, args );
  339. va_end( args );
  340. return ret;
  341. }
  342. int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... )
  343. {
  344. #define NESTWIDTH 2 ///< how many spaces per nestLevel
  345. va_list args;
  346. va_start( args, fmt );
  347. int result = 0;
  348. int total = 0;
  349. for( int i = 0; i < nestLevel; ++i )
  350. {
  351. // no error checking needed, an exception indicates an error.
  352. result = sprint( "%*c", NESTWIDTH, ' ' );
  353. total += result;
  354. }
  355. // no error checking needed, an exception indicates an error.
  356. result = vprint( fmt, args );
  357. va_end( args );
  358. total += result;
  359. return total;
  360. }
  361. int OUTPUTFORMATTER::Print( const char* fmt, ... )
  362. {
  363. va_list args;
  364. va_start( args, fmt );
  365. int result = 0;
  366. // no error checking needed, an exception indicates an error.
  367. result = vprint( fmt, args );
  368. va_end( args );
  369. return result;
  370. }
  371. std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) const
  372. {
  373. std::string ret;
  374. ret.reserve( aWrapee.size() * 2 + 2 );
  375. ret += '"';
  376. for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
  377. {
  378. switch( *it )
  379. {
  380. case '\n':
  381. ret += '\\';
  382. ret += 'n';
  383. break;
  384. case '\r':
  385. ret += '\\';
  386. ret += 'r';
  387. break;
  388. case '\\':
  389. ret += '\\';
  390. ret += '\\';
  391. break;
  392. case '"':
  393. ret += '\\';
  394. ret += '"';
  395. break;
  396. default:
  397. ret += *it;
  398. }
  399. }
  400. ret += '"';
  401. return ret;
  402. }
  403. std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) const
  404. {
  405. // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
  406. // The non-virtual function calls the virtual workhorse function, and if
  407. // a different quoting or escaping strategy is desired from the standard,
  408. // a derived class can overload Quotes() above, but
  409. // should never be a reason to overload this Quotew() here.
  410. return Quotes( (const char*) aWrapee.utf8_str() );
  411. }
  412. //-----<STRING_FORMATTER>----------------------------------------------------
  413. void STRING_FORMATTER::write( const char* aOutBuf, int aCount )
  414. {
  415. m_mystring.append( aOutBuf, aCount );
  416. }
  417. void STRING_FORMATTER::StripUseless()
  418. {
  419. std::string copy = m_mystring;
  420. m_mystring.clear();
  421. for( std::string::iterator i = copy.begin(); i != copy.end(); ++i )
  422. {
  423. if( !isspace( *i ) && *i != ')' && *i != '(' && *i != '"' )
  424. {
  425. m_mystring += *i;
  426. }
  427. }
  428. }
  429. FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode,
  430. char aQuoteChar ):
  431. OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
  432. m_filename( aFileName )
  433. {
  434. m_fp = wxFopen( aFileName, aMode );
  435. if( !m_fp )
  436. THROW_IO_ERROR( strerror( errno ) );
  437. }
  438. FILE_OUTPUTFORMATTER::~FILE_OUTPUTFORMATTER()
  439. {
  440. if( m_fp )
  441. fclose( m_fp );
  442. }
  443. void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
  444. {
  445. if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
  446. THROW_IO_ERROR( strerror( errno ) );
  447. }
  448. PRETTIFIED_FILE_OUTPUTFORMATTER::PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName,
  449. const wxChar* aMode,
  450. char aQuoteChar ) :
  451. OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar )
  452. {
  453. m_fp = wxFopen( aFileName, aMode );
  454. if( !m_fp )
  455. THROW_IO_ERROR( strerror( errno ) );
  456. }
  457. PRETTIFIED_FILE_OUTPUTFORMATTER::~PRETTIFIED_FILE_OUTPUTFORMATTER()
  458. {
  459. try
  460. {
  461. PRETTIFIED_FILE_OUTPUTFORMATTER::Finish();
  462. }
  463. catch( ... )
  464. {}
  465. }
  466. bool PRETTIFIED_FILE_OUTPUTFORMATTER::Finish()
  467. {
  468. if( !m_fp )
  469. return false;
  470. KICAD_FORMAT::Prettify( m_buf, ADVANCED_CFG::GetCfg().m_CompactSave );
  471. if( fwrite( m_buf.c_str(), m_buf.length(), 1, m_fp ) != 1 )
  472. THROW_IO_ERROR( strerror( errno ) );
  473. fclose( m_fp );
  474. m_fp = nullptr;
  475. return true;
  476. }
  477. void PRETTIFIED_FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
  478. {
  479. m_buf.append( aOutBuf, aCount );
  480. }