/** * HTML Tag Checker * @author Robert J Morton * @version 10 July 2018 * @copyright Sep 2018 Robert J Morton (all rights reserved) */ /* This program counts the number of lines in C-program files and java program files. Command: java lc Default root: /home/rpb/Private/website */ import java.io.*; class lc { private static FileReader fr; // for each file scanned private static String bd = "", // first command line arguement [parent file path] sd = "", // 2nd command line arguement [directory to be word-counted] fp; // full path to file being line counted private static int lineCnts[] = new int[3], fileType = 0; /* This method looks for 'new-line' characters in program files. Called from only one place in scan(). */ static void lineCount() { int d = 0; // current character retrieved from the curent file try { fr = new FileReader(fp); // create a new file reader /* Read and examine each character in turn from the current file until the end-of-file is encountered. */ while((d = fr.read()) != -1) { if(d == '\n') ++lineCnts[fileType]; } fr.close(); // close the file reader } catch(Exception e) { } // catches end-of-file exception } /* When invoked, it examines the files and directories contained within the directory 'd' passed to it as its parameter. If an entry is a program file, it calls the lineCount() method above. The 'relative' filespec is the path +filename from the point of view of the current directory. If an entry is a directory, it simply calls itself to deal with that (sub) directory as it is doing with the current directory. Thus it can handle any depth of sub-directories from the parent. This method is called by itself and from one place in main(). */ private static void scan(String d) throws IOException { char ch = ' '; File fd = new File(d); // create file object for given directory name String D[] = fd.list(); // list all items in this directory /* For each entry in the sub-directory, get name of [next] sub-directory and create a file object for it. */ for(int i = 0; i < D.length; i++) { fp = d + "/" + D[i]; // full path to HTML file being examined File fs = new File(fp); if(fs.isDirectory()) // Provided it is an existing directory, scan(fp); // re-enter this method. /* Else it should be a file. So, if the file exists and it is a C or Java file, call the lineCount() method above. */ else if(fs.isFile()) if(fp.endsWith(".c")) { fileType = 0; // a C-file is type 0 lineCount(); // go count the lines in this file } else if(fp.endsWith(".java")) { fileType = 1; // a Java-file is type 1 lineCount(); // go count the lines in this file } else if(fp.endsWith(".BAS") || // a program written in BASIC fp.endsWith(".FMM") || // a Finite State Machine fp.endsWith(".INC")) { // a BASIC 'include' file fileType = 2; lineCount(); // go count the lines in this file } } // end of the for() loop } public static void main(String args[]) throws Exception { /* Provided at least the two mandatory command line arguements have been entered, set the first arguement as the name of the given document base directory and the second arguement as the sub-directory to be word-counted. */ bd = "/home/rob/Private"; sd = "website"; // bd = "/home/rob/MKTR"; // sd = "Msource"; String d = bd + "/" + sd; // form the full path File pd = new File(d); // create a file object from the full path /* If command line argument is an existing directory, open the output file writer in order to be able to use write() */ if(pd.isDirectory()) { scan(d); // scan for HTML files in the specified directory tree System.out.println("" + lineCnts[0] + "lines of C code"); System.out.println("" + lineCnts[1] + "lines of Java code"); System.out.println("" + lineCnts[2] + "lines of BASIC code"); } else System.out.println(d + " is not a directory."); } }