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