Просмотр исходного кода

Added 'isDirectory' method to the File class.

Damian Kołakowski 10 лет назад
Родитель
Сommit
cedbec42a1
1 измененных файлов с 10 добавлено и 1 удалено
  1. 10 1
      Sources/File.swift

+ 10 - 1
Sources/File.swift

@@ -17,6 +17,7 @@ public enum FileError: ErrorType {
     case ReadFailed(String)
     case ReadFailed(String)
     case SeekFailed(String)
     case SeekFailed(String)
     case GetCurrentWorkingDirectoryFailed(String)
     case GetCurrentWorkingDirectoryFailed(String)
+    case IsDirectoryFailed(String)
 }
 }
 
 
 public class File {
 public class File {
@@ -41,13 +42,21 @@ public class File {
         return File(file)
         return File(file)
     }
     }
     
     
+    public static func isDirectory(path: String) throws -> Bool {
+        var s = stat()
+        guard stat(path, &s) == 0 else {
+            throw FileError.IsDirectoryFailed(descriptionOfLastError())
+        }
+        return s.st_mode & S_IFMT == S_IFDIR
+    }
+    
     public static func currentWorkingDirectory() throws -> String {
     public static func currentWorkingDirectory() throws -> String {
         let path = getcwd(nil, 0)
         let path = getcwd(nil, 0)
         if path == nil {
         if path == nil {
             throw FileError.GetCurrentWorkingDirectoryFailed(descriptionOfLastError())
             throw FileError.GetCurrentWorkingDirectoryFailed(descriptionOfLastError())
         }
         }
         guard let result = String.fromCString(path) else {
         guard let result = String.fromCString(path) else {
-            throw FileError.GetCurrentWorkingDirectoryFailed("Could not convert getcwd(...)'s result to String.")
+            throw FileError.GetCurrentWorkingDirectoryFailed("Could not convert getcwd(...)'s result (\(path)) to String.")
         }
         }
         return result
         return result
     }
     }