source: trunk/ab5.0/ablib/src/Classes/System/IO/StreamReader.ab@ 665

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

TextWriter同様TextReaderでバッファリングし、StreamReaderはMultiByteToWideChar固定で仮実装。これで、マルチバイトモードでもStringReaderが使えるようにした。
(#235)

File size: 1.8 KB
Line 
1'Classes/System/IO/StreamReader.ab
2
3Namespace System
4Namespace IO
5
6/*
7@brief ストリームから読み取りを行うTextReaderの実装。
8@date 2008/02/25
9@auther Egtra
10*/
11Class StreamReader
12 Inherits TextReader
13Public
14 /*
15 @date 2008/02/25
16 @auther Egtra
17 */
18 Sub StreamReader(path As String)
19 init(New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
20 End Sub
21
22 /*
23 @date 2008/02/25
24 @auther Egtra
25 */
26 Sub StreamReader(stream As Stream)
27 init(stream)
28 End Sub
29
30Public
31 /*
32 @brief 基になるストリームを取得する
33 @date 2008/09/02
34 @auther NoWest
35 */
36 Function BaseStream () As Stream
37 Return s
38 End Function
39
40
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
51 End If
52 s = Nothing
53 End Sub
54
55 /*
56 @date 2008/12/27
57 @auther Egtra
58 */
59 Override Function Underflow() As Boolean
60 Dim wcBuf[4095] As WCHAR
61 Dim mbBuf[4095] As SByte
62 Dim mbLen = s.Read(mbBuf As *Byte, 0, Len(buf))
63 If mbLen = 0 Then
64 Underflow = False
65 Exit Function
66 End If
67 Dim pNext = mbBuf As PSTR
68 Do
69 Dim q = CharNextExA(cp, pNext, 0)
70 If q = pNext Then
71 Exit Do
72 End If
73 pNext = q As PSTR
74 Loop
75 If pNext <> mbBuf + mbLen Then
76 leadByte = mbBuf[mbLen - 1]
77 End If
78 Dim wcLen = MultiByteToWideChar(cp, 0, mbBuf, (pNext - mbBuf) As Long, wcBuf, 4095)
79 Dim s = New String(wcBuf, wcLen)
80 Dim buf = Buffer
81 buf.Append(s)
82 Underflow = True
83 End Function
84Private
85
86 /*
87 @date 2008/02/25
88 @auther Egtra
89 */
90 Sub init(str As Stream)
91 s = str
92 cp = CP_ACP '暫定。
93 leadByte = 0
94 End Sub
95
96 s As Stream
97 cp As Word
98 leadByte As Byte
99End Class
100
101End Namespace
102End Namespace
Note: See TracBrowser for help on using the repository browser.