Repo // Regularize FileHandle API usages for future purposes.

This commit is contained in:
ShikiSuen 2022-12-28 11:20:40 +08:00
parent 1305ce2a26
commit fdd767069e
8 changed files with 81 additions and 23 deletions

View File

@ -11,6 +11,7 @@
import Cocoa import Cocoa
import IMKUtils import IMKUtils
import InputMethodKit import InputMethodKit
import SwiftExtension
public let kTargetBin = "vChewing" public let kTargetBin = "vChewing"
public let kTargetBinPhraseEditor = "vChewingPhraseEditor" public let kTargetBinPhraseEditor = "vChewingPhraseEditor"
@ -166,9 +167,15 @@ class AppDelegate: NSWindowController, NSApplicationDelegate {
task.launch() task.launch()
} }
let data = pipe.fileHandleForReading.readDataToEndOfFile() var output = ""
let output = String(data: data, encoding: .utf8)! do {
let data = try pipe.fileHandleForReading.readToEnd()
if let data = data, let str = String(data: data, encoding: .utf8) {
output.append(str)
}
} catch {
return ""
}
return output return output
} }
} }

View File

@ -12,11 +12,15 @@ let package = Package(
targets: ["LineReader"] targets: ["LineReader"]
) )
], ],
dependencies: [], dependencies: [
.package(path: "../vChewing_SwiftExtension")
],
targets: [ targets: [
.target( .target(
name: "LineReader", name: "LineReader",
dependencies: [] dependencies: [
.product(name: "SwiftExtension", package: "vChewing_SwiftExtension")
]
) )
] ]
) )

View File

@ -1,6 +1,7 @@
// (c) 2019 and onwards Robert Muckle-Jones (Apache 2.0 License). // (c) 2019 and onwards Robert Muckle-Jones (Apache 2.0 License).
import Foundation import Foundation
import SwiftExtension
public class LineReader { public class LineReader {
let encoding: String.Encoding let encoding: String.Encoding
@ -36,17 +37,22 @@ public class LineReader {
return line.trimmingCharacters(in: .newlines) return line.trimmingCharacters(in: .newlines)
} }
let nextData = fileHandle.readData(ofLength: chunkSize) fileRead: do {
if !nextData.isEmpty { let nextData = try fileHandle.read(upToCount: chunkSize)
buffer.append(nextData) if let nextData = nextData, !nextData.isEmpty {
} else { buffer.append(nextData)
// End of file or read error continue
atEof = true
if !buffer.isEmpty {
// Buffer contains last line in file (not terminated by delimiter).
let line = String(data: buffer as Data, encoding: encoding)!
return line.trimmingCharacters(in: .newlines)
} }
} catch {
break fileRead
}
// End of file or read error
atEof = true
if !buffer.isEmpty {
// Buffer contains last line in file (not terminated by delimiter).
let line = String(data: buffer as Data, encoding: encoding)!
return line.trimmingCharacters(in: .newlines)
} }
} }
return nil return nil

View File

@ -13,13 +13,15 @@ let package = Package(
) )
], ],
dependencies: [ dependencies: [
.package(path: "../vChewing_IMKUtils") .package(path: "../vChewing_IMKUtils"),
.package(path: "../vChewing_SwiftExtension"),
], ],
targets: [ targets: [
.target( .target(
name: "CocoaExtension", name: "CocoaExtension",
dependencies: [ dependencies: [
.product(name: "IMKUtils", package: "vChewing_IMKUtils") .product(name: "IMKUtils", package: "vChewing_IMKUtils"),
.product(name: "SwiftExtension", package: "vChewing_SwiftExtension"),
] ]
) )
] ]

View File

@ -7,6 +7,7 @@
// requirements defined in MIT License. // requirements defined in MIT License.
import Cocoa import Cocoa
import SwiftExtension
// MARK: - NSMutableString extension // MARK: - NSMutableString extension
@ -45,9 +46,15 @@ extension NSApplication {
task.launch() task.launch()
} }
let data = pipe.fileHandleForReading.readDataToEndOfFile() var output = ""
let output = String(data: data, encoding: .utf8)! do {
let data = try pipe.fileHandleForReading.readToEnd()
if let data = data, let str = String(data: data, encoding: .utf8) {
output.append(str)
}
} catch {
return ""
}
return output return output
} }
} }

View File

@ -205,3 +205,16 @@ extension BinaryInteger {
return formatter.string(from: NSDecimalNumber(string: "\(self)")) ?? "" return formatter.string(from: NSDecimalNumber(string: "\(self)")) ?? ""
} }
} }
// MARK: - File Handle API Compatibility for macOS 10.15.3 and Earlier.
@available(macOS, deprecated: 10.15.4)
extension FileHandle {
public func read(upToCount count: Int) throws -> Data? {
readData(ofLength: count)
}
public func readToEnd() throws -> Data? {
readDataToEndOfFile()
}
}

View File

