hpdir.java

来自「一个简单的visio程序。」· Java 代码 · 共 730 行 · 第 1/2 页

JAVA
730
字号
/**
 * @(#)HpDir.java	98/06/18
 *
 * Copyright 1997-1998 by Halcyon Software Inc.,
 * All rights reserved.
 *
 * This program collect miscellaneous functions about file directory &
 * file path, it include:
 *	Dir[(pathname[, attributes])],
 *	CurDir[(drive)]
 *
 * @Version IB4J1.3
 */

package HPCore.stdfunc;

import  HPCore.Exception.*;
import  HECore.stddata.*;
import java.io.File;
import java.util.*;

public class HpDir
{
	static final    int RAND_MAX = 0x7fff; //32767
	static final    char FSEPARATOR = System.getProperty("file.separator").charAt(0);
	static String   curr_fname = "";
	static String   curr_path = "";
	static int      curr_index = -1;
	static boolean         FALSEFNAME = false;
	static String   FINDFN = null;

   /**
	* @Function: Dir[(pathname[, attributes])]
	*
	* This Function returns the name of a file or directory that
	* matches a specified pattern or file attribute, or the volume
	* label of a drive.
	*
	* Note: Dir supports the use of * (multiple character) and ? (single
	* character) wildcards to specify multiple files. You must specify
	* pathname the first time you call the Dir function, or an error
	* occurs.
	* If you also specify file attributes, pathname must be included.
	*/

   /**
	* Without arguments as if DIR$() & DIR(), they apply to second time
	* call or again, it return the next file in the same directory of
	* prevous call.
	*/
	public static String DIR$() throws HpException
	{
		File     fdir;
		String[] strArr;
		int      i;

		if (curr_fname == "" || (curr_fname != "" && curr_index == -1))
			throw new HpException(8, "Illegal function call");

		if (curr_fname.equals("."))
			curr_fname = "..";
		else if (curr_fname.equals(".."))
			curr_fname = "";
		else {  //last display is file
			fdir = new File(curr_path);
			if (!fdir.exists())
				throw new HpException( 76, "Path not found");

			strArr = fdir.list();
			if (curr_index >= strArr.length)
				curr_fname = "";
			else if( FALSEFNAME == true ) {
				curr_fname = getfn(FINDFN, curr_path);
			}
			else {
				File flist;
				for (; (curr_index < strArr.length) &&
						(flist = new File(curr_path + strArr[curr_index])).isDirectory(); )
					curr_index++;

				if (curr_index >= strArr.length)
					curr_fname = "";
				else
					curr_fname = strArr[curr_index++]; //curr_index added 1 at here
			}
		}
		return  curr_fname;
	}

	public static Variant DIR() throws HpException {
		return new VString(DIR$());
	}

	public static Variant DIR(String varfileName) throws HpException {
		return new VString(DIR$(varfileName));
	}

	public static String DIR$(String fileName) throws HpException
	{
		File        file;
		int         i, idx;
		String[]    strArr;
		char        lastChar, separaChar;

		curr_index = -1;

		if (fileName == "")
			throw new HpException(13, "Type mismatch");

		fileName = fileName.trim();
		if (fileName.length() == 0)
			fileName = System.getProperty("user.dir") + FSEPARATOR;

		if (System.getProperty("os.name").equalsIgnoreCase("Windows Nt") ||
			System.getProperty("os.name").equalsIgnoreCase("Windows 95"))
		{
			fileName = fileName.replace( '/', FSEPARATOR );
		}
		else
			fileName = fileName.replace( '\\', FSEPARATOR );

		if (fileName.length() != 1 && fileName.charAt(fileName.length() - 1) == ':')
			fileName = fileName + FSEPARATOR;

		file = new File(fileName);
		if (file.isDirectory())  //CASE: fileName is directory name.
		{
			lastChar = fileName.charAt(fileName.length() - 1);
			idx = fileName.lastIndexOf(FSEPARATOR);
			if (fileName.equals(FSEPARATOR +"..") )
				throw new HpException(52, "Bad file name or number");

			if (lastChar == '.' || lastChar == FSEPARATOR)
			{
				File  flist;
				if (lastChar == FSEPARATOR )
					curr_path = fileName;
				else
					curr_path = fileName + FSEPARATOR;

				strArr = file.list();
				for (i = 0; (i < strArr.length) &&
					(flist = new File(curr_path + strArr[i])).isDirectory(); i++)
					;

				if (i < strArr.length)
				{
					curr_fname = strArr[i];
					curr_index = i + 1;
				}
				else
					curr_fname = ""; //no file on a directroy
			}
			else  //CASE: "?\directoryName"
				curr_fname = "";
		}
		else   //CASE: "?fileName"
		{
			File tmpf;

			String  tmpcurr_path = file.getParent();
			if (tmpcurr_path != null)
			{
				tmpcurr_path = tmpcurr_path + FSEPARATOR;
				if (tmpcurr_path.charAt(0) == ' ' )
					throw new HpException( 76, "Path not found");
			}

			String tempfn = file.getName();
			if (tmpcurr_path == null && tempfn != null )
				tmpcurr_path = System.getProperty("user.dir");

			if (tempfn.charAt(0) == ' ' || tempfn.charAt(0) == '.')
				return "";
			FINDFN  = tempfn;
			FALSEFNAME = true;
			tmpf = new File(tmpcurr_path);
			if (!tmpf.exists())
				curr_path = null;
			else
				curr_fname = getfn(tempfn, tmpcurr_path);
		}
		return curr_fname;
	}

