premake/util/sdl_file.lua
author Sam Lantinga
Thu, 01 Sep 2016 01:26:56 -0700
changeset 10304 ee83e0b4a36f
parent 9998 f67cf37e9cd4
permissions -rwxr-xr-x
wayland: Add support for relative mouse mode, by Jonas Ådahl <jadahl@gmail.com>

Generate the C protocol files from the protocol XML files installed by
wayland-protocols, and use them to implement support for relative pointer
motions and pointer locking.

Note that at the time, the protocol is unstable and may change in the future.
Any future breaking changes will, however, fail gracefully and result in no
regressions compared to before this patch.
     1 -- Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     2 --
     3 -- This software is provided 'as-is', without any express or implied
     4 -- warranty.  In no event will the authors be held liable for any damages
     5 -- arising from the use of this software.
     6 --
     7 -- Permission is granted to anyone to use this software for any purpose,
     8 -- including commercial applications, and to alter it and redistribute it
     9 -- freely.
    10 --
    11 -- Meta-build system using premake created and maintained by
    12 -- Benjamin Henning <b.henning@digipen.edu>
    13 
    14 --[[
    15 sdl_file.lua
    16 
    17 	This function contains a wrapper for the I/O file operations, providing a few
    18 	custom functions which simplify the file I/O process (especially useful for
    19 	the vast amount of generation used by the meta-build system).
    20 ]]
    21 
    22 -- Given a filename and open mode (look at io.open for more information), opens
    23 -- the file with various contained functions for printing to the file, writing
    24 -- to the file, reading from the file, or closing the file. If the filename is
    25 -- nil, then this will open a file in a special text mode. In that case, the
    26 -- mode is ignored. Returned is an instanced table with all of the
    27 -- aforementioned functions.
    28 --
    29 -- The print function is associated with textprint/fileprint, the write function
    30 -- with textwrite/filewrite, the read function with fileread, and the close
    31 -- function with textclose/fileclose.
    32 function fileopen(file, mode)
    33 	if file == nil then
    34 		return { texth = "", print = textprint, write = textwrite, read = nil, close = textclose }
    35 	else
    36 		return { fileh = io.open(file, mode), print = fileprint, write = filewrite, read = fileread, close = fileclose }
    37 	end
    38 end
    39 
    40 -- Given a filename and file mode, reads the entire contents of the file and
    41 -- returns the contents as a string.
    42 function readfile(file, mode)
    43 	local file = fileopen(file, mode)
    44 	local content = file:read()
    45 	file:close()
    46 	return content
    47 end
    48 
    49 -- Given a file, the number of tabs to indent, and a line to print, append the
    50 -- line tabbed n times with an appended newline to the end of the input text.
    51 function textprint(f, tabs, line)
    52 	for i = 0, tabs - 1, 1 do
    53 		f.texth = f.texth .. "\t"
    54 	end
    55 	f.texth = f.texth .. line .. "\n"
    56 end
    57 
    58 -- Given a file, the number of tabs to indent, and a line to print, append the
    59 -- line tabbed n times with an appended newline to the end of the input file.
    60 function fileprint(f, tabs, line)
    61 	for i = 0, tabs - 1, 1 do
    62 		f.fileh:write("\t")
    63 	end
    64 	f.fileh:write(line .. "\n")
    65 end
    66 
    67 -- Given a file and some text, append the text to the end of the input text.
    68 function textwrite(f, text)
    69 	f.texth = f.texth .. text
    70 end
    71 
    72 -- Given a file and some text, append the text to the end of the input file.
    73 function filewrite(f, text)
    74 	f.fileh:write(text)
    75 end
    76 
    77 -- Given a file, read all the contents of the file and return them as a string.
    78 function fileread(file)
    79 	return file.fileh:read("*all")
    80 end
    81 
    82 -- Given a file opened in text mode, return the result of the current file
    83 -- operations as a text string.
    84 function textclose(file)
    85 	return file.texth
    86 end
    87 
    88 -- Given a file opened regularly, close the file handle resource, preventing
    89 -- any future I/O operations.
    90 function fileclose(file)
    91 	file.fileh:close()
    92 end
    93 
    94 -- Given a source path, builds a table containing all directories and recursive
    95 -- subdirectories which contain files, and returns the table. Each entry in the
    96 -- table will have a '/' at the end of its path, plus they will all be relative
    97 -- to the parent source path. The table will contain a single entry with the
    98 -- value '/' to indicate the source path itself.
    99 function createDirTable(sourcePath)
   100 	local dirs = os.matchdirs(sourcePath.."/**")
   101 	for k,d in pairs(dirs) do
   102 		dirs[k] = string.sub(d, #sourcePath + 1) .. "/"
   103 	end
   104 	table.insert(dirs, "/")
   105 	return dirs
   106 end
   107 
   108 -- This works like os.pathsearch, but for directories. Look at the premake
   109 -- documentation for os.pathsearch for more information.
   110 os.dirpathsearch = function(subdir, path, path_delimiter)
   111 	for i,p in ipairs(explode(path, path_delimiter)) do
   112 		local needle = p .. "/" .. subdir
   113 		if os.isdir(needle) then
   114 			return needle
   115 		end
   116 	end
   117 	return nil
   118 end
   119 
   120 -- Given a variable number of environmental variable names, this will join them
   121 -- together based on the current OS path delimeter and quietly ignoring those
   122 -- variables which do not exist on this system. The resulting path is always
   123 -- normalized for Unix-based path separators, regardless of the system.
   124 os.getenvpath = function(...)
   125 	local path = ""
   126 	local pathDelimeter = ":"
   127 	if os.is("windows") then
   128 		pathDelimeter = ";"
   129 	end
   130 	for i,a in ipairs(arg) do
   131 		local value = os.getenv(a)
   132 		if value then
   133 			if #path > 0 then
   134 				path = path .. pathDelimeter
   135 			end
   136 			path = path .. value
   137 		end
   138 	end
   139 	-- normalize path to unix
   140 	return path:gsub("\\", "/"):gsub("//", "/")
   141 end