cleanup.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. lineno = 0
  28. incmt = false
  29. for l in lines do
  30. lineno = lineno + 1
  31. local lt = trimright(l)
  32. if (lt ~= l) then
  33. lineend = true
  34. changed = true
  35. end
  36. local mcmt = l:find("%/%*");
  37. if mcmt then
  38. if incmt then
  39. print("line " .. lineno .. " nested comment")
  40. end
  41. if not (l:sub(mcmt):find("%*%/")) then
  42. -- multiline comment begins here
  43. incmt = true
  44. end
  45. elseif incmt then
  46. if not l:find("^%s*%*") then
  47. print("line " .. lineno .. " multiline comment without leading *")
  48. end
  49. if l:find("%*%/") then
  50. incmt = false
  51. end
  52. else
  53. local cmt = l:find("//")
  54. if (cmt) and (l:sub(cmt-5, cmt+1) ~= "http://") and (l:sub(cmt-6, cmt+1) ~= "https://") then
  55. print("line " .. lineno .. " has C++ comment //")
  56. end
  57. end
  58. local lts = lt:gsub('\t', ' ')
  59. if (lts ~= lt) then
  60. tabspace = true
  61. changed = true
  62. end
  63. for i=1,#lts do
  64. local b = string.byte(lts,i)
  65. if b<32 or b>=127 then
  66. print("Letter " .. string.byte(l,i) .. " (" .. b .. ") found in line " .. lts)
  67. invalid = true
  68. end
  69. end
  70. newfile[#newfile + 1] = lts
  71. end
  72. print("Line endings trimmed: " .. tostring(lineend))
  73. print("Tabs converted to spaces: " .. tostring(tabspace))
  74. print("Invalid characters: " .. tostring(invalid))
  75. if changed then
  76. local f = io.open(clean, "wb")
  77. for i=1,#newfile do
  78. f:write(newfile[i])
  79. f:write("\n")
  80. end
  81. f:close()
  82. print("File cleaned")
  83. end