aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Clojure/Readers/PushbackTextReader.cs
blob: 4761e4ccb72964c972f16f3d86e23710e5ea0daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace clojure.lang
{
    public class PushbackTextReader : TextReader, IDisposable
    {
        #region Data

        protected TextReader _baseReader;
        protected TextReader BaseReader
        {
            get { return _baseReader; }
        }

        protected int _unreadChar;
        protected bool _hasUnread = false;

        #endregion

        #region C-tors

        public PushbackTextReader(TextReader reader)
        {
            _baseReader = reader;
        }

        #endregion

        #region Lookahead

        public override int Peek()
        {
            return _baseReader.Peek();
        }

        #endregion

        #region Unreading

        public virtual void Unread(int ch)
        {
            if (_hasUnread)
                throw new IOException("Can't unread a second character.");

            _unreadChar = ch;
            _hasUnread = true;
 
        }


        #endregion

        #region Basic reading

        public override int Read()
        {
            int ret;
            if (_hasUnread)
            {
                ret = _unreadChar;
                _hasUnread = false;
            }
            else
                ret = _baseReader.Read();

            return ret;
        }

       #endregion

        #region Lifetime methods

        public override void Close()
        {
            _baseReader.Close();
            base.Close();
        }

        void IDisposable.Dispose()
        {
            _baseReader.Dispose();
            base.Dispose();
        }

        #endregion

    }
}