reader.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: reader.cpp
  3. // Purpose: Life! pattern reader (writer coming soon)
  4. // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
  5. // Modified by:
  6. // Created: Jan/2000
  7. // Copyright: (c) 2000, Guillermo Rodriguez Garcia
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ==========================================================================
  11. // headers, declarations, constants
  12. // ==========================================================================
  13. // For compilers that support precompilation, includes "wx/wx.h".
  14. #include "wx/wxprec.h"
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18. #ifndef WX_PRECOMP
  19. #include "wx/wx.h"
  20. #endif
  21. #include "wx/txtstrm.h"
  22. #include "reader.h"
  23. // ==========================================================================
  24. // LifeReader
  25. // ==========================================================================
  26. #define LIFE_CHECKVAL( msg ) \
  27. if (!m_ok) \
  28. { \
  29. wxMessageBox( msg, _("Error"), wxICON_EXCLAMATION | wxOK ); \
  30. return; \
  31. }
  32. LifeReader::LifeReader(wxInputStream& is)
  33. {
  34. wxBufferedInputStream buff_is(is);
  35. wxTextInputStream text_is(buff_is);
  36. wxString line, rest;
  37. // check stream
  38. m_ok = is.IsOk();
  39. LIFE_CHECKVAL(_("Couldn't read any data"));
  40. // read signature
  41. m_ok = text_is.ReadLine().Contains(wxT("#Life 1.05"));
  42. LIFE_CHECKVAL(_("Error reading signature. Not a Life pattern?"));
  43. // read description
  44. m_description = wxEmptyString;
  45. line = text_is.ReadLine();
  46. while (buff_is.IsOk() && line.StartsWith(wxT("#D"), &rest))
  47. {
  48. m_description += rest.Trim(false);
  49. m_description += wxT("\n");
  50. line = text_is.ReadLine();
  51. }
  52. m_ok = buff_is.IsOk();
  53. LIFE_CHECKVAL(_("Unexpected EOF while reading description"));
  54. // read rules
  55. m_ok = line.StartsWith(wxT("#N"));
  56. LIFE_CHECKVAL(_("Sorry, non-conway rules not supported yet"));
  57. // read shape
  58. while (buff_is.IsOk())
  59. {
  60. line = ( text_is.ReadLine() ).Trim();
  61. if (!line.empty())
  62. {
  63. if (line.StartsWith(wxT("#P "), &rest))
  64. m_shape.Add(rest);
  65. else
  66. m_shape.Add(line);
  67. }
  68. }
  69. }