premake/util/sdl_file.lua
author Ryan C. Gordon
Tue, 29 Dec 2015 02:29:56 -0500
changeset 9986 081fbd89a347
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rwxr-xr-x
NetBSD: improved joystick support (thanks, Thomas!).

This patch skips non-joystick HID devices and gives joysticks on NetBSD
a human readable name.

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