]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | import Foundation | |
21 | ||
22 | #if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) | |
23 | import Darwin | |
24 | #elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) | |
25 | import Glibc | |
26 | #endif | |
27 | ||
28 | /// TFileTransport | |
29 | /// Foundation-less Swift File transport. | |
30 | /// Uses C fopen/fread/fwrite, | |
31 | /// provided by Glibc in linux and Darwin on OSX/iOS | |
32 | public class TFileTransport: TTransport { | |
33 | var fileHandle: UnsafeMutablePointer<FILE>? = nil | |
34 | ||
35 | public init (fileHandle: UnsafeMutablePointer<FILE>) { | |
36 | self.fileHandle = fileHandle | |
37 | } | |
38 | ||
39 | public convenience init(filename: String) throws { | |
40 | var fileHandle: UnsafeMutablePointer<FILE>? | |
41 | filename.withCString({ cFilename in | |
42 | "rw".withCString({ cMode in | |
43 | fileHandle = fopen(cFilename, cMode) | |
44 | }) | |
45 | }) | |
46 | if let fileHandle = fileHandle { | |
47 | self.init(fileHandle: fileHandle) | |
48 | } else { | |
49 | throw TTransportError(error: .notOpen) | |
50 | } | |
51 | } | |
52 | ||
53 | deinit { | |
54 | fclose(self.fileHandle) | |
55 | } | |
56 | ||
57 | public func readAll(size: Int) throws -> Data { | |
58 | let read = try self.read(size: size) | |
59 | ||
60 | if read.count != size { | |
61 | throw TTransportError(error: .endOfFile) | |
62 | } | |
63 | return read | |
64 | } | |
65 | ||
66 | public func read(size: Int) throws -> Data { | |
67 | // set up read buffer, position 0 | |
68 | var read = Data(capacity: size) | |
69 | var position = 0 | |
70 | ||
71 | // read character buffer | |
72 | var nextChar: UInt8 = 0 | |
73 | ||
74 | // continue until we've read size bytes | |
75 | while read.count < size { | |
76 | if fread(&nextChar, 1, 1, self.fileHandle) == 1 { | |
77 | read[position] = nextChar | |
78 | ||
79 | // Increment output byte pointer | |
80 | position += 1 | |
81 | ||
82 | } else { | |
83 | throw TTransportError(error: .endOfFile) | |
84 | } | |
85 | } | |
86 | return read | |
87 | } | |
88 | ||
89 | public func write(data: Data) throws { | |
90 | let bytesWritten = data.withUnsafeBytes { | |
91 | fwrite($0, 1, data.count, self.fileHandle) | |
92 | } | |
93 | if bytesWritten != data.count { | |
94 | throw TTransportError(error: .unknown) | |
95 | } | |
96 | } | |
97 | ||
98 | public func flush() throws { | |
99 | return | |
100 | } | |
101 | } |