/* Copyright (c) 2003, Derek Park All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Gelaed nor Derek Park nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.io.*; import java.util.*; public class CountLines { public static void main(String [] args) { if (args.length == 0 || (args.length == 1 && (args[0].equals("/?") || args[0].equals("-help") || args[0].equals("--help") || args[0].equals("-h")))) { printUsage(null); return; } Vector files = new Vector(); Vector directories = new Vector(); Vector extensions = new Vector(); if (!args[0].equals("-d") && !args[0].equals("-f") && !args[0].equals("-e")) { printUsage("Invalid Arguments"); return; } try { int arg = 0; while (arg < args.length) { if (args[arg].equals("-f")) { arg++; while(arg < args.length && !args[arg].equals("-d") && !args[arg].equals("-f") && !args[arg].equals("-e")) { //System.out.println("files: " + args[arg]); files.add(new File(args[arg]).getCanonicalFile()); arg++; } } else if (args[arg].equals("-d")) { arg++; while(arg < args.length && !args[arg].equals("-d") && !args[arg].equals("-f") && !args[arg].equals("-e")) { //System.out.println("directories: " + args[arg]); directories.add(args[arg]); arg++; } } else if (args[arg].equals("-e")) { arg++; while(arg < args.length && !args[arg].equals("-d") && !args[arg].equals("-f") && !args[arg].equals("-e")) { //System.out.println("extensions: " + args[arg]); extensions.add(args[arg]); arg++; } } else { printUsage("Invalid Arguments"); return; } } String [] exts = new String[extensions.size()]; for (int i = 0; i < extensions.size(); i++) { exts[i] = (String)extensions.get(i); } FilenameFilter filter = new ExtensionFilenameFilter(exts); for (int i = 0; i < directories.size(); i++) { File dir = new File((String)directories.get(i)); File [] fileList = dir.listFiles(filter); for (int j = 0; j < fileList.length; j++) { files.add(fileList[j].getCanonicalFile()); } } int count = 0; for (int i = 0; i < files.size(); i++) { BufferedReader reader = new BufferedReader(new FileReader((File)files.get(i))); while (reader.readLine() != null) count++; reader.close(); } System.out.println("number of lines in all files matching criteria: " + count); } catch (Exception exc) { exc.printStackTrace(); } } private static void printUsage(String s) { if (s != null) System.out.println(s + "\n"); System.out.println("usage: java CountLines"); System.out.println("-f "); System.out.println("-d "); System.out.println("-e "); } } class ExtensionFilenameFilter implements FilenameFilter { private String [] exts; public ExtensionFilenameFilter(String [] extensions) { exts = new String [extensions.length]; for (int i = 0; i < exts.length; i++) { exts[i] = extensions[i]; } } public boolean accept(File dir, String filename) { //System.out.println("filter: " + filename); if (new File(filename).isDirectory()) return false; if (exts.length == 0) return true; // no exts means accept all for (int i = 0; i < exts.length; i++) { if (filename.endsWith("." + exts[i])) return true; } return false; } }