//
//  LKFOutputErrorlistFile.swift
//  lkfc
//
//  Created by Alexander Holzer on 01.07.18.
//  Copyright © 2018 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFOutputErrorlistFile : LKFOutputFile {
    
    public override init(context: LKFContext) {
        super.init(context: context)
    }
    
    public override var defaultfileName : String {
        return "errorlist\(context.model).csv"
    }
    
    public override func getOutput() -> [String] {
       
        var pList : OpaquePointer?, pErrorInfo : OpaquePointer?
        let lineBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:2048)
        var outputStrings = [String]()
        
        guard context.ptr != nil
        else {
            return []
        }
        pList = LibLKF.lkf_create_errorinfo (context.ptr!, 0xFFFFFFFF)
        LibLKF.lkf_errorinfo_getHeadline (pList, lineBuffer, 1024);
        outputStrings.append(String(cString:lineBuffer))
        
        let errorcount = LibLKF.lkf_errorinfo_getCount (pList);
        
        for i in 0..<errorcount
        {
            pErrorInfo = LibLKF.lkf_errorinfo_getError (pList, i);
            LibLKF.lkf_errorinfo_getErrorLine (pErrorInfo, lineBuffer, 1024);
            outputStrings.append(String(cString:lineBuffer))
        }
        
        LibLKF.lkf_free_errorinfo (pList);
        
        lineBuffer.deallocate()
      
        return outputStrings
    }
}
