1 | Namespace System
|
---|
2 | Namespace IO
|
---|
3 |
|
---|
4 | Class TextReader
|
---|
5 | Implements System.IDisposable
|
---|
6 | Public
|
---|
7 | Virtual Sub ~TextReader()
|
---|
8 | Dispose(False)
|
---|
9 | End Sub
|
---|
10 |
|
---|
11 | ' Static Null = StreamReader.Null As TextReader
|
---|
12 |
|
---|
13 | Public
|
---|
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 |
|
---|
101 | Protected
|
---|
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
|
---|
125 | End Class
|
---|
126 |
|
---|
127 | Namespace Detail
|
---|
128 |
|
---|
129 | Class SynchronizedTextReader
|
---|
130 | Inherits TextReader
|
---|
131 | Public
|
---|
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 |
|
---|
149 | Protected
|
---|
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
|
---|
166 | Private
|
---|
167 | cs As ActiveBasic.Windows.CriticalSection
|
---|
168 | base As TextReader
|
---|
169 | End Class
|
---|
170 |
|
---|
171 | End Namespace
|
---|
172 |
|
---|
173 | End Namespace
|
---|
174 | End Namespace
|
---|