local configParser = {}

--[[----------------------------------------------------------------------------
Author: Frikkie Jansen van Rensburg
Email: frikkie@kses.net or jvrfrik@gmail.com

Date: March 2017

Lua config parser for xml, java properties, and ini files. Parses the config
file into a lua table with nested structures

This is the script that the user interfaces with. It serves as a buffer to load
in the neccessary parsers only when needed.

USAGE: There are 2 main functions that the user interfaces with.

configParser.parse(file, filetype)

Params:
file = "somedir/myconfig.xml"
filetype = "xml" , "ini", "properties"

Returns:
Returns a table containing the config file's information, or nil on failure

configParser.write(file, filetype, table)

Params:
file = "somedir/myconfig.xml" -> file to write to
filetype = "xml" , "ini", "properties" -> type
table = config table that you would like to write to a config file. Ensure the
table is in the correct format for the intended filetype

See README.MD to see how to interface with the parser and the resulting table

git: http://gitlab.kses.net/frikkie/ConfigParser.git
----------------------------------------------------------------------------]]--

------------------------------------Module--------------------------------------
-- module (public) functions
-- parses the file according to the file type and returns a lua table
function configParser.parse(file, filetype)
  t = {} -- shared "pointer" to store your position in the array
  temp = {}
  local ct = {}
  -- note the parsers are loaded in only when needed
  filetype = string.upper(filetype)
  if filetype == "INI" then
    local iniParser = require("configParser.iniParser")
    status,ct = pcall(iniParser.parse,file)
  elseif filetype == "XML" then
    local xmlParser = require("configParser.xmlParser")
    status,ct = pcall(xmlParser.parse,file)
  elseif filetype == "PROPERTIES" or filetype == "PROP" then
    local propParser = require("configParser.propParser")
    status,ct = pcall(propParser.parse,file)
  else
    return nil
  end
  if status then
    return ct
  else
    print(ct)
    return nil
  end
end

function configParser.write(file, filetype, table)
  local ret = false
  filetype = string.upper(filetype)
  if filetype == "INI" then
    local iniParser = require("configParser.iniParser")
    status,ret = pcall(iniParser.write,file,table)
  elseif filetype == "XML" then
    local xmlParser = require("configParser.xmlParser")
    status,ret = pcall(xmlParser.write,file,table)
  elseif (filetype == "PROPERTIES") or (filetype == "PROP") then
    local propParser = require("configParser.propParser")
    status,ret = pcall(propParser.write,file,table)
  else
    return nil
  end
  if status then
    return ret
  else
    print(ret)
    return nil
  end
end
-----------------------------------Module-END-----------------------------------
return configParser
