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 // RCS-ID:      $Id: reader.cpp 35650 2005-09-23 12:56:45Z MR $
8 // Copyright:   (c) 2000, Guillermo Rodriguez Garcia
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 
12 // ==========================================================================
13 // headers, declarations, constants
14 // ==========================================================================
15 
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18 
19 #ifdef __BORLANDC__
20     #pragma hdrstop
21 #endif
22 
23 #ifndef WX_PRECOMP
24     #include "wx/wx.h"
25 #endif
26 
27 #include "wx/txtstrm.h"
28 #include "reader.h"
29 
30 
31 // ==========================================================================
32 // LifeReader
33 // ==========================================================================
34 
35 #define LIFE_CHECKVAL( msg )                                        \
36     if (!m_ok)                                                      \
37     {                                                               \
38         wxMessageBox( msg, _("Error"), wxICON_EXCLAMATION | wxOK ); \
39         return;                                                     \
40     }
41 
LifeReader(wxInputStream & is)42 LifeReader::LifeReader(wxInputStream& is)
43 {
44     wxBufferedInputStream buff_is(is);
45     wxTextInputStream     text_is(buff_is);
46     wxString              line, rest;
47 
48     // check stream
49     m_ok = is.IsOk();
50     LIFE_CHECKVAL(_("Couldn't read any data"));
51 
52     // read signature
53     m_ok = text_is.ReadLine().Contains(wxT("#Life 1.05"));
54     LIFE_CHECKVAL(_("Error reading signature. Not a Life pattern?"));
55 
56     // read description
57     m_description = wxEmptyString;
58     line = text_is.ReadLine();
59     while (buff_is.IsOk() && line.StartsWith(wxT("#D"), &rest))
60     {
61         m_description += rest.Trim(false);
62         m_description += wxT("\n");
63         line = text_is.ReadLine();
64     }
65 
66     m_ok = buff_is.IsOk();
67     LIFE_CHECKVAL(_("Unexpected EOF while reading description"));
68 
69     // read rules
70     m_ok = line.StartsWith(wxT("#N"));
71     LIFE_CHECKVAL(_("Sorry, non-conway rules not supported yet"));
72 
73     // read shape
74     while (buff_is.IsOk())
75     {
76         line = ( text_is.ReadLine() ).Trim();
77 
78         if (!line.empty())
79         {
80             if (line.StartsWith(wxT("#P "), &rest))
81                 m_shape.Add(rest);
82             else
83                 m_shape.Add(line);
84         }
85     }
86 }
87 
88