io.lua 934 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. --
  2. -- io.lua
  3. -- Additions to the I/O namespace.
  4. -- Copyright (c) 2008-2014 Jason Perkins and the Premake project
  5. --
  6. --
  7. -- Open an overload of the io.open() function, which will create any missing
  8. -- subdirectories in the filename if "mode" is set to writeable.
  9. --
  10. premake.override(io, "open", function(base, fname, mode)
  11. if mode and (mode:find("w") or mode:find("a")) then
  12. local dir = path.getdirectory(fname)
  13. ok, err = os.mkdir(dir)
  14. if not ok then
  15. error(err, 0)
  16. end
  17. end
  18. return base(fname, mode)
  19. end)
  20. --
  21. -- Write content to a new file.
  22. --
  23. function io.writefile(filename, content)
  24. local file = io.open(filename, "w+b")
  25. if file then
  26. file:write(content)
  27. file:close()
  28. return true
  29. end
  30. end
  31. --
  32. -- Read content from new file.
  33. --
  34. function io.readfile(filename)
  35. local file = io.open(filename, "rb")
  36. if file then
  37. local content = file:read("*a")
  38. file:close()
  39. return content
  40. end
  41. end