[404] | 1 | /*!
|
---|
| 2 | @file Classes/System/IO/Path.ab
|
---|
| 3 | @brief ファイルパス文字列を操作する
|
---|
| 4 | @author OverTaker
|
---|
| 5 | */
|
---|
[142] | 6 |
|
---|
[271] | 7 | Namespace System
|
---|
| 8 | Namespace IO
|
---|
| 9 |
|
---|
[61] | 10 | Class Path
|
---|
| 11 | Public
|
---|
[429] | 12 | Static Const AltDirectorySeparatorChar = &H2F As StrChar '/
|
---|
| 13 | Static Const DirectorySeparatorChar = &H5C As StrChar '\
|
---|
| 14 | Static Const PathSeparator = &H3B As StrChar ';
|
---|
| 15 | Static Const VolumeSeparatorChar = &H3A As StrChar ':
|
---|
[61] | 16 |
|
---|
[429] | 17 | '----------------------------------------------------------------
|
---|
| 18 | ' パブリック メソッド
|
---|
| 19 | '----------------------------------------------------------------
|
---|
| 20 |
|
---|
| 21 | /*
|
---|
| 22 | @brief 拡張子を変更する
|
---|
| 23 | @param パス
|
---|
| 24 | @param 変更する拡張子(.を含む)
|
---|
| 25 | @return 拡張子変更後のパス
|
---|
[404] | 26 | */
|
---|
[429] | 27 | Static Function ChangeExtension(path As String, extension As String) As String
|
---|
| 28 | path = CheckPath(path)
|
---|
| 29 | If HasExtension(path) Then
|
---|
| 30 | Return path.Remove(GetExtensionIndex(path)) + extension
|
---|
| 31 | Else
|
---|
| 32 | Return path + extension
|
---|
| 33 | End If
|
---|
[61] | 34 | End Function
|
---|
| 35 |
|
---|
[429] | 36 | /*
|
---|
| 37 | @brief 二つのパスを結合する
|
---|
| 38 | @param 結合されるパス
|
---|
| 39 | @param 結合するパス
|
---|
| 40 | @return パス
|
---|
[404] | 41 | */
|
---|
[429] | 42 | Static Function Combine(path1 As String, path2 As String) As String
|
---|
| 43 | path1 = CheckPath(path1)
|
---|
| 44 | path2 = CheckPath(path2)
|
---|
| 45 | If IsPathRooted(path2) Then
|
---|
| 46 | Return path2
|
---|
| 47 | Else
|
---|
| 48 | If Not path1.EndsWith(DirectorySeparatorChar) Then
|
---|
| 49 | path1 += Chr$(DirectorySeparatorChar)
|
---|
| 50 | End If
|
---|
| 51 | Return path1 + path2
|
---|
| 52 | End If
|
---|
[61] | 53 | End Function
|
---|
| 54 |
|
---|
[429] | 55 | /*
|
---|
| 56 | @brief パス文字列からファイル名を取得する
|
---|
| 57 | @param パス
|
---|
[404] | 58 | @return ファイル名
|
---|
| 59 | */
|
---|
[429] | 60 | Static Function GetFileName(path As String) As String
|
---|
| 61 | path = CheckPath(path)
|
---|
| 62 | Dim lastSeparatorIndex = GetLastSeparatorIndex(path) As Long
|
---|
| 63 | If lastSeparatorIndex <> -1 Then
|
---|
| 64 | Return path.Substring(lastSeparatorIndex + 1)
|
---|
| 65 | Else
|
---|
| 66 | Return path
|
---|
| 67 | End If
|
---|
[61] | 68 | End Function
|
---|
| 69 |
|
---|
[429] | 70 | /*
|
---|
| 71 | @brief パス文字列からファイル名を拡張子を除いて取得する
|
---|
| 72 | @param パス
|
---|
| 73 | @return 拡張子を除いたファイル名
|
---|
[404] | 74 | */
|
---|
[429] | 75 | Static Function GetFileNameWithoutExtension(path As String) As String
|
---|
| 76 | path = GetFileName(path)
|
---|
| 77 | If HasExtension(path) Then
|
---|
| 78 | Return path.Remove(GetExtensionIndex(path))
|
---|
| 79 | Else
|
---|
| 80 | Return path
|
---|
| 81 | End If
|
---|
[61] | 82 | End Function
|
---|
[142] | 83 |
|
---|
[429] | 84 | /*
|
---|
| 85 | @brief 絶対パスを取得する
|
---|
| 86 | @param 相対パス
|
---|
| 87 | @return 絶対パス
|
---|
[404] | 88 | */
|
---|
[429] | 89 | Static Function GetFullPath(path As String) As String
|
---|
[468] | 90 | If IsPathRooted(path) Then
|
---|
| 91 | Return path
|
---|
| 92 | Else
|
---|
| 93 | Return Combine(System.Environment.CurrentDirectory, path)
|
---|
| 94 | End If
|
---|
[61] | 95 | End Function
|
---|
| 96 |
|
---|
[429] | 97 | /*
|
---|
| 98 | @brief パス文字列から拡張子を取得する
|
---|
| 99 | @param パス
|
---|
| 100 | @return .を含む拡張子
|
---|
[404] | 101 | */
|
---|
[429] | 102 | Static Function GetExtension(path As String) As String
|
---|
| 103 | path = CheckPath(path)
|
---|
| 104 | Dim extensionIndex = GetExtensionIndex(path)
|
---|
| 105 | If extensionIndex <> -1 Then
|
---|
| 106 | Return path.Substring(extensionIndex)
|
---|
[61] | 107 | Else
|
---|
[429] | 108 | Return New String()
|
---|
[61] | 109 | End If
|
---|
| 110 | End Function
|
---|
| 111 |
|
---|
[429] | 112 | /*
|
---|
| 113 | @brief ひとつ上のディレクトリを取得する
|
---|
| 114 | @param パス
|
---|
| 115 | @return ディレクトリパス
|
---|
[404] | 116 | */
|
---|
[429] | 117 | Static Function GetDirectoryName(path As String) As String
|
---|
| 118 | path = CheckPath(path)
|
---|
| 119 | Dim lastSeparatorIndex = GetLastSeparatorIndex(path)
|
---|
| 120 | If lastSeparatorIndex = -1 Then
|
---|
| 121 | Return New String()
|
---|
| 122 | Else
|
---|
| 123 | Return path.Substring(0, lastSeparatorIndex)
|
---|
[125] | 124 | End If
|
---|
[61] | 125 | End Function
|
---|
| 126 |
|
---|
[429] | 127 | /*
|
---|
| 128 | @brief ルートディレクトリを取得する
|
---|
| 129 | @param パス
|
---|
| 130 | @return ルートディレクトリ
|
---|
[404] | 131 | */
|
---|
[429] | 132 | Static Function GetPathRoot(path As String) As String
|
---|
| 133 | path = CheckPath(path)
|
---|
[142] | 134 | If IsPathRooted(path) Then
|
---|
[429] | 135 | If path.Contains(UniformNamingConventionString) Then
|
---|
| 136 | Return path.Remove(path.IndexOf(DirectorySeparatorChar,
|
---|
| 137 | UniformNamingConventionString.Length) + 1)
|
---|
| 138 | Else
|
---|
| 139 | Return path.Remove(3)
|
---|
| 140 | End If
|
---|
[142] | 141 | Else
|
---|
[429] | 142 | Return New String()
|
---|
[142] | 143 | End If
|
---|
[61] | 144 | End Function
|
---|
| 145 |
|
---|
[404] | 146 | /*!
|
---|
[429] | 147 | @brief ランダムなファイル名を取得する
|
---|
| 148 | @param ファイルパス
|
---|
| 149 | @return ファイル名
|
---|
| 150 | 手抜き実装
|
---|
[404] | 151 | */
|
---|
[429] | 152 | Static Function GetRandomFileName() As String
|
---|
| 153 | Randomize
|
---|
| 154 | Return Str$(((Rnd() * 900000000) As Long) + 10000000)
|
---|
[61] | 155 | End Function
|
---|
| 156 |
|
---|
[404] | 157 | /*!
|
---|
[429] | 158 | @brief 一時ファイルを作成し、ファイルパスを取得する
|
---|
| 159 | @return パス
|
---|
[404] | 160 | */
|
---|
[429] | 161 | Static Function GetTempFileName() As String
|
---|
| 162 | Dim tempFileName[ELM(MAX_PATH)] As TCHAR
|
---|
| 163 | Dim len = WIN32API_GetTempFileName(GetTempPath(), "ABT", 0, tempFileName)
|
---|
| 164 | If len <> 0 Then
|
---|
| 165 | Return New String(tempFileName, len As Long)
|
---|
[61] | 166 | Else
|
---|
[429] | 167 | Throw New IOException("Path.GetTempFileName: Failed to GetTempFileName.")
|
---|
[61] | 168 | End If
|
---|
| 169 | End Function
|
---|
| 170 |
|
---|
[429] | 171 |
|
---|
| 172 | /*
|
---|
| 173 | @brief システムの一時フォルダのパス取得する
|
---|
| 174 | @return パス
|
---|
[404] | 175 | */
|
---|
[429] | 176 | Static Function GetTempPath() As String
|
---|
| 177 | Dim size = WIN32API_GetTempPath(0, 0)
|
---|
[468] | 178 | Dim buf = New Text.StringBuilder(size)
|
---|
| 179 | buf.Length = size
|
---|
| 180 | Dim len = WIN32API_GetTempPath(size, StrPtr(buf))
|
---|
[429] | 181 | If (len > size) or len = 0 Then
|
---|
| 182 | Throw New IOException("Path.GetTempPath: Failed to GetTempPath.")
|
---|
[61] | 183 | Else
|
---|
[468] | 184 | buf.Length = len
|
---|
| 185 | Return buf.ToString
|
---|
[61] | 186 | End If
|
---|
| 187 | End Function
|
---|
| 188 |
|
---|
[429] | 189 |
|
---|
| 190 | /*
|
---|
| 191 | @brief パスに拡張子が含まれるかどうか
|
---|
| 192 | @param パス
|
---|
| 193 | @retval True 含まれる
|
---|
| 194 | @retval False 含まれない
|
---|
[404] | 195 | */
|
---|
[429] | 196 | Static Function HasExtension(path As String) As Boolean
|
---|
| 197 | path = CheckPath(path)
|
---|
| 198 | Return GetExtensionIndex(path) <> -1
|
---|
| 199 | End Function
|
---|
[61] | 200 |
|
---|
[429] | 201 | /*
|
---|
| 202 | @brief パスが絶対パスか取得する
|
---|
| 203 | @param パス
|
---|
| 204 | @retval True 絶対パス
|
---|
| 205 | @retval False 相対パス
|
---|
| 206 | */
|
---|
| 207 | Static Function IsPathRooted(path As String) As Boolean
|
---|
| 208 | path = CheckPath(path)
|
---|
| 209 | Return path.Contains(UniformNamingConventionString) _
|
---|
| 210 | or path.Contains(VolumeSeparatorChar) _
|
---|
| 211 | or path.StartsWith(DirectorySeparatorChar)
|
---|
[61] | 212 | End Function
|
---|
| 213 |
|
---|
| 214 | Private
|
---|
[429] | 215 | Static Const ExtensionSeparatorChar = &H2E As StrChar
|
---|
[404] | 216 | Static Const InvalidPathChars = Ex"\q<>|\0\t" As String
|
---|
[468] | 217 | Static Const UniformNamingConventionString = "\\" As String
|
---|
[61] | 218 |
|
---|
[429] | 219 | '----------------------------------------------------------------
|
---|
| 220 | ' プライベート メソッド
|
---|
| 221 | '----------------------------------------------------------------
|
---|
| 222 |
|
---|
| 223 | /*
|
---|
| 224 | @brief パスに不正な文字が含まれていないか調べる。
|
---|
| 225 | @param パス
|
---|
| 226 | @return 正しく直されたパス
|
---|
| 227 | パス末端にディレクトリ区切り記号があった場合除去する
|
---|
| 228 | ..\などが含まれるパスについては未実装
|
---|
[404] | 229 | */
|
---|
[429] | 230 | Static Function CheckPath(path As String) As String
|
---|
| 231 | path = path.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar)
|
---|
| 232 |
|
---|
[404] | 233 | Dim i As Long
|
---|
| 234 | For i = 0 To ELM(InvalidPathChars.Length)
|
---|
[468] | 235 | If path.Contains(InvalidPathChars[i]) Then
|
---|
[429] | 236 | Throw New IOException("Path.CheckPath: The path contains invalidPathChars.")
|
---|
[404] | 237 | End If
|
---|
| 238 | Next
|
---|
[61] | 239 |
|
---|
[429] | 240 | If path.LastIndexOf(UniformNamingConventionString) > 0 Then
|
---|
| 241 | Throw New IOException("Path.CheckPath: The path is invalid value.")
|
---|
| 242 | End If
|
---|
| 243 |
|
---|
| 244 | If path.EndsWith(DirectorySeparatorChar) Then
|
---|
| 245 | Return path.Remove(path.Length - 1)
|
---|
| 246 | Else
|
---|
| 247 | Return path
|
---|
| 248 | End If
|
---|
[61] | 249 | End Function
|
---|
[404] | 250 |
|
---|
[429] | 251 | /*
|
---|
| 252 | @brief パスの最も後ろにあるディレクトリ区切り記号の位置を取得する
|
---|
| 253 | @param パス
|
---|
| 254 | @return インデックス
|
---|
[404] | 255 | */
|
---|
| 256 | Static Function GetLastSeparatorIndex(path As String) As Long
|
---|
[429] | 257 | Return System.Math.Max(path.LastIndexOf(DirectorySeparatorChar),
|
---|
| 258 | path.LastIndexOf(AltDirectorySeparatorChar)) As Long
|
---|
[404] | 259 | End Function
|
---|
[429] | 260 |
|
---|
| 261 | /*
|
---|
| 262 | @brief 拡張子の位置を取得する。
|
---|
| 263 | @param パス
|
---|
| 264 | @return インデックス
|
---|
| 265 | */
|
---|
| 266 | Static Function GetExtensionIndex(path As String) As Long
|
---|
| 267 | Dim extensionIndex = path.LastIndexOf(ExtensionSeparatorChar) As Long
|
---|
| 268 | If extensionIndex > GetLastSeparatorIndex(path) Then
|
---|
| 269 | Return extensionIndex
|
---|
| 270 | Else
|
---|
| 271 | Return -1
|
---|
| 272 | End If
|
---|
| 273 | End Function
|
---|
[61] | 274 | End Class
|
---|
| 275 |
|
---|
[429] | 276 | 'メソッドと名前が被るAPI
|
---|
| 277 | Function WIN32API_GetTempPath(nBufferLength As DWord, lpBuffer As PTSTR) As DWord
|
---|
[61] | 278 | Return GetTempPath(nBufferLength, lpBuffer)
|
---|
[125] | 279 | End Function
|
---|
| 280 |
|
---|
[429] | 281 | Function WIN32API_GetTempFileName (pPathName As PCTSTR, pPrefixString As PCTSTR, uUnique As DWord, pTempFileName As PTSTR) As DWord
|
---|
[125] | 282 | Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
|
---|
[271] | 283 | End Function
|
---|
| 284 |
|
---|
| 285 |
|
---|
| 286 | End Namespace
|
---|
| 287 | End Namespace
|
---|