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.

90 lines
2.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <qa_utils/stdstream_line_reader.h>
  24. #include <ios>
  25. STDISTREAM_LINE_READER::STDISTREAM_LINE_READER() :
  26. LINE_READER( 0 ),
  27. m_stream( nullptr )
  28. {
  29. m_line = nullptr;
  30. m_lineNum = 0;
  31. }
  32. STDISTREAM_LINE_READER::~STDISTREAM_LINE_READER()
  33. {
  34. // this is only a view into a string, it can't be deleted by the base
  35. m_line = nullptr;
  36. }
  37. char* STDISTREAM_LINE_READER::ReadLine()
  38. {
  39. getline( *m_stream, m_buffer );
  40. m_buffer.append( 1, '\n' );
  41. m_length = m_buffer.size();
  42. m_line = (char*) m_buffer.data(); //ew why no const??
  43. // lineNum is incremented even if there was no line read, because this
  44. // leads to better error reporting when we hit an end of file.
  45. ++m_lineNum;
  46. return m_stream->eof() ? nullptr : m_line;
  47. }
  48. void STDISTREAM_LINE_READER::SetStream( std::istream& aStream )
  49. {
  50. // Could be done with a virtual getStream function, but the
  51. // virtual function call is a noticeable (but minor) penalty within
  52. // ReadLine() in tight loops
  53. m_stream = &aStream;
  54. }
  55. IFSTREAM_LINE_READER::IFSTREAM_LINE_READER( const wxFileName& aFileName ) :
  56. m_fStream( aFileName.GetFullPath().ToUTF8() )
  57. {
  58. if( !m_fStream.is_open() )
  59. {
  60. wxString msg = wxString::Format(
  61. _( "Unable to open filename \"%s\" for reading" ), aFileName.GetFullPath().GetData() );
  62. THROW_IO_ERROR( msg );
  63. }
  64. SetStream( m_fStream );
  65. m_source = aFileName.GetFullPath();
  66. }
  67. void IFSTREAM_LINE_READER::Rewind()
  68. {
  69. m_fStream.clear() ;
  70. m_fStream.seekg(0, std::ios::beg );
  71. }