LMAssembly // Store file paths of all sub-language-models.

- Also: LMPlainBPMF // Variable name fix.
This commit is contained in:
ShikiSuen 2022-12-02 17:27:15 +08:00
parent cd85f821d2
commit 8f5bd48beb
6 changed files with 48 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import Shared
extension vChewingLM { extension vChewingLM {
@frozen public struct LMAssociates { @frozen public struct LMAssociates {
public private(set) var filePath: String?
var rangeMap: [String: [(Range<String.Index>, Int)]] = [:] var rangeMap: [String: [(Range<String.Index>, Int)]] = [:]
var strData: String = "" var strData: String = ""
@ -37,6 +38,8 @@ extension vChewingLM {
@discardableResult public mutating func open(_ path: String) -> Bool { @discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false } if isLoaded { return false }
let oldPath = filePath
filePath = nil
LMConsolidator.fixEOF(path: path) LMConsolidator.fixEOF(path: path)
LMConsolidator.consolidate(path: path, pragma: true) LMConsolidator.consolidate(path: path, pragma: true)
@ -45,11 +48,13 @@ extension vChewingLM {
let rawStrData = try String(contentsOfFile: path, encoding: .utf8) let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
replaceData(textData: rawStrData) replaceData(textData: rawStrData)
} catch { } catch {
filePath = oldPath
vCLog("\(error)") vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).") vCLog("↑ Exception happened when reading data at: \(path).")
return false return false
} }
filePath = path
return true return true
} }
@ -74,6 +79,7 @@ extension vChewingLM {
} }
public mutating func clear() { public mutating func clear() {
filePath = nil
rangeMap.removeAll() rangeMap.removeAll()
} }

View File

