Commit 04bc5916 authored by Drew's avatar Drew

Upgrade to Swift Preview 6

parent 09eb6280
Pipeline #2462 failed with stage
in 3 minutes and 18 seconds
......@@ -14,7 +14,7 @@
/// Time interval
public struct TimeInterval {
private let interval: Int
fileprivate let interval: Int
/// Initialize on second scale
///
......
......@@ -132,10 +132,17 @@ public class File {
/// - Parameter binary: optional, open in binary mode, defaults to text mode
public convenience init(tempFileAtPath path: Path, prefix: String, binary: Bool = false) throws {
let p = path.appending(prefix + ".XXXXXXX")
var buf = Array(p.description.utf8)
buf.append(0)
let fd = mkstemp(UnsafeMutablePointer(buf))
if let filename = String(validatingUTF8: UnsafeMutablePointer(buf)) {
var buf = p.description.utf8CString
let fd = buf.withUnsafeMutableBufferPointer {
mkstemp($0.baseAddress)
}
let filename_ = buf.withUnsafeBufferPointer { (ptr) -> String? in
if let o = ptr.baseAddress {
return String(cString: o)
}
return nil
}
if let filename = filename_ {
try self.init(fd: fd, mode: .ReadAndWrite, binary: binary, takeOwnership: true)
self.path = Path(filename)
} else {
......@@ -166,7 +173,9 @@ public class File {
public func read(size: Int) throws -> String? {
var buffer:[UInt8] = try self.read(size: size)
buffer.append(0)
return String(validatingUTF8: UnsafePointer<CChar>(buffer))
return buffer.withUnsafeBufferPointer {
String(cString: $0.baseAddress!)
}
}
/// Read bytes from file
......@@ -175,7 +184,7 @@ public class File {
/// - Returns: UInt8 array with bytes
public func read(size: Int) throws -> [UInt8] {
var buffer = [UInt8](repeating: 0, count: size)
let read = fread(UnsafeMutablePointer(buffer), 1, size, self.fp)
let read = fread(UnsafeMutablePointer(mutating: buffer), 1, size, self.fp)
if read == 0 {
if feof(self.fp) == 0 {
throw SysError(errno: errno)
......@@ -195,7 +204,9 @@ public class File {
public func readAll() throws -> String? {
var buffer:[UInt8] = try self.readAll()
buffer.append(0)
return String(validatingUTF8: UnsafePointer<CChar>(buffer))
return buffer.withUnsafeBufferPointer {
String(cString: $0.baseAddress!)
}
}
/// Read complete file
......@@ -217,8 +228,11 @@ public class File {
///
/// - Returns: String read from file (newline included) if valid UTF-8 or nil
public func readLine() throws -> String? {
let buffer = [UInt8](repeating: 0, count: 64 * 1024 + 1)
let read = fgets(UnsafeMutablePointer(buffer), 64 * 1024, self.fp)
var buffer = [UInt8](repeating: 0, count: 64 * 1024 + 1)
let read = buffer.withUnsafeMutableBufferPointer() { (ptr_) -> UnsafeMutablePointer<Int8>? in
let ptr = UnsafeMutableRawPointer(ptr_.baseAddress)!
return fgets(ptr.assumingMemoryBound(to: Int8.self), 64 * 1024, self.fp)
}
if read == nil {
if feof(self.fp) == 0 {
throw SysError(errno: errno)
......@@ -226,7 +240,9 @@ public class File {
throw SysError.EndOfFile
}
}
return String(validatingUTF8: UnsafePointer<CChar>(buffer))
return buffer.withUnsafeBufferPointer {
return String(cString: $0.baseAddress!)
}
}
/// Write a string to the file
......
......@@ -101,7 +101,7 @@ public enum FileType: Mode_t {
case Socket = 12
case Whiteout = 14
private init?(statMode: Mode_t) {
fileprivate init?(statMode: Mode_t) {
self.init(rawValue: (statMode & 0xf000) >> 12)
}
}
......
......@@ -409,7 +409,7 @@ public class FS {
let result = rename(from.description, to.description)
if result < 0 {
let error = SysError(errno: errno, to, from)
if case .CrossDeviceLink = error where !atomic {
if case .CrossDeviceLink = error, !atomic {
try FS.copyItem(from: from, to: to, recursive: true)
try FS.removeItem(path: from, recursive: true)
} else {
......@@ -445,12 +445,20 @@ public class FS {
/// - Returns: path to the already created directory
public class func temporaryDirectory(prefix: String = "tempdir") throws -> Path {
let p = Path.tempDirectory().appending(prefix + ".XXXXXXX")
var buf = Array(p.description.utf8)
buf.append(0)
if mkdtemp(UnsafeMutablePointer(buf)) == nil {
var buf = p.description.utf8CString
let result = buf.withUnsafeMutableBufferPointer {
mkdtemp($0.baseAddress)
}
if result == nil {
throw SysError(errno: errno, p)
}
if let dirname = String(validatingUTF8: UnsafeMutablePointer(buf)) {
let dirname_ = buf.withUnsafeBufferPointer { (ptr) -> String? in
if let o = ptr.baseAddress {
return String(cString: o)
}
return nil
}
if let dirname = dirname_ {
return Path(dirname)
} else {
throw SysError.UnknownError
......
......@@ -105,15 +105,15 @@ public class StdErrLogger: LoggerProcotol {
public func log(_ level: Log.LogLevel, _ msg: [Any], date: Date?, file: String?, line: Int?, function: String?) {
self.colorize(stderr, level)
if let date = date where self.logDate {
if let date = date, self.logDate {
self._fwrite(stderr, "\(date.isoDateString!) ")
}
if let file = file, line = line where self.logFileAndLine || level == .Fatal {
if let file = file, let line = line, self.logFileAndLine || level == .Fatal {
self._fwrite(stderr, "\(file):\(line): ")
}
for item in msg {
self._fwrite(stderr, String(item) + " ")
self._fwrite(stderr, String(describing: item) + " ")
}
self.colorize(stderr, nil)
self._fwrite(stderr, "\n")
......@@ -132,15 +132,15 @@ public class StdOutLogger: LoggerProcotol {
public func log(_ level: Log.LogLevel, _ msg: [Any], date: Date?, file: String?, line: Int?, function: String?) {
self.colorize(stdout, level)
if let date = date where self.logDate {
if let date = date, self.logDate {
self._fwrite(stdout, "\(date.isoDateString!) ")
}
if let file = file, line = line where self.logFileAndLine || level == .Fatal {
if let file = file, let line = line, self.logFileAndLine || level == .Fatal {
self._fwrite(stdout, "\(file):\(line): ")
}
for item in msg {
self._fwrite(stdout, String(item) + " ")
self._fwrite(stdout, String(describing: item) + " ")
}
self.colorize(stdout, nil)
self._fwrite(stdout, "\n")
......@@ -197,16 +197,16 @@ public class FileLogger: LoggerProcotol {
public func log(_ level: Log.LogLevel, _ msg: [Any], date: Date?, file: String?, line: Int?, function: String?) {
if let logFile = self.logFile {
do {
if let date = date where self.logDate {
if let date = date, self.logDate {
try date.isoDateString!.write(to: logFile)
}
if let file = file, line = line where self.logFileAndLine || level == .Fatal {
if let file = file, let line = line, self.logFileAndLine || level == .Fatal {
try " [\(level)] \(file):\(line): ".write(to: logFile)
} else {
try " [\(level)]: ".write(to: logFile)
}
for item in msg {
try (String(item) + " ").write(to: logFile)
try (String(describing: item) + " ").write(to: logFile)
}
try "\n".write(to: logFile)
logFile.flush()
......
......@@ -18,7 +18,7 @@ public extension String {
///
/// - Parameter path: a path
public init(path: Path) {
self.init(path.description)
self.init(path.description)!
}
/// Load a string from a file
......
......@@ -21,10 +21,10 @@
// So apparently this is how you access a fixed length C array from
// an imported struct -.-
public func fixedCArrayToArray<T, E>(data: inout T) -> [E] {
return withUnsafePointer(&data) { ptr -> [E] in
return withUnsafePointer(to: &data) { ptr -> [E] in
let buffer = UnsafeBufferPointer(
start: unsafeBitCast(ptr, to: UnsafePointer<E>.self),
count: sizeofValue(data)
count: MemoryLayout<T>.size
)
return [E](buffer)
}
......
......@@ -83,7 +83,7 @@ public struct URL {
case .Schema(let data):
var schema: String? = nil
(newState, schema) = self._parseSchema(character, data)
if let schema = schema where schema.characters.count > 0 {
if let schema = schema, schema.characters.count > 0 {
self.schema = schema
}
case .AfterSchema:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment