Ignore:
Timestamp:
Feb 12, 2008, 3:40:11 PM (16 years ago)
Author:
OverTaker
Message:

地味に修整

File:
1 edited

Legend:

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

    r388 r404  
    1 ' System/IO/Path.ab
     1/*!
     2@file   Classes/System/IO/Path.ab
     3@brief  ファイルパス文字列を操作する
     4@author OverTaker
     5*/
    26
    37#require <Classes/System/Environment.ab>
    4 
    58
    69Namespace System
    710Namespace IO
    8 
    911
    1012Class Path
    1113Public
    1214    Static AltDirectorySeparatorChar = &H2F As StrChar '/
    13     Static DirectorySeparatorChar = &H5C As StrChar    '\
    14     Static PathSeparator = &H3B As StrChar             ';
    15     Static VolumeSeparatorChar = &H3A As StrChar       ':
    16 
     15    Static DirectorySeparatorChar    = &H5C As StrChar '\
     16    Static PathSeparator             = &H3B As StrChar ';
     17    Static VolumeSeparatorChar       = &H3A As StrChar ':
     18
     19    /*!
     20    @brief  ファイル名を取得する
     21    @author OverTaker
     22    @date   
     23    @param  ファイルパス
     24    @return ファイル名
     25    */
    1726    Static Function GetFileName(path As String) As String
    18         Return path.Remove(0, getLastSeparatorPosision(path) + 1)
    19     End Function
    20 
     27        CheckPath(path)
     28        Return path.Remove(0, GetLastSeparatorIndex(path) + 1)
     29    End Function
     30
     31    /*!
     32    @brief  拡張子を除いたファイル名を取得する
     33    @author OverTaker
     34    @date   
     35    @param  ファイルパス
     36    @return ファイル名
     37    */
    2138    Static Function GetFileNameWithoutExtension(path As String) As String
     39        CheckPath(path)
    2240        Dim fileName = GetFileName(path) As String
    23         Dim extPos = getExtensionPosition(fileName) As Long
     41        Dim extPos = GetExtensionIndex(fileName) As Long
    2442        If extPos = -1 Then Return ""
    2543
     
    2745    End Function
    2846
    29     '手抜き
     47    /*!
     48    @brief  ランダムなファイル名を取得する
     49    @author OverTaker
     50    @date   
     51    @param  ファイルパス
     52    @return ファイル名
     53    */
    3054    Static Function GetRandomFileName() As String
    3155        Randomize
     
    3559    End Function
    3660
     61    /*!
     62    @brief  ファイルの拡張子を取得する
     63    @author OverTaker
     64    @date   
     65    @param  ファイルパス
     66    @return 拡張子(.を含む)
     67    */
    3768    Static Function GetExtension(path As String) As String
    38         Dim extPos = getExtensionPosition(path) As Long
     69        CheckPath(path)
     70        Dim extPos = GetExtensionIndex(path) As Long
    3971        If extPos = -1 Then Return ""
    4072
     
    4274    End Function
    4375
     76    /*!
     77    @brief  ファイル拡張子を変更する
     78    @author OverTaker
     79    @date   
     80    @param  拡張子(.を含む)
     81    @return 拡張子を変更したファイルパス
     82    */
    4483    Static Function ChangeExtension(path As String, extension As String) As String
    45         Dim extPos = getExtensionPosition(path) As Long
     84        Dim extPos = GetExtensionIndex(path) As Long
    4685        If extPos => 0 Then
    4786            path = path.Remove(extPos)
    4887        End If
    4988
     89        CheckPath(path)
    5090        Return path + extension
    5191    End Function
    5292
    53     Static Function HasExtension(ByRef path As String) As Boolean
     93    /*!
     94    @brief  ファイルパスの拡張子が含まれるかどうか
     95    @author OverTaker
     96    @date   
     97    @param  ファイルパス
     98    @return 拡張子があればTure,なければFalse
     99    */
     100    Static Function HasExtension(path As String) As Boolean
     101        CheckPath(path)
    54102        If GetExtension(path) <> "" Then
    55103            Return True
     
    59107    End Function
    60108
     109    /*!
     110    @brief  一時ファイルを作成し、ファイルパスを取得する
     111    @author OverTaker
     112    @date   
     113    @return 一時ファイルを示すファイルパス
     114    */
    61115    Static Function GetTempFileName() As String
    62116        Dim tempPathSize = __GetTempPath(0, 0)
     
    76130        Return New String(tempFileName, len As Long)
    77131    End Function
    78 
     132   
     133    /*!
     134    @brief  システムの一時フォルダを取得する
     135    @author OverTaker
     136    @date   
     137    @return ファイルパス
     138    */
    79139    Static Function GetTempPath() As String
    80140        Dim size = __GetTempPath(0, 0)
     
    85145    End Function
    86146
     147    /*!
     148    @brief  フルパスを取得する
     149    @author OverTaker
     150    @date   
     151    @param  ファイルパス
     152    @return ファイルパス
     153    */
    87154    Static Function GetFullPath(path As String) As String
     155        CheckPath(path)
    88156        If IsPathRooted(path) Then
    89157            Return path
     
    93161    End Function
    94162
     163    /*!
     164    @brief  ひとつ上のディレクトリを取得する
     165    @author OverTaker
     166    @date   
     167    @param  ファイルパス
     168    @return ひとつ上のディレクトリを示すファイルパス
     169    */
    95170    Static Function GetDirectoryName(path As String) As String
    96         Dim lastSepPos = getLastSeparatorPosision(path) As Long
     171        CheckPath(path)
     172        Dim lastSepPos = GetLastSeparatorIndex(path) As Long
    97173        If lastSepPos = -1 Then Return ""
    98174
     
    104180    End Function
    105181
     182    /*!
     183    @brief  ルートディレクトリを取得する
     184    @author OverTaker
     185    @date   
     186    @param  ファイルパス
     187    @return ルートディレクトリを示すパス
     188    */
    106189    Static Function GetPathRoot(path As String) As String
     190        CheckPath(path)
    107191        If IsPathRooted(path) Then
    108192            Return path.Remove(3)
     
    112196    End Function
    113197
     198    /*!
     199    @brief  パスにルートディレクトリが含まれるかどうか
     200    @author OverTaker
     201    @date   
     202    @param  ファイルパス
     203    @return 含まれる場合True,そうでない場合False
     204    */
    114205    Static Function IsPathRooted(path As String) As Boolean
     206        CheckPath(path)
    115207        If path.IndexOf(Chr$(VolumeSeparatorChar), 1, 1) = 1 Then
    116208            Return True
     
    120212    End Function
    121213
     214    /*!
     215    @brief  二つのパスを結合します
     216    @author OverTaker
     217    @date   
     218    @param  結合されるファイルパス
     219    @param  結合するファイルパス
     220    @return 結合されたファイルパス
     221    */
    122222    Static Function Combine(path1 As String, path2 As String) As String
     223        CheckPath(path1)
     224        CheckPath(path2)
    123225        If path1.LastIndexOf(VolumeSeparatorChar) And path1.Length = 2 Then
    124226            Return path1 + path2
     
    133235
    134236Private
    135     Static Function getExtensionPosition(path As String) As Long
    136         Dim lastSepPos = getLastSeparatorPosision(path) As Long
    137         If lastSepPos = -1 Then
    138             lastSepPos = 0
    139         End If
    140         getExtensionPosition = path.LastIndexOf(Asc("."), ELM(path.Length), path.Length - lastSepPos)
    141     End Function
    142 
    143     Static Function getLastSeparatorPosision(path As String) As Long
    144         Dim lastSepPos = path.LastIndexOf(DirectorySeparatorChar) As Long
    145         If lastSepPos <> -1 Then Return lastSepPos
    146 
    147         lastSepPos = path.LastIndexOf(VolumeSeparatorChar)
    148         Return lastSepPos
     237    Static Const InvalidPathChars = Ex"\q<>|\0\t" As String
     238
     239    /*!
     240    @brief  無効なパス文字列がないか調べる
     241    @author OverTaker
     242    @date   
     243    @param  ファイルパス
     244    */
     245    Static Sub CheckPath(path As String)
     246        Dim i As Long
     247        For i = 0 To ELM(InvalidPathChars.Length)
     248            If path.Contains(InvalidPathChars.Substring(i, 1)) Then
     249                Throw
     250            End If
     251        Next
     252    End Sub
     253
     254    /*!
     255    @brief  ファイルの拡張子の位置を取得する
     256    @author OverTaker
     257    @date   
     258    @param  ファイルパス
     259    @return 0から始まるインデックス値。みつからない場合-1
     260    */
     261    Static Function GetExtensionIndex(path As String) As Long
     262        Dim lastSepIndex = GetLastSeparatorIndex(path) As Long
     263        If lastSepIndex = -1 Then lastSepIndex = 0
     264        Return path.LastIndexOf(Asc("."), ELM(path.Length), path.Length - lastSepIndex)
     265    End Function
     266
     267    /*!
     268    @brief  最も後ろにあるディレクトリ区切り文字の位置を取得する
     269    @author OverTaker
     270    @date   
     271    @param  ファイルパス
     272    @return 0から始まるインデックス値。みつからない場合-1
     273    */
     274    Static Function GetLastSeparatorIndex(path As String) As Long
     275        Return System.Math.Max( path.LastIndexOf(DirectorySeparatorChar),
     276                                path.LastIndexOf(VolumeSeparatorChar) )
    149277    End Function
    150278End Class
Note: See TracChangeset for help on using the changeset viewer.