Swift for Xojo Developers : Part 4 - TextOutputFile

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
        }
    }
}
2 Likes

What makes you think it hasn’t garnered any interest? This is a small community so far, and I’m (a) a Swift newbie, and (b) currently very busy with non-programming tasks. But I am very interested and curious how this will go.

Well then … .that being said… I will decide what the next one will be…

Graphics?
Devices?
SQLite [this is a complex one]
Strings? [encapulates into Xojo familar syntax]
Math? [same reason as String]

Sorry if you think there’s a lack of enthusiasm @DaveS, I think the classes are really good and I’ve bookmarked them. I’m currently writing a Xojo physics engine (another one) so haven’t got any free time to play with Swift at the moment.

Graphics +1