]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/resources/cleanup.lua
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / civetweb / resources / cleanup.lua
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
13 clean = arg[1]
14 print("Cleaning " .. clean)
15
16 lines = io.lines(clean)
17 if not lines then
18 print("Can not open file " .. clean)
19 return
20 end
21
22 function trimright(s)
23 return s:match "^(.-)%s*$"
24 end
25
26 local lineend = false
27 local tabspace = false
28 local changed = false
29 local invalid = false
30 local newfile = {}
31
32 lineno = 0
33 incmt = false
34
35 for l in lines do
36 lineno = lineno + 1
37 local lt = trimright(l)
38 if (lt ~= l) then
39 lineend = true
40 changed = true
41 end
42 local mcmt = l:find("%/%*");
43 if mcmt then
44 if incmt then
45 print("line " .. lineno .. " nested comment")
46 end
47 if not (l:sub(mcmt):find("%*%/")) then
48 -- multiline comment begins here
49 incmt = true
50 end
51 elseif incmt then
52 if not l:find("^%s*%*") then
53 print("line " .. lineno .. " multiline comment without leading *")
54 end
55 if l:find("%*%/") then
56 incmt = false
57 end
58 else
59 local cmt = l:find("//")
60 if (cmt) and (l:sub(cmt-5, cmt+1) ~= "http://") and (l:sub(cmt-6, cmt+1) ~= "https://") then
61 print("line " .. lineno .. " has C++ comment //")
62 end
63 end
64 local lts = lt:gsub('\t', ' ')
65 if (lts ~= lt) then
66 tabspace = true
67 changed = true
68 end
69 for i=1,#lts do
70 local b = string.byte(lts,i)
71 if b<32 or b>=127 then
72 print("Letter " .. string.byte(l,i) .. " (" .. b .. ") found in line " .. lts)
73 invalid = true
74 end
75 end
76
77 newfile[#newfile + 1] = lts
78 end
79
80 print("Line endings trimmed: " .. tostring(lineend))
81 print("Tabs converted to spaces: " .. tostring(tabspace))
82 print("Invalid characters: " .. tostring(invalid))
83
84 if changed then
85 local f = io.open(clean, "wb")
86 for i=1,#newfile do
87 f:write(newfile[i])
88 f:write("\n")
89 end
90 f:close()
91 print("File cleaned")
92 end