cleanup.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -- Lua script used to clean up tabs and spaces in C, CPP and H files.
  2. -- Copyright (c) 2014, bel
  3. -- MIT License (http://opensource.org/licenses/mit-license.php)
  4. --
  5. -- It can be used from the command line:
  6. -- Call Lua5.1 or Lua5.2 + this script file + the C/CPP/H file to clean
  7. --
  8. -- It can be used in Visual Studio as an external tool:
  9. -- command: Lua5.1.exe or Lua5.2.exe
  10. -- argument: "X:\civetweb\resources\cleanup.lua" $(ItemPath)
  11. --
  12. clean = arg[1]
  13. print("Cleaning " .. clean)
  14. lines = io.lines(clean)
  15. if not lines then
  16. print("Can not open file " .. clean)
  17. return
  18. end
  19. function trimright(s)
  20. return s:match "^(.-)%s*$"
  21. end
  22. local lineend = false
  23. local tabspace = false
  24. local changed = false
  25. local invalid = false
  26. local newfile = {}
  27. for l in lines do
  28. local lt = trimright(l)
  29. if (lt ~= l) then
  30. lineend = true
  31. changed = true
  32. end
  33. local lts = lt:gsub('\t', ' ')
  34. if (lts ~= lt) then
  35. tabspace = true
  36. changed = true
  37. end
  38. for i=1,#lts do
  39. local b = string.byte(lts,i)
  40. if b<32 or b>=127 then
  41. print("Letter " .. string.byte(l,i) .. " (" .. b .. ") found in line " .. lts)
  42. invalid = true
  43. end
  44. end
  45. newfile[#newfile + 1] = lts
  46. end
  47. print("Line endings trimmed: " .. tostring(lineend))
  48. print("Tabs converted to spaces: " .. tostring(tabspace))
  49. print("Invalid characters: " .. tostring(invalid))
  50. if changed then
  51. local f = io.open(clean, "w")
  52. for i=1,#newfile do
  53. f:write(newfile[i])
  54. f:write("\n")
  55. end
  56. f:close()
  57. print("File cleaned")
  58. end