0001    // File.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    #if os(Linux)
0026    	import Glibc
0027    #else
0028    	import Darwin.C
0029    #endif
0030    
0031    public class File {
0032    	
0033    	public enum Error
File.swift:52
		guard fp != nil else { throw Error.OpenError(String.fromCString(strerror(errno)) ?? "") }
File.swift:61
		guard count == data.length else { throw Error.WriteError(String.fromCString(strerror(ferror(fp))) ?? "") }
File.swift:71
			guard ferror(fp) == 0 else { throw Error.ReadError(String.fromCString(strerror(ferror(fp))) ?? "") }
: ErrorType { 0034 case OpenError
File.swift:52
		guard fp != nil else { throw Error.OpenError(String.fromCString(strerror(errno)) ?? "") }
(String) 0035 case ReadError
File.swift:71
			guard ferror(fp) == 0 else { throw Error.ReadError(String.fromCString(strerror(ferror(fp))) ?? "") }
(String) 0036 case WriteError
File.swift:61
		guard count == data.length else { throw Error.WriteError(String.fromCString(strerror(ferror(fp))) ?? "") }
(String) 0037 } 0038 0039 public enum Mode
File.swift:50
	public init(path: String, mode: Mode = .ReadUpdate) throws {
: String { 0040 case Read = "r" // Open file for reading 0041 case Write = "w" // Truncate to zero length or create file for writing 0042 case Append = "a" // Append; open or create file for writing at end-of-file 0043 case ReadUpdate
File.swift:50
	public init(path: String, mode: Mode = .ReadUpdate) throws {
= "r+" // Open file for update (reading and writing) 0044 case WriteUpdate = "w+" // Truncate to zero length or create file for update 0045 case AppendUpdate = "a+" // Append; open or create file for update, writing at end-of-file 0046 } 0047 0048 private let fp
File.swift:51
		fp = fopen(path, mode.rawValue)
File.swift:52
		guard fp != nil else { throw Error.OpenError(String.fromCString(strerror(errno)) ?? "") }
File.swift:60
		let count = fwrite(data.uBytes, 1, data.length, fp)
File.swift:61
		guard count == data.length else { throw Error.WriteError(String.fromCString(strerror(ferror(fp))) ?? "") }
File.swift:70
			let count = fread(buffer, 1, min(remaining, 1024), fp)
File.swift:71
			guard ferror(fp) == 0 else { throw Error.ReadError(String.fromCString(strerror(ferror(fp))) ?? "") }
File.swift:71
			guard ferror(fp) == 0 else { throw Error.ReadError(String.fromCString(strerror(ferror(fp))) ?? "") }
File.swift:75
		} while remaining > 0 && feof(fp) == 0
File.swift:80
		if fp != nil {
File.swift:81
			fclose(fp)
: UnsafeMutablePointer<FILE> 0049 0050 public init(path: String, mode: Mode = .ReadUpdate) throws { 0051 fp = fopen(path, mode.rawValue) 0052 guard fp != nil else { throw Error.OpenError(String.fromCString(strerror(errno)) ?? "") } 0053 } 0054 0055 deinit { 0056 close() 0057 } 0058 0059 public func write(data: Data) throws { 0060 let count = fwrite(data.uBytes, 1, data.length, fp) 0061 guard count == data.length else { throw Error.WriteError(String.fromCString(strerror(ferror(fp))) ?? "") } 0062 } 0063 0064 public func read(length length: Int = Int.max) throws -> Data { 0065 var bytes: [UInt8] = [] 0066 var remaining = length 0067 let buffer = UnsafeMutablePointer<UInt8>.alloc(1024) 0068 defer { buffer.dealloc(1024) } 0069 repeat { 0070 let count = fread(buffer, 1, min(remaining, 1024), fp) 0071 guard ferror(fp) == 0 else { throw Error.ReadError(String.fromCString(strerror(ferror(fp))) ?? "") } 0072 guard count > 0 else { continue } 0073 bytes += Array(UnsafeBufferPointer(start: buffer, count: count).generate()).prefix(count) 0074 remaining -= count 0075 } while remaining > 0 && feof(fp) == 0 0076 return Data(uBytes: bytes) 0077 } 0078 0079 public func close
File.swift:56
		close()
() { 0080 if fp != nil { 0081 fclose(fp) 0082 } 0083 } 0084 0085 } 0086