summaryrefslogtreecommitdiffstats
path: root/src/toolsconfig/cppcheck.java
blob: d36c11f6f13267c767c70ca52ffc4b79d2108d41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package toolsconfig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class cppcheck extends ToolsConfig {
    public cppcheck() {
        this.name = "cppcheck";
        ToolsRegistry.registerTool(this.name, this);
    }

    @Override
    public void execute(String src) {
        // FIXME:这段代码原因不详,十分令人疑惑
//        if (changedpathcpp) {
//            MyContorlUtil.loadToolsRoad(settingsXML, this);
//            changedpathcpp = false;
//        }

        // 路径未配置,直接返回
        if (path == null) {
            return;
        }

        Runtime[] runtime = {Runtime.getRuntime(), Runtime.getRuntime()};
        Process[] p = new Process[2];
        try {
            System.out.printf(path + " %s %s%n", params, src);
            // 指定模板执行cppcheck,用于生成“结果”表格
            p[0] = runtime[0].exec(String.format(path +
                    " --template=\"cppcheck${file}${line}${severity}${id}${message}${cwe}\" %s %s",
                params, src));
            // 不使用模板,在底部标签页显示纯文本结果
            p[1] = runtime[1].exec(String.format(path + " %s %s", params, src));

            final InputStream[] stdout =
                {p[0].getInputStream(), p[1].getInputStream()};
            final InputStream[] stderr =
                {p[0].getErrorStream(), p[1].getErrorStream()};

            // 模板标准输出
            new Thread() {
                @SuppressWarnings("resource")
                public void run() {
                    BufferedReader br1 =
                        new BufferedReader(new InputStreamReader(stdout[0]));
                    StringBuilder l1 = new StringBuilder();
                    try {
                        String line1 = null;
                        int i = 0;
                        uiFrame.textCheck.setText("");
                        while ((line1 = br1.readLine()) != null) {
                            uiFrame.textCheck.append(
                                i + " " + line1 + ENTERWindows);
                            i++;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("inputError");
                    } finally {
                        try {
                            stdout[0].close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

            // 模板错误流,用于生成结果表格
            new Thread() {
                public void run() {
                    BufferedReader br2 =
                        new BufferedReader(new InputStreamReader(stderr[0]));
                    StringBuilder l2 = new StringBuilder();
                    try {
                        String line2 = null;
                        int i = 0;
                        while ((line2 = br2.readLine()) != null) {
                            uiFrame.ct.toRowsOfCppcheckResult(line2);
                            l2.append(i + " " + line2 + ENTERWindows);
                            i++;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            result.setText(new String(l2.toString().getBytes(),
                                StandardCharsets.UTF_8));
                            stderr[0].close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

            // 纯文本的错误流,用于在底部标签页显示
            new Thread() {
                public void run() {
                    BufferedReader br2 =
                        new BufferedReader(new InputStreamReader(stderr[1]));
                    StringBuilder l2 = new StringBuilder();
                    result.setText("");
                    try {
                        String line2 = null;
                        int i = 0;
                        while ((line2 = br2.readLine()) != null) {
                            l2.append(i + " " + line2 + ENTERWindows);
                            i++;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            result.setText(new String(l2.toString().getBytes(),
                                StandardCharsets.UTF_8));
                            stderr[1].close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

            // 等待两个进程结束
            int exitCode = p[0].waitFor();
            int exitCode1 = p[1].waitFor();
            if (exitCode == SUCCESS) {
                System.out.println(SUCCESS_MESSAGE);
            } else {
                System.err.println(ERROR_MESSAGE + exitCode);
            }
            p[0].destroy();
            p[1].destroy();
        } catch (Exception e) {
            try {
                // 出现异常则关闭相关流
                p[0].getErrorStream().close();
                p[0].getInputStream().close();
                p[0].getOutputStream().close();
                p[1].getErrorStream().close();
                p[1].getInputStream().close();
                p[1].getOutputStream().close();
            } catch (Exception ee) {
            }
        }
    }
}