OSDN Git Service

判定式33に対応
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
1 //
2 //  ApplicationDirecrories.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/08.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10
11 private func supportDirName() -> String {
12     let main = Bundle.main
13     return main.bundleIdentifier
14         ?? main.infoDictionary?["CFBundleName"] as? String
15         ?? main.infoDictionary?["CFBundleExecutable"] as? String
16         ?? "UnknownAppliation"
17 }
18
19 struct ApplicationDirecrories {
20     
21     static let support = searchedURL(for: .applicationSupportDirectory)
22         .appendingPathComponent(supportDirName())
23     
24     static let documents = searchedURL(for: .documentDirectory)
25     
26     static let pictures = searchedURL(for: .picturesDirectory)
27     
28     private static func searchedURL(for directory: FileManager.SearchPathDirectory) -> URL {
29         
30         return FileManager.default.urls(for: directory,
31                                         in: .userDomainMask)
32             .last
33             ?? URL(fileURLWithPath: NSHomeDirectory())
34     }
35 }
36
37 func checkDirectory(_ url: URL) -> Bool {
38     var success = true
39     
40     do {
41         let p = try url.resourceValues(forKeys: [.isDirectoryKey])
42         if !p.isDirectory! {
43             print("Expected a folder to store application data, found a file \(url.path).")
44             success = false
45         }
46     } catch {
47         let nserror = error as NSError
48         if nserror.code == NSFileReadNoSuchFileError {
49             do {
50                 try FileManager
51                     .default
52                     .createDirectory(at: url,
53                                      withIntermediateDirectories: false,
54                                      attributes: nil)
55             } catch {
56                 success = false
57             }
58         } else {
59             success = false
60         }
61     }
62     
63     return success
64 }