OSDN Git Service

shared instanceを持つようにした
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
index c0f8ea7..bbab898 100644 (file)
@@ -18,8 +18,21 @@ private func supportDirName() -> String {
         ?? "UnknownAppliation"
 }
 
+func localizedAppName() -> String {
+    
+    guard let name = Bundle.main.localizedInfoDictionary?["CFBundleName"] as? String,
+        !name.isEmpty else {
+            
+            return supportDirName()
+    }
+    
+    return name
+}
+
 struct ApplicationDirecrories {
     
+    static let shared = ApplicationDirecrories()
+    
     static let support = searchedURL(for: .applicationSupportDirectory)
         .appendingPathComponent(supportDirName())
     
@@ -35,41 +48,44 @@ struct ApplicationDirecrories {
     }
 }
 
-func checkDirectory(_ url: URL) -> Bool {
+
+func createDirectory(_ url: URL) -> Bool {
     
-    var success = true
+    do {
+        
+        try FileManager.default.createDirectory(at: url,
+                                                withIntermediateDirectories: false,
+                                                attributes: nil)
+        
+        return true
+        
+    } catch {
+        
+        return false
+    }
+}
+
+func checkDirectory(_ url: URL, create: Bool) -> Bool {
     
     do {
         
-        let p = try url.resourceValues(forKeys: [.isDirectoryKey])
-        if !p.isDirectory! {
+        let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
+        if !resourceValue.isDirectory! {
             
             print("Expected a folder to store application data, found a file \(url.path).")
-            success = false
+            
+            return false
         }
         
-    } catch {
+        return true
         
-        let nserror = error as NSError
-        if nserror.code == NSFileReadNoSuchFileError {
-            
-            do {
-                
-                try FileManager
-                    .default
-                    .createDirectory(at: url,
-                                     withIntermediateDirectories: false,
-                                     attributes: nil)
-                
-            } catch {
-                
-                success = false
-            }
-        } else {
+    } catch let error as NSError {
+        
+        if create, error.code == NSFileReadNoSuchFileError {
             
-            success = false
+            return createDirectory(url)
         }
+        
+        return false
     }
-    
-    return success
 }