1 | NameSpace System
|
---|
2 | NameSpace IO
|
---|
3 |
|
---|
4 | Class TextReader
|
---|
5 | Implements System.IDisposable
|
---|
6 |
|
---|
7 | Public
|
---|
8 | 'Protected
|
---|
9 | Sub TextReader()
|
---|
10 | End Sub
|
---|
11 | Public
|
---|
12 | Virtual Sub ~TextReader()
|
---|
13 | Dispose(False)
|
---|
14 | End Sub
|
---|
15 |
|
---|
16 | ' Static Null = StreamReader.Null As TextReader
|
---|
17 |
|
---|
18 | Public
|
---|
19 | Sub Close()
|
---|
20 | Dispose(True)
|
---|
21 | End Sub
|
---|
22 |
|
---|
23 | Sub Dispose()
|
---|
24 | Dispose(True)
|
---|
25 | End Sub
|
---|
26 |
|
---|
27 | Abstract Function Peek() As Long
|
---|
28 | Abstract Function Read() As Long
|
---|
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
|
---|
38 | Read = ReadImpl(buffer, index, count)
|
---|
39 | End Function
|
---|
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 | /*
|
---|
83 | @brief 現在位置からストリームの終わりまで読み込む。
|
---|
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 |
|
---|
99 | Static Function Synchronized(reader As TextReader) As TextReader
|
---|
100 | End Function
|
---|
101 |
|
---|
102 | Protected
|
---|
103 | Virtual Sub Dispose(disposing As Boolean)
|
---|
104 | End Sub
|
---|
105 |
|
---|
106 | /*
|
---|
107 | @date 2008/02/26
|
---|
108 | @auther Egtra
|
---|
109 | */
|
---|
110 | Virtual Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
|
---|
111 | Dim i As Long
|
---|
112 | Dim p = VarPtr(buffer[index])
|
---|
113 | For i = 0 To ELM(count)
|
---|
114 | Dim c = Read()
|
---|
115 | If c = -1 Then
|
---|
116 | ReadImpl = i - 1
|
---|
117 | Exit Function
|
---|
118 | Else
|
---|
119 | p[i] = c As StrChar
|
---|
120 | End If
|
---|
121 | Next
|
---|
122 | ReadImpl = i - 1
|
---|
123 | End Function
|
---|
124 | End Class
|
---|
125 |
|
---|
126 | Namespace Detail
|
---|
127 |
|
---|
128 | Class SynchronizedTextReader
|
---|
129 | Inherits TextReader
|
---|
130 | Public
|
---|
131 | Sub SynchronizedTextReader(reader As TextReader)
|
---|
132 | cs = New ActiveBasic.Windows.CriticalSection
|
---|
133 | base = reader
|
---|
134 | End Sub
|
---|
135 |
|
---|
136 | Override Function Peek() As Long
|
---|
137 | ' Using lock = cs.Lock
|
---|
138 | Peek = base.Peek
|
---|
139 | ' End Using
|
---|
140 | End Function
|
---|
141 |
|
---|
142 | Override Function Read() As Long
|
---|
143 | ' Using lock = cs.Lock
|
---|
144 | Read = base.Read
|
---|
145 | ' End Using
|
---|
146 | End Function
|
---|
147 |
|
---|
148 | Override Function ReadLine() As String
|
---|
149 | ' Using lock = cs.Lock
|
---|
150 | ReadLine = base.ReadLine
|
---|
151 | ' End Using
|
---|
152 | End Function
|
---|
153 |
|
---|
154 | Override Function ReadToEnd() As String
|
---|
155 | ' Using lock = cs.Lock
|
---|
156 | ReadToEnd = base.ReadToEnd
|
---|
157 | ' End Using
|
---|
158 | End Function
|
---|
159 |
|
---|
160 | Protected
|
---|
161 | Override Sub Dispose(disposing As Boolean)
|
---|
162 | Dim s = Nothing As Stream
|
---|
163 | SetPointer(VarPtr(s) As *VoidPtr, InterlockedExchangePointer(ByVal VarPtr(base) As *VoidPtr, 0))
|
---|
164 | If disposing Then
|
---|
165 | If Not ActiveBasic.IsNothing(s) Then
|
---|
166 | s.Dispose()
|
---|
167 | End If
|
---|
168 | cs.Dispose()
|
---|
169 | End If
|
---|
170 | End Sub
|
---|
171 |
|
---|
172 | Override Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
|
---|
173 | ' Using lock = cs.Lock
|
---|
174 | ReadImpl = base.ReadImpl(buffer, index, count)
|
---|
175 | ' End Using
|
---|
176 | End Function
|
---|
177 | Private
|
---|
178 | cs As ActiveBasic.Windows.CriticalSection
|
---|
179 | base As TextReader
|
---|
180 | End Class
|
---|
181 |
|
---|
182 | End Namespace
|
---|
183 |
|
---|
184 | End NameSpace
|
---|
185 | End NameSpace
|
---|