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.

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