source: trunk/Include/Classes/System/IO/TextReader.ab@ 457

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

(SPrintF.ab) FormatIntegerExにStringBuilderを引数に取る版を追加。

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