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
Line 
1Namespace System
2Namespace IO
3
4Class TextReader
5 Implements System.IDisposable
6Public
7 Virtual Sub ~TextReader()
8 Dispose(False)
9 End Sub
10
11' Static Null = StreamReader.Null As TextReader
12
13Public
14 Sub Close()
15 Dispose(True)
16 End Sub
17
18 Sub Dispose()
19 Dispose(True)
20 End Sub
21
22 Abstract Function Peek() As Long
23 Abstract Function Read() As Long
24 /*
25 @date 2008/02/26
26 @auther Egtra
27 */
28 Function Read(buffer As *WCHAR, index As Long, count As Long) As Long
29 If buffer = 0 Then
30 Throw New ArgumentNullException("buffer")
31 ElseIf index < 0 Then
32 Throw New ArgumentOutOfRangeException("index")
33 ElseIf count < 0 Then
34 Throw New ArgumentOutOfRangeException("count")
35 End If
36 Read = ReadImpl(VarPtr(buffer[index]), count)
37 End Function
38
39 Static Function Synchronized(reader As TextReader) As TextReader
40 Synchronized = New Detail.SynchronizedTextReader(reader)
41 End Function
42
43 /*
44 @date 2008/02/26
45 @auther Egtra
46 @retval Nothing EOFに達しているとき
47 @retval 有効なStringインスタンス 読み取った1行
48 */
49 Function ReadLine() As String
50 If Peek() = -1 Then
51 Exit Function
52 End If
53 Dim sb = New Collections.Generic.List<WCHAR>
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
73 Case &h85 'NEL
74 Exit Do
75 Case &h2028 'LS
76 Exit Do
77 Case &h2029 'PS
78 Exit Do
79 End Select
80 sb.Add(ch As WCHAR)
81 Loop
82 ReadLine = New String(sb As *WCHAR, sb.Count)
83 End Function
84 /*
85 @brief 現在位置からストリームの終わりまで読み込む。
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
97 sb.Append(ch As Char)
98 Loop
99 End Function
100
101Protected
102 Sub TextReader()
103 End Sub
104
105 Virtual Sub Dispose(disposing As Boolean)
106 End Sub
107
108 /*
109 @date 2008/02/26
110 @auther Egtra
111 */
112 Virtual Function ReadImpl(buffer As *WCHAR, count As Long) As Long
113 Dim i As Long
114 For i = 0 To ELM(count)
115 Dim c = Read()
116 If c = -1 Then
117 ReadImpl = i
118 Exit Function
119 Else
120 buffer[i] = c As Char
121 End If
122 Next
123 ReadImpl = i
124 End Function
125End Class
126
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
161 Override Function ReadImpl(buffer As *WCHAR, count As Long) As Long
162' Using lock = cs.Lock
163 ReadImpl = base.ReadImpl(buffer, count)
164' End Using
165 End Function
166Private
167 cs As ActiveBasic.Windows.CriticalSection
168 base As TextReader
169End Class
170
171End Namespace
172
173End Namespace
174End Namespace
Note: See TracBrowser for help on using the repository browser.