001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.metrics;
010:
011: import java.io.*;
012: import org.acm.seguin.io.DirectoryTreeTraversal;
013: import org.acm.seguin.tools.RefactoryInstaller;
014:
015: /**
016: * Counts the number of lines in a file
017: *
018: *@author Chris Seguin
019: *@created June 30, 1999
020: */
021: public class LCTraversal extends DirectoryTreeTraversal {
022: // Instance Variables
023: private long total;
024: private int fileCount;
025:
026: /**
027: * Traverses a directory tree structure
028: *
029: *@param init the initial directory
030: */
031: public LCTraversal(String init) {
032: super (init);
033:
034: total = 0;
035: fileCount = 0;
036: }
037:
038: /**
039: * Starts the tree traversal
040: */
041: public void run() {
042: super .run();
043:
044: long count = total;
045: if (count < 10) {
046: System.out.print(" " + count);
047: } else if (count < 100) {
048: System.out.print(" " + count);
049: } else if (count < 1000) {
050: System.out.print(" " + count);
051: } else if (count < 10000) {
052: System.out.print(" " + count);
053: } else if (count < 100000) {
054: System.out.print(" " + count);
055: } else {
056: System.out.print(" " + count);
057: }
058: System.out.println(" total lines in " + fileCount + " files");
059:
060: double top = count;
061: double bottom = fileCount;
062:
063: System.out.println("Average: " + (top / bottom));
064: }
065:
066: /**
067: * Determines if this file should be handled by this traversal
068: *
069: *@param currentFile the current file
070: *@return true if the file should be handled
071: */
072: protected boolean isTarget(File currentFile) {
073: String filename = currentFile.getName().toLowerCase();
074: return (filename.indexOf(".java") >= 0)
075: || (filename.indexOf(".h") >= 0)
076: || (filename.indexOf(".cpp") >= 0);
077: }
078:
079: /**
080: * Visits the current file
081: *
082: *@param currentFile the current file
083: */
084: protected void visit(File currentFile) {
085: int count = (new LineCounter(currentFile)).printMessage();
086: total += count;
087: fileCount++;
088: }
089:
090: /**
091: * Main program
092: *
093: *@param args Command line arguments
094: */
095: public static void main(String[] args) {
096: // Make sure everything is installed properly
097: (new RefactoryInstaller(false)).run();
098:
099: if (args.length == 0) {
100: System.out
101: .println("Syntax: java org.acm.seguin.metrics.LCTraversal <directory>");
102: return;
103: }
104:
105: (new LCTraversal(args[0])).run();
106: }
107: }
|