package yi.util.io;

import java.util.Vector;
import java.io.*;
import java.util.*;

// read the arguments of a program from a file
public final class File
{
    static boolean VERBOSE= false;
    public static String[] getFileNames(String dirName)
    {
            java.io.File f = new java.io.File(dirName);
            String[] fileNames = f.list();
            if (VERBOSE) {
                System.err.println("Got " + fileNames.length + " files in the directory " + dirName);
                System.err.println(fileNames[0]);
            }
            //make up the full name of the files
            String[] fullNames = new String[fileNames.length];
            for (int x = 0 ; x < fileNames.length ; x++)
                    fullNames[x]= dirName+"/"+fileNames[x];
            return fullNames;
    }


    //return the subdirectory short names under a directory
    public static Vector getSubDirNames(String directoryName) {
        java.io.File f = new java.io.File(directoryName);
            String[] fileNames = f.list();
            if (f==null) {
                return new Vector();
            }
            Vector subdirNames= new Vector();
            for (int i=0; i<fileNames.length;i++) {
                String fullName=directoryName+"/"+fileNames[i];
                java.io.File d= new java.io.File(fullName);
                if (d.isDirectory()) {
                    subdirNames.addElement(fileNames[i]);
                }
            }
            return subdirNames;
        }

    public static void getImagesNameRecursively(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()) {
                    getImagesNameRecursively(v, fullName);
                    numDirectory++;
                }
                else {
//	            if ((fullName.endsWith("JPG"))||(fullName.endsWith("GIF"))||
//	                (fullName.endsWith("jpg"))||(fullName.endsWith("gif"))) {
                    if ((fullName.endsWith("JPG"))||(fullName.endsWith("jpg"))) {
                            v.addElement(fullName);
                            numImageFile++;
                        }
                            else
                                if (VERBOSE) 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] + "...");
    }

    public static String[] getImagesNameRecursively(String directoryName) {
        Vector v= new Vector();
        getImagesNameRecursively(v,directoryName);
            String[] rst= new String[v.size()];
            for (int i=0;i<v.size();i++) rst[i]= (String)v.elementAt(i);
            return rst;
    }

    public static String getShortName (String full_name)
    {
        int last_slash= full_name.lastIndexOf("/");
        if (last_slash==-1) last_slash= full_name.lastIndexOf("\\");
        if (last_slash==-1) return full_name;
        return full_name.substring(last_slash+1);
    }

    public static String getPath(String full_name)
    {
        int last_slash= full_name.lastIndexOf("/");
        if (last_slash==-1) last_slash= full_name.lastIndexOf("\\");
        if (last_slash==-1) return new String();
        return full_name.substring(0, last_slash);
    }

    /**
     * Change the postfix of a file name.  E.g. "1.jpg" to "1.bmp".  If the
     * give name don't have a "dot", the new postfix will be added at the end
     * @param name
     * @param new_postfix
     * @return
     */
    public static String changePostFix(String name, String new_postfix) {
      int last_slash= name.lastIndexOf("/");
      if (last_slash==-1) last_slash= name.lastIndexOf("\\");

      int last_dot = name.lastIndexOf(".");
      if (last_dot <= last_slash) return name + "." + new_postfix;
      return name.substring(0, last_dot) + "." + new_postfix;

    }

    /**
     * Make all the necessary directory in the given path
     * @param path
     * @param ignoreLast the file name is included in the given path, so it
     *                   should be ignored.
     */
    public static boolean makeDirectory(String path, boolean ignoreLast) {
      if (ignoreLast) {
        int last_slash= path.lastIndexOf("/");
        if (last_slash==-1) last_slash= path.lastIndexOf("\\");
        if (last_slash >0) path = path.substring(0, last_slash);
        else return true;
      }
      java.io.File f = new java.io.File(path);
      if (f.isDirectory()) return true;
      return f.mkdirs();
    }

    public static void main(String[] args) {
      makeDirectory("as", true);
      makeDirectory("E:/temp/a/s/as", true);
      makeDirectory("E:/temp/b/s/as", false);


//      System.out.println(changePostFix("a", "jpg"));
//      System.out.println(changePostFix("a.bmp", "jpg"));
//      System.out.println(changePostFix("/b/c/a.bmp", "jpg"));
//      System.out.println(changePostFix("\\b\\c\\a.bmp", "jpg"));
//      System.out.println(changePostFix("\\b\\c\\a", "jpg"));
//      System.out.println(changePostFix("\\b.c\\a", "jpg"));
    }


}