分割源文件的Java程序

由于从VC6到VS2005均不能调试超过65535行的代码,但是在工作中生成的解码库的源文件会远远超过这个数字,因此写了个程序来解决这个问题。程序编程语言选择Java,因为用Java来处理使用方便,编写也容易,此处对效率没有要求因此不选择用C了。

程序基于花括号匹配的原理来拆分文件,在第一个文件中include之后的文件,并将之后的文件都以.h来重命名。

用法:
java SplitFile

 

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class SplitFile { private static void usage(){ System.out.println("Usage:"); System.out.println("-l : limit the line number"); System.out.println("-b : whether keep the old source file"); System.out.println("-f : split which file"); } public static void main(String[] args){ //parse args int argc = args.length; if(argc == 0){ System.out.println("No parameter!"); System.exit(0); } else if(argc == 1){ if(args[0].equals("-h")||args[0].equals("--help")){ usage(); System.exit(0); } } int i = 0; boolean bakFlag = false; int limitFileLine = 65535; String fileName = null; while(i < argc){ if(args[i].equals("-l")){ try{ limitFileLine = Integer.parseInt(args[i+1]); } catch(NumberFormatException e){ System.out.println("Param error!"); System.exit(0); } } else if(args[i].equals("-b")){ bakFlag = true; } else if(args[i].equals("-f")){ fileName = args[i+1]; } i++; } SplitFile spliter = new SplitFile(); spliter.setLimitFileLine(limitFileLine); // String fileName = "bin/test.txt";//the file path should be get from input param FileReader fr = null; try { fr = new FileReader(fileName); } catch (FileNotFoundException e) { System.out.println("Can't find the file "+fileName); return; } BufferedReader br = new BufferedReader(fr); try { int fileLine = spliter.getTotalLineNumber(br); br.close(); if(fileLine > limitFileLine){ //need to be split String[] strArray = new String[fileLine]; int[] intArray = new int[fileLine]; BufferedReader br1 = new BufferedReader(new FileReader(fileName)); int index = 0; int bracketsNum = 0; String lineStr = br1.readLine(); while(lineStr != null){ strArray[index] = lineStr; bracketsNum = spliter.processLineString(lineStr); // System.out.println(index); /*special process for function declare like this * int a() * { * xxx * } */ if(bracketsNum == 1 && index > 1 && intArray[index-1]==0 && intArray[index-2]==0 && strArray[index].length()!=0){ intArray[index-1] = 1; } intArray[index] = bracketsNum; index++; lineStr = br1.readLine(); } br1.close(); //process the result List lineList = spliter.processLineNumber(intArray); //file process int postfixIndex = 0; int allIndex = 0; String seqFileName = spliter.takeoutFilePostfix(fileName); while(postfixIndex < lineList.size()){ String addtionFileName = seqFileName + "_" + postfixIndex + ".h"; PrintWriter pw = new PrintWriter(new FileWriter(addtionFileName)); while(allIndex < ((Integer)lineList.get(postfixIndex)).intValue()){ pw.println(strArray[allIndex]); allIndex++; } if(postfixIndex == 0){ String bareFileName = spliter.takeoutPath(fileName); for(int j = 1;j< lineList.size();j++){ pw.println("#include /"" + bareFileName + "_" + j + ".h" + "/""); } } postfixIndex++; pw.flush(); pw.close(); } //change the first file to the initial name File originalFile = new File(fileName); if(bakFlag){ File bakFile = new File(fileName+".bak"); if(bakFile.exists()) bakFile.delete(); else originalFile.renameTo(bakFile); } else{ originalFile.delete(); } File file = new File(seqFileName + "_0.h"); file.renameTo(originalFile); System.out.println("The File " + fileName + " Split Completed."); } else{ System.out.println("The file " + fileName +" does not need to be split!"); } } catch (IOException e) { e.printStackTrace(); } } //takeout the path prefix should be implemented public String takeoutPath(String str){ String temp = takeoutFilePostfix(str); int index = temp.lastIndexOf(System.getProperty("file.separator")); return temp.substring(index+1); } /* * change file postfix ".c" or ".cpp" to ".h" */ public String takeoutFilePostfix(String str){ String temp = str; int index = temp.lastIndexOf("."); temp = temp.substring(0, index); return temp; } public List processLineNumber(int[] intArray){ int line = 0; int oldValue = line;//may be the line span is set too small to split the same function List list = new ArrayList(); while (line < totalLineNumber) { int targetLine = limitFileLine + line; if (isInFunction(targetLine, intArray)) { line = findLastFunctionEnd(targetLine, intArray); if(line == oldValue){ System.out.println("The line span is set too small,please reset!Program Exit!"); System.exit(0); } } else{ line = targetLine; if(line > totalLineNumber) line = totalLineNumber; } // System.out.println(line); oldValue = line; list.add(new Integer(line)); } return list; } private int getTotalLineNumber(BufferedReader reader) throws IOException{ int i = 0; String s = reader.readLine(); while(s != null){ i++; s = reader.readLine(); } totalLineNumber = i; return i; } public int processLineString(String str){ int pos = -1; if((pos = str.indexOf("{"))>-1){ //there is a left bracket in this line leftBracketNum++; if(pos + 1 != str.length()){ processLineString(str.substring(pos+1)); } } else if((pos = str.indexOf("}"))>-1){ leftBracketNum--; if(pos + 1 != str.length()){ processLineString(str.substring(pos+1)); } } return leftBracketNum; } /* * @param lineNumber from which Line * @param intArray collection */ public int findLastFunctionEnd(int lineNumber,int[] intArray){ int index = lineNumber-1; while(index > 0){ if(intArray[index]==0&& intArray[index-1]>0) break; index--; } if(index == 1) return 0; return index+1; } public boolean isInFunction(int lineNumber,int[] intArray){ if(lineNumber > totalLineNumber) return false; return intArray[lineNumber-1]>0?true:false; } private int leftBracketNum = 0; private int totalLineNumber = 0; private int limitFileLine = 65535; public int getLimitFileLine() { return limitFileLine; } public void setLimitFileLine(int limitFileLine) { this.limitFileLine = limitFileLine; } }

 

你可能感兴趣的:(J2SE,java,string,file,integer,list,path)