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