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

View File

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

View File

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

View File

@ -15,6 +15,7 @@ extension vChewingLM {
/// mac
/// 使 plist
@frozen public struct LMCoreNS {
public private(set) var filePath: String?
/// UTF8
var rangeMap: [String: [Data]] = [:]
/// LMCoreNS
@ -59,6 +60,8 @@ extension vChewingLM {
/// - path:
@discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false }
let oldPath = filePath
filePath = nil
do {
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()
rangeMap = rawPlist
} catch {
filePath = oldPath
vCLog("↑ Exception happened when reading plist file at: \(path).")
return false
}
filePath = path
return true
}
///
public mutating func clear() {
filePath = nil
rangeMap.removeAll()
}

View File

@ -12,46 +12,52 @@ import Shared
extension vChewingLM {
@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() {
rangeMap = [:]
dataMap = [:]
}
public var isLoaded: Bool { !rangeMap.isEmpty }
public var isLoaded: Bool { !dataMap.isEmpty }
@discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false }
let oldPath = filePath
filePath = nil
do {
let rawData = try Data(contentsOf: URL(fileURLWithPath: path))
let rawPlist: [String: String] =
try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: String] ?? .init()
rangeMap = rawPlist
dataMap = rawPlist
} catch {
filePath = oldPath
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
filePath = path
return true
}
public mutating func clear() {
rangeMap.removeAll()
filePath = nil
dataMap.removeAll()
}
public func valuesFor(key: String) -> [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) })
}
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 {
@frozen public struct LMReplacements {
public private(set) var filePath: String?
var rangeMap: [String: Range<String.Index>] = [:]
var strData: String = ""
@ -24,6 +25,8 @@ extension vChewingLM {
@discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false }
let oldPath = filePath
filePath = nil
LMConsolidator.fixEOF(path: path)
LMConsolidator.consolidate(path: path, pragma: true)
@ -32,11 +35,13 @@ extension vChewingLM {
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
replaceData(textData: rawStrData)
} catch {
filePath = oldPath
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
filePath = path
return true
}
@ -59,6 +64,7 @@ extension vChewingLM {
}
public mutating func clear() {
filePath = nil
rangeMap.removeAll()
}