 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 //
+
+typealias Offset = off_t
+typealias ByteCount = ssize_t
 
 class BinaryFile {
     let fileDescriptor: CInt

        }
    }
    
    func readBuffer(buffer: ByteBuffer, inout error: Int) -> Int? {
        let bytesRead = read(fileDescriptor, buffer.data + buffer.position, UInt(buffer.remaining))
        
        if (bytesRead < 0) {
            // error!
            error = Int(errno)
            return nil
        } else if (bytesRead == 0) {
            // EOF
            return -1
        } else {
            buffer.position += bytesRead
            return bytesRead
        }
    }
    
    func writeBuffer(buffer: ByteBuffer, inout error: Int) -> Int? {
        let bytesWritten = write(fileDescriptor, buffer.data + buffer.position, UInt(buffer.remaining))
        
        if (bytesWritten < 0) {
            error = Int(errno)
            return nil
        } else {
            buffer.position += bytesWritten

        }
    }
    
    func seekFromStart(offset: Offset, inout error: Error) -> Offset? {
        let offset = lseek(fileDescriptor, off_t(offset), SEEK_SET)
        if (offset < 0) {
            error = Error(code: Int(errno))
            return nil
        } else {
            return offset
        }
    }
    
    func seekFromCurrent(offset: Offset, inout error: Error) -> Offset? {
        let offset = lseek(fileDescriptor, off_t(offset), SEEK_CUR)
        if (offset < 0) {
            error = Error(code: Int(errno))
            return nil
        } else {
            return offset
        }
    }
    
    func seekFromEnd(offset: Offset, inout error: Error) -> Offset? {
        let offset = lseek(fileDescriptor, off_t(offset), SEEK_END)
        if (offset < 0) {
            error = Error(code: Int(errno))
            return nil
        } else {
            return offset
        }
    }
    
    struct Error {
        let code: Int
    }
}
