source: branch/egtra-stream-without-en_dec/Classes/System/IO/StreamReader.ab@ 673

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

Encoder/Decoderを排除した実装(UTF8Encodingは修正していません)。

File size: 2.5 KB
RevLine 
[432]1'Classes/System/IO/StreamReader.ab
2
[271]3Namespace System
4Namespace IO
5
[432]6/*
7@brief ストリームから読み取りを行うTextReaderの実装。
8@date 2008/02/25
9@auther Egtra
10*/
[426]11Class StreamReader
12 Inherits TextReader
13Public
[432]14 /*
15 @date 2008/02/25
16 @auther Egtra
17 */
[426]18 Sub StreamReader(path As String)
19 init(New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
20 End Sub
[271]21
[432]22 /*
23 @date 2008/02/25
24 @auther Egtra
25 */
[426]26 Sub StreamReader(stream As Stream)
27 init(stream)
28 End Sub
29
[627]30Public
[432]31 /*
[627]32 @brief 基になるストリームを取得する
33 @date 2008/09/02
34 @auther NoWest
35 */
36 Function BaseStream () As Stream
37 Return s
38 End Function
39
[426]40
[432]41Protected
42 /*
43 @date 2008/02/25
44 @auther Egtra
45 */
46 Override Sub Dispose(disposing As Boolean)
47 If disposing Then
48 If Not ActiveBasic.IsNothing(s) Then
49 s.Dispose(True)
50 End If
[426]51 End If
[432]52 s = Nothing
53 End Sub
[426]54
[432]55 /*
[665]56 @date 2008/12/27
[432]57 @auther Egtra
58 */
[665]59 Override Function Underflow() As Boolean
[673]60 Dim nextRead As *Byte
61 Dim readSize = s.Read(mbBuf + bufSize, 0, bufCapacity - bufSize)
62 If readSize = 0 Then
63 If bufSize <> 0 Then
64#ifdef UNCODE
65 Buffer.Append(&hfffd As WCHAR)
66#else
67 Buffer.Append("?")
68#endif
69 End If
[665]70 Underflow = False
[426]71 Exit Function
72 End If
[673]73 Dim mbLen = bufSize + readSize
74 '文字全体が揃っている部分を抽出する。
75 Dim pNext = mbBuf
[665]76 Do
[673]77 Dim q = e.NextChar(pNext, ((mbBuf + mbLen) - pNext) As Long)
[665]78 If q = pNext Then
79 Exit Do
80 End If
[673]81 pNext = q
[665]82 Loop
[673]83 '文字全体が揃っている部分を変換しTextReaderのバッファに送る。
84 Dim wcBufCapacity = e.GetMaxCharCount(mbLen)
85 Dim wcBuf = GC_malloc_atomic(wcBufCapacity * SizeOf (WCHAR)) As *WCHAR
86 Dim wcLen = e.GetChars(mbBuf, (pNext - mbBuf) As Long, wcBuf, wcBufCapacity)
87 Dim appendStr As PCTSTR
88 Dim appendLen = GetStr(wcBuf, wcLen As SIZE_T, appendStr)
89 Dim readerBuffer = Buffer
90 readerBuffer.Append(appendStr, 0, appendLen)
91 'マルチバイトの揃っていない部分を手前に連れてくる。
92 bufSize = ((mbBuf + mbLen) - pNext) As Long
93 If bufSize <> 0 Then
94 memmove(mbBuf, mbBuf + mbLen, bufSize)
[426]95 End If
[665]96 Underflow = True
[426]97 End Function
[665]98Private
[426]99
[432]100 /*
101 @date 2008/02/25
102 @auther Egtra
103 */
[426]104 Sub init(str As Stream)
105 s = str
[673]106 e = New Text.Detail.WindowsCodePageEncoding(CP_ACP) '暫定。
107 bufCapacity = 8192
108 mbBuf = GC_malloc_atomic(bufCapacity) As *Byte
109 bufSize = 0
[426]110 End Sub
111
112 s As Stream
[673]113 e As Text.Encoding
114 mbBuf As *Byte
115 bufSize As Long
116 bufCapacity As Long
[271]117End Class
118
119End Namespace
120End Namespace
Note: See TracBrowser for help on using the repository browser.