1 #Region "Microsoft.VisualBasic::dd816a1bf45802e40b07dd782592f003, Microsoft.VisualBasic.Core\Text\IO\UnbufferedStringReader.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class UnbufferedStringReader
35     
36     '         Properties: Position
37     
38     '         Constructor: (+1 OverloadsSub New
39     
40     '         Function: Peek, (+2 Overloads) Read, ReadLine, ReadToEnd
41     
42     '         Sub: Close, Dispose
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Collections.Generic
50 Imports System.Text
51 Imports System.IO
52
53 Namespace Text
54
55     ''' <summary>
56     ''' Represents a reader that can read a sequential series of characters.
57     ''' </summary>
58     <Serializable> Public Class UnbufferedStringReader
59         Inherits TextReader
60
61         Dim _length As Integer
62         Dim _pos As Integer
63         Dim _s As String
64
65         Public Sub New(s As String)
66             If s Is Nothing Then
67                 Throw New ArgumentNullException("s")
68             End If
69             Me._s = s
70             Me._length = If((s Is Nothing), 0, s.Length)
71         End Sub
72
73         ''' <summary>
74         ''' Closes the System.IO.TextReader and releases any system resources associated
75         ''' with the TextReader.
76         ''' </summary>
77         Public Overrides Sub Close()
78             Me.Dispose(True)
79         End Sub
80
81         Protected Overrides Sub Dispose(disposing As Boolean)
82             Me._s = Nothing
83             Me._pos = 0
84             Me._length = 0
85             MyBase.Dispose(disposing)
86         End Sub
87
88         ''' <summary>
89         ''' Reads the next character without changing the state of the reader or the character
90         ''' source. Returns the next available character without actually reading it from
91         ''' the reader.
92         ''' </summary>
93         ''' <returns></returns>
94         Public Overrides Function Peek() As Integer
95             If Me._s Is Nothing Then
96                 Throw New Exception("object closed")
97             End If
98             If Me._pos = Me._length Then
99                 Return -1
100             End If
101             Return AscW(Me._s(Me._pos))
102         End Function
103
104         ''' <summary>
105         ''' Reads the next character from the text reader and advances the character position
106         ''' by one character.
107         ''' </summary>
108         ''' <returns></returns>
109         Public Overrides Function Read() As Integer
110             If Me._s Is Nothing Then
111                 Throw New Exception("object closed")
112             End If
113             If Me._pos = Me._length Then
114                 Return -1
115             End If
116             Dim c As Char = Me._s(Me._pos)
117             Me._pos += 1
118             Return AscW(c)
119         End Function
120
121         ''' <summary>
122         ''' Reads a specified maximum number of characters from the current reader and writes
123         ''' the data to a buffer, beginning at the specified index.
124         ''' </summary>
125         ''' <param name="buffer"></param>
126         ''' <param name="index"></param>
127         ''' <param name="count"></param>
128         ''' <returns></returns>
129         Public Overrides Function Read(buffer As Char(), index As Integer, count As IntegerAs Integer
130             If buffer Is Nothing Then
131                 Throw New ArgumentNullException("buffer")
132             End If
133             If index < 0 Then
134                 Throw New ArgumentOutOfRangeException("index")
135             End If
136             If count < 0 Then
137                 Throw New ArgumentOutOfRangeException("count")
138             End If
139             If (buffer.Length - index) < count Then
140                 Throw New ArgumentException("invalid offset length")
141             End If
142             If Me._s Is Nothing Then
143                 Throw New Exception("object closed")
144             End If
145             Dim num As Integer = Me._length - Me._pos
146             If num > 0 Then
147                 If num > count Then
148                     num = count
149                 End If
150                 Me._s.CopyTo(Me._pos, buffer, index, num)
151                 Me._pos += num
152             End If
153             Return num
154         End Function
155
156         ''' <summary>
157         ''' Reads a line of characters from the text reader and returns the data as a string.
158         ''' </summary>
159         ''' <returns></returns>
160         Public Overrides Function ReadLine() As String
161             If Me._s Is Nothing Then
162                 Throw New Exception("object closed")
163             End If
164             Dim num As Integer = Me._pos
165             While num < Me._length
166                 Dim ch As Char = Me._s(num)
167                 Select Case ch
168                     Case ControlChars.Cr, ControlChars.Lf
169                         If True Then
170                             Dim text As String = Me._s.Substring(Me._pos, num - Me._pos)
171                             Me._pos = num + 1
172                             If ((ch = ControlChars.Cr) AndAlso (Me._pos < Me._length)) AndAlso (Me._s(Me._pos) = ControlChars.Lf) Then
173                                 Me._pos += 1
174                             End If
175                             Return text
176                         End If
177                 End Select
178                 num += 1
179             End While
180             If num > Me._pos Then
181                 Dim text2 As String = Me._s.Substring(Me._pos, num - Me._pos)
182                 Me._pos = num
183                 Return text2
184             End If
185             Return Nothing
186         End Function
187
188         ''' <summary>
189         ''' Reads all characters from the current position to the end of the text reader
190         ''' and returns them as one string.
191         ''' </summary>
192         ''' <returns></returns>
193         Public Overrides Function ReadToEnd() As String
194             Dim text As String
195             If Me._s Is Nothing Then
196                 Throw New Exception("object closed")
197             End If
198             If Me._pos = 0 Then
199                 text = Me._s
200             Else
201                 text = Me._s.Substring(Me._pos, Me._length - Me._pos)
202             End If
203             Me._pos = Me._length
204             Return text
205         End Function
206
207         ''' <summary>
208         ''' The current read position.
209         ''' </summary>
210         ''' <returns></returns>
211         Public ReadOnly Property Position() As Integer
212             Get
213                 Return _pos
214             End Get
215         End Property
216     End Class
217 End Namespace