You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.4 KiB
70 lines
2.4 KiB
//
|
|
// FileWriter.swift
|
|
// Poker Analytics 4
|
|
//
|
|
// Created by Laurent Morvillier on 04/09/2018.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum FileError : Error {
|
|
case documentDirectoryNotFound
|
|
}
|
|
|
|
enum FileFormat {
|
|
case csv
|
|
case html
|
|
}
|
|
|
|
class FileUtils {
|
|
|
|
static func pathsFromDocumentsDirectory() throws -> [String] {
|
|
let documentsURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
|
return try FileManager.default.contentsOfDirectory(atPath: documentsURL.path)
|
|
}
|
|
|
|
static func readDocumentFile(fileName: String) throws -> String {
|
|
let fileURL: URL = try self.directoryURLForFileName(fileName)
|
|
// Logger.log("url = \(fileURL.absoluteString)")
|
|
return try String(contentsOf: fileURL, encoding: .utf8)
|
|
|
|
// if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
|
// let fileURL: URL = dir.appendingPathComponent(fileName)
|
|
// Logger.log("url = \(fileURL.absoluteString)")
|
|
// return try String(contentsOf: fileURL, encoding: .utf8)
|
|
// }
|
|
// throw FileError.documentDirectoryNotFound
|
|
}
|
|
|
|
static func readFile(fileURL: URL) throws -> String {
|
|
return try String(contentsOf: fileURL, encoding: .utf8)
|
|
}
|
|
|
|
static func directoryURLForFileName(_ fileName: String) throws -> URL {
|
|
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
|
return dir.appendingPathComponent(fileName)
|
|
}
|
|
throw FileError.documentDirectoryNotFound
|
|
}
|
|
|
|
static func writeToDocumentDirectory(content: String, fileName: String) throws -> URL {
|
|
|
|
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
|
let fileURL: URL = dir.appendingPathComponent(fileName)
|
|
try content.write(to: fileURL, atomically: false, encoding: .utf8)
|
|
return fileURL
|
|
}
|
|
throw FileError.documentDirectoryNotFound
|
|
}
|
|
|
|
@discardableResult static func writeToDocumentDirectory(data: Data, fileName: String) throws -> URL {
|
|
|
|
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
|
let fileURL: URL = dir.appendingPathComponent(fileName)
|
|
try data.write(to: fileURL)
|
|
return fileURL
|
|
}
|
|
throw FileError.documentDirectoryNotFound
|
|
}
|
|
|
|
}
|
|
|