Repo // Regularize FileHandle API usages for future purposes.
This commit is contained in:
parent
1305ce2a26
commit
fdd767069e
|
@ -11,6 +11,7 @@
|
|||
import Cocoa
|
||||
import IMKUtils
|
||||
import InputMethodKit
|
||||
import SwiftExtension
|
||||
|
||||
public let kTargetBin = "vChewing"
|
||||
public let kTargetBinPhraseEditor = "vChewingPhraseEditor"
|
||||
|
@ -166,9 +167,15 @@ class AppDelegate: NSWindowController, NSApplicationDelegate {
|
|||
task.launch()
|
||||
}
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output = String(data: data, encoding: .utf8)!
|
||||
|
||||
var output = ""
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,15 @@ let package = Package(
|
|||
targets: ["LineReader"]
|
||||
)
|
||||
],
|
||||
dependencies: [],
|
||||
dependencies: [
|
||||
.package(path: "../vChewing_SwiftExtension")
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
name: "LineReader",
|
||||
dependencies: []
|
||||
dependencies: [
|
||||
.product(name: "SwiftExtension", package: "vChewing_SwiftExtension")
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// (c) 2019 and onwards Robert Muckle-Jones (Apache 2.0 License).
|
||||
|
||||
import Foundation
|
||||
import SwiftExtension
|
||||
|
||||
public class LineReader {
|
||||
let encoding: String.Encoding
|
||||
|
@ -36,17 +37,22 @@ public class LineReader {
|
|||
return line.trimmingCharacters(in: .newlines)
|
||||
}
|
||||
|
||||
let nextData = fileHandle.readData(ofLength: chunkSize)
|
||||
if !nextData.isEmpty {
|
||||
buffer.append(nextData)
|
||||
} else {
|
||||
// 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)
|
||||
fileRead: do {
|
||||
let nextData = try fileHandle.read(upToCount: chunkSize)
|
||||
if let nextData = nextData, !nextData.isEmpty {
|
||||
buffer.append(nextData)
|
||||
continue
|
||||
}
|
||||
} 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
|
||||
|
|
|
@ -13,13 +13,15 @@ let package = Package(
|
|||
)
|
||||
],
|
||||
dependencies: [
|
||||
.package(path: "../vChewing_IMKUtils")
|
||||
.package(path: "../vChewing_IMKUtils"),
|
||||
.package(path: "../vChewing_SwiftExtension"),
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
name: "CocoaExtension",
|
||||
dependencies: [
|
||||
.product(name: "IMKUtils", package: "vChewing_IMKUtils")
|
||||
.product(name: "IMKUtils", package: "vChewing_IMKUtils"),
|
||||
.product(name: "SwiftExtension", package: "vChewing_SwiftExtension"),
|
||||
]
|
||||
)
|
||||
]
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
// requirements defined in MIT License.
|
||||
|
||||
import Cocoa
|
||||
import SwiftExtension
|
||||
|
||||
// MARK: - NSMutableString extension
|
||||
|
||||
|
@ -45,9 +46,15 @@ extension NSApplication {
|
|||
task.launch()
|
||||
}
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output = String(data: data, encoding: .utf8)!
|
||||
|
||||
var output = ""
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -205,3 +205,16 @@ extension BinaryInteger {
|
|||
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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import LangModelAssembly
|
|||
import NotifierUI
|
||||
import PhraseEditorUI
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
|
||||
/// 使用者辭典資料預設範例檔案名稱。
|
||||
private let kTemplateNameUserPhrases = "template-userphrases"
|
||||
|
@ -675,9 +676,13 @@ public class LMMgr {
|
|||
} catch {
|
||||
NSWorkspace.shared.openFile(url.path, withApplication: "TextEdit")
|
||||
}
|
||||
if let outStr = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) {
|
||||
vCLog(outStr)
|
||||
}
|
||||
do {
|
||||
if let theData = try fileHandle.readToEnd(),
|
||||
let outStr = String(data: theData, encoding: .utf8)
|
||||
{
|
||||
vCLog(outStr)
|
||||
}
|
||||
} catch {}
|
||||
case "Finder":
|
||||
NSWorkspace.shared.activateFileViewerSelecting([url])
|
||||
default:
|
||||
|
|
|
@ -49,6 +49,8 @@
|
|||
5B963CA328D5C23600DCEE88 /* SwiftExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B963CA228D5C23600DCEE88 /* SwiftExtension */; };
|
||||
5B963CA828D5DB1400DCEE88 /* PrefMgr_Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B963CA728D5DB1400DCEE88 /* PrefMgr_Core.swift */; };
|
||||
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 */; };
|
||||
5BA9FD0F27FEDB6B002DE248 /* VwrPrefPaneGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0A27FEDB6B002DE248 /* VwrPrefPaneGeneral.swift */; };
|
||||
5BA9FD1027FEDB6B002DE248 /* VwrPrefPaneKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0B27FEDB6B002DE248 /* VwrPrefPaneKeyboard.swift */; };
|
||||
|
@ -376,6 +378,8 @@
|
|||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
5B9A62D9295BEA3400F79F3C /* SwiftExtension in Frameworks */,
|
||||
5B9A62DB295BEA4500F79F3C /* CocoaExtension in Frameworks */,
|
||||
5BFC63D128D4B9F7004A77B7 /* IMKUtils in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -824,6 +828,8 @@
|
|||
name = vChewingInstaller;
|
||||
packageProductDependencies = (
|
||||
5BFC63D028D4B9F7004A77B7 /* IMKUtils */,
|
||||
5B9A62D8295BEA3400F79F3C /* SwiftExtension */,
|
||||
5B9A62DA295BEA4500F79F3C /* CocoaExtension */,
|
||||
);
|
||||
productName = vChewingInstaller;
|
||||
productReference = 6ACA41CB15FC1D7500935EF6 /* vChewingInstaller.app */;
|
||||
|
@ -1821,6 +1827,14 @@
|
|||
isa = XCSwiftPackageProductDependency;
|
||||
productName = PinyinPhonaConverter;
|
||||
};
|
||||
5B9A62D8295BEA3400F79F3C /* SwiftExtension */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
productName = SwiftExtension;
|
||||
};
|
||||
5B9A62DA295BEA4500F79F3C /* CocoaExtension */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
productName = CocoaExtension;
|
||||
};
|
||||
5BA8C30228DF0360004C5CC4 /* CandidateWindow */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
productName = CandidateWindow;
|
||||
|
|
Loading…
Reference in New Issue