@ -15,6 +15,7 @@ import Shared
extension vChewingLM { extension vChewingLM {
/// 便使 /// 便使
@frozen public struct LMCassette { @frozen public struct LMCassette {
public private(set) var filePath: String?
public private(set) var nameShort: String = "" public private(set) var nameShort: String = ""
public private(set) var nameENG: String = "" public private(set) var nameENG: String = ""
public private(set) var nameCJK: String = "" public private(set) var nameCJK: String = ""
@ -67,6 +68,8 @@ extension vChewingLM {
/// - Returns: /// - Returns:
@discardableResult public mutating func open(_ path: String) -> Bool { @discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false } if isLoaded { return false }
let oldPath = filePath
filePath = nil
if FileManager.default.fileExists(atPath: path) { if FileManager.default.fileExists(atPath: path) {
do { do {
guard let fileHandle = FileHandle(forReadingAtPath: path) else { guard let fileHandle = FileHandle(forReadingAtPath: path) else {
@ -146,17 +149,20 @@ extension vChewingLM {
} }
maxKeyLength = theMaxKeyLength maxKeyLength = theMaxKeyLength
keyNameMap[wildcardKey] = keyNameMap[wildcardKey] ?? "" keyNameMap[wildcardKey] = keyNameMap[wildcardKey] ?? ""
filePath = path
return true return true
} catch { } catch {
vCLog("CIN Loading Failed: File Access Error.") vCLog("CIN Loading Failed: File Access Error.")
return false
} }
} else {
vCLog("CIN Loading Failed: File Missing.")
} }
vCLog("CIN Loading Failed: File Missing.") filePath = oldPath
return false return false
} }
public mutating func clear() { public mutating func clear() {
filePath = nil
keyNameMap.removeAll() keyNameMap.removeAll()
charDefMap.removeAll() charDefMap.removeAll()
charDefWildcardMap.removeAll() charDefWildcardMap.removeAll()

View File

@ -17,6 +17,7 @@ extension vChewingLM {
/// C++ ParselessLM Swift /// C++ ParselessLM Swift
/// For /// For
@frozen public struct LMCoreEX { @frozen public struct LMCoreEX {
public private(set) var filePath: String?
/// 便 strData /// 便 strData
var rangeMap: [String: [Range<String.Index>]] = [:] var rangeMap: [String: [Range<String.Index>]] = [:]
/// ///
@ -60,6 +61,10 @@ extension vChewingLM {
/// - path: /// - path:
@discardableResult public mutating func open(_ path: String) -> Bool { @discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false } if isLoaded { return false }
let oldPath = filePath
filePath = nil
var consolidated = false var consolidated = false
if allowConsolidation { if allowConsolidation {
@ -76,11 +81,13 @@ extension vChewingLM {
} }
replaceData(textData: rawStrData) replaceData(textData: rawStrData)
} catch { } catch {
filePath = oldPath
vCLog("\(error)") vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).") vCLog("↑ Exception happened when reading data at: \(path).")
return false return false
} }
filePath = path
return true return true
} }
@ -106,6 +113,7 @@ extension vChewingLM {
/// ///
public mutating func clear() { public mutating func clear() {
filePath = nil
rangeMap.removeAll() rangeMap.removeAll()
} }

View File

@ -15,6 +15,7 @@ extension vChewingLM {
/// mac /// mac
/// 使 plist /// 使 plist
@frozen public struct LMCoreNS { @frozen public struct LMCoreNS {
public private(set) var filePath: String?
/// UTF8 /// UTF8
var rangeMap: [String: [Data]] = [:] var rangeMap: [String: [Data]] = [:]
/// LMCoreNS /// LMCoreNS
@ -59,6 +60,8 @@ extension vChewingLM {
/// - path: /// - path:
@discardableResult public mutating func open(_ path: String) -> Bool { @discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false } if isLoaded { return false }
let oldPath = filePath
filePath = nil
do { do {
let rawData = try Data(contentsOf: URL(fileURLWithPath: path)) let rawData = try Data(contentsOf: URL(fileURLWithPath: path))
@ -66,15 +69,18 @@ extension vChewingLM {
try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: [Data]] ?? .init() try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: [Data]] ?? .init()
rangeMap = rawPlist rangeMap = rawPlist
} catch { } catch {
filePath = oldPath
vCLog("↑ Exception happened when reading plist file at: \(path).") vCLog("↑ Exception happened when reading plist file at: \(path).")
return false return false
} }
filePath = path
return true return true
} }
/// ///
public mutating func clear() { public mutating func clear() {
filePath = nil
rangeMap.removeAll() rangeMap.removeAll()
} }

View File

@ -12,46 +12,52 @@ import Shared
extension vChewingLM { extension vChewingLM {
@frozen public struct LMPlainBopomofo { @frozen public struct LMPlainBopomofo {
var rangeMap: [String: String] = [:] public private(set) var filePath: String?
var dataMap: [String: String] = [:]
public var count: Int { rangeMap.count } public var count: Int { dataMap.count }
public init() { public init() {
rangeMap = [:] dataMap = [:]
} }
public var isLoaded: Bool { !rangeMap.isEmpty } public var isLoaded: Bool { !dataMap.isEmpty }
@discardableResult public mutating func open(_ path: String) -> Bool { @discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false } if isLoaded { return false }
let oldPath = filePath
filePath = nil
do { do {
let rawData = try Data(contentsOf: URL(fileURLWithPath: path)) let rawData = try Data(contentsOf: URL(fileURLWithPath: path))
let rawPlist: [String: String] = let rawPlist: [String: String] =
try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: String] ?? .init() try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: String] ?? .init()
rangeMap = rawPlist dataMap = rawPlist
} catch { } catch {
filePath = oldPath
vCLog("\(error)") vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).") vCLog("↑ Exception happened when reading data at: \(path).")
return false return false
} }
filePath = path
return true return true
} }
public mutating func clear() { public mutating func clear() {
rangeMap.removeAll() filePath = nil
dataMap.removeAll()
} }
public func valuesFor(key: String) -> [String] { public func valuesFor(key: String) -> [String] {
var pairs: [String] = [] var pairs: [String] = []
if let arrRangeRecords: String = rangeMap[key]?.trimmingCharacters(in: .newlines) { if let arrRangeRecords: String = dataMap[key]?.trimmingCharacters(in: .newlines) {
pairs.append(contentsOf: arrRangeRecords.map { String($0) }) pairs.append(contentsOf: arrRangeRecords.map { String($0) })
} }
return pairs.deduplicated return pairs.deduplicated
} }
public func hasValuesFor(key: String) -> Bool { rangeMap.keys.contains(key) } public func hasValuesFor(key: String) -> Bool { dataMap.keys.contains(key) }
} }
} }

View File

@ -11,6 +11,7 @@ import Shared
extension vChewingLM { extension vChewingLM {
@frozen public struct LMReplacements { @frozen public struct LMReplacements {
public private(set) var filePath: String?
var rangeMap: [String: Range<String.Index>] = [:] var rangeMap: [String: Range<String.Index>] = [:]
var strData: String = "" var strData: String = ""
@ -24,6 +25,8 @@ extension vChewingLM {
@discardableResult public mutating func open(_ path: String) -> Bool { @discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false } if isLoaded { return false }
let oldPath = filePath
filePath = nil
LMConsolidator.fixEOF(path: path) LMConsolidator.fixEOF(path: path)
LMConsolidator.consolidate(path: path, pragma: true) LMConsolidator.consolidate(path: path, pragma: true)
@ -32,11 +35,13 @@ extension vChewingLM {
let rawStrData = try String(contentsOfFile: path, encoding: .utf8) let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
replaceData(textData: rawStrData) replaceData(textData: rawStrData)
} catch { } catch {
filePath = oldPath
vCLog("\(error)") vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).") vCLog("↑ Exception happened when reading data at: \(path).")
return false return false
} }
filePath = path
return true return true
} }
@ -59,6 +64,7 @@ extension vChewingLM {
} }
public mutating func clear() { public mutating func clear() {
filePath = nil
rangeMap.removeAll() rangeMap.removeAll()
} }