Commits

Anonymous committed 3bc314c

Conditional LFS support now exists.

Comments (0)

Files changed (3)

modules/CommonStyle.lua

 --[[Useful style utility functions. This file will contain commonly useful strings and functions that generate useful data.]]
 
-local TabbedFile = require "TabbedFile"
 local util = require "util"
 local struct = require "Structure"
 
 
 --Creates a tabbed file.
 function common.CreateFile(filename, indent)
-	local hFile = io.open(filename, "w")
-	return TabbedFile.TabbedFile(hFile, indent)
+	return util.CreateFile(filename, indent)
 end
 
 --Retrieves the common typedefs used by OpenGL 1.1.

modules/Structure.lua

 
 	local filename = self:CallFunction(context)
 	
-	local hFile = io.open(filename, "w")
-	context.hFile = TabbedFile.TabbedFile(hFile, context.options.indent)
+	context.hFile = util.CreateFile(filename, context.options.indent)
 end
 
 function fileAction:PostProcess(context)
 local blockAction = {}
 
 function blockAction:PreProcess(context)
-	assert(context.hFile, "Cannot write a block outside of a file.")
+	assert(context.hFile, "Cannot write a block outside of a file. " .. self.name)
 	self:CallFunction(context, "WriteBlockBegin" .. self.name)
 end
 
+
+local TabbedFile = require "TabbedFile"
 
 local util = {}
 
 
 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
+	
+	local mode = 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
+				mode = 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