glLoadGen / modules / util.lua

local TabbedFile = require "TabbedFile"

local util = {}

function util.GetSpecFilePath()
	return FixupPath("glspecs/");
end

function util.GetDataFilePath()
	return FixupPath("data/");
end

function util.InvertTable(tbl)
	local ret = {}
	for i, val in ipairs(tbl) do
		ret[val] = true
	end
	return ret
end

--Returns two values: the base filename and the directory.
--If the base filename is nil, it doesn't have a pathname.
--If the directory is nil, then there was no directory.
function util.ParsePath(pathname)
	local base = pathname:match("([^\\/]+)$")
	local dir = pathname:match("^(.*[\\/])")
	
	return base, dir
end

local function DeepCopyTable(tbl, destTbl)
	local ret = destTbl or {}
	for key, value in pairs(tbl) do
		if(type(value) == "table") then
			if(type(ret[key]) == "table") then
				ret[key] = DeepCopyTable(value, ret[key])
			else
				ret[key] = DeepCopyTable(value)
			end
		else
			ret[key] = value
		end
	end
	return ret
end

util.DeepCopyTable = DeepCopyTable

--Ensures the given path exists. Creates the directories when they don't.
--Note: Only works if LFS is available.
--`path` should end in a directory separator.
function util.EnsurePath(path)
	local status, lfs = pcall(require, "lfs")
	
	if(not status) then return end
	
	--strip the last directory separator off.
	path = path:match("^(.+)[/\\]$")
	
	local mode, err = lfs.attributes(path, "mode")
	if(not mode) then
		local creates = {}
		local currPath = path
		repeat
			table.insert(creates, 1, currPath)
			currPath = currPath:match("(.*[/\\])[^/\\]*$")
			if(currPath) then
				currPath = currPath:match("^(.+)[/\\]$")
				mode, err = lfs.attributes(currPath, "mode")
			end
		until(mode or currPath == nil)
		
		for _, newDir in ipairs(creates) do
			assert(lfs.mkdir(newDir))
		end
	end
end

function util.CreateFile(filename, indent)
	local base, dir = util.ParsePath(filename)
	util.EnsurePath(dir)
	local hFile = assert(io.open(filename, "w"))
	return TabbedFile.TabbedFile(hFile, indent)
end


return util
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.