[420] | 1 | NameSpace System
|
---|
| 2 | NameSpace IO
|
---|
| 3 |
|
---|
| 4 | Class TextReader
|
---|
| 5 | Implements System.IDisposable
|
---|
| 6 |
|
---|
| 7 | Public
|
---|
[426] | 8 | 'Protected
|
---|
| 9 | Sub TextReader()
|
---|
| 10 | End Sub
|
---|
| 11 | Public
|
---|
| 12 | Virtual Sub ~TextReader()
|
---|
| 13 | Dispose(False)
|
---|
| 14 | End Sub
|
---|
[420] | 15 |
|
---|
[426] | 16 | ' Static Null = StreamReader.Null As TextReader
|
---|
[420] | 17 |
|
---|
| 18 | Public
|
---|
[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
|
---|
[473] | 100 | Synchronized = New Detail.SynchronizedTextReader(reader)
|
---|
[457] | 101 | End Function
|
---|
| 102 |
|
---|
[432] | 103 | Protected
|
---|
[468] | 104 | Virtual Sub Dispose(disposing As Boolean)
|
---|
| 105 | End Sub
|
---|
[432] | 106 |
|
---|
| 107 | /*
|
---|
| 108 | @date 2008/02/26
|
---|
| 109 | @auther Egtra
|
---|
| 110 | */
|
---|
| 111 | Virtual Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
|
---|
| 112 | Dim i As Long
|
---|
| 113 | Dim p = VarPtr(buffer[index])
|
---|
| 114 | For i = 0 To ELM(count)
|
---|
| 115 | Dim c = Read()
|
---|
| 116 | If c = -1 Then
|
---|
| 117 | ReadImpl = i - 1
|
---|
| 118 | Exit Function
|
---|
| 119 | Else
|
---|
| 120 | p[i] = c As StrChar
|
---|
| 121 | End If
|
---|
| 122 | Next
|
---|
| 123 | ReadImpl = i - 1
|
---|
| 124 | End Function
|
---|
[420] | 125 | End Class
|
---|
| 126 |
|
---|
[457] | 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 | Override Function ReadLine() As String
|
---|
| 150 | ' Using lock = cs.Lock
|
---|
| 151 | ReadLine = base.ReadLine
|
---|
| 152 | ' End Using
|
---|
| 153 | End Function
|
---|
| 154 |
|
---|
| 155 | Override Function ReadToEnd() As String
|
---|
| 156 | ' Using lock = cs.Lock
|
---|
| 157 | ReadToEnd = base.ReadToEnd
|
---|
| 158 | ' End Using
|
---|
| 159 | End Function
|
---|
| 160 |
|
---|
| 161 | Protected
|
---|
| 162 | Override Sub Dispose(disposing As Boolean)
|
---|
| 163 | Dim s = Nothing As Stream
|
---|
| 164 | SetPointer(VarPtr(s) As *VoidPtr, InterlockedExchangePointer(ByVal VarPtr(base) As *VoidPtr, 0))
|
---|
| 165 | If disposing Then
|
---|
| 166 | If Not ActiveBasic.IsNothing(s) Then
|
---|
| 167 | s.Dispose()
|
---|
| 168 | End If
|
---|
| 169 | cs.Dispose()
|
---|
| 170 | End If
|
---|
| 171 | End Sub
|
---|
| 172 |
|
---|
| 173 | Override Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
|
---|
| 174 | ' Using lock = cs.Lock
|
---|
| 175 | ReadImpl = base.ReadImpl(buffer, index, count)
|
---|
| 176 | ' End Using
|
---|
| 177 | End Function
|
---|
| 178 | Private
|
---|
| 179 | cs As ActiveBasic.Windows.CriticalSection
|
---|
| 180 | base As TextReader
|
---|
| 181 | End Class
|
---|
| 182 |
|
---|
| 183 | End Namespace
|
---|
| 184 |
|
---|
[420] | 185 | End NameSpace
|
---|
| 186 | End NameSpace
|
---|