⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codeswitcher.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    int testFile(String name) {

        File f = new File(name);
        LineNumberReader read = null;

        try {
            read = new LineNumberReader(new FileReader(f));
            int              l        = 1,
            maxline  = 0;
            boolean          longline = false;

            while (true) {
                String line = read.readLine();

                if (line == null) {
                    break;
                }

                if (line.length() > MAX_LINELENGTH
                        &&!line.startsWith("org.hsqldb.test.Profile.")) {
                    System.out.println("long line in " + name + " at line "
                            + l);
                }

                if (line.startsWith(" ")) {
                    int spaces = 0;

                    for (; spaces < line.length(); spaces++) {
                        if (line.charAt(spaces) != ' ') {
                            break;
                        }
                    }

                    if (spaces > 3 && testLine(line) &&!longline) {
                        maxline++;
                    } else if (isLongline(line)) {
                        longline = true;
                    } else {
                        longline = false;
                    }

                    String s = line.substring(spaces);

                    if (s.startsWith("if(")) {
                        if (!s.endsWith(" {")) {
                            System.out.println("if( without { in " + name
                                    + " at line " + l);
                        }
                    } else if (s.startsWith("} else if(")) {
                        if (!s.endsWith(" {")) {
                            System.out.println("} else if without { in "
                                    + name + " at line " + l);
                        }
                    } else if (s.startsWith("while(")) {
                        if (!s.endsWith(" {")) {
                            System.out.println("while( without { in " + name
                                    + " at line " + l);
                        }
                    } else if (s.startsWith("switch(")) {
                        if (!s.endsWith(" {")) {
                            System.out.println("switch( without { in " + name
                                    + " at line " + l);
                        }
                    } else if (s.startsWith("do ")) {
                        if (!s.endsWith(" {")) {
                            System.out.println("do without { in " + name
                                    + " at line " + l);
                        }
                    }
                }

                l++;
            }

            return maxline;
        } catch (Exception e) {
            printError(e.getMessage());
        }
        finally {

            if(read != null) {
                try {
                    read.close();
                } catch (IOException e1) {
                }
            }
        }

        return -1;
    }

    /**
     * Method declaration
     *
     *
     * @param line
     *
     * @return
     */
    boolean testLine(String line) {

        if (!line.endsWith(";")) {
            return false;
        }

        if (line.trim().startsWith("super(")) {
            return false;
        }

        return true;
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @return
     */
    boolean isLongline(String s) {

        char c = s.charAt(s.length() - 1);

        if (",(+-&|".indexOf(c) >= 0) {
            return true;
        }

        return false;
    }

    /**
     * Method declaration
     *
     *
     * @param name
     *
     * @return
     */
    boolean processFile(String name) {

        File    f         = new File(name);
        File    fnew      = new File(name + ".new");
        int     state     = 0;    // 0=normal 1=inside_if 2=inside_else
        boolean switchoff = false;
        boolean working   = false;
        int removeFrom = -1;

        try {
            Vector v  = getFileLines(f);
            Vector v1 = new Vector(v.size());

            for (int i = 0; i < v.size(); i++) {
                v1.addElement(v.elementAt(i));
            }

            for (int i = 0; i < v.size(); i++) {
                String line = (String) v.elementAt(i);

                if (line == null) {
                    break;
                }
                
                String lineTrimmed = trimBoth(line);
                String lineStripped = stripSpaces(line);
                //System.out.println("Line stipped='" + lineStripped + "'");
                //System.out.println("Line trimmed='" + lineTrimmed + "'");
                
//                if(lineStripped.indexOf("XTRA") != -1 || lineStripped.indexOf("//#") != -1) {
//                    System.out.println(i + ":" + f.getName() + "  >" + lineStripped + "<");
//                }

                if (working) {
                    if (lineTrimmed.equals("/*") || lineTrimmed.equals("*/")) {
                        v.removeElementAt(i--);
                        continue;
                    }
                    else if(lineTrimmed.startsWith("*")) {
                      int idx = line.indexOf('*');
                      v.setElementAt(line.substring(0, idx) + line.substring(idx + 1), i);
                    }
                    
                }

                if (lineStripped.indexOf("//#") != -1) {
                    if (lineStripped.startsWith("//#ifdef")) {
                        if (state != 0) {
                            printError(
                            "'#ifdef' not allowed inside '#ifdef'");

                            return false;
                        }

                        state = 1;
                        removeFrom = -1;

                        String s = lineStripped.substring(8);

                        if ( vSwitchOn.indexOf(s) != -1) {
                            printMessage(f, i, "Including " + s);
                            working   = true;
                            switchoff = false;
                        } else if (vSwitchOff.indexOf(s) != -1) {
                            printMessage(f, i, "Excluding " + s);
                            working = true;
                            if(comment) {
                                v.insertElementAt("/*", ++i);
                            }
                            else {
                                removeFrom = i;
                            }

                            switchoff = true;
                        }

                        if (vSwitches.indexOf(s) == -1) {
                            vSwitches.addElement(s);
                        }
                    } else if (lineStripped.startsWith("//#else")) {
                        if (state != 1) {
                            printError("'#else' without '#ifdef'");

                            return false;
                        }

                        state = 2;

                        if (!working) {}
                        else if (switchoff) {
                            if(comment) {
                                if (v.elementAt(i - 1).equals("")) {
                                    v.insertElementAt("*/", i - 1);
    
                                    i++;
                                } else {
                                    v.insertElementAt("*/", i++);
                                }
                            }
                            else {
                                if(removeFrom != -1) {
                                    for(int j = i ; j >= removeFrom; j--) {
                                        v.remove(j);
                                        i--;
                                    }
                                    removeFrom = -1;
                                }
                            }

                            switchoff = false;
                        } else {
                            if(comment) {
                                v.insertElementAt("/*", ++i);
                            }
                            else {
                                if(removeFrom != -1) {
                                    for(int j = i ; j >= removeFrom; j--) {
                                        v.remove(j);
                                        i--;
                                    }
                                    removeFrom = -1;
                                }
                            }

                            switchoff = true;
                        }
                    } else if (lineStripped.startsWith("//#endif")) {
                        if (state == 0) {
                            printError("'#endif' without '#ifdef'");

                            return false;
                        }

                        state = 0;

                        if (working && switchoff) {
                            if (v.elementAt(i - 1).equals("")) {
                                if(comment) {
                                    v.insertElementAt("*/", i - 1);
                                }

                                i++;
                            } else {
                                if(comment) {
                                    v.insertElementAt("*/", i++);
                                }
                            }
                            
                            if(removeFrom != -1) {
                                printMessage(f, removeFrom, "Removing " + ( i - removeFrom) + " lines");
                                for(int j = i ; j >= removeFrom; j--) {
                                    v.remove(j);
                                    i--;
                                }
                                removeFrom = -1;
                            }
                        }

                        working = false;
                        switchoff = false;
                    } else {}
                }
            }

            if (state != 0) {
                printError("'#endif' missing");

                return false;
            }

            boolean filechanged = false;

            for (int i = 0; i < v.size(); i++) {
                if (!v1.elementAt(i).equals(v.elementAt(i))) {
                    filechanged = true;

                    break;
                }
            }

            if (!filechanged) {
                return true;
            }

            writeFileLines(v, fnew);

            File fbak = new File(name + ".bak");

            fbak.delete();
            f.renameTo(fbak);

            File fcopy = new File(name);

            fnew.renameTo(fcopy);
            fbak.delete();

            return true;
        } catch (Exception e) {
            printError(e.getMessage());

            return false;
        }
    }

    static Vector getFileLines(File f) throws IOException {

        LineNumberReader read = null ; 
        Vector           v    = new Vector();
        try {
            read = new LineNumberReader(new FileReader(f));

        for (;;) {
            String line = read.readLine();

            if (line == null) {
                break;
            }

            v.addElement(line);
        }
        }
        finally {
            if(read != null) {
                read.close();
            }
        }

        return v;
    }
    
    static String trimBoth(String text) {
      text = text.trim();
      int s = text.length();
      char ch;
      int i;
      for(i = 0 ; i < s && Character.isWhitespace((ch = text.charAt(i))); i++);
      return i < s ? text.substring(i) : text;      
    }
    
    static String stripSpaces(String text) {
      StringBuffer buf = new StringBuffer();
      int s = text.length();
      char ch;
      for(int i = 0 ; i < s; i++) {
        ch = text.charAt(i);
        if(!Character.isWhitespace(ch)) {
          buf.append(ch);
        }
      }
      return buf.toString();
    }

    static void writeFileLines(Vector v, File f) throws IOException {

        FileWriter write = null; 
        try {
            write = new FileWriter(f);

        for (int i = 0; i < v.size(); i++) {
            write.write((String) v.elementAt(i));
            write.write(ls);
        }

        write.flush();
        }
        finally {
            if(write != null ) {
                write.close();
                
            }
        }
    }

    /**
     * Method declaration
     *
     *
     * @param error
     */
    static void printError(String error) {
        System.out.println("ERROR: " + error);
    }

    /**
     * Method declaration
     *
     *
     * @param error
     */
    static void printMessage(File f, int line, String message) {
        System.out.println("MSG: " + f.getName() + "[" + line + "] " + message);
    }
    
    public static void Xmain(String args[]) {
        System.out.println(stripSpaces(trimBoth("//#ifdef XTRA")));
        System.out.println(stripSpaces(trimBoth("\t\t \t//#ifdef XTRA")));
        System.out.println(stripSpaces(trimBoth("\t\t \t//# ifdef XTRA")));
        System.out.println(stripSpaces(trimBoth("\t\t \t// # ifdef XTRA")));
        System.out.println(stripSpaces(trimBoth("//# ifdef XTRA")));
        System.out.println(stripSpaces(trimBoth("// # ifdef\t \t XTRA")));
        System.out.println(stripSpaces(trimBoth("//  #   ifdef XTRA")));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -