Changeset 466 for trunk/Include


Ignore:
Timestamp:
Mar 8, 2008, 7:33:08 PM (17 years ago)
Author:
OverTaker
Message:

File.ab,FileInfo.abをそれなりに実装

Location:
trunk/Include
Files:
4 edited

Legend:

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

    r435 r466  
    44
    55Enum FileAccess
    6     Read = -2147483648 'GENERIC_READ
    7     ReadWrite = -1073741824 'GENERIC_READ Or GENERIC_WRITE
    8     Write = 1073741824' GENERIC_WRITE
     6    Read      = GENERIC_READ
     7    ReadWrite = GENERIC_READ Or GENERIC_WRITE
     8    Write     = GENERIC_WRITE
    99End Enum
    1010
    1111Enum FileAttributes
    12     Archive           = 32 'FILE_ATTRIBUTE_ARCHIVE
    13     Compressed        = 2048 'FILE_ATTRIBUTE_COMPRESSED
    14     Device            = 64 'FILE_ATTRIBUTE_DEVICE
    15     Directory         = 16 'FILE_ATTRIBUTE_DIRECTORY
    16     Encrypted         = 16384 'FILE_ATTRIBUTE_ENCRYPTED
    17     Hidden            = 2 'FILE_ATTRIBUTE_HIDDEN
    18     Normal            = 128 'FILE_ATTRIBUTE_NORMAL
    19     NotContentIndexed = 8192 'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
    20     Offline           = 4096 'FILE_ATTRIBUTE_OFFLINE
    21     ReadOnly          = 1 'FILE_ATTRIBUTE_READONLY
    22     ReparsePoint      = 1024 'FILE_ATTRIBUTE_REPARSE_POINT
    23     SparseFile        = 512 'FILE_ATTRIBUTE_SPARSE_FILE
    24     System            = 4 'FILE_ATTRIBUTE_SYSTEM
    25     Temporary         = 256 'FILE_ATTRIBUTE_TEMPORARY
     12    Archive           = FILE_ATTRIBUTE_ARCHIVE
     13    Compressed        = FILE_ATTRIBUTE_COMPRESSED
     14    Device            = FILE_ATTRIBUTE_DEVICE
     15    Directory         = FILE_ATTRIBUTE_DIRECTORY
     16    Encrypted         = FILE_ATTRIBUTE_ENCRYPTED
     17    Hidden            = FILE_ATTRIBUTE_HIDDEN
     18    Normal            = FILE_ATTRIBUTE_NORMAL
     19    NotContentIndexed = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
     20    Offline           = FILE_ATTRIBUTE_OFFLINE
     21    ReadOnly          = FILE_ATTRIBUTE_READONLY
     22    ReparsePoint      = FILE_ATTRIBUTE_REPARSE_POINT
     23    SparseFile        = FILE_ATTRIBUTE_SPARSE_FILE
     24    System            = FILE_ATTRIBUTE_SYSTEM
     25    Temporary         = FILE_ATTRIBUTE_TEMPORARY
    2626End Enum
    2727
    2828Enum FileMode
    29     Append = 4 'OPEN_ALWAYS
    30     Create = 2 'CREATE_ALWAYS
    31     CreateNew = 1 'CREATE_NEW
    32     Open = 3 'OPEN_EXISTING
    33     OpenOrCreate = 4 'OPEN_ALWAYS
    34     Truncate = 5 'TRUNCATE_EXISTING
     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     None = 0
    39     Read = FILE_SHARE_READ
    40     Write = FILE_SHARE_WRITE
    41     ReadWrite = FILE_SHARE_READ Or FILE_SHARE_WRITE
     38    None       = 0
     39    Read       = FILE_SHARE_READ
     40    Write      = FILE_SHARE_WRITE
     41    ReadWrite  = FILE_SHARE_READ Or FILE_SHARE_WRITE
    4242    DeleteFile = FILE_SHARE_DELETE
    4343End Enum
     
    4646Public
    4747
    48     Static Sub AppendAllText( path As String, contents As String )
    49         ' TODO: 実装
    50     End Sub
     48    '----------------------------------------------------------------
     49    ' パブリック メソッド
     50    '----------------------------------------------------------------
     51
     52/*  Static Sub AppendAllText( path As String, contents As String )
     53        ' TODO: 実装
     54    End Sub*/
    5155
    5256/*  Static Sub AppendAllText( path As String, contents As String, encoding As Encoding )
     
    5458    End Sub */
    5559
    56     Static Function AppendText( path As String ) As StreamWriter
    57         ' TODO: 実装
    58     End Function
     60/*  Static Function AppendText( path As String ) As StreamWriter
     61        ' TODO: 実装
     62    End Function*/
    5963
    6064    Static Sub Copy( sourceFileName As String, destFileName As String )
    61         ' TODO: 実装
     65        Copy(sourceFileName, destFileName, False)
    6266    End Sub
    6367
    6468    Static Sub Copy( sourceFileName As String, destFileName As String, overwrite As Boolean )
    65         ' TODO: 実装
     69        If Not CopyFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName), overwrite) Then
     70            Throw New IOException( "FileInfo: Failed to CopyFile." )
     71        End If
    6672    End Sub
    6773
    6874    Static Function Create( path As String ) As FileStream
    69         ' TODO: 実装
    70     End Function
    71 
    72     Static Function Create( path As String, bufferSize As Long ) As FileStream
    73         ' TODO: 実装
    74     End Function
     75        Return New FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite)
     76    End Function
     77
     78/*  Static Function Create( path As String, bufferSize As Long ) As FileStream
     79        ' TODO: 実装
     80    End Function */
    7581
    7682/*  Static Function Create( path As String, bufferSize As Long, options As FileOptions ) As FileStream
     
    8288    End Function */
    8389
    84     Static Function CreateText( path As String ) As StreamWriter
    85         ' TODO: 実装
    86     End Function
    87 
    88     Static Sub Decrypt( path As String )
    89         ' TODO: 実装
    90     End Sub
    91 
    92     Static Sub DeleteFile( path As String )
    93         ' TODO: 実装
    94     End Sub
    95 
    96     Static Sub Encrypt( path As String )
    97         ' TODO: 実装
    98     End Sub
     90/*  Static Function CreateText( path As String ) As StreamWriter
     91        ' TODO: 実装
     92    End Function*/
     93
     94/*  Static Sub Decrypt( path As String )
     95        ' TODO: 実装
     96    End Sub*/
     97
     98    Static Sub Delete( path As String )
     99        If Not DeleteFile(path) Then
     100            Throw New IOException("File.Delete: Failed to DeleteFile.")
     101        End If
     102    End Sub
     103
     104/*  Static Sub Encrypt( path As String )
     105        ' TODO: 実装
     106    End Sub*/
    99107
    100108    Static Function Exists( path As String ) As Boolean
    101         ' TODO: 実装
     109        Dim data As WIN32_FIND_DATA
     110        Dim hFind = FindFirstFile(ToTCStr(path), data)
     111        FindClose(hFind)
     112
     113        If hFind <> INVALID_HANDLE_VALUE Then
     114            Return True
     115        Else
     116            Return False
     117        End If
    102118    End Function
    103119
     
    111127
    112128    Static Function GetAttributes( path As String ) As FileAttributes
    113         ' TODO: 実装
     129        Return New FileAttributes(getFileData(path).dwFileAttributes As Long, "FileAttributes")
    114130    End Function
    115131
    116132    Static Function GetCreationTime( path As String ) As DateTime
    117         ' TODO: 実装
     133        Return System.DateTime.FromFileTime(getFileData(path).ftCreationTime)
    118134    End Function
    119135
    120136    Static Function GetCreationTimeUtc( path As String ) As DateTime
    121         ' TODO: 実装
     137        Return System.DateTime.FromFileTimeUtc(getFileData(path).ftCreationTime)
    122138    End Function
    123139
    124140    Static Function GetLastAccessTime( path As String ) As DateTime
    125         ' TODO: 実装
     141        Return System.DateTime.FromFileTime(getFileData(path).ftLastAccessTime)
    126142    End Function
    127143
    128144    Static Function GetLastAccessTimeUtc( path As String ) As DateTime
    129         ' TODO: 実装
     145        Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastAccessTime)
    130146    End Function
    131147
    132148    Static Function GetLastWriteTime( path As String ) As DateTime
    133         ' TODO: 実装
     149        Return System.DateTime.FromFileTime(getFileData(path).ftLastWriteTime)
    134150    End Function
    135151
    136152    Static Function GetLastWriteTimeUtc( path As String ) As DateTime
    137         ' TODO: 実装
     153        Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastWriteTime)
    138154    End Function
    139155
    140156    Static Sub Move( sourceFileName As String, destFileName As String )
    141         ' TODO: 実装
     157        If Not MoveFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName)) Then
     158            Throw New IOException("File.Move: Failed to MoveFile.")
     159        End If
    142160    End Sub
    143161
    144162    Static Function Open( path As String, mode As FileMode ) As FileStream
    145         ' TODO: 実装
     163        Return New FileStream(path, mode)
    146164    End Function
    147165
    148166    Static Function Open( path As String, mode As FileMode, access As FileAccess ) As FileStream
    149         ' TODO: 実装
     167        Return New FileStream(path, mode, access)
    150168    End Function
    151169
    152170    Static Function Open( path As String, mode As FileMode, access As FileAccess, share As FileShare ) As FileStream
    153         ' TODO: 実装
     171        Return New FileStream(path, mode, access, share)
    154172    End Function
    155173
    156174    Static Function OpenRead( path As String ) As FileStream
    157         ' TODO: 実装
    158     End Function
    159 
    160 '   Static Function OpenText( path As String ) As StreamReader
    161         ' TODO: 実装
    162 '   End Function
     175        Return Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)
     176    End Function
     177
     178    Static Function OpenText( path As String ) As StreamReader
     179        Return New StreamReader(path)
     180    End Function
    163181
    164182    Static Function OpenWrite( path As String ) As FileStream
    165         ' TODO: 実装
    166     End Function
    167 
    168     Static Function ReadAllBytes( path As String ) As *Byte
    169         ' TODO: 実装
    170     End Function
     183        Return Open(path, FileMode.Open, FileAccess.Write)
     184    End Function
     185
     186/*  Static Function ReadAllBytes( path As String ) As *Byte
     187        ' TODO: 実装
     188    End Function*/
    171189
    172190/*  Static Function ReadAllLines( path As String ) As Strings
     
    178196    End Function */
    179197
    180     Static Function ReadAllText( path As String ) As String
    181         ' TODO: 実装
    182     End Function
     198/*  Static Function ReadAllText( path As String ) As String
     199        ' TODO: 実装
     200    End Function*/
    183201
    184202/*  Static Function ReadAllText( path As String, encoding As Encoding ) As String
     
    186204    End Function */
    187205
    188     Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String )
    189         ' TODO: 実装
    190     End Sub
    191 
    192     Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean )
    193         ' TODO: 実装
    194     End Sub
     206/*  Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String )
     207        ' TODO: 実装
     208    End Sub*/
     209
     210/*  Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean )
     211        ' TODO: 実装
     212    End Sub*/
    195213
    196214/*  Static Sub SetAccessControl( path As String, fileSecurity As FileSecurity )
     
    199217
    200218    Static Sub SetAttributes( path As String, fileAttributes As FileAttributes )
    201         ' TODO: 実装
     219        If Not SetFileAttributes(ToTCStr(path), fileAttributes) Then
     220            Throw New IOException("File.SetAttributes: Failed to SetFileAttributes.")
     221        End If
    202222    End Sub
    203223
    204224    Static Sub SetCreationTime( path As String, creationTime As DateTime )
    205         ' TODO: 実装
    206     End Sub
    207 
    208     Static Sub SetCreationTimeUtc( path As String, creationTime As DateTime )
    209         ' TODO: 実装
     225        SetCreationTimeUtc(path, creationTime)
     226    End Sub
     227
     228    Static Sub SetCreationTimeUtc( path As String, creationTimeUtc As DateTime )
     229        Dim hFile = createFileToSetTime(path) As HANDLE
     230        SetFileTime(hFile, creationTimeUtc.ToFileTimeUtc(), ByVal 0, ByVal 0)
     231        CloseHandle(hFile)
    210232    End Sub
    211233
    212234    Static Sub SetLastAccessTime( path As String, lastAccessTime As DateTime )
    213         ' TODO: 実装
     235        SetLastAccessTimeUtc(path, lastAccessTime)
    214236    End Sub
    215237
    216238    Static Sub SetLastAccessTimeUtc( path As String, lastAccessTimeUtc As DateTime )
    217         ' TODO: 実装
     239        Dim hFile = createFileToSetTime(path) As HANDLE
     240        SetFileTime(hFile, ByVal 0, lastAccessTimeUtc.ToFileTimeUtc(), ByVal 0)
     241        CloseHandle(hFile)
    218242    End Sub
    219243
    220244    Static Sub SetLastWriteTime( path As String, lastWriteTime As DateTime )
    221         ' TODO: 実装
     245        SetLastWriteTimeUtc(path, lastWriteTime)
    222246    End Sub
    223247
    224248    Static Sub SetLastWriteTimeUtc( path As String, lastWriteTimeUtc As DateTime )
    225         ' TODO: 実装
    226     End Sub
    227 
    228     Static Sub WriteAllBytes( path As String, bytes As *Byte )
    229         ' TODO: 実装
    230     End Sub
     249        Dim hFile = createFileToSetTime(path) As HANDLE
     250        SetFileTime(hFile, ByVal 0, ByVal 0, lastWriteTimeUtc.ToFileTimeUtc())
     251        CloseHandle(hFile)
     252    End Sub
     253
     254/*  Static Sub WriteAllBytes( path As String, bytes As *Byte )
     255        ' TODO: 実装
     256    End Sub*/
    231257
    232258/*  Static Sub WriteAllLines( path As String, contents As Strings )
     
    238264    End Sub */
    239265
    240     Static Sub WriteAllText( path As String, contents As String )
    241         ' TODO: 実装
    242     End Sub
     266/*  Static Sub WriteAllText( path As String, contents As String )
     267        ' TODO: 実装
     268    End Sub*/
    243269
    244270/*  Static Sub WriteAllText( path As String, contents As String, encoding As Enconding )
     
    246272    End Sub */
    247273
     274Private
     275
     276    Static Function getFileData(path As String) As WIN32_FIND_DATA
     277        Dim data As WIN32_FIND_DATA
     278        Dim hFind = FindFirstFile(ToTCStr(Path.GetFullPath(path)), data)
     279        FindClose(hFind)
     280
     281        If hFind <> INVALID_HANDLE_VALUE Then
     282            Return data
     283        Else
     284            Throw New IOException("File.getFileData: Failed to FindFirstFile.")
     285        End If
     286    End Function
     287
     288    Static Function createFileToSetTime(path As String) As HANDLE
     289        Dim hFile As HANDLE
     290        hFile = CreateFile(ToTCStr(path), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
     291        If hFile = INVALID_HANDLE_VALUE Then
     292            CloseHandle(hFile)
     293            Throw New IOException("File.setFileTime: Failed to CreateFile.")
     294            Exit Function
     295        End If
     296        Return hFile
     297    End Function
    248298End Class
    249299
  • trunk/Include/Classes/System/IO/FileInfo.ab

    r406 r466  
    1515
    1616    '----------------------------------------------------------------
    17     ' Public properties
     17    ' パブリック プロパティ
    1818    '----------------------------------------------------------------
    1919
     
    3333        End If
    3434    End Function
     35
     36    Function Length() As QWord
     37        Dim hFile = CreateFile(ToTCStr(FullPath), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
     38        If hFile = INVALID_HANDLE_VALUE Then
     39            Throw New IOException("FileInfo.Length: Failed to CreateFile.")
     40            Exit Function
     41        End If
     42
     43        Dim length As QWord
     44        If GetFileSizeEx(hFile, VarPtr(length)) Then
     45            CloseHandle(hFile)
     46            Return length
     47        Else
     48            CloseHandle(hFile)
     49            Throw New IOException("FileInfo.Length: Failed to GetFileSize")
     50        End If
     51    End Function
     52
     53    '----------------------------------------------------------------
     54    ' パブリック メソッド
     55    '----------------------------------------------------------------
     56
     57/*  Function AppendText() As StreamWriter
     58    End Function*/
     59
     60    Function CopyTo(destFileName As String) As FileInfo
     61        Return CopyTo(destFileName, False)
     62    End Function
     63
     64    Function CopyTo(destFileName As String, overwrite As Boolean) As FileInfo
     65        File.Copy(FullPath, destFileName, overwrite)
     66        Return New FileInfo(destFileName)
     67    End Function
     68
     69    Function Create() As FileStream
     70        Return File.Create(FullPath)
     71    End Function
     72
     73/*  Function CreateText() As StreamWriter
     74    End Function*/
     75
     76/*  Sub Decrypt()
     77    End Sub*/
     78
     79    Override Sub Delete()
     80        File.Delete(FullPath)
     81    End Sub
     82
     83/*  Sub Encrypt()
     84    End Sub*/
     85
     86/*  Function GetAccessControl() As FileSecurity
     87    End Function*/
     88
     89/*  Function GetAccessControl(includeSections As AccessControlSections) As FileScurity
     90    End Function*/
     91
     92    Sub MoveTo(destFileName As String)
     93        File.Move(FullPath, destFileName)
     94    End Sub
     95
     96    Function Open(mode As FileMode) As FileStream
     97        Return New FileStream(FullPath, mode)
     98    End Function
     99
     100    Function Open(mode As FileMode, access As FileAccess) As FileStream
     101        Return New FileStream(FullPath, mode, access)
     102    End Function
     103
     104    Function Open(mode As FileMode, access As FileAccess, share As FileShare ) As FileStream
     105        Return New FileStream(FullPath, mode, access, share)
     106    End Function
     107
     108    Function OpenRead() As FileStream
     109        Return Open(FileMode.Open, FileAccess.Read, FileShare.Read)
     110    End Function
     111
     112    Function OpenText() As StreamReader
     113        Return New StreamReader(FullPath)
     114    End Function
     115
     116    Function OpenWrite() As FileStream
     117        Return Open(FileMode.Open, FileAccess.Write)
     118    End Function
     119
     120/*  Function Replace(destinationFileName As String, destinationBackupFileName As String) As FileInfo
     121    End Function*/
     122
     123/*  Function Replace(destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean) As FileInfo
     124    End Function*/
     125
     126/*  Sub SetAccessControl(fileSecurity As FileSecurity)
     127    End Sub*/
     128
     129    Override Function ToString() As String
     130        Return FullPath
     131    End Function
     132
    35133End Class
    36 
    37134
    38135
  • trunk/Include/Classes/System/IO/FileSystemInfo.ab

    r462 r466  
    7373    */
    7474    Sub Attributes(value As FileAttributes)
    75         If SetFileAttributes(ToTCStr(FullPath), value) = FALSE Then
    76             'Exception
    77             Debug
     75        If Not SetFileAttributes(ToTCStr(FullPath), value) Then
     76            Throw New IOException("FileSystemInfo.Attributes: Failed to SetFileAttributes.")
    7877        End If
    7978    End Sub
     
    199198    */
    200199    Function Exists() As Boolean
    201         Dim data As WIN32_FIND_DATA
    202         Dim hFind = FindFirstFile(ToTCStr(FullPath), data)
    203         FindClose(hFind)
    204 
    205         If hFind <> INVALID_HANDLE_VALUE Then
    206             Return True
    207         Else
    208             Return False
    209         End If
     200        Return File.Exists(FullPath)
    210201    End Function
    211202
  • trunk/Include/api_system.sbp

    r415 r466  
    562562) As BOOL
    563563Declare Function GetFileSize Lib "kernel32" (hFile As HANDLE, pFileSizeHigh As *DWord) As DWord
     564Declare Function GetFileSizeEx Lib "kernel32" (hFile As HANDLE, pFileSizeHigh As *QWord) As Boolean
    564565Declare Function GetFileTime Lib "kernel32" (hFile As HANDLE, ByRef lpCreationTime As FILETIME, ByRef lpLastAccessTime As FILETIME, ByRef lpLastWriteTime As FILETIME) As BOOL
    565566
Note: See TracChangeset for help on using the changeset viewer.