diff options
Diffstat (limited to 'src/toolsconfig/cppcheck.java')
-rw-r--r-- | src/toolsconfig/cppcheck.java | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/src/toolsconfig/cppcheck.java b/src/toolsconfig/cppcheck.java new file mode 100644 index 0000000..d36c11f --- /dev/null +++ b/src/toolsconfig/cppcheck.java | |||
@@ -0,0 +1,152 @@ | |||
1 | package toolsconfig; | ||
2 | |||
3 | import java.io.BufferedReader; | ||
4 | import java.io.IOException; | ||
5 | import java.io.InputStream; | ||
6 | import java.io.InputStreamReader; | ||
7 | import java.nio.charset.StandardCharsets; | ||
8 | |||
9 | public class cppcheck extends ToolsConfig { | ||
10 | public cppcheck() { | ||
11 | this.name = "cppcheck"; | ||
12 | ToolsRegistry.registerTool(this.name, this); | ||
13 | } | ||
14 | |||
15 | @Override | ||
16 | public void execute(String src) { | ||
17 | // FIXME:这段代码原因不详,十分令人疑惑 | ||
18 | // if (changedpathcpp) { | ||
19 | // MyContorlUtil.loadToolsRoad(settingsXML, this); | ||
20 | // changedpathcpp = false; | ||
21 | // } | ||
22 | |||
23 | // 路径未配置,直接返回 | ||
24 | if (path == null) { | ||
25 | return; | ||
26 | } | ||
27 | |||
28 | Runtime[] runtime = {Runtime.getRuntime(), Runtime.getRuntime()}; | ||
29 | Process[] p = new Process[2]; | ||
30 | try { | ||
31 | System.out.printf(path + " %s %s%n", params, src); | ||
32 | // 指定模板执行cppcheck,用于生成“结果”表格 | ||
33 | p[0] = runtime[0].exec(String.format(path + | ||
34 | " --template=\"cppcheck${file}${line}${severity}${id}${message}${cwe}\" %s %s", | ||
35 | params, src)); | ||
36 | // 不使用模板,在底部标签页显示纯文本结果 | ||
37 | p[1] = runtime[1].exec(String.format(path + " %s %s", params, src)); | ||
38 | |||
39 | final InputStream[] stdout = | ||
40 | {p[0].getInputStream(), p[1].getInputStream()}; | ||
41 | final InputStream[] stderr = | ||
42 | {p[0].getErrorStream(), p[1].getErrorStream()}; | ||
43 | |||
44 | // 模板标准输出 | ||
45 | new Thread() { | ||
46 | @SuppressWarnings("resource") | ||
47 | public void run() { | ||
48 | BufferedReader br1 = | ||
49 | new BufferedReader(new InputStreamReader(stdout[0])); | ||
50 | StringBuilder l1 = new StringBuilder(); | ||
51 | try { | ||
52 | String line1 = null; | ||
53 | int i = 0; | ||
54 | uiFrame.textCheck.setText(""); | ||
55 | while ((line1 = br1.readLine()) != null) { | ||
56 | uiFrame.textCheck.append( | ||
57 | i + " " + line1 + ENTERWindows); | ||
58 | i++; | ||
59 | } | ||
60 | } catch (Exception e) { | ||
61 | e.printStackTrace(); | ||
62 | System.out.println("inputError"); | ||
63 | } finally { | ||
64 | try { | ||
65 | stdout[0].close(); | ||
66 | } catch (IOException e) { | ||
67 | e.printStackTrace(); | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | }.start(); | ||
72 | |||
73 | // 模板错误流,用于生成结果表格 | ||
74 | new Thread() { | ||
75 | public void run() { | ||
76 | BufferedReader br2 = | ||
77 | new BufferedReader(new InputStreamReader(stderr[0])); | ||
78 | StringBuilder l2 = new StringBuilder(); | ||
79 | try { | ||
80 | String line2 = null; | ||
81 | int i = 0; | ||
82 | while ((line2 = br2.readLine()) != null) { | ||
83 | uiFrame.ct.toRowsOfCppcheckResult(line2); | ||
84 | l2.append(i + " " + line2 + ENTERWindows); | ||
85 | i++; | ||
86 | } | ||
87 | } catch (Exception e) { | ||
88 | e.printStackTrace(); | ||
89 | } finally { | ||
90 | try { | ||
91 | result.setText(new String(l2.toString().getBytes(), | ||
92 | StandardCharsets.UTF_8)); | ||
93 | stderr[0].close(); | ||
94 | } catch (IOException e) { | ||
95 | e.printStackTrace(); | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | }.start(); | ||
100 | |||
101 | // 纯文本的错误流,用于在底部标签页显示 | ||
102 | new Thread() { | ||
103 | public void run() { | ||
104 | BufferedReader br2 = | ||
105 | new BufferedReader(new InputStreamReader(stderr[1])); | ||
106 | StringBuilder l2 = new StringBuilder(); | ||
107 | result.setText(""); | ||
108 | try { | ||
109 | String line2 = null; | ||
110 | int i = 0; | ||
111 | while ((line2 = br2.readLine()) != null) { | ||
112 | l2.append(i + " " + line2 + ENTERWindows); | ||
113 | i++; | ||
114 | } | ||
115 | } catch (Exception e) { | ||
116 | e.printStackTrace(); | ||
117 | } finally { | ||
118 | try { | ||
119 | result.setText(new String(l2.toString().getBytes(), | ||
120 | StandardCharsets.UTF_8)); | ||
121 | stderr[1].close(); | ||
122 | } catch (IOException e) { | ||
123 | e.printStackTrace(); | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | }.start(); | ||
128 | |||
129 | // 等待两个进程结束 | ||
130 | int exitCode = p[0].waitFor(); | ||
131 | int exitCode1 = p[1].waitFor(); | ||
132 | if (exitCode == SUCCESS) { | ||
133 | System.out.println(SUCCESS_MESSAGE); | ||
134 | } else { | ||
135 | System.err.println(ERROR_MESSAGE + exitCode); | ||
136 | } | ||
137 | p[0].destroy(); | ||
138 | p[1].destroy(); | ||
139 | } catch (Exception e) { | ||
140 | try { | ||
141 | // 出现异常则关闭相关流 | ||
142 | p[0].getErrorStream().close(); | ||
143 | p[0].getInputStream().close(); | ||
144 | p[0].getOutputStream().close(); | ||
145 | p[1].getErrorStream().close(); | ||
146 | p[1].getInputStream().close(); | ||
147 | p[1].getOutputStream().close(); | ||
148 | } catch (Exception ee) { | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | } | ||