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.

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