| 1 | /*!
|
|---|
| 2 | @file Classes/System/IO/Path.ab
|
|---|
| 3 | @brief ファイルパス文字列を操作する
|
|---|
| 4 | @author OverTaker
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | Namespace System
|
|---|
| 8 | Namespace IO
|
|---|
| 9 |
|
|---|
| 10 | Class Path
|
|---|
| 11 | Public
|
|---|
| 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 ':
|
|---|
| 16 |
|
|---|
| 17 | '----------------------------------------------------------------
|
|---|
| 18 | ' パブリック メソッド
|
|---|
| 19 | '----------------------------------------------------------------
|
|---|
| 20 |
|
|---|
| 21 | /*
|
|---|
| 22 | @brief 拡張子を変更する
|
|---|
| 23 | @param パス
|
|---|
| 24 | @param 変更する拡張子(.を含む)
|
|---|
| 25 | @return 拡張子変更後のパス
|
|---|
| 26 | */
|
|---|
| 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
|
|---|
| 34 | End Function
|
|---|
| 35 |
|
|---|
| 36 | /*
|
|---|
| 37 | @brief 二つのパスを結合する
|
|---|
| 38 | @param 結合されるパス
|
|---|
| 39 | @param 結合するパス
|
|---|
| 40 | @return パス
|
|---|
| 41 | */
|
|---|
| 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
|
|---|
| 53 | End Function
|
|---|
| 54 |
|
|---|
| 55 | /*
|
|---|
| 56 | @brief パス文字列からファイル名を取得する
|
|---|
| 57 | @param パス
|
|---|
| 58 | @return ファイル名
|
|---|
| 59 | */
|
|---|
| 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
|
|---|
| 68 | End Function
|
|---|
| 69 |
|
|---|
| 70 | /*
|
|---|
| 71 | @brief パス文字列からファイル名を拡張子を除いて取得する
|
|---|
| 72 | @param パス
|
|---|
| 73 | @return 拡張子を除いたファイル名
|
|---|
| 74 | */
|
|---|
| 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
|
|---|
| 82 | End Function
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | @brief 絶対パスを取得する
|
|---|
| 86 | @param 相対パス
|
|---|
| 87 | @return 絶対パス
|
|---|
| 88 | */
|
|---|
| 89 | Static Function GetFullPath(path As String) As String
|
|---|
| 90 | If IsPathRooted(path) Then
|
|---|
| 91 | Return path
|
|---|
| 92 | Else
|
|---|
| 93 | Return Combine(System.Environment.CurrentDirectory, path)
|
|---|
| 94 | End If
|
|---|
| 95 | End Function
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | @brief パス文字列から拡張子を取得する
|
|---|
| 99 | @param パス
|
|---|
| 100 | @return .を含む拡張子
|
|---|
| 101 | */
|
|---|
| 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)
|
|---|
| 107 | Else
|
|---|
| 108 | Return New String()
|
|---|
| 109 | End If
|
|---|
| 110 | End Function
|
|---|
| 111 |
|
|---|
| 112 | /*
|
|---|
| 113 | @brief ひとつ上のディレクトリを取得する
|
|---|
| 114 | @param パス
|
|---|
| 115 | @return ディレクトリパス
|
|---|
| 116 | */
|
|---|
| 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)
|
|---|
| 124 | End If
|
|---|
| 125 | End Function
|
|---|
| 126 |
|
|---|
| 127 | /*
|
|---|
| 128 | @brief ルートディレクトリを取得する
|
|---|
| 129 | @param パス
|
|---|
| 130 | @return ルートディレクトリ
|
|---|
| 131 | */
|
|---|
| 132 | Static Function GetPathRoot(path As String) As String
|
|---|
| 133 | path = CheckPath(path)
|
|---|
| 134 | If IsPathRooted(path) Then
|
|---|
| 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
|
|---|
| 141 | Else
|
|---|
| 142 | Return New String()
|
|---|
| 143 | End If
|
|---|
| 144 | End Function
|
|---|
| 145 |
|
|---|
| 146 | /*!
|
|---|
| 147 | @brief ランダムなファイル名を取得する
|
|---|
| 148 | @param ファイルパス
|
|---|
| 149 | @return ファイル名
|
|---|
| 150 | 手抜き実装
|
|---|
| 151 | */
|
|---|
| 152 | Static Function GetRandomFileName() As String
|
|---|
| 153 | Randomize
|
|---|
| 154 | Return Str$(((Rnd() * 900000000) As Long) + 10000000)
|
|---|
| 155 | End Function
|
|---|
| 156 |
|
|---|
| 157 | /*!
|
|---|
| 158 | @brief 一時ファイルを作成し、ファイルパスを取得する
|
|---|
| 159 | @return パス
|
|---|
| 160 | */
|
|---|
| 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)
|
|---|
| 166 | Else
|
|---|
| 167 | Throw New IOException("Path.GetTempFileName: Failed to GetTempFileName.")
|
|---|
| 168 | End If
|
|---|
| 169 | End Function
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | /*
|
|---|
| 173 | @brief システムの一時フォルダのパス取得する
|
|---|
| 174 | @return パス
|
|---|
| 175 | */
|
|---|
| 176 | Static Function GetTempPath() As String
|
|---|
| 177 | Dim size = WIN32API_GetTempPath(0, 0)
|
|---|
| 178 | Dim buf = New Text.StringBuilder(size)
|
|---|
| 179 | buf.Length = size
|
|---|
| 180 | Dim len = WIN32API_GetTempPath(size, StrPtr(buf))
|
|---|
| 181 | If (len > size) or len = 0 Then
|
|---|
| 182 | Throw New IOException("Path.GetTempPath: Failed to GetTempPath.")
|
|---|
| 183 | Else
|
|---|
| 184 | buf.Length = len
|
|---|
| 185 | Return buf.ToString
|
|---|
| 186 | End If
|
|---|
| 187 | End Function
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | /*
|
|---|
| 191 | @brief パスに拡張子が含まれるかどうか
|
|---|
| 192 | @param パス
|
|---|
| 193 | @retval True 含まれる
|
|---|
| 194 | @retval False 含まれない
|
|---|
| 195 | */
|
|---|
| 196 | Static Function HasExtension(path As String) As Boolean
|
|---|
| 197 | path = CheckPath(path)
|
|---|
| 198 | Return GetExtensionIndex(path) <> -1
|
|---|
| 199 | End Function
|
|---|
| 200 |
|
|---|
| 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)
|
|---|
| 212 | End Function
|
|---|
| 213 |
|
|---|
| 214 | Private
|
|---|
| 215 | Static Const ExtensionSeparatorChar = &H2E As StrChar
|
|---|
| 216 | Static Const InvalidPathChars = Ex"\q<>|\0\t" As String
|
|---|
| 217 | Static Const UniformNamingConventionString = "\\" As String
|
|---|
| 218 |
|
|---|
| 219 | '----------------------------------------------------------------
|
|---|
| 220 | ' プライベート メソッド
|
|---|
| 221 | '----------------------------------------------------------------
|
|---|
| 222 |
|
|---|
| 223 | /*
|
|---|
| 224 | @brief パスに不正な文字が含まれていないか調べる。
|
|---|
| 225 | @param パス
|
|---|
| 226 | @return 正しく直されたパス
|
|---|
| 227 | パス末端にディレクトリ区切り記号があった場合除去する
|
|---|
| 228 | ..\などが含まれるパスについては未実装
|
|---|
| 229 | */
|
|---|
| 230 | Static Function CheckPath(path As String) As String
|
|---|
| 231 | path = path.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar)
|
|---|
| 232 |
|
|---|
| 233 | Dim i As Long
|
|---|
| 234 | For i = 0 To ELM(InvalidPathChars.Length)
|
|---|
| 235 | If path.Contains(InvalidPathChars[i]) Then
|
|---|
| 236 | Throw New IOException("Path.CheckPath: The path contains invalidPathChars.")
|
|---|
| 237 | End If
|
|---|
| 238 | Next
|
|---|
| 239 |
|
|---|
| 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
|
|---|
| 249 | End Function
|
|---|
| 250 |
|
|---|
| 251 | /*
|
|---|
| 252 | @brief パスの最も後ろにあるディレクトリ区切り記号の位置を取得する
|
|---|
| 253 | @param パス
|
|---|
| 254 | @return インデックス
|
|---|
| 255 | */
|
|---|
| 256 | Static Function GetLastSeparatorIndex(path As String) As Long
|
|---|
| 257 | Return System.Math.Max(path.LastIndexOf(DirectorySeparatorChar),
|
|---|
| 258 | path.LastIndexOf(AltDirectorySeparatorChar)) As Long
|
|---|
| 259 | End Function
|
|---|
| 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
|
|---|
| 274 | End Class
|
|---|
| 275 |
|
|---|
| 276 | 'メソッドと名前が被るAPI
|
|---|
| 277 | Function WIN32API_GetTempPath(nBufferLength As DWord, lpBuffer As PTSTR) As DWord
|
|---|
| 278 | Return GetTempPath(nBufferLength, lpBuffer)
|
|---|
| 279 | End Function
|
|---|
| 280 |
|
|---|
| 281 | Function WIN32API_GetTempFileName (pPathName As PCTSTR, pPrefixString As PCTSTR, uUnique As DWord, pTempFileName As PTSTR) As DWord
|
|---|
| 282 | Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
|
|---|
| 283 | End Function
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 | End Namespace
|
|---|
| 287 | End Namespace
|
|---|