LMConsolidator // Optimize fixEOF().
This commit is contained in:
parent
145fa34ebd
commit
80f9b9bb46
|
@ -46,34 +46,30 @@ public extension vChewingLM {
|
||||||
/// - Parameter path: 給定檔案路徑。
|
/// - Parameter path: 給定檔案路徑。
|
||||||
/// - Returns: 結果正常或修復順利則為真,其餘為假。
|
/// - Returns: 結果正常或修復順利則為真,其餘為假。
|
||||||
@discardableResult public static func fixEOF(path: String) -> Bool {
|
@discardableResult public static func fixEOF(path: String) -> Bool {
|
||||||
let urlPath = URL(fileURLWithPath: path)
|
var fileSize: UInt64?
|
||||||
if FileManager.default.fileExists(atPath: path) {
|
do {
|
||||||
var strIncoming = ""
|
let dict = try FileManager.default.attributesOfItem(atPath: path)
|
||||||
do {
|
if let value = dict[FileAttributeKey.size] as? UInt64 { fileSize = value }
|
||||||
strIncoming += try String(contentsOf: urlPath, encoding: .utf8)
|
} catch {
|
||||||
/// 注意:Swift 版 LMConsolidator 並未在此安排對 EOF 的去重複工序。
|
vCLog("EOF Fix Failed: File Missing at \(path).")
|
||||||
/// 但這個函式執行完之後往往就會 consolidate() 整理格式,所以不會有差。
|
return false
|
||||||
if !strIncoming.hasSuffix("\n") {
|
}
|
||||||
vCLog("EOF Fix Necessity Confirmed, Start Fixing.")
|
guard let fileSize = fileSize else { return false }
|
||||||
if let writeFile = FileHandle(forUpdatingAtPath: path),
|
guard let writeFile = FileHandle(forUpdatingAtPath: path) else {
|
||||||
let endl = "\n".data(using: .utf8)
|
vCLog("EOF Fix Failed: File Not Writable at \(path).")
|
||||||
{
|
return false
|
||||||
writeFile.seekToEndOfFile()
|
}
|
||||||
writeFile.write(endl)
|
defer { writeFile.closeFile() }
|
||||||
writeFile.closeFile()
|
/// 注意:Swift 版 LMConsolidator 並未在此安排對 EOF 的去重複工序。
|
||||||
} else {
|
/// 但這個函式執行完之後往往就會 consolidate() 整理格式,所以不會有差。
|
||||||
return false
|
writeFile.seek(toFileOffset: fileSize - 1)
|
||||||
}
|
if writeFile.readDataToEndOfFile().first != 0x0A {
|
||||||
}
|
vCLog("EOF Missing Confirmed, Start Fixing.")
|
||||||
} catch {
|
var newData = Data()
|
||||||
vCLog("EOF Fix Failed w/ File: \(path)")
|
newData.append(0x0A)
|
||||||
vCLog("EOF Fix Failed w/ Error: \(error).")
|
writeFile.write(newData)
|
||||||
return false
|
vCLog("EOF Successfully Assured.")
|
||||||
}
|
|
||||||
vCLog("EOF Successfully Ensured (with possible autofixes performed).")
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
vCLog("EOF Fix Failed: File Missing at \(path).")
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue