'Classes/System/IO/StringReader.ab

Namespace System
Namespace IO

/*
@brief Stringから読み込みを行うTextReaderの実装。
@date 2008/02/26
@auther Egtra
*/
Class StringReader
	Inherits TextReader
Public
	/*
	@brief コンストラクタ
	@date 2008/02/26
	@auther Egtra
	@param str 読み取る基となる文字列。
	@exception ArgumentNullException strがNothingのとき。
	*/
	Sub StringReader(str As String)
		If ActiveBasic.IsNothing(str) Then
			Throw New ArgumentNullException("str")
		End If
		s = str
		i = 0
	End Sub

	/*
	@date 2008/02/26
	@auther Egtra
	*/
	Override Function Peek() As Long
		If i = s.Length Then
			Peek = -1
		Else
			Peek = s[i]
		End If
	End Function

	/*
	@date 2008/02/26
	@auther Egtra
	*/
	Override Function Read() As Long
		If i = s.Length Then
			Read = -1
		Else
			Read = s[i]
			i++
		End If
	End Function

	/*
	@date 2008/02/26
	@auther Egtra
	*/
	Override Function ReadToEnd() As String
		ReadToEnd = s.Substring(i)
		i = s.Length
	End Function

Protected
	/*
	@date 2008/02/26
	@auther Egtra
	*/
	Override Sub Dispose(disposing As Boolean)
		s = Nothing
		i = 0
	End Sub

	/*
	@date 2008/02/26
	@auther Egtra
	*/
	Override Function ReadImpl(buffer As *Char, index As Long, count As Long) As Long
		ReadImpl = Math.Min(count, s.Length - i)
		ActiveBasic.Strings.ChrCopy(VarPtr(buffer[index]) As *Char, (StrPtr(s) + i * SizeOf (Char)) As *Char, ReadImpl As SIZE_T) 'ToDo: ポインタに対する+演算
	End Function

Private
	s As String
	i As Long
End Class

End Namespace
End Namespace
