sdl_file.lua 4.8 KB

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