source: trunk/ab5.0/ablib/src/Classes/System/IO/TextReader.ab@ 655

Last change on this file since 655 was 655, checked in by イグトランス (egtra), 15 years ago

#161完了。StreamReaderのUnicode対応。

File size: 3.5 KB
RevLine 
[655]1Namespace System
2Namespace IO
[420]3
4Class TextReader
5 Implements System.IDisposable
6Public
[426]7 Virtual Sub ~TextReader()
8 Dispose(False)
9 End Sub
[420]10
[426]11' Static Null = StreamReader.Null As TextReader
[420]12
13Public
[426]14 Sub Close()
15 Dispose(True)
[420]16 End Sub
[426]17
18 Sub Dispose()
19 Dispose(True)
20 End Sub
21
[420]22 Abstract Function Peek() As Long
23 Abstract Function Read() As Long
[432]24 /*
25 @date 2008/02/26
26 @auther Egtra
27 */
[655]28 Function Read(buffer As *WCHAR, index As Long, count As Long) As Long
[432]29 If buffer = 0 Then
[655]30 Throw New ArgumentNullException("buffer")
[432]31 ElseIf index < 0 Then
[655]32 Throw New ArgumentOutOfRangeException("index")
[432]33 ElseIf count < 0 Then
[655]34 Throw New ArgumentOutOfRangeException("count")
[432]35 End If
[655]36 Read = ReadImpl(VarPtr(buffer[index]), count)
[426]37 End Function
[432]38
[655]39 Static Function Synchronized(reader As TextReader) As TextReader
40 Synchronized = New Detail.SynchronizedTextReader(reader)
41 End Function
42
[432]43 /*
44 @date 2008/02/26
45 @auther Egtra
46 @retval Nothing EOFに達しているとき
47 @retval 有効なStringインスタンス 読み取った1行
48 */
[655]49 Function ReadLine() As String
[432]50 If Peek() = -1 Then
51 Exit Function
52 End If
[655]53 Dim sb = New Collections.Generic.List<WCHAR>
[432]54 Do
55 Dim ch = Read()
56 If ch = &h0D Then
57 If Peek() = &h0A Then
58 Read() 'CR LFの場合
59 End If
60 Exit Do
61 End If
62 Select Case ch
63 Case -1 'EOF
64 Exit Do
65 Case &h0A 'LF
66 Exit Do
67 Case &h0B 'VT
68 Exit Do
69 Case &h0C 'FF
70 Exit Do
71 Case &h0D 'CR
72 Exit Do
[655]73 Case &h85 'NEL
74 Exit Do
75 Case &h2028 'LS
76 Exit Do
77 Case &h2029 'PS
78 Exit Do
[432]79 End Select
[655]80 sb.Add(ch As WCHAR)
[432]81 Loop
[655]82 ReadLine = New String(sb As *WCHAR, sb.Count)
[432]83 End Function
84 /*
[457]85 @brief 現在位置からストリームの終わりまで読み込む。
[432]86 @date 2008/02/26
87 @auther Egtra
88 */
89 Virtual Function ReadToEnd() As String
90 Dim sb = New Text.StringBuilder(8192)
91 Do
92 Dim ch = Read()
93 If ch = -1 Then
94 ReadToEnd = sb.ToString
95 Exit Function
96 End If
[497]97 sb.Append(ch As Char)
[432]98 Loop
99 End Function
100
[655]101Protected
102 Sub TextReader()
103 End Sub
[457]104
[468]105 Virtual Sub Dispose(disposing As Boolean)
106 End Sub
[432]107
108 /*
109 @date 2008/02/26
110 @auther Egtra
111 */
[655]112 Virtual Function ReadImpl(buffer As *WCHAR, count As Long) As Long
[432]113 Dim i As Long
114 For i = 0 To ELM(count)
115 Dim c = Read()
116 If c = -1 Then
[655]117 ReadImpl = i
[432]118 Exit Function
119 Else
[655]120 buffer[i] = c As Char
[432]121 End If
122 Next
[655]123 ReadImpl = i
[432]124 End Function
[420]125End Class
126
[457]127Namespace Detail
128
129Class SynchronizedTextReader
130 Inherits TextReader
131Public
132 Sub SynchronizedTextReader(reader As TextReader)
133 cs = New ActiveBasic.Windows.CriticalSection
134 base = reader
135 End Sub
136
137 Override Function Peek() As Long
138' Using lock = cs.Lock
139 Peek = base.Peek
140' End Using
141 End Function
142
143 Override Function Read() As Long
144' Using lock = cs.Lock
145 Read = base.Read
146' End Using
147 End Function
148
149Protected
150 Override Sub Dispose(disposing As Boolean)
151 Dim s = Nothing As Stream
152 SetPointer(VarPtr(s) As *VoidPtr, InterlockedExchangePointer(ByVal VarPtr(base) As *VoidPtr, 0))
153 If disposing Then
154 If Not ActiveBasic.IsNothing(s) Then
155 s.Dispose()
156 End If
157 cs.Dispose()
158 End If
159 End Sub
160
[655]161 Override Function ReadImpl(buffer As *WCHAR, count As Long) As Long
[457]162' Using lock = cs.Lock
[655]163 ReadImpl = base.ReadImpl(buffer, count)
[457]164' End Using
165 End Function
166Private
167 cs As ActiveBasic.Windows.CriticalSection
168 base As TextReader
169End Class
170
171End Namespace
172
[655]173End Namespace
174End Namespace
Note: See TracBrowser for help on using the repository browser.