Changeset 655 for trunk/ab5.0
- Timestamp:
- Nov 9, 2008, 2:21:49 PM (16 years ago)
- Location:
- trunk/ab5.0/ablib/src/Classes/System
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/src/Classes/System/IO/StreamReader.ab
r627 r655 43 43 */ 44 44 Override Function Peek() As Long 45 If cur = last Then 46 last = s.Read(buf As *Byte, 0, size) 47 cur = 0 48 If last = 0 Then 45 If charSize = charCur Then 46 fillCharBuf() 47 If charSize = charCur Then 49 48 Peek = -1 50 49 Exit Function 51 50 End If 52 51 End If 53 Peek = buf[cur]52 Peek = charBuf[charCur] 54 53 End Function 55 54 … … 61 60 Read = Peek() 62 61 If Read <> -1 Then 63 c ur++62 charCur++ 64 63 End If 65 End Function66 67 /*68 @date 2008/02/2669 @auther Egtra70 */71 Override Function ReadToEnd() As String72 Dim sb = New Text.StringBuilder(65536)73 sb.Append(buf, cur, last - cur)74 Do75 Dim read = Read(buf, 0, size)76 sb.Append(buf, 0, read)77 If read < size Then78 ReadToEnd = sb.ToString79 Exit Function80 End If81 Loop82 64 End Function 83 65 … … 94 76 End If 95 77 s = Nothing 96 size = 097 cur = 098 last = 099 78 End Sub 100 79 … … 103 82 @auther Egtra 104 83 */ 105 Override Function ReadImpl(buffer As * Char, index As Long, count As Long) As Long106 Dim n = last - cur107 If count <= n Then108 ReadImpl = ReadFromBuffer(buffer, index, count)84 Override Function ReadImpl(buffer As *WCHAR, count As Long) As Long 85 Dim n = charSize - charCur '現在のバッファに溜まっている文字数 86 If count <= n And charEof <> False Then 87 ReadImpl = readFromBuffer(buffer, count) 109 88 Exit Function 110 89 End If 111 Dim p = VarPtr(buffer[index]) 112 ReadImpl = ReadFromBuffer(p, 0, n) 90 ReadImpl = readFromBuffer(buffer, n) 113 91 If ReadImpl = count Then 'バッファの中身で足りた場合 114 92 Exit Function 115 93 End If 116 p = VarPtr(p[n]) 117 count -= n 118 If count > size Then 119 n = (count \ size) * size 'sizeの倍数分だけは直接bufferへ読み込ませる 120 Dim read = s.Read(p As *Byte, 0, n) 121 If read = 0 Then 'EOF 122 Exit Function 123 End If 124 p = VarPtr(p[n]) 125 ReadImpl += n 126 count -= n 127 End If 128 last = s.Read(buffer As *Byte, 0, size) 129 cur = 0 130 If last = 0 Then 'EOF 131 Exit Function 132 End If 133 ReadImpl += ReadFromBuffer(p, 0, Math.Min(last, count)) 94 fillCharBuf() 95 ReadImpl += ReadImpl(VarPtr(buffer[n]), count - n) 134 96 End Function 135 97 … … 141 103 Sub init(str As Stream) 142 104 s = str 143 size = 4096 144 last = 0 145 cur = 0 146 buf = GC_malloc(size) 105 '暫定。正式版ではUTF-8を標準とする。 106 encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP) 107 decoder = encoding.GetDecoder() 108 charCapacity = 4096 109 byteCapacity = 4096 110 charBuf = GC_malloc(charCapacity * SizeOf(WCHAR)) 111 byteBuf = GC_malloc(byteCapacity) 112 charCur = 0 113 byteCur = 0 114 charSize = 0 115 byteSize = 0 116 charEof = False 117 byteEof = False 147 118 End Sub 148 119 … … 153 124 文字数が足りなくても、元のストリームまで読みには行かない。 154 125 */ 155 Function ReadFromBuffer(p As *Char, index As Long, count As Long) As Long156 memcpy( VarPtr(p[index]), VarPtr(buf[cur]), count * SizeOf (Char))157 c ur += count158 ReadFromBuffer = count126 Function readFromBuffer(p As *WCHAR, count As Long) As Long 127 memcpy(p, VarPtr(charBuf[charCur]), count * SizeOf (WCHAR)) 128 charCur += count 129 readFromBuffer = count 159 130 End Function 160 131 132 /*! 133 @brief byteBufを基にcharBufを埋める。charBufが空になったときに用いる。 134 @date 2008/11/08 135 @auther Egtra 136 */ 137 Sub fillCharBuf() 138 charCur = 0 139 charSize = 0 140 If charEof = False Then 141 If byteCur = byteSize Then 142 byteSize = s.Read(byteBuf, 0, byteCapacity) 143 byteCur = 0 144 If byteSize = 0 Then 145 byteEof = True 146 End If 147 End If 148 Dim charUsed As Long 149 Dim byteUsed As Long 150 decoder.Convert(VarPtr(byteBuf[byteCur]), byteSize - byteCur, 151 VarPtr(charBuf[charSize]), charCapacity - charSize, 152 byteEof, charUsed, byteUsed, charEof) 153 byteCur += byteUsed 154 charSize += charUsed 155 End If 156 End Sub 157 161 158 s As Stream 162 size As Long 163 cur As Long 164 last As Long '中身の終わり 165 buf As *SByte '暫定 159 encoding As Text.Encoding 160 decoder As Text.Decoder 161 charBuf As *WCHAR 162 byteBuf As *Byte 163 charCur As Long '読み取っていないデータの開始位置 164 byteCur As Long 165 charSize As Long 'Bufの内、有効なデータの数 166 byteSize As Long 167 charCapacity As Long '確保済み容量 = GC_mallocしたときの引数の値 168 byteCapacity As Long 169 charEof As Boolean 'Trueのとき、次にcur = sizeになるまでしかデータがことを示す 170 byteEof As Boolean 171 '常に0 <= cur <= size <= capacity 166 172 End Class 167 173 -
trunk/ab5.0/ablib/src/Classes/System/IO/StringReader.ab
r497 r655 23 23 Throw New ArgumentNullException("str") 24 24 End If 25 s = str 25 Dim length = GetStr(str, s) 26 If length > LONG_MAX Then 27 Throw New ArgumentException("Must be Length <= LONG_MAX", "str") 28 End If 29 len = length As Long 26 30 i = 0 27 31 End Sub … … 32 36 */ 33 37 Override Function Peek() As Long 34 If i = s.LengthThen38 If i = len Then 35 39 Peek = -1 36 40 Else … … 44 48 */ 45 49 Override Function Read() As Long 46 If i = s.Length Then 47 Read = -1 48 Else 49 Read = s[i] 50 i++ 51 End If 50 Read = Peek() 51 i++ 52 52 End Function 53 53 … … 56 56 @auther Egtra 57 57 */ 58 /* 58 59 Override Function ReadToEnd() As String 59 60 ReadToEnd = s.Substring(i) 60 61 i = s.Length 61 62 End Function 62 63 */ 63 64 Protected 64 65 /* … … 75 76 @auther Egtra 76 77 */ 77 Override Function ReadImpl(buffer As * Char, index As Long, count As Long) As Long78 ReadImpl = Math.Min(count, s.Length- i)79 ActiveBasic.Strings.ChrCopy( VarPtr(buffer[index]) As *Char, (StrPtr(s) + i * SizeOf (Char)) As *Char, ReadImpl As SIZE_T) 'ToDo: ポインタに対する+演算78 Override Function ReadImpl(buffer As *WCHAR, count As Long) As Long 79 ReadImpl = Math.Min(count, len - i) 80 ActiveBasic.Strings.ChrCopy(buffer, VarPtr(s[i]), ReadImpl As SIZE_T) 'ToDo: ポインタに対する+演算 80 81 End Function 81 82 82 83 Private 83 s As String 84 s As *WCHAR 85 len As Long 84 86 i As Long 85 87 End Class -
trunk/ab5.0/ablib/src/Classes/System/IO/TextReader.ab
r497 r655 1 Name Space System2 Name Space IO1 Namespace System 2 Namespace IO 3 3 4 4 Class TextReader 5 5 Implements System.IDisposable 6 7 Public8 'Protected9 Sub TextReader()10 End Sub11 6 Public 12 7 Virtual Sub ~TextReader() … … 31 26 @auther Egtra 32 27 */ 33 Function Read(buffer As * Char, index As Long, count As Long) As Long28 Function Read(buffer As *WCHAR, index As Long, count As Long) As Long 34 29 If buffer = 0 Then 30 Throw New ArgumentNullException("buffer") 35 31 ElseIf index < 0 Then 32 Throw New ArgumentOutOfRangeException("index") 36 33 ElseIf count < 0 Then 34 Throw New ArgumentOutOfRangeException("count") 37 35 End If 38 Read = ReadImpl(buffer, index, count) 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) 39 41 End Function 40 42 … … 45 47 @retval 有効なStringインスタンス 読み取った1行 46 48 */ 47 VirtualFunction ReadLine() As String49 Function ReadLine() As String 48 50 If Peek() = -1 Then 49 51 Exit Function 50 52 End If 51 Dim sb = New Text.StringBuilder(256)53 Dim sb = New Collections.Generic.List<WCHAR> 52 54 Do 53 55 Dim ch = Read() … … 69 71 Case &h0D 'CR 70 72 Exit Do 71 'Case &h85 'NEL72 'Exit Do73 'Case &h2028 'LS74 'Exit Do75 'Case &h2029 'PS76 'Exit Do73 Case &h85 'NEL 74 Exit Do 75 Case &h2028 'LS 76 Exit Do 77 Case &h2029 'PS 78 Exit Do 77 79 End Select 78 sb.A ppend(ch As Char) 'ToDo キャスト不要にすべきというチケットを書くこと80 sb.Add(ch As WCHAR) 79 81 Loop 80 ReadLine = sb.ToString82 ReadLine = New String(sb As *WCHAR, sb.Count) 81 83 End Function 82 84 /* … … 97 99 End Function 98 100 99 Static Function Synchronized(reader As TextReader) As TextReader 100 Synchronized = New Detail.SynchronizedTextReader(reader)101 End Function101 Protected 102 Sub TextReader() 103 End Sub 102 104 103 Protected104 105 Virtual Sub Dispose(disposing As Boolean) 105 106 End Sub … … 109 110 @auther Egtra 110 111 */ 111 Virtual Function ReadImpl(buffer As * Char, index As Long, count As Long) As Long112 Virtual Function ReadImpl(buffer As *WCHAR, count As Long) As Long 112 113 Dim i As Long 113 Dim p = VarPtr(buffer[index])114 114 For i = 0 To ELM(count) 115 115 Dim c = Read() 116 116 If c = -1 Then 117 ReadImpl = i - 1117 ReadImpl = i 118 118 Exit Function 119 119 Else 120 p[i] = c As Char120 buffer[i] = c As Char 121 121 End If 122 122 Next 123 ReadImpl = i - 1123 ReadImpl = i 124 124 End Function 125 125 End Class … … 147 147 End Function 148 148 149 Override Function ReadLine() As String150 ' Using lock = cs.Lock151 ReadLine = base.ReadLine152 ' End Using153 End Function154 155 Override Function ReadToEnd() As String156 ' Using lock = cs.Lock157 ReadToEnd = base.ReadToEnd158 ' End Using159 End Function160 161 149 Protected 162 150 Override Sub Dispose(disposing As Boolean) … … 171 159 End Sub 172 160 173 Override Function ReadImpl(buffer As * Char, index As Long, count As Long) As Long161 Override Function ReadImpl(buffer As *WCHAR, count As Long) As Long 174 162 ' Using lock = cs.Lock 175 ReadImpl = base.ReadImpl(buffer, index,count)163 ReadImpl = base.ReadImpl(buffer, count) 176 164 ' End Using 177 165 End Function … … 183 171 End Namespace 184 172 185 End Name Space186 End Name Space173 End Namespace 174 End Namespace -
trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab
r653 r655 353 353 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean) 354 354 355 If src = 0 Then355 If src = 0 And srcCount > 0 Then 356 356 Throw New ArgumentNullException("src") 357 357 ElseIf srcCount < 0 Then … … 452 452 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean) 453 453 454 If src = 0 Then454 If src = 0 And srcCount > 0 Then 455 455 Throw New ArgumentNullException("src") 456 456 ElseIf srcCount < 0 Then … … 641 641 642 642 Override Function GetBytesCountCore(src As *WCHAR, srcCount As Long, flush As Boolean) As Long 643 GetBytesCountCore = srcCount * 2 + 1 '暫定 643 644 End Function 644 645 … … 658 659 Override Sub ConvertCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long, flush As Boolean, 659 660 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean) 661 662 Dim srcPos = 0 As Long 663 Dim dstPos = 0 As Long 664 If dstCount > 0 And nextByte <> 0 Then 665 Dim buf[1] As CHAR 666 buf[0] = nextByte As CHAR 667 buf[1] = src[1] As CHAR 668 Dim len = MultiByteToWideChar(cp, 0, buf, Len(buf), VarPtr(dst[dstPos]), 10) 669 If len = 0 Then 670 ActiveBasic.Windows.ThrowWithLastError() 671 End If 672 srcPos++ 673 dstPos++ 674 nextByte = 0 675 End If 676 While srcPos < srcCount And dstPos < srcCount 677 Dim srcCharSize = 1 As Long 678 If IsDBCSLeadByteEx(cp, src[srcPos]) Then 679 srcCharSize = 2 680 If srcPos + 1 = srcCount Then 681 nextByte = src[srcPos] 682 Exit While 683 End If 684 End If 685 '将来的には行毎に変換しMB_USEGLYPHCHARSを使うようにしたい。 686 Dim len = MultiByteToWideChar(cp, 0, VarPtr(src[srcPos]) As *CHAR, srcCharSize, VarPtr(dst[dstPos]), 1) 687 If len = 0 Then 688 ActiveBasic.Windows.ThrowWithLastError() 689 End If 690 srcPos += srcCharSize 691 dstPos++ 692 Wend 693 srcUsed = srcPos 694 dstUsed = dstPos 695 completed = (srcPos = srcCount And dstPos = srcCount And nextByte = 0) 660 696 End Sub 661 697 662 698 Override Function GetCharsCountCore(src As *Byte, srcCount As Long, flush As Boolean) As Long 699 GetCharsCountCore = srcCount + 1 '暫定 663 700 End Function 664 701 665 702 Private 666 703 cp As DWord 704 nextByte As Byte 667 705 End Class 668 706 -
trunk/ab5.0/ablib/src/Classes/System/Xml/XmlDocument.ab
r497 r655 95 95 /*! 96 96 @brief 指定したストリームからXML文書を読み込む。 97 @param reader 読み込むリーダ。 98 */ 99 Virtual Sub Load( reader As System.IO.TextReader ) 100 This.RemoveAll() 101 ActiveBasic.Xml.Parser.Parse( reader.ReadToEnd(), This ) 102 End Sub 103 104 /*! 105 @brief 指定したストリームからXML文書を読み込む。 97 106 @param stream 読み込み先のストリーム。 98 107 */ 99 108 Virtual Sub Load( inStream As System.IO.Stream ) 100 Dim length = inStream.Length As DWord 101 Dim xmlBuffer = calloc( length + 1 ) As *Char 102 inStream.Read( xmlBuffer As *Byte, 0, length ) 103 inStream.Close() 104 Dim xmlString = New String( xmlBuffer ) 105 free( xmlBuffer ) 106 107 This.RemoveAll() 108 ActiveBasic.Xml.Parser.Parse( xmlString, This ) 109 Load( New System.IO.StreamReader( inStream ) ) 109 110 End Sub 110 111 … … 114 115 */ 115 116 Virtual Sub Load( filename As String ) 116 Dim fileStream As System.IO.FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read ) 117 Load( fileStream ) 117 Dim r = New System.IO.StreamReader( filename ) 118 Load( r ) 119 r.Dispose() 118 120 End Sub 119 121 … … 123 125 */ 124 126 Virtual Sub Save( outStream As System.IO.Stream ) 125 Dim xmlStr = InnerXmlSupportedIndent( True)126 outStream.Write( xmlStr.StrPtr As *Byte, 0, xmlStr.Length * SizeOf( Char ))127 outStream.Close()127 Dim writer = New IO.StreamWriter( outStream ) 128 Save( writer ) 129 writer.Flush() 128 130 End Sub 129 131 … … 133 135 */ 134 136 Virtual Sub Save( filename As String ) 135 Dim fileStream As System.IO.FileStream( filename, System.IO.FileMode.Create, System.IO.FileAccess.Write ) 136 Save( fileStream ) 137 Dim w = New IO.StreamWriter( filename ) 138 Save( w ) 139 w.Dispose() 137 140 End Sub 138 141 … … 142 145 */ 143 146 Virtual Sub Save( writer As System.IO.TextWriter ) 144 writer.Write( InnerXmlSupportedIndent( True ))147 writer.Write( InnerXmlSupportedIndent( True ) ) 145 148 End Sub 146 149 End Class
Note:
See TracChangeset
for help on using the changeset viewer.