premake/util/sdl_file.lua
changeset 7925 f090a47eb7f7
child 8149 681eb46b8ac4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/premake/util/sdl_file.lua	Sun Nov 10 00:38:37 2013 -0500
     1.3 @@ -0,0 +1,141 @@
     1.4 +-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.5 +--
     1.6 +-- This software is provided 'as-is', without any express or implied
     1.7 +-- warranty.  In no event will the authors be held liable for any damages
     1.8 +-- arising from the use of this software.
     1.9 +--
    1.10 +-- Permission is granted to anyone to use this software for any purpose,
    1.11 +-- including commercial applications, and to alter it and redistribute it
    1.12 +-- freely.
    1.13 +--
    1.14 +-- Meta-build system using premake created and maintained by
    1.15 +-- Benjamin Henning <b.henning@digipen.edu>
    1.16 +
    1.17 +--[[
    1.18 +sdl_file.lua
    1.19 +
    1.20 +	This function contains a wrapper for the I/O file operations, providing a few
    1.21 +	custom functions which simplify the file I/O process (especially useful for
    1.22 +	the vast amount of generation used by the meta-build system).
    1.23 +]]
    1.24 +
    1.25 +-- Given a filename and open mode (look at io.open for more information), opens
    1.26 +-- the file with various contained functions for printing to the file, writing
    1.27 +-- to the file, reading from the file, or closing the file. If the filename is
    1.28 +-- nil, then this will open a file in a special text mode. In that case, the
    1.29 +-- mode is ignored. Returned is an instanced table with all of the
    1.30 +-- aforementioned functions.
    1.31 +--
    1.32 +-- The print function is associated with textprint/fileprint, the write function
    1.33 +-- with textwrite/filewrite, the read function with fileread, and the close
    1.34 +-- function with textclose/fileclose.
    1.35 +function fileopen(file, mode)
    1.36 +	if file == nil then
    1.37 +		return { texth = "", print = textprint, write = textwrite, read = nil, close = textclose }
    1.38 +	else
    1.39 +		return { fileh = io.open(file, mode), print = fileprint, write = filewrite, read = fileread, close = fileclose }
    1.40 +	end
    1.41 +end
    1.42 +
    1.43 +-- Given a filename and file mode, reads the entire contents of the file and
    1.44 +-- returns the contents as a string.
    1.45 +function readfile(file, mode)
    1.46 +	local file = fileopen(file, mode)
    1.47 +	local content = file:read()
    1.48 +	file:close()
    1.49 +	return content
    1.50 +end
    1.51 +
    1.52 +-- Given a file, the number of tabs to indent, and a line to print, append the
    1.53 +-- line tabbed n times with an appended newline to the end of the input text.
    1.54 +function textprint(f, tabs, line)
    1.55 +	for i = 0, tabs - 1, 1 do
    1.56 +		f.texth = f.texth .. "\t"
    1.57 +	end
    1.58 +	f.texth = f.texth .. line .. "\n"
    1.59 +end
    1.60 +
    1.61 +-- Given a file, the number of tabs to indent, and a line to print, append the
    1.62 +-- line tabbed n times with an appended newline to the end of the input file.
    1.63 +function fileprint(f, tabs, line)
    1.64 +	for i = 0, tabs - 1, 1 do
    1.65 +		f.fileh:write("\t")
    1.66 +	end
    1.67 +	f.fileh:write(line .. "\n")
    1.68 +end
    1.69 +
    1.70 +-- Given a file and some text, append the text to the end of the input text.
    1.71 +function textwrite(f, text)
    1.72 +	f.texth = f.texth .. text
    1.73 +end
    1.74 +
    1.75 +-- Given a file and some text, append the text to the end of the input file.
    1.76 +function filewrite(f, text)
    1.77 +	f.fileh:write(text)
    1.78 +end
    1.79 +
    1.80 +-- Given a file, read all the contents of the file and return them as a string.
    1.81 +function fileread(file)
    1.82 +	return file.fileh:read("*all")
    1.83 +end
    1.84 +
    1.85 +-- Given a file opened in text mode, return the result of the current file
    1.86 +-- operations as a text string.
    1.87 +function textclose(file)
    1.88 +	return file.texth
    1.89 +end
    1.90 +
    1.91 +-- Given a file opened regularly, close the file handle resource, preventing
    1.92 +-- any future I/O operations.
    1.93 +function fileclose(file)
    1.94 +	file.fileh:close()
    1.95 +end
    1.96 +
    1.97 +-- Given a source path, builds a table containing all directories and recursive
    1.98 +-- subdirectories which contain files, and returns the table. Each entry in the
    1.99 +-- table will have a '/' at the end of its path, plus they will all be relative
   1.100 +-- to the parent source path. The table will contain a single entry with the
   1.101 +-- value '/' to indicate the source path itself.
   1.102 +function createDirTable(sourcePath)
   1.103 +	local dirs = os.matchdirs(sourcePath.."/**")
   1.104 +	for k,d in pairs(dirs) do
   1.105 +		dirs[k] = string.sub(d, #sourcePath + 1) .. "/"
   1.106 +	end
   1.107 +	table.insert(dirs, "/")
   1.108 +	return dirs
   1.109 +end
   1.110 +
   1.111 +-- This works like os.pathsearch, but for directories. Look at the premake
   1.112 +-- documentation for os.pathsearch for more information.
   1.113 +os.dirpathsearch = function(subdir, path, path_delimiter)
   1.114 +	for i,p in ipairs(explode(path, path_delimiter)) do
   1.115 +		local needle = p .. "/" .. subdir
   1.116 +		if os.isdir(needle) then
   1.117 +			return needle
   1.118 +		end
   1.119 +	end
   1.120 +	return nil
   1.121 +end
   1.122 +
   1.123 +-- Given a variable number of environmental variable names, this will join them
   1.124 +-- together based on the current OS path delimeter and quietly ignoring those
   1.125 +-- variables which do not exist on this system. The resulting path is always
   1.126 +-- normalized for Unix-based path separators, regardless of the system.
   1.127 +os.getenvpath = function(...)
   1.128 +	local path = ""
   1.129 +	local pathDelimeter = ":"
   1.130 +	if os.is("windows") then
   1.131 +		pathDelimeter = ";"
   1.132 +	end
   1.133 +	for i,a in ipairs(arg) do
   1.134 +		local value = os.getenv(a)
   1.135 +		if value then
   1.136 +			if #path > 0 then
   1.137 +				path = path .. pathDelimeter
   1.138 +			end
   1.139 +			path = path .. value
   1.140 +		end
   1.141 +	end
   1.142 +	-- normalize path to unix
   1.143 +	return path:gsub("\\", "/"):gsub("//", "/")
   1.144 +end
   1.145 \ No newline at end of file