You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.5 KiB

  1. -- 扩展io一些功能
  2. function io.exists(path)
  3. local file = io.open(path, "r")
  4. if file then
  5. io.close(file)
  6. return true
  7. end
  8. return false
  9. end
  10. function io.readfile(path)
  11. local file = io.open(path, "r")
  12. if file then
  13. local content = file:read("*a")
  14. io.close(file)
  15. return content
  16. end
  17. return nil
  18. end
  19. function io.writefile(path, content, mode)
  20. mode = mode or "w+b"
  21. local file = io.open(path, mode)
  22. if file then
  23. if file:write(content) == nil then return false end
  24. io.close(file)
  25. return true
  26. else
  27. return false
  28. end
  29. end
  30. function io.pathinfo(path)
  31. local pos = string.len(path)
  32. local extpos = pos + 1
  33. while pos > 0 do
  34. local b = string.byte(path, pos)
  35. if b == 46 then -- 46 = char "."
  36. extpos = pos
  37. elseif b == 47 then -- 47 = char "/"
  38. break
  39. end
  40. pos = pos - 1
  41. end
  42. local dirname = string.sub(path, 1, pos)
  43. local filename = string.sub(path, pos + 1)
  44. extpos = extpos - pos
  45. local basename = string.sub(filename, 1, extpos - 1)
  46. local extname = string.sub(filename, extpos)
  47. return {
  48. dirname = dirname,
  49. filename = filename,
  50. basename = basename,
  51. extname = extname
  52. }
  53. end
  54. function io.filesize(path)
  55. local size = false
  56. local file = io.open(path, "r")
  57. if file then
  58. local current = file:seek()
  59. size = file:seek("end")
  60. file:seek("set", current)
  61. io.close(file)
  62. end
  63. return size
  64. end