[655] | 1 | Namespace System
|
---|
| 2 | Namespace IO
|
---|
[420] | 3 |
|
---|
| 4 | Class TextReader
|
---|
| 5 | Implements System.IDisposable
|
---|
| 6 | Public
|
---|
[426] | 7 | Virtual Sub ~TextReader()
|
---|
| 8 | Dispose(False)
|
---|
| 9 | End Sub
|
---|
[420] | 10 |
|
---|
[426] | 11 | ' Static Null = StreamReader.Null As TextReader
|
---|
[420] | 12 |
|
---|
| 13 | Public
|
---|
[426] | 14 | Sub Close()
|
---|
| 15 | Dispose(True)
|
---|
[420] | 16 | End Sub
|
---|
[426] | 17 |
|
---|
| 18 | Sub Dispose()
|
---|
| 19 | Dispose(True)
|
---|
| 20 | End Sub
|
---|
| 21 |
|
---|
[665] | 22 | Virtual Function Peek() As Long
|
---|
| 23 | If pos >= buf.Length Then
|
---|
| 24 | If Not Underflow() Then
|
---|
| 25 | Peek = -1
|
---|
| 26 | Exit Function
|
---|
| 27 | End If
|
---|
| 28 | End If
|
---|
| 29 | Peek = buf[pos]
|
---|
| 30 | End Function
|
---|
| 31 |
|
---|
| 32 | Virtual Function Read() As Long
|
---|
| 33 | Read = Peek()
|
---|
| 34 | pos++
|
---|
| 35 | End Function
|
---|
[432] | 36 | /*
|
---|
| 37 | @date 2008/02/26
|
---|
| 38 | @auther Egtra
|
---|
| 39 | */
|
---|
[665] | 40 | Virtual Function Read(buffer As *Char, index As Long, count As Long) As Long
|
---|
[432] | 41 | If buffer = 0 Then
|
---|
[655] | 42 | Throw New ArgumentNullException("buffer")
|
---|
[432] | 43 | ElseIf index < 0 Then
|
---|
[655] | 44 | Throw New ArgumentOutOfRangeException("index")
|
---|
[432] | 45 | ElseIf count < 0 Then
|
---|
[655] | 46 | Throw New ArgumentOutOfRangeException("count")
|
---|
[432] | 47 | End If
|
---|
[655] | 48 | Read = ReadImpl(VarPtr(buffer[index]), count)
|
---|
[426] | 49 | End Function
|
---|
[432] | 50 |
|
---|
[655] | 51 | Static Function Synchronized(reader As TextReader) As TextReader
|
---|
| 52 | Synchronized = New Detail.SynchronizedTextReader(reader)
|
---|
| 53 | End Function
|
---|
| 54 |
|
---|
[432] | 55 | /*
|
---|
| 56 | @date 2008/02/26
|
---|
| 57 | @auther Egtra
|
---|
| 58 | @retval Nothing EOFに達しているとき
|
---|
| 59 | @retval 有効なStringインスタンス 読み取った1行
|
---|
| 60 | */
|
---|
[665] | 61 | Virtual Function ReadLine() As String
|
---|
[432] | 62 | If Peek() = -1 Then
|
---|
| 63 | Exit Function
|
---|
| 64 | End If
|
---|
[665] | 65 | Dim sb = New Text.StringBuilder
|
---|
| 66 | Dim isLead = FALSE
|
---|
[432] | 67 | Do
|
---|
| 68 | Dim ch = Read()
|
---|
[665] | 69 | If ch = -1 Then Exit Do
|
---|
| 70 | If isLead = FALSE Then
|
---|
| 71 | If ch = &h0D Then
|
---|
| 72 | If Peek() = &h0A Then
|
---|
| 73 | Read() 'CR LFの場合
|
---|
| 74 | End If
|
---|
| 75 | Exit Do
|
---|
[432] | 76 | End If
|
---|
[665] | 77 | Select Case ch
|
---|
| 78 | Case -1 'EOF
|
---|
| 79 | Exit Do
|
---|
| 80 | Case &h0A 'LF
|
---|
| 81 | Exit Do
|
---|
| 82 | Case &h0B 'VT
|
---|
| 83 | Exit Do
|
---|
| 84 | Case &h0C 'FF
|
---|
| 85 | Exit Do
|
---|
| 86 | Case &h0D 'CR
|
---|
| 87 | Exit Do
|
---|
| 88 | #ifdef UNICODE
|
---|
| 89 | Case &h85 'NEL
|
---|
| 90 | Exit Do
|
---|
| 91 | Case &h2028 'LS
|
---|
| 92 | Exit Do
|
---|
| 93 | Case &h2029 'PS
|
---|
| 94 | Exit Do
|
---|
| 95 | #endif
|
---|
| 96 | End Select
|
---|
[432] | 97 | End If
|
---|
[665] | 98 | sb.Append(ch As Char)
|
---|
| 99 | #ifndef UNICODE
|
---|
| 100 | isLead = IsDBCSLeadByte(ch As Byte)
|
---|
| 101 | #endif
|
---|
[432] | 102 | Loop
|
---|
[665] | 103 | ReadLine = sb.ToString()
|
---|
[432] | 104 | End Function
|
---|
[665] | 105 |
|
---|
| 106 | /*!
|
---|
[457] | 107 | @brief 現在位置からストリームの終わりまで読み込む。
|
---|
[432] | 108 | @date 2008/02/26
|
---|
| 109 | @auther Egtra
|
---|
| 110 | */
|
---|
| 111 | Virtual Function ReadToEnd() As String
|
---|
[665] | 112 | buf.Remove(0, pos)
|
---|
| 113 | While Underflow()
|
---|
| 114 | Wend
|
---|
| 115 | ReadToEnd = buf.ToString()
|
---|
[432] | 116 | End Function
|
---|
| 117 |
|
---|
[655] | 118 | Protected
|
---|
| 119 | Sub TextReader()
|
---|
[665] | 120 | buf = New Text.StringBuilder
|
---|
| 121 | pos = 0
|
---|
[655] | 122 | End Sub
|
---|
[457] | 123 |
|
---|
[468] | 124 | Virtual Sub Dispose(disposing As Boolean)
|
---|
| 125 | End Sub
|
---|
[432] | 126 |
|
---|
[665] | 127 | /*!
|
---|
| 128 | @brief バッファが足りなくなったときに呼ばれる。
|
---|
| 129 | @date 2008/12/27
|
---|
| 130 | @auther Egtra
|
---|
| 131 | @retval True まだEOFに達していない場合
|
---|
| 132 | @retval False EOFに達した場合
|
---|
| 133 | */
|
---|
| 134 | Virtual Function Underflow() As Boolean
|
---|
| 135 | Underflow = False
|
---|
| 136 | End Function
|
---|
| 137 |
|
---|
| 138 | /*!
|
---|
| 139 | @brief 内部バッファを返す
|
---|
| 140 | @date 2008/12/27
|
---|
| 141 | @auther Egtra
|
---|
| 142 | @return 内部バッファ
|
---|
| 143 | */
|
---|
| 144 | Function Buffer() As Text.StringBuilder
|
---|
| 145 | Buffer = buf
|
---|
| 146 | End Function
|
---|
| 147 |
|
---|
| 148 | /*!
|
---|
[432] | 149 | @date 2008/02/26
|
---|
| 150 | @auther Egtra
|
---|
| 151 | */
|
---|
[665] | 152 | Function ReadImpl(buffer As *Char, count As Long) As Long
|
---|
| 153 | Dim p = StrPtr(buf)
|
---|
| 154 | While buf.Length - pos < count
|
---|
| 155 | If Underflow() = False Then Exit While
|
---|
| 156 | Wend
|
---|
| 157 | ReadImpl = Math.Min(buf.Length - pos, count)
|
---|
| 158 | ActiveBasic.Strings.ChrCopy(buffer, VarPtr(p[buf]), ReadImpl As SIZE_T)
|
---|
| 159 | pos += ReadImpl
|
---|
[432] | 160 | End Function
|
---|
[665] | 161 |
|
---|
| 162 | Private
|
---|
| 163 | buf As Text.StringBuilder
|
---|
| 164 | pos As Long
|
---|
[420] | 165 | End Class
|
---|
| 166 |
|
---|
[457] | 167 | Namespace Detail
|
---|
| 168 |
|
---|
| 169 | Class SynchronizedTextReader
|
---|
| 170 | Inherits TextReader
|
---|
| 171 | Public
|
---|
| 172 | Sub SynchronizedTextReader(reader As TextReader)
|
---|
| 173 | cs = New ActiveBasic.Windows.CriticalSection
|
---|
| 174 | base = reader
|
---|
| 175 | End Sub
|
---|
| 176 |
|
---|
| 177 | Override Function Peek() As Long
|
---|
[665] | 178 | ' Using lock = cs.Lock()
|
---|
| 179 | Peek = base.Peek()
|
---|
[457] | 180 | ' End Using
|
---|
| 181 | End Function
|
---|
| 182 |
|
---|
| 183 | Override Function Read() As Long
|
---|
[665] | 184 | ' Using lock = cs.Lock()
|
---|
| 185 | Read = base.Read()
|
---|
| 186 | ' End Using
|
---|
| 187 | End Function
|
---|
| 188 |
|
---|
| 189 | Override Function Read(buffer As *Char, index As Long, count As Long) As Long
|
---|
| 190 | ' Using lock = cs.Lock()
|
---|
| 191 | Read = base.Read(buffer, index, count)
|
---|
| 192 | ' End Using
|
---|
| 193 | End Function
|
---|
| 194 |
|
---|
| 195 | Override Function ReadToEnd() As String
|
---|
[457] | 196 | ' Using lock = cs.Lock
|
---|
[665] | 197 | ReadToEnd = base.ReadToEnd
|
---|
[457] | 198 | ' End Using
|
---|
| 199 | End Function
|
---|
| 200 |
|
---|
| 201 | Protected
|
---|
| 202 | Override Sub Dispose(disposing As Boolean)
|
---|
| 203 | Dim s = Nothing As Stream
|
---|
| 204 | SetPointer(VarPtr(s) As *VoidPtr, InterlockedExchangePointer(ByVal VarPtr(base) As *VoidPtr, 0))
|
---|
| 205 | If disposing Then
|
---|
| 206 | If Not ActiveBasic.IsNothing(s) Then
|
---|
| 207 | s.Dispose()
|
---|
| 208 | End If
|
---|
| 209 | cs.Dispose()
|
---|
| 210 | End If
|
---|
| 211 | End Sub
|
---|
| 212 |
|
---|
| 213 | Private
|
---|
| 214 | cs As ActiveBasic.Windows.CriticalSection
|
---|
| 215 | base As TextReader
|
---|
| 216 | End Class
|
---|
| 217 |
|
---|
| 218 | End Namespace
|
---|
| 219 |
|
---|
[655] | 220 | End Namespace
|
---|
| 221 | End Namespace
|
---|