Skip to content

Commit 2ccad34

Browse files
committed
Update some FileIo calls to FileHandle.
FileHandle.sandardInput and FileHandle.standardOutput are part of Foundation, and are available in Swift 3.2.
1 parent 54de7dd commit 2ccad34

File tree

2 files changed

+2
-33
lines changed

2 files changed

+2
-33
lines changed

Sources/protoc-gen-swift/FileIo.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,8 @@ class Stderr {
4646

4747
class Stdout {
4848
static func print(_ s: String) { printToFd(s, fd: 1) }
49-
static func write(bytes: Data) {
50-
bytes.withUnsafeBytes { (p: UnsafePointer<UInt8>) -> () in
51-
_ = _write(1, p, bytes.count)
52-
}
53-
}
5449
}
5550

56-
class Stdin {
57-
static func readall() -> Data? {
58-
let fd: Int32 = 0
59-
let buffSize = 1024
60-
var buff = [UInt8]()
61-
var fragment = [UInt8](repeating: 0, count: buffSize)
62-
while true {
63-
let count = read(fd, &fragment, buffSize)
64-
if count < 0 {
65-
return nil
66-
}
67-
if count < buffSize {
68-
if count > 0 {
69-
buff += fragment[0..<count]
70-
}
71-
return Data(bytes: buff)
72-
}
73-
buff += fragment
74-
}
75-
}
76-
}
77-
78-
7951
func readFileData(filename: String) throws -> Data {
8052
let url = URL(fileURLWithPath: filename)
8153
return try Data(contentsOf: url)

Sources/protoc-gen-swift/main.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ struct GeneratorPlugin {
135135
}
136136

137137
private func generateFromStdin() -> Int32 {
138-
guard let requestData = Stdin.readall() else {
139-
Stderr.print("Failed to read request")
140-
return 1
141-
}
138+
let requestData = FileHandle.standardInput.readDataToEndOfFile()
142139

143140
// Support for loggin the request. Useful when protoc/protoc-gen-swift are
144141
// being invoked from some build system/script. protoc-gen-swift supports
@@ -263,7 +260,7 @@ struct GeneratorPlugin {
263260
Stderr.print("Failure while serializing response: \(e)")
264261
return false
265262
}
266-
Stdout.write(bytes: serializedResponse)
263+
FileHandle.standardOutput.write(serializedResponse)
267264
return true
268265
}
269266

0 commit comments

Comments
 (0)