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.

577 lines
15 KiB

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