local base = _G local socket = require("socket") module("murgaLua") -- -- This is where interesting functions impleted in the lua language itself go. -- -- The first two in the series are inspired by code I had seen at : -- http://www.gammon.com.au/welcome.html -- -- Nice table filter / copy function :-) -- Basically provide a function that returns true when you want an item to -- copied ... That way you can filter your table.. function filterTable (myTable, myFunction) -- Check your parameters base.assert (base.type (myTable) == "table") base.assert (base.type (myFunction) == "function") -- Work variables local myKey, myValue local resultTable = {} -- And then just a simple loop to perfom the filter/insert base.table.foreach(myTable, function (myKey, myValue) if myFunction (myValue) then resultTable[myKey] = myValue -- base.table.insert (resultTable, myValue) -- resultTable.__public[# resultTable] = myKey end end ) -- Return the new table return resultTable end -- This is pretty much a copy of what I saw at the gammon web-site ... -- Just a nice little debig print thing :-) function printDebug (myTable, indent, done) done = done or {} indent = indent or 0 if base.type(myTable) == "table" then for key, value in base.pairs (myTable) do base.io.write(base.string.rep (" ", indent)) -- indent it if base.type (value) == "table" and not done [value] then done [value] = true base.io.write(base.string.format("[%s] => table\n", base.tostring (key))); base.io.write(base.string.rep (" ", indent+4)) -- indent it base.io.write("(\n"); printDebug (value, indent + 7, done) base.io.write(base.string.rep (" ", indent+4)) -- indent it base.io.write(")\n"); else base.io.write(base.string.format("[%s] => %s\n", base.tostring (key),value)) end end else base.io.write(myTable .. "\n") end end function findXmlNode (xmlTable, elementPath) currentNode = xmlTable for currentElement in base.string.gmatch(elementPath, "([^/]+)") do currentNode = findNextXmlNode(currentNode, currentElement) if not currentNode then break end end return currentNode end function findNextXmlNode (xmlTable, nodeName, instance) if not xmlTable.n then return nil end if not instance then instance = 1 end currentInstance = 1 for counter=1,xmlTable.n do if (xmlTable[counter].name == nodeName) then if (currentInstance == instance) then return xmlTable[counter] end currentInstance = currentInstance + 1 end end return nil end function exportXml(xmlNode, outFile, indent) if not indent then indent=-4 end -- Big HACK if base.type(xmlNode) == "table" then exportXmlTable(xmlNode, outFile, indent) else exportXmlString(xmlNode, outFile, indent) end end function exportXmlTable(xmlNode, outFile, indent) endChars = ">\n" if (xmlNode.n == nil) then endChars = "/>\n" end if (xmlNode.name ~= nil) then outFile:write(base.string.rep (" ", indent) .. "<" .. xmlNode.name .. exportXmlAttrs(xmlNode.attr) .. endChars) end if (xmlNode.n ~= nil) then for counter=1,xmlNode.n do exportXml(xmlNode[counter], outFile, indent+4) end end if (xmlNode.name ~= nil and xmlNode.n ~= nil) then outFile:write(base.string.rep (" ", indent) .. "\n") end end function exportXmlString(xmlNode, outFile, indent) if (base.string.find(xmlNode, " ") or base.string.find(xmlNode, "\n") or base.string.find(xmlNode, "<") or base.string.find(xmlNode, "&")) then outFile:write(base.string.rep (" ", indent) .. "\n") else outFile:write(base.string.rep (" ", indent) .. xmlNode .. "\n") end end function exportXmlAttrs(myTable, outFile) attrStr = "" if (myTable) then for key, value in base.pairs (myTable) do if (base.string.find(value, "\"") or base.string.find(value, "&")) then value = base.string.gsub(value, "&", "&") value = base.string.gsub(value, "\"", """) end attrStr = attrStr .. " " .. key .. "=\"" .. value .. "\"" end end return attrStr end -- This call is a clone of sleep that I created in the socket library function sleep (milliseconds) socket.sleepMilliseconds(milliseconds) end -- This creates a dummy time control function createFltkTimer() return base.fltk:Fl_murgaLuaTimer(0,0,0,0,"") end -- Host OS name command should be concatenated here.