@ -11,6 +11,7 @@ import LangModelAssembly
import NotifierUI import NotifierUI
import PhraseEditorUI import PhraseEditorUI
import Shared import Shared
import SwiftExtension
/// 使 /// 使
private let kTemplateNameUserPhrases = "template-userphrases" private let kTemplateNameUserPhrases = "template-userphrases"
@ -675,9 +676,13 @@ public class LMMgr {
} catch { } catch {
NSWorkspace.shared.openFile(url.path, withApplication: "TextEdit") NSWorkspace.shared.openFile(url.path, withApplication: "TextEdit")
} }
if let outStr = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) { do {
vCLog(outStr) if let theData = try fileHandle.readToEnd(),
} let outStr = String(data: theData, encoding: .utf8)
{
vCLog(outStr)
}
} catch {}
case "Finder": case "Finder":
NSWorkspace.shared.activateFileViewerSelecting([url]) NSWorkspace.shared.activateFileViewerSelecting([url])
default: default:

View File

@ -49,6 +49,8 @@
5B963CA328D5C23600DCEE88 /* SwiftExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B963CA228D5C23600DCEE88 /* SwiftExtension */; }; 5B963CA328D5C23600DCEE88 /* SwiftExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B963CA228D5C23600DCEE88 /* SwiftExtension */; };
5B963CA828D5DB1400DCEE88 /* PrefMgr_Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B963CA728D5DB1400DCEE88 /* PrefMgr_Core.swift */; }; 5B963CA828D5DB1400DCEE88 /* PrefMgr_Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B963CA728D5DB1400DCEE88 /* PrefMgr_Core.swift */; };
5B98114828D6198700CBC605 /* PinyinPhonaConverter in Frameworks */ = {isa = PBXBuildFile; productRef = 5B98114728D6198700CBC605 /* PinyinPhonaConverter */; }; 5B98114828D6198700CBC605 /* PinyinPhonaConverter in Frameworks */ = {isa = PBXBuildFile; productRef = 5B98114728D6198700CBC605 /* PinyinPhonaConverter */; };
5B9A62D9295BEA3400F79F3C /* SwiftExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B9A62D8295BEA3400F79F3C /* SwiftExtension */; };
5B9A62DB295BEA4500F79F3C /* CocoaExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B9A62DA295BEA4500F79F3C /* CocoaExtension */; };
5BA8C30328DF0360004C5CC4 /* CandidateWindow in Frameworks */ = {isa = PBXBuildFile; productRef = 5BA8C30228DF0360004C5CC4 /* CandidateWindow */; }; 5BA8C30328DF0360004C5CC4 /* CandidateWindow in Frameworks */ = {isa = PBXBuildFile; productRef = 5BA8C30228DF0360004C5CC4 /* CandidateWindow */; };
5BA9FD0F27FEDB6B002DE248 /* VwrPrefPaneGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0A27FEDB6B002DE248 /* VwrPrefPaneGeneral.swift */; }; 5BA9FD0F27FEDB6B002DE248 /* VwrPrefPaneGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0A27FEDB6B002DE248 /* VwrPrefPaneGeneral.swift */; };
5BA9FD1027FEDB6B002DE248 /* VwrPrefPaneKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0B27FEDB6B002DE248 /* VwrPrefPaneKeyboard.swift */; }; 5BA9FD1027FEDB6B002DE248 /* VwrPrefPaneKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0B27FEDB6B002DE248 /* VwrPrefPaneKeyboard.swift */; };
@ -376,6 +378,8 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
5B9A62D9295BEA3400F79F3C /* SwiftExtension in Frameworks */,
5B9A62DB295BEA4500F79F3C /* CocoaExtension in Frameworks */,
5BFC63D128D4B9F7004A77B7 /* IMKUtils in Frameworks */, 5BFC63D128D4B9F7004A77B7 /* IMKUtils in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
@ -824,6 +828,8 @@
name = vChewingInstaller; name = vChewingInstaller;
packageProductDependencies = ( packageProductDependencies = (
5BFC63D028D4B9F7004A77B7 /* IMKUtils */, 5BFC63D028D4B9F7004A77B7 /* IMKUtils */,
5B9A62D8295BEA3400F79F3C /* SwiftExtension */,
5B9A62DA295BEA4500F79F3C /* CocoaExtension */,
); );
productName = vChewingInstaller; productName = vChewingInstaller;
productReference = 6ACA41CB15FC1D7500935EF6 /* vChewingInstaller.app */; productReference = 6ACA41CB15FC1D7500935EF6 /* vChewingInstaller.app */;
@ -1821,6 +1827,14 @@
isa = XCSwiftPackageProductDependency; isa = XCSwiftPackageProductDependency;
productName = PinyinPhonaConverter; productName = PinyinPhonaConverter;
}; };
5B9A62D8295BEA3400F79F3C /* SwiftExtension */ = {
isa = XCSwiftPackageProductDependency;
productName = SwiftExtension;
};
5B9A62DA295BEA4500F79F3C /* CocoaExtension */ = {
isa = XCSwiftPackageProductDependency;
productName = CocoaExtension;
};
5BA8C30228DF0360004C5CC4 /* CandidateWindow */ = { 5BA8C30228DF0360004C5CC4 /* CandidateWindow */ = {
isa = XCSwiftPackageProductDependency; isa = XCSwiftPackageProductDependency;
productName = CandidateWindow; productName = CandidateWindow;