	static String getfn(String fname, String tempcurpath) throws HpException
	{
		File        myfile, lst;
		int         i = -1, n, chr = -1;

		if( tempcurpath.indexOf(FSEPARATOR + "..") != -1 ||
			tempcurpath.indexOf(".." + FSEPARATOR) != -1 )
			curr_path = getTruePath(tempcurpath) + FSEPARATOR;
		else
			curr_path = tempcurpath;

		myfile = new File( curr_path);
		String[] sArr = myfile.list();
		if( sArr.length != 0 ) {
			for(int temp = 0; temp < sArr.length; temp ++)
				sArr[temp] = sArr[temp].toUpperCase();
		}

		int dot = fname.indexOf('.');
		if( dot == -1 || fname.charAt(fname.length() - 1) == '.' )   //eg. aa/ aa. /*.  aa*, aa?
		{
			if( curr_index == -1 )
			{
				File fn = new File( curr_path + FSEPARATOR + fname );
				chr = fname.indexOf('*');
				if( chr != -1 )
				{
					n = 0;
					while (n < fname.length())
					{
						if( fname.charAt(n)=='?' )	n++;
						else break;
					}
					if (n != 0)
					{
						fname = fname.substring(n);
						chr = fname.indexOf('*');
					}
					if(chr == 0) //eg. "*", "**?aa"
					{
						fname = fname.substring(chr);
						if( fname.length() == 0 ) //"*"
						{
							curr_fname = sArr[0];
							curr_index = 1;
						}
						else  //"**?aa"
						{
							n=0;
							while ( n!=fname.length() )
							{
								if( fname.charAt(n)=='*' || fname.charAt(n)=='?' )
									n++;
								else break;
							}
							fname = fname.substring(n);
							if(fname.length() == 0)
								i = 0;
							else {
								for (i=0; i < sArr.length ;i++)
									if( sArr[i].indexOf(fname)!=-1 )
										break;
							}
						}
					} //end of chr==0
					else // "??aa*", "aa*aa"
					{
						fname = fname.substring(0,chr);
						n = 0;
						while(n < fname.length())
						{
							if (fname.charAt(n) == '?')
								n++;
							else break;
						}
						if(n != 0)
							fname = fname.substring(n);
						for(i=0 ; i<sArr.length; i++)
						{
							String tmp_st = sArr[i].substring(0, fname.length());
							if(tmp_st.indexOf(fname)!=-1 )
								break;
						}
					}
				}
				else if( fn.isFile() )
				{
					for (i = 0; i < sArr.length; i++)
						if( sArr[i].equalsIgnoreCase(fname) )
							break;
				}
				else
				{
					for (i=0; (i < sArr.length) &&
						((lst = new File(curr_path + sArr[i])).isDirectory() || sArr[i].indexOf('.')!=-1 ) ; i++)
						;
				}

				if (i < sArr.length && i>=0)
				{
					if( sArr[i].indexOf(fname) != -1 )
					{
						curr_fname = sArr[i];
						curr_index = i + 1;
					}
					else
						curr_fname = "";
				}
				else
					curr_fname = ""; //no file on a directroy
			} //end of curr_index==-1
			else
			{
				File fn = new File( curr_path + FSEPARATOR + fname );
				chr = fname.indexOf('*');
				if( chr != -1 )
				{
					n = 0;
					while (n < fname.length())
					{
						if(fname.charAt(n)=='?') n++;
						else break;
					}
					if (n != 0)
					{
						fname = fname.substring(n);
						chr = fname.indexOf('*');
					}
					if( chr==0 ) //eg. "*", "**?aa"
					{
						fname = fname.substring(chr);

						if ( fname.length()!=0 )  //"**?aa"
						{
							n=0;
							while ( n!=fname.length() )
							{
								if( fname.charAt(n) == '*' || fname.charAt(n)=='?' )
									n++;
								else break;
							}
							fname = fname.substring(n);
							if( fname.length()!=0 )
							{
								for ( i= curr_index; i < sArr.length ;i ++)
								if( sArr[i].indexOf(fname) != -1 )
									break;
								curr_index = i;
							}
						}
					}//end of chr==0
					else // "??aa*", "aa*aa"
					{
						fname = fname.substring(0,chr);
						n = 0;
						while(n<fname.length() )
						{
							if (fname.charAt(n)=='?') n++;
							else break;
						}
						if (n != 0)
							fname = fname.substring(n);

						for(i = curr_index; i < sArr.length; i++)
						{
							String tmp = sArr[i].substring(0,fname.length() );
							if( tmp.indexOf(fname)!=-1 )
								break;
						}
						curr_index = i;
					}
				}
				else if (fn.isFile())
				{
					for (i = curr_index; i < sArr.length; i ++)
					if( sArr[i].equalsIgnoreCase(fname) )
						break;
					curr_index = i;
				}
				else

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?