1--
2-- io.lua
3-- Additions to the I/O namespace.
4-- Copyright (c) 2008-2014 Jason Perkins and the Premake project
5--
6
7
8--
9-- Open an overload of the io.open() function, which will create any missing
10-- subdirectories in the filename if "mode" is set to writeable.
11--
12
13	premake.override(io, "open", function(base, fname, mode)
14		if mode and (mode:find("w") or mode:find("a"))  then
15			local dir = path.getdirectory(fname)
16			ok, err = os.mkdir(dir)
17			if not ok then
18				error(err, 0)
19			end
20		end
21		return base(fname, mode)
22	end)
23
24
25--
26-- Write content to a new file.
27--
28	function io.writefile(filename, content)
29		local file = io.open(filename, "w+b")
30		if file then
31			file:write(content)
32			file:close()
33			return true
34		end
35	end
36
37--
38-- Read content from new file.
39--
40	function io.readfile(filename)
41		local file = io.open(filename, "rb")
42		if file then
43			local content = file:read("*a")
44			file:close()
45			return content
46		end
47	end