summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/lib/swift/Sources/TFileTransport.swift
blob: fe2253da4c6d41718c5b9ed576f67283526b298e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import Foundation

#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
  import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
  import Glibc
#endif

/// TFileTransport
/// Foundation-less Swift File transport.
/// Uses C fopen/fread/fwrite,
/// provided by Glibc in linux and Darwin on OSX/iOS
public class TFileTransport: TTransport {
  var fileHandle: UnsafeMutablePointer<FILE>? = nil
  
  public init (fileHandle: UnsafeMutablePointer<FILE>) {
    self.fileHandle = fileHandle
  }

  public convenience init(filename: String) throws {
    var fileHandle: UnsafeMutablePointer<FILE>?
    filename.withCString({ cFilename in
      "rw".withCString({ cMode in
        fileHandle = fopen(cFilename, cMode)
      })
    })
    if let fileHandle = fileHandle {
      self.init(fileHandle: fileHandle)
    } else {
      throw TTransportError(error: .notOpen)
    }
  }
  
  deinit {
    fclose(self.fileHandle)
  }
  
  public func readAll(size: Int) throws -> Data {
    let read = try self.read(size: size)
    
    if read.count != size {
      throw TTransportError(error: .endOfFile)
    }
    return read
  }
  
  public func read(size: Int) throws -> Data {
    // set up read buffer, position 0
    var read = Data(capacity: size)
    var position = 0
    
    // read character buffer
    var nextChar: UInt8 = 0
    
    // continue until we've read size bytes
    while read.count < size {
      if fread(&nextChar, 1, 1, self.fileHandle) == 1 {
        read[position] = nextChar

        // Increment output byte pointer
        position += 1
        
      } else {
        throw TTransportError(error: .endOfFile)
      }
    }
    return read
  }
  
  public func write(data: Data) throws {
    let bytesWritten = data.withUnsafeBytes {
      fwrite($0, 1, data.count, self.fileHandle)
    }
    if bytesWritten != data.count {
      throw TTransportError(error: .unknown)
    }
  }
  
  public func flush() throws {
    return
  }
}