Ignore:
Timestamp:
Jan 13, 2009, 2:01:38 AM (16 years ago)
Author:
イグトランス (egtra)
Message:

#231に関連して、エンコーディング周りを見直し、Encoder/Decoderをストリーム用に特化。
UTF8Encodingをコンパイル可能にし、ビルドに含めるようにした。ただし、実装が不完全なためテストは不可。
(#231)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/src/Classes/System/Console.ab

    r636 r676  
    272272        End If
    273273        Console.err = newErr
    274         Dim sw = Console.err As IO.StreamWriter
    275         Dim fs = sw.BaseStream() As IO.FileStream
    276         Console.hconsoleerr = fs.Handle()
     274'       Dim sw = Console.err As IO.StreamWriter
     275'       Dim fs = sw.BaseStream() As IO.FileStream
     276'       Console.hconsoleerr = fs.Handle()
    277277    End Sub
    278278
     
    296296        End If
    297297        Console.out = newOut
    298         Dim sw = Console.out As IO.StreamWriter
    299         Dim fs = sw.BaseStream() As IO.FileStream
    300         Console.hconsoleout = fs.Handle()
    301298    End Sub
    302299
     
    441438        End If
    442439        Console.in = newIn
    443         Dim sr = Console.in As IO.StreamReader
    444         Dim fs = sr.BaseStream() As IO.FileStream
    445         Console.hconsolein = fs.Handle()
    446440    End Sub
    447441
     
    503497    */
    504498    Static Sub OpenStandardError()
    505         Console.SetError(System.IO.TextWriter.Synchronized(New System.IO.StreamWriter(New System.IO.FileStream(GetStdHandle(STD_ERROR_HANDLE), System.IO.FileAccess.Write, False))))
     499        Dim w = New Detail.ConsoleWriter(hconsoleerr)
     500        Console.SetOut(System.IO.TextWriter.Synchronized(w))
    506501    End Sub
    507502
     
    512507    */
    513508    Static Sub OpenStandardInput()
    514         Console.SetIn(System.IO.TextReader.Synchronized(New System.IO.StreamReader(New System.IO.FileStream(GetStdHandle(STD_INPUT_HANDLE), System.IO.FileAccess.Read, False))))
     509        Dim w = New Detail.ConsoleReader(hconsolein)
     510        Console.SetIn(System.IO.TextReader.Synchronized(w))
    515511    End Sub
    516512
     
    521517    */
    522518    Static Sub OpenStandardOutput()
    523         Console.SetOut(System.IO.TextWriter.Synchronized(New System.IO.StreamWriter(New System.IO.FileStream(GetStdHandle(STD_OUTPUT_HANDLE), System.IO.FileAccess.Write, False))))
     519        Dim w = New Detail.ConsoleWriter(hconsoleout)
     520        Console.SetOut(System.IO.TextWriter.Synchronized(w))
    524521        Console.defBC = This.ConsoleColorToTextAttribute(Console.BackgroundColor)
    525522        Console.defFC = This.ConsoleColorToTextAttribute(Console.ForegroundColor)
     
    654651    End Sub
    655652
    656     Static hconsoleerr = NULL As HANDLE
    657     Static hconsolein = NULL As HANDLE
    658     Static hconsoleout = NULL As HANDLE
     653    Static hconsoleerr = GetStdHandle(STD_ERROR_HANDLE) As HANDLE
     654    Static hconsolein = GetStdHandle(STD_INPUT_HANDLE) As HANDLE
     655    Static hconsoleout = GetStdHandle(STD_OUTPUT_HANDLE) As HANDLE
    659656    Static in = Nothing As IO.TextReader
    660657    Static out = Nothing As IO.TextWriter
     
    665662End Class
    666663
     664Namespace Detail
     665
     666Class ConsoleWriter
     667    Inherits IO.TextWriter
     668Public
     669    Sub ConsoleWriter(hOut As HANDLE)
     670        h = hOut
     671    End Sub
     672
     673    Override Sub Flush()
     674        Dim b = Buffer
     675        Dim written As DWord
     676        WriteConsole(h, StrPtr(b), b.Length As DWord, written, 0)
     677        b.Remove(0, written As Long)
     678    End Sub
     679
     680Private
     681    h As HANDLE
     682End Class
     683
     684Class ConsoleReader
     685    Inherits IO.TextReader
     686Public
     687    Sub ConsoleReader(hIn As HANDLE)
     688        h = hIn
     689    End Sub
     690
     691Protected
     692    Override Function Underflow() As Boolean
     693        Dim b = Buffer
     694        Dim currentBufLength = b.Length
     695        b.Length = currentBufLength + 256
     696        Dim p = StrPtr(b)
     697        Dim read As DWord
     698        If ReadConsole(h, VarPtr(p[currentBufLength]), 256, read, 0) = 0 Then
     699            IO.Detail.ThrowWinLastErrorIOException()
     700        End If
     701        b.Length = currentBufLength + read
     702    End Function
     703
     704Private
     705    h As HANDLE
     706End Class
     707
     708End Namespace 'Detail
     709
    667710End Namespace 'System
Note: See TracChangeset for help on using the changeset viewer.