This class completes the Filemanager Library I use.
But I guess I will hold off on posting anymore of the libraries I have, as so far this hasn’t seemed to garner any interest.
/*
+------------+----------------------------------------------------------------+
| Name | textOutputFile [textoutputstream already taken by Swift] |
+------------+----------------------------------------------------------------+
| Type | class |
+------------+----------------------------------------------------------------+
| Properties| lastErrorCode writeError |
+------------+----------------------------------------------------------------+
| Methods| append Create writeLine |
| | close write |
+------------+----------------------------------------------------------------+
*/
class textOutputFile {
private var fileHandle : FileHandle?
private var zIsOpen : Bool = false
required init() {
zIsOpen=false
ThrowErrorMessage( .OK )
}
public func Append(_ f : folderitem ) {
open_the_file(f, false)
fileHandle?.seekToEndOfFile()
}
public func Create(_ f : folderitem ) { open_the_file(f, true) }
public var lastErrorCode : Int { get { return zLastErrorCode } }
public var writeError : Bool { get { return zLastErrorCode != 0 } }
public var lastErrorMessage : String { get { return zLastErrorMsg } }
public func close() {
if zIsOpen {
fileHandle?.closeFile()
zIsOpen=false
}
}
public func writeLine(_ data : String) { write("\(data)\n") }
public func write(_ data : String) {
if zIsOpen {
fileHandle?.seekToEndOfFile()
let out_data = data.data(using: String.Encoding.utf8, allowLossyConversion: false)!
fileHandle?.write(out_data)
} else {
ThrowErrorMessage( .TEXTOUT)
}
}
//
// ------ P R I V A T E F U N C T I O N S ------
//
private func open_the_file(_ f : folderitem,_ delete_existing:Bool) {
var NeedNewFile : Bool = !f.exists
zIsOpen=true
if f.exists==true && delete_existing==true {
f.Delete()
NeedNewFile = true
}
if NeedNewFile {
fileMANAGER.createFile(atPath: f.path, contents: nil, attributes: nil)
}
fileHandle = FileHandle(forWritingAtPath: f.path)!
if fileHandle==nil {
ThrowErrorMessage( .BADFILE)
zIsOpen=false
}
}
}