1 -- Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
7 -- Permission is granted to anyone to use this software for any purpose,
8 -- including commercial applications, and to alter it and redistribute it
11 -- Meta-build system using premake created and maintained by
12 -- Benjamin Henning <b.henning@digipen.edu>
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).
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.
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)
34 return { texth = "", print = textprint, write = textwrite, read = nil, close = textclose }
36 return { fileh = io.open(file, mode), print = fileprint, write = filewrite, read = fileread, close = fileclose }
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()
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"
55 f.texth = f.texth .. line .. "\n"
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
64 f.fileh:write(line .. "\n")
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
72 -- Given a file and some text, append the text to the end of the input file.
73 function filewrite(f, text)
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")
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)
88 -- Given a file opened regularly, close the file handle resource, preventing
89 -- any future I/O operations.
90 function fileclose(file)
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) .. "/"
104 table.insert(dirs, "/")
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
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(...)
126 local pathDelimeter = ":"
127 if os.is("windows") then
130 for i,a in ipairs(arg) do
131 local value = os.getenv(a)
134 path = path .. pathDelimeter
139 -- normalize path to unix
140 return path:gsub("\\", "/"):gsub("//", "/")