package yi.or.db;

/******************************************************************************
This class is used to retrieval all the images under a dirctry (in a DB)
using:
    String[] allNames = FindAllImages.everySingleName(dir_name);
    or
    String[] allNames = FindAllImages.everySingleName(); which use the default dir
******************************************************************************/
import java.io.*;
import java.util.*;

public class FindAllImages {
    static boolean VERBOSE=false;
    static String DEFAULTDIR= "/gts";
/*    
    public static void addImages(Vector v,String directory) {
	    String[] newImages = getImageNames(directory);
	    for (int x = 0 ; x < newImages.length;  x++) {
		    if (	(newImages[x].endsWith("JPG"))||
			    (newImages[x].endsWith("GIF")) ||
			    (newImages[x].endsWith("jpg")) ||
			    (newImages[x].endsWith("gif")))
			    v.addElement(newImages[x]);
		    else
			    System.err.println("Found improper suffix on " + newImages[x]);
	    }	
	    return;
    }
*/
    // This method adds all the images files in the directory recursively.
    // The image files found are kept in the vector v.  liyi
    // The directory name must omit the "/", say "dir" instead of "dir/"
    public static void addImagesRecursively(Vector v,String directoryName) {
        java.io.File f = new java.io.File(directoryName);
	    String[] fileNames = f.list();
	    if (VERBOSE) System.err.println("Got " + fileNames.length + " entries in the directory " + directoryName);
	    if (VERBOSE) System.err.println(fileNames[0]);
    	
	    // check every entry in this current directory, to decide whether it a file or a directory
	    int numImageFile=0, numDirectory=0;
	    for (int i=0; i<fileNames.length;i++) {
	        String fullName=directoryName+"/"+fileNames[i];
	        java.io.File d= new java.io.File(fullName);
	        // if the current entry is another directory, call itself recuresively
	        if (d.isDirectory()) {
	            addImagesRecursively(v, fullName);
	            numDirectory++;
	        }
	        else {
	            if ((fullName.endsWith("JPG"))||(fullName.endsWith("GIF"))||
	                (fullName.endsWith("jpg"))||(fullName.endsWith("gif"))) {
	                    v.addElement(fullName);
	                    numImageFile++;
	                }
			    else
			        System.err.println("Found improper suffix on " + fullName);
		    }	    
	    }
    	
	    if (VERBOSE) System.err.println("Got " + numImageFile + " image files in the directory " + directoryName);
	    if (VERBOSE) System.err.println("Got " + numDirectory + " directories in the directory " + directoryName);
	    if (VERBOSE) System.err.println("    " + fileNames[0] + "...");
    	
    }

    // Return all the image files name in the directory name provided, including subdir.  by liyi
    public static String[] everySingleName(String dirName) {
	    Vector namesVector = new Vector();
	    addImagesRecursively(namesVector,dirName);
	    
	    //construct a array instead of vector
	    int sz = namesVector.size();
	    String[] rtn = new String[sz];
	    for (int x = 0 ; x < sz ; x++)
		    rtn[x] = (String) namesVector.elementAt(x);
		
		namesVector=null; System.gc();
	    return rtn;	
    }
    
    public static String[] everySingleName() {
	    return everySingleName(DEFAULTDIR);	
    }
    
    public static void addObjFileNameRecursively(Vector v,String directoryName) {
        java.io.File f = new java.io.File(directoryName);
	    String[] fileNames = f.list();
	    if (VERBOSE) System.err.println("Got " + fileNames.length + " entries in the directory " + directoryName);
	    if (VERBOSE) System.err.println(fileNames[0]);
    	
	    // check every entry in this current directory, to decide whether it a file or a directory
	    int numImageFile=0, numDirectory=0;
	    for (int i=0; i<fileNames.length;i++) {
	        String fullName=directoryName+"/"+fileNames[i];
	        java.io.File d= new java.io.File(fullName);
	        // if the current entry is another directory, call itself recuresively
	        if (d.isDirectory()) {
	            addObjFileNameRecursively(v, fullName);
	            numDirectory++;
	        }
	        else {
	            if (fullName.endsWith("objects")) {
	                    v.addElement(fullName);
	                    numImageFile++;
	                }
		    }	    
	    }
    	
	    if (VERBOSE) System.err.println("Got " + numImageFile + " image files in the directory " + directoryName);
	    if (VERBOSE) System.err.println("Got " + numDirectory + " directories in the directory " + directoryName);
	    if (VERBOSE) System.err.println("    " + fileNames[0] + "...");
    	
    }

    

    public static String[] getObjFileNames(String dirName) {
	    Vector namesVector = new Vector();
	    addObjFileNameRecursively(namesVector,dirName);
	    
	    //construct a array instead of vector
	    int sz = namesVector.size();
	    String[] rtn = new String[sz];
	    for (int x = 0 ; x < sz ; x++)
		    rtn[x] = (String) namesVector.elementAt(x);
		
		namesVector=null; System.gc();
	    return rtn;	
    }

    public static String[] getObjFileNames() {
	    return getObjFileNames(DEFAULTDIR);	
    }


/*
    public static String[] getImageNames(String directoryName) {
    	
	    java.io.File f = new java.io.File(directoryName);
	    String[] imageNames = f.list();
	    System.err.println("Got " + imageNames.length + " files in the directory " + directoryName);
	    System.err.println(imageNames[0]);
	    String[] fullNames = new String[imageNames.length];
	    for (int x = 0 ; x < imageNames.length ; x++) 
		    fullNames[x]= directoryName+imageNames[x];
	    return fullNames;
    }
*/
    public static void main(String args[]) {
	    String[] allNames = everySingleName();
	    System.err.println("Length = "+ allNames.length);
	    return;
    }
}