UserPhrase // Remove forced post-write file consolidation.

This commit is contained in:
ShikiSuen 2023-09-03 14:08:56 +08:00
parent 480b362bf4
commit 145fa34ebd
1 changed files with 20 additions and 14 deletions

View File

@ -52,25 +52,31 @@ public extension LMMgr {
public func write(toFilter: Bool) -> Bool { public func write(toFilter: Bool) -> Bool {
guard LMMgr.chkUserLMFilesExist(inputMode) else { return false } guard LMMgr.chkUserLMFilesExist(inputMode) else { return false }
///
/// 使
///
///
let theType: vChewingLM.ReplacableUserDataType = toFilter ? .theFilter : .thePhrases let theType: vChewingLM.ReplacableUserDataType = toFilter ? .theFilter : .thePhrases
let theURL = LMMgr.userDictDataURL(mode: inputMode, type: theType) let theURL = LMMgr.userDictDataURL(mode: inputMode, type: theType)
var fileSize: UInt64?
if let writeFile = FileHandle(forUpdatingAtPath: theURL.path), do {
let data = "\n\(description)\n".data(using: .utf8) let dict = try FileManager.default.attributesOfItem(atPath: theURL.path)
{ if let value = dict[FileAttributeKey.size] as? UInt64 { fileSize = value }
writeFile.seekToEndOfFile() } catch {
writeFile.write(data)
writeFile.closeFile()
} else {
return false return false
} }
guard let fileSize = fileSize else { return false }
// We enforce the format consolidation here, since the pragma header guard var dataToInsert = "\(description)\n".data(using: .utf8) else { return false }
// will let the UserPhraseLM bypasses the consolidating process on load. guard let writeFile = FileHandle(forUpdatingAtPath: theURL.path) else { return false }
if !vChewingLM.LMConsolidator.consolidate(path: theURL.path, pragma: false) { defer { writeFile.closeFile() }
return false if fileSize > 0 {
writeFile.seek(toFileOffset: fileSize - 1)
if writeFile.readDataToEndOfFile().first != 0x0A {
dataToInsert.insert(0x0A, at: 0)
}
} }
writeFile.seekToEndOfFile()
writeFile.write(dataToInsert)
return true return true
} }
} }