Java program for splitting source files

Source: Internet
Author: User

Since no more than 65535 lines of code can be debugged from vc6 to vs2005, the source file of the decoder library generated at work will far exceed this number, so I wrote a program to solve this problem. Java is the programming language, because it is convenient to use Java for processing and writing. Therefore, C is not required for efficiency.

The Program Splits the file based on the matching principle of curly brackets, includes the file in the first file, and renames the subsequent files with. h.

Usage:
Java splitfile <option>
<Option> cocould be follow:
-H: Show Help Information
-L <line_number>: Limit the line number
-B: whether keep the old source file
-F <file_name>: Split which file

 

Import Java. io. bufferedreader; <br/> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. filereader; <br/> Import Java. io. filewriter; <br/> Import Java. io. ioexception; <br/> Import Java. io. printwriter; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> public class splitfile {<br/> Private Static void usage () {<br/> system. out. println ("Usage:"); <br/> emerge E M. out. println ("-l <line_number>: Limit the line number"); <br/> system. out. println ("-B: whether keep the old source file"); <br/> system. out. println ("-F <file_name>: Split which file"); <br/>}< br/> Public static void main (string [] ARGs) {<br/> // parse ARGs <br/> int argc = args. length; <br/> If (argc = 0) {<br/> system. out. println ("No parameter! "); <Br/> system. exit (0); <br/>}< br/> else if (argc = 1) {<br/> If (ARGs [0]. equals ("-h") | ARGs [0]. equals ("-- Help") {<br/> usage (); <br/> system. exit (0); <br/>}< br/> int I = 0; <br/> Boolean bakflag = false; <br/> int limitfills Eline = 65535; <br/> string filename = NULL; <br/> while (I <argc) {<br/> If (ARGs [I]. equals ("-l") {<br/> try {<br/> limitfileline = integer. parseint (ARGs [I + 1]); <br/>}< br/> catch (Numberformatexception e) {<br/> system. Out. println ("Param error! "); <Br/> system. exit (0); <br/>}< br/> else if (ARGs [I]. equals ("-B") {<br/> bakflag = true; <br/>}< br/> else if (ARGs [I]. equals ("-F") {<br/> filename = ARGs [I + 1]; <br/>}< br/> I ++; <br/>}</P> <p> splitfile spliter = new splitfile (); <br/> spliter. setlimitfileline (limitfileline); <br/> // string filename = "bin/test.txt"; // the file path shocould be get from input Param <br/> filereader Fr = NULL; <br/> try {<Br/> Fr = new filereader (filename); <br/>} catch (filenotfoundexception e) {<br/> system. out. println ("can't find the file" + filename); <br/> return; <br/>}< br/> bufferedreader BR = new bufferedreader (FR ); </P> <p> try {<br/> int fileline = spliter. gettotallinenumber (BR); <br/> Br. close (); <br/> If (fileline> limitfileline) {<br/> // need to be split <br/> string [] strarray = new string [fileline]; <br/> I NT [] intarray = new int [fileline]; <br/> bufferedreader br1 = new bufferedreader (New filereader (filename); <br/> int Index = 0; <br/> int bracketsnum = 0; <br/> string linestr = br1.readline (); <br/> while (linestr! = NULL) {<br/> strarray [Index] = linestr; <br/> bracketsnum = spliter. processlinestring (linestr); <br/> // system. out. println (INDEX); <br/>/* Special Process for function declare like this <br/> * int () <br/> * {<br/> * XXX <br/> *} <br/> */<br/> If (bracketsnum = 1 & index> 1 & & intarray [index-1] = 0 & intarray [index-2] = 0 <br/> & strarray [Index]. length ()! = 0) {<br/> intarray [index-1] = 1; <br/>}< br/> intarray [Index] = bracketsnum; <br/> index ++; <br/> linestr = br1.readline (); <br/>}< br/> br1.close (); <br/> // process the result <br/> List linelist = spliter. processlinenumber (intarray); <br/> // file Process <br/> int postfixindex = 0; <br/> int allindex = 0; <br/> string seqfilename = spliter. takeoutfilepostfix (filename); <br/> while (postfixindex <linelist. si Ze () {<br/> string addtionfilename = seqfilename + "_" + postfixindex + ". H "; <br/> printwriter PW = new printwriter (New filewriter (addtionfilename); <br/> while (allindex <(integer) linelist. get (postfixindex )). intvalue () {<br/> PW. println (strarray [allindex]); <br/> allindex ++; <br/>}< br/> If (postfixindex = 0) {<br/> string barefilename = spliter. takeoutpath (filename); <br/> for (Int J = 1; j <linelist. Size (); j ++) {<br/> PW. println ("# include/" "+ barefilename +" _ "+ J + ". H "+"/""); <br/>}< br/> postfixindex ++; <br/> PW. flush (); <br/> PW. close (); <br/>}< br/> // change the first file to the initial name <br/> file originalfile = new file (filename ); <br/> If (bakflag) {<br/> file bakfile = new file (filename + ". bak "); <br/> If (bakfile. exists () <br/> bakfile. delete (); <br/> else <br/> originalfile. ren Ameto (bakfile); <br/>}< br/> else {<br/> originalfile. delete (); <br/>}< br/> file = new file (seqfilename + "_ 0.h"); <br/> file. renameto (originalfile); <br/> system. out. println ("the file" + filename + "split completed. "); <br/>}< br/> else {<br/> system. out. println ("the file" + filename + "does not need to be split! "); <Br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p >}< br/> // takeout the path prefix shocould be implemented <br/> Public String takeoutpath (string Str) {<br/> string temp = takeoutfilepostfix (STR); <br/> int Index = temp. lastindexof (system. getproperty ("file. separator "); <br/> return temp. substring (index + 1); <br/>}< br/>/* <br/> * change file Postfix ". C "or ". CPP "". H "<B R/> */<br/> Public String takeoutfilepostfix (string Str) {<br/> string temp = STR; <br/> int Index = temp. lastindexof (". "); <br/> temp = temp. substring (0, index); <br/> return temp; <br/>}</P> <p> public list processlinenumber (INT [] intarray) {<br/> int line = 0; <br/> int oldvalue = line; // may be the line span is set too small to split the same function <br/> List list = new arraylist (); <br/> while (line <Totallinenumber) {<br/> int targetline = limitfileline + line; <br/> If (isinfunction (targetline, intarray) {<br/> line = findlastfunctionend (targetline, intarray); <br/> If (line = oldvalue) {<br/> system. out. println ("the line span is set too small, Please reset! Program exit! "); <Br/> system. exit (0); <br/>}< br/> else {<br/> line = targetline; <br/> If (line> totallinenumber) <br/> line = totallinenumber; <br/>}< br/> // system. out. println (line); <br/> oldvalue = line; <br/> list. add (New INTEGER (line); <br/>}< br/> return list; <br/>}</P> <p> private int gettotallinenumber (bufferedreader reader) throws ioexception {<br/> int I = 0; <br/> string S = reader. readline (); <br/> wh Ile (s! = NULL) {<br/> I ++; <br/> S = reader. readline (); <br/>}< br/> totallinenumber = I; <br/> return I; <br/>}</P> <p> Public int processlinestring (string Str) {<br/> int Pos =-1; <br/> If (Pos = Str. indexof ("{")>-1) {<br/> // There is a left bracket in this line <br/> leftbracketnum ++; <br/> If (Pos + 1! = Str. length () {<br/> processlinestring (Str. substring (Pos + 1); <br/>}< br/> else if (Pos = Str. indexof ("}")>-1) {<br/> leftbracketnum --; <br/> If (Pos + 1! = Str. length () {<br/> processlinestring (Str. substring (Pos + 1); <br/>}< br/> return leftbracketnum; <br/>}< br/>/* <br/> * @ Param linenumber from which line <br/> * @ Param intarray collection <br/> */<br/> Public int findlastfunctionend (INT linenumber, int [] intarray) {<br/> int Index = lineNumber-1; <br/> while (index> 0) {<br/> If (intarray [Index] = 0 & intarray [index-1]> 0) <br/> break; <br/> index- -; <Br/>}< br/> If (Index = 1) return 0; <br/> return index + 1; <br/>}</P> <p> Public Boolean isinfunction (INT linenumber, int [] intarray) {<br/> If (linenumber> totallinenumber) <br/> return false; <br/> return intarray [lineNumber-1]> 0? True: false; <br/>}</P> <p> private int leftbracketnum = 0; <br/> private int totallinenumber = 0; <br/> private int limitfileline = 65535; <br/> Public int getlimitfileline () {<br/> return limitfileline; <br/>}< br/> Public void setlimitfileline (INT limitfileline) {<br/> This. limitfileline = limitfileline; <br/>}</P> <p >}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.