Changeset 432


Ignore:
Timestamp:
Feb 27, 2008, 1:04:59 AM (15 years ago)
Author:
イグトランス (egtra)
Message:

StreamReaderの完成。StringReaderの追加。
Consoleの追加(現在入力関係の一部のみ)。

Location:
trunk
Files:
3 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/Classes/System/IO/File.ab

    r406 r432  
    44
    55Enum FileAccess
    6     Read
    7     ReadWrite
    8     Write
     6    Read = -2147483648 'GENERIC_READ
     7    ReadWrite = -1073741824 'GENERIC_READ Or GENERIC_WRITE
     8    Write = 1073741824' GENERIC_WRITE
    99End Enum
    1010
     
    2727
    2828Enum FileMode
    29     Append
    30     Create
    31     CreateNew
    32     Open
    33     OpenOrCreate
    34     Truncate
     29    Append = OPEN_ALWAYS
     30    Create = CREATE_ALWAYS
     31    CreateNew = CREATE_NEW
     32    Open = OPEN_EXISTING
     33    OpenOrCreate = OPEN_ALWAYS
     34    Truncate = TRUNCATE_EXISTING
    3535End Enum
    3636
    3737Enum FileShare
    38     DeleteFile
    39     None
    40     Read
    41     ReadWrite
    42     Write
     38    None = 0
     39    Read = FILE_SHARE_READ
     40    Write = FILE_SHARE_WRITE
     41    ReadWrite = FILE_SHARE_READ Or FILE_SHARE_WRITE
     42    DeleteFile = FILE_SHARE_DELETE
    4343End Enum
    4444
     
    158158    End Function
    159159
    160     Static Function OpenText( path As String ) As StreamReader
    161         ' TODO: 実装
    162     End Function
     160'   Static Function OpenText( path As String ) As StreamReader
     161        ' TODO: 実装
     162'   End Function
    163163
    164164    Static Function OpenWrite( path As String ) As FileStream
  • trunk/Include/Classes/System/IO/FileStream.ab

    r430 r432  
    2727    fileShare As DWord
    2828    fileOptions As DWord
     29    ownsHandle As Boolean
    2930   
    3031    offset As QWord 'オーバーラップドIO用
     
    3334    /* コンストラクタ.NETと同じように実装は難しい、一先ず動くものを実装したが変更が必要だと思う */
    3435    Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare, options As FileOptions)
    35         Dim ac As DWord
    36         Dim mo As DWord
    37         Dim sh As DWord
    38         Dim op As DWord
    39 
    40         Select Case access
    41             Case FileAccess.Read
    42                 ac=GENERIC_READ
    43             Case FileAccess.ReadWrite
    44                 ac=GENERIC_READ or GENERIC_WRITE
    45             Case FileAccess.Write
    46                 ac=GENERIC_WRITE
    47         End Select
    48 
    49         Select Case share
    50             Case FileShare.DeleteFile
    51                 sh=FILE_SHARE_DELETE
    52             Case FileShare.None
    53                 sh=0
    54             Case FileShare.Read
    55                 sh=FILE_SHARE_READ
    56             Case FileShare.ReadWrite
    57                 sh=FILE_SHARE_READ or FILE_SHARE_WRITE
    58             Case FileShare.Write
    59                 sh=FILE_SHARE_WRITE
    60         End Select
    61 
    62         Select Case mode
    63             Case FileMode.Append
    64                 mo=OPEN_ALWAYS
    65             Case FileMode.Create
    66                 mo=CREATE_ALWAYS
    67             Case FileMode.CreateNew
    68                 mo=CREATE_NEW
    69             Case FileMode.Open
    70                 mo=OPEN_EXISTING
    71             Case FileMode.OpenOrCreate
    72                 mo=OPEN_ALWAYS
    73             Case FileMode.Truncate
    74                 mo=TRUNCATE_EXISTING
    75         End Select
    76 
    77         op = options As DWord
     36        Dim ac = access As DWord
     37        Dim sh = share As DWord
     38        Dim mo = mode As DWord
     39        Dim op = options As DWord
    7840'       If (Environment.OSVersion.Platform As DWord) <> (PlatformID.Win32NT As DWord) Then 'ToDo: なぜかアクセス違反になる
    7941            op And= Not FILE_FLAG_OVERLAPPED
     
    9759        This.fileOptions = op
    9860        This.offset = 0
     61        This.ownsHandle = True
    9962    End Sub
    10063    Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare)
     
    12285        This.FileStream(path,mode,access,FileShare.None,FileOptions.None)
    12386    End Sub
     87    /*
     88    @date 2008/02/26
     89    @auther Egtra
     90    '不要になったら削除すること
     91    */
     92    Sub FileStream(h As HANDLE, access As FileAccess, owns As Boolean)
     93        handle = h
     94        fileAccess = access As DWord
     95        ownsHandle = owns
     96    End Sub
     97
    12498Public
    12599    /*!
     
    259233/*  CreateObjRef*/
    260234   
    261     Override Sub Dispose(disposing As Boolean)
    262         Flush()
    263         CloseHandle(InterlockedExchangePointer(VarPtr(This.handle),NULL))
    264     End Sub
    265 
    266235    Override Function EndRead(asyncResult As System.IAsyncResult) As Long
    267236        'TODO
     
    450419
    451420Protected
     421    Override Sub Dispose(disposing As Boolean)
     422        If handle <> 0 Then
     423            Flush()
     424            CloseHandle(InterlockedExchangePointer(VarPtr(handle), NULL))
     425        End If
     426    End Sub
     427
    452428    Override Function CreateWaitHandle() As System.Threading.WaitHandle
    453         Return New System.Threading.AutoResetEvent(False)
     429        '調査した限りでは、System.Threading.EventWaitHandleクラスをNewする模様。
     430        '現状ではSystem.Threading.WaitHandleクラスをNewしてからHandleにて設定
     431        Dim wh As System.Threading.WaitHandle
     432        wh.Handle=CreateEvent(NULL,TRUE,FALSE,NULL)
     433        Return wh
    454434    End Function
    455435
  • trunk/Include/Classes/System/IO/Stream.ab

    r391 r432  
    4848        Write(buffer,offset,count)
    4949    End Function
    50     Virtual Sub Close()
     50    Sub Close()
    5151        Dispose(True)
    5252    End Sub
    53     Virtual Sub Dispose()
     53    Sub Dispose()
    5454        Dispose(True)
    5555    End Sub
    56     Virtual Sub Dispose(disposing As Boolean):  End Sub
    5756    Virtual Function EndRead(ByRef asyncResult As System.IAsyncResult) As Long: End Function
    5857    Virtual Sub EndWrite(ByRef asyncResult As System.IAsyncResult): End Sub
     
    7877    End Sub
    7978Protected
    80     Virtual Function CreateWaitHandle() As System.Threading.WaitHandle: End Function
     79    Virtual Sub Dispose(disposing As Boolean)
     80    End Sub
     81    Virtual Function CreateWaitHandle() As System.Threading.WaitHandle
     82    End Function
    8183End Class
    8284
  • trunk/Include/Classes/System/IO/StreamReader.ab

    r426 r432  
     1'Classes/System/IO/StreamReader.ab
     2
    13Namespace System
    24Namespace IO
    35
     6/*
     7@brief ストリームから読み取りを行うTextReaderの実装。
     8@date 2008/02/25
     9@auther Egtra
     10*/
    411Class StreamReader
    512    Inherits TextReader
    613Public
     14    /*
     15    @date 2008/02/25
     16    @auther Egtra
     17    */
    718    Sub StreamReader(path As String)
    819        init(New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
    920    End Sub
    1021
     22    /*
     23    @date 2008/02/25
     24    @auther Egtra
     25    */
    1126    Sub StreamReader(stream As Stream)
    1227        init(stream)
    1328    End Sub
    1429
    15     Override Sub Dispose(disposing As Boolean)
    16         s.Dispose(disposing)
    17         size = 0
    18         cur = 0
    19         last = 0
    20     End Sub
    21 
     30    /*
     31    @date 2008/02/25
     32    @auther Egtra
     33    */
    2234    Override Function Peek() As Long
    2335        If cur = last Then
     
    3244    End Function
    3345
     46    /*
     47    @date 2008/02/25
     48    @auther Egtra
     49    */
    3450    Override Function Read() As Long
    3551        Read = Peek()
     
    3753    End Function
    3854
    39     Override Function Read(buffer As *StrChar, index As Long, count As Long) As Long
    40         If buffer = 0 Then
    41         ElseIf index < 0 Then
    42         ElseIf count < 0 Then
     55    /*
     56    @date 2008/02/26
     57    @auther Egtra
     58    */
     59    Override Function ReadToEnd() As String
     60        Dim sb = New Text.StringBuilder(65536)
     61        sb.Append(buf, cur, last - cur)
     62        Do
     63            Dim read = Read(buf, 0, size)
     64            sb.Append(buf, 0, read)
     65            If read < size Then
     66                ReadToEnd = sb.ToString
     67                Exit Function
     68            End If
     69        Loop
     70    End Function
     71
     72Protected
     73    /*
     74    @date 2008/02/25
     75    @auther Egtra
     76    */
     77    Override Sub Dispose(disposing As Boolean)
     78        If disposing Then
     79            If Not ActiveBasic.IsNothing(s) Then
     80                s.Dispose(True)
     81            End If
    4382        End If
     83        s = Nothing
     84        size = 0
     85        cur = 0
     86        last = 0
     87    End Sub
    4488
     89    /*
     90    @date 2008/02/25
     91    @auther Egtra
     92    */
     93    Override Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
    4594        Dim n = last - cur
    4695        If count <= n Then
    47             Read = ReadFromBuffer(buffer, index, count)
     96            ReadImpl = ReadFromBuffer(buffer, index, count)
    4897            Exit Function
    4998        End If
    5099        Dim p = VarPtr(buffer[index])
    51         Read = ReadFromBuffer(p, 0, n)
    52         If Read = count Then 'バッファの中身で足りた場合
     100        ReadImpl = ReadFromBuffer(p, 0, n)
     101        If ReadImpl = count Then 'バッファの中身で足りた場合
    53102            Exit Function
    54103        End If
     
    62111            End If
    63112            p = VarPtr(p[n])
    64             Read += n
     113            ReadImpl += n
    65114            count -= n
    66115        End If
     
    70119            Exit Function
    71120        End If
    72         Read += ReadFromBuffer(p, 0, Math.Min(last, count))
    73     End Function
    74 
    75     Override Function ReadLine() As String
    76         Dim sb = New Text.StringBuilder(256)
    77         Do
    78             Dim ch = Read()
    79             If ch = &h0D Then
    80                 If Peek() = &h0A Then
    81                     Read() 'CR LFの場合
    82                 End If
    83                 Exit Do
    84             End If
    85             Select Case ch
    86                 Case -1 'EOF
    87                     Exit Do
    88                 Case &h0A 'LF
    89                     Exit Do
    90                 Case &h0B 'VT
    91                     Exit Do
    92                 Case &h0C 'FF
    93                     Exit Do
    94                 Case &h0D 'CR
    95                     Exit Do
    96 '               Case &h85 'NEL
    97 '                   Exit Do
    98 '               Case &h2028 'LS
    99 '                   Exit Do
    100 '               Case &h2029 'PS
    101 '                   Exit Do
    102             End Select
    103             sb.Append(ch As StrChar) 'ToDo キャスト不要にすべきというチケットを書くこと
    104         Loop
    105         ReadLine = sb.ToString
    106     End Function
    107 
    108     Override Function ReadToEnd() As String
     121        ReadImpl += ReadFromBuffer(p, 0, Math.Min(last, count))
    109122    End Function
    110123
    111124Private
     125    /*
     126    @date 2008/02/25
     127    @auther Egtra
     128    */
    112129    Sub init(str As Stream)
    113130        s = str
     
    120137    /**
    121138    @brief バッファの中身から読み取る。
     139    @date 2008/02/25
     140    @auther Egtra
    122141    文字数が足りなくても、元のストリームまで読みには行かない。
    123142    */
     
    132151    cur As Long
    133152    last As Long '中身の終わり
    134     buf As *Byte '暫定
     153    buf As *SByte '暫定
    135154End Class
    136155
  • trunk/Include/Classes/System/IO/TextReader.ab

    r426 r432  
    2525    End Sub
    2626
     27    Abstract Function Peek() As Long
     28    Abstract Function Read() As Long
     29    /*
     30    @date 2008/02/26
     31    @auther Egtra
     32    */
     33    Function Read(buffer As *StrChar, index As Long, count As Long) As Long
     34        If buffer = 0 Then
     35        ElseIf index < 0 Then
     36        ElseIf count < 0 Then
     37        End If
     38    End Function
     39
     40    /*
     41    @date 2008/02/26
     42    @auther Egtra
     43    @retval Nothing EOFに達しているとき
     44    @retval 有効なStringインスタンス 読み取った1行
     45    */
     46    Virtual Function ReadLine() As String
     47        If Peek() = -1 Then
     48            Exit Function
     49        End If
     50        Dim sb = New Text.StringBuilder(256)
     51        Do
     52            Dim ch = Read()
     53            If ch = &h0D Then
     54                If Peek() = &h0A Then
     55                    Read() 'CR LFの場合
     56                End If
     57                Exit Do
     58            End If
     59            Select Case ch
     60                Case -1 'EOF
     61                    Exit Do
     62                Case &h0A 'LF
     63                    Exit Do
     64                Case &h0B 'VT
     65                    Exit Do
     66                Case &h0C 'FF
     67                    Exit Do
     68                Case &h0D 'CR
     69                    Exit Do
     70'               Case &h85 'NEL
     71'                   Exit Do
     72'               Case &h2028 'LS
     73'                   Exit Do
     74'               Case &h2029 'PS
     75'                   Exit Do
     76            End Select
     77            sb.Append(ch As StrChar) 'ToDo キャスト不要にすべきというチケットを書くこと
     78        Loop
     79        ReadLine = sb.ToString
     80    End Function
     81    /*
     82    @date 2008/02/26
     83    @auther Egtra
     84    */
     85    Virtual Function ReadToEnd() As String
     86        Dim sb = New Text.StringBuilder(8192)
     87        Do
     88            Dim ch = Read()
     89            If ch = -1 Then
     90                ReadToEnd = sb.ToString
     91                Exit Function
     92            End If
     93            sb.Append(ch As StrChar)
     94        Loop
     95    End Function
     96
     97Protected
    2798    Abstract Sub Dispose(disposing As Boolean)
    2899
    29     Abstract Function Peek() As Long
    30     Abstract Function Read() As Long
    31     Abstract Function Read(buffer As *StrChar, index As Long, count As Long) As Long
    32     Virtual Function ReadBlock(buffer As *StrChar, index As Long, count As Long) As Long
    33         ReadBlock = Read(buffer, index, count)
     100    /*
     101    @date 2008/02/26
     102    @auther Egtra
     103    */
     104    Virtual Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
     105        Dim i As Long
     106        Dim p = VarPtr(buffer[index])
     107        For i = 0 To ELM(count)
     108            Dim c = Read()
     109            If c = -1 Then
     110                ReadImpl = i - 1
     111                Exit Function
     112            Else
     113                p[i] = c As StrChar
     114            End If
     115        Next
     116        ReadImpl = i - 1
    34117    End Function
    35     Abstract Function ReadLine() As String
    36     Abstract Function ReadToEnd() As String
    37118End Class
    38119
  • trunk/Include/Classes/index.ab

    r424 r432  
    1010#require "./ActiveBasic/Windows/Windows.ab"
    1111#require "./System/Blittable.ab"
     12#require "./System/Console.ab"
    1213#require "./System/DateTime.ab"
    1314#require "./System/Delegate.ab"
     
    6768#require "./System/IO/Path.ab"
    6869#require "./System/IO/Stream.ab"
     70#require "./System/IO/TextReader.ab"
    6971#require "./System/IO/StreamReader.ab"
     72#require "./System/IO/StringReader.ab"
    7073#require "./System/IO/StreamWriter.ab"
    7174#require "./System/IO/TextReader.ab"
  • trunk/Include/basic/dos_console.sbp

    r391 r432  
    99Dim _System_hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
    1010Dim _System_hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
     11System.Console.SetIn(
     12    New System.IO.StreamReader(
     13        New System.IO.FileStream(_System_hConsoleIn, System.IO.FileAccess.Read, False)))
    1114
    1215'---------- command.sbp内で定義済み ----------
     
    1518'---------------------------------------------
    1619Sub INPUT_FromPrompt(ShowStr As String)
    17     Dim InputBuf[1023] As TCHAR
    18     Dim dwAccessBytes As DWord
    19 
    20 *InputReStart
    21 
     20*InputReStart '多重Continueがあるので、これはループ構文に直せない。
    2221    PRINT_ToPrompt(ShowStr)
    2322
    2423    '入力
    25     ReadConsole(_System_hConsoleIn, InputBuf, Len(InputBuf), dwAccessBytes, 0)
    26     If InputBuf[dwAccessBytes-2] = &h0d And InputBuf[dwAccessBytes-1] = &h0a Then
    27         InputBuf[dwAccessBytes-2] = 0
    28         dwAccessBytes -= 2
     24    Dim input = System.Console.ReadLine()
     25    If ActiveBasic.IsNothing(input) Then
     26        Exit Sub
    2927    End If
    3028
    31     If dwAccessBytes = 0 Then Goto *InputReStart
     29    If input.Length = 0 Then Goto *InputReStart
    3230
    3331    'データを変数に格納
    3432    Const comma = &h2c As StrChar 'Asc(",")
    35     Dim broken = ActiveBasic.Strings.Detail.Split(New String(InputBuf, dwAccessBytes As Long), comma)
     33    Dim broken = ActiveBasic.Strings.Detail.Split(input, comma)
    3634    Dim i As Long
    3735    For i = 0 To ELM(broken.Count)
    3836        If _System_InputDataPtr[i] = 0 Then
    3937            PRINT_ToPrompt(Ex"入力データの個数が多すぎます\r\n")
    40             Goto *InputReStart
     38             Goto *InputReStart
    4139        End If
    4240        _System_Input_SetArgument(_System_InputDataPtr[i], _System_InputDataType[i], broken[i])
     
    4543    If _System_InputDataPtr[i]<>0 Then
    4644        PRINT_ToPrompt(Ex"入力データの個数が足りません\r\n")
    47         Goto *InputReStart
     45         Goto *InputReStart
    4846    End If
    4947End Sub
     
    5452
    5553Sub PRINT_ToPrompt(buf As String)
     54    If String.IsNullOrEmpty(buf) Then
     55        Exit Sub
     56    End If
     57
    5658    Dim dwAccessBytes As DWord
    5759#ifdef __STRING_UNICODE_WINDOWS_ANSI
  • trunk/TestCase/SimpleTestCase/ExceptionTest.ab

    r388 r432  
    3333
    3434End Namespace
    35 
    3635ExceptionTest.TestMain()
  • trunk/TestCase/SimpleTestCase/SimpleTestCase.idx

    r424 r432  
    3434#include "EncodingTest.ab"
    3535_ClearNamespaceImported
     36#include "StreamTest.ab"
     37_ClearNamespaceImported
    3638#include "XmlTest.ab"
    3739_ClearNamespaceImported
  • trunk/TestCase/SimpleTestCase/SimpleTestCase.pj

    r424 r432  
    4242TypeInfoTest.ab
    4343EncodingTest.ab
     44StreamTest.ab
    4445XmlTest.ab
Note: See TracChangeset for help on using the changeset viewer.