hpdir.java

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

JAVA
730
字号
				{
					for (i=curr_index; (i < sArr.length) &&
						(( lst = new File(curr_path + sArr[i]) ).isDirectory() || sArr[i].indexOf('.')!=-1 ) ; i++)
						;
					curr_index = i;
				}

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

			}//end of curr_index!=-1
		}
		else
		{   //dot is exist.  eg. aaa.*/aa.a/*.a
			String startpart = fname.substring(0,dot);
			String endpart = fname.substring(dot);
			if( startpart.charAt(0)=='*' && endpart.charAt(1)=='*' )
			{ //*.* ||*.*k
				if( curr_index==-1 )
				{
					for (i=0; (i < sArr.length) &&
						(lst = new File(curr_path + sArr[i]) ).isDirectory(); i++)
						;
					if (i < sArr.length )
					{
						if(startpart.length() != 1 )
						{
							startpart = startpart.substring(1);
							if (sArr[i].indexOf(startpart) == -1)
								curr_fname = "";
							else
							{
								curr_fname = sArr[i];
								curr_index = i + 1;
							}
						}
						else
						{
							curr_fname = sArr[i];
							curr_index = i + 1;
						}
					}
					else
						curr_fname = ""; //no file on a directroy
				}
				else
				{
					for (; (curr_index < sArr.length) &&
						(lst = new File(curr_path + sArr[curr_index])).isDirectory(); )
						curr_index++;
					if (curr_index >= sArr.length)
						curr_fname = "";
					else
						curr_fname = sArr[curr_index++]; //curr_index added 1 at here
				}
			} //end of *.*
			else if( startpart.charAt(0)=='*' && endpart.charAt(1)!='*' )
			{  // *.aaa  || *.aaa*
				if( endpart.charAt(1)==' ' )
					return curr_fname = "";
				if( endpart.indexOf('*') != -1 )
					endpart = endpart.substring(0,endpart.indexOf('*') );
				if( curr_index==-1 )
				{
					for (i=0; (i < sArr.length) &&
						((lst = new File(curr_path + sArr[i]) ).isDirectory() || (sArr[i].toUpperCase().indexOf(endpart)==-1) ); i++)
						;
					if (i < sArr.length )
					{
						curr_fname = sArr[i];
						curr_index = i + 1;
					}
					else
                    curr_fname = ""; //no file on a directroy
				}
				else
				{
					for (; (curr_index < sArr.length) &&
						((lst = new File(curr_path + sArr[curr_index])).isDirectory() || (sArr[curr_index].toUpperCase().indexOf(endpart)==-1) ); )
						curr_index++;

					if (curr_index >= sArr.length)
						curr_fname = "";
					else
					curr_fname = sArr[curr_index++]; //curr_index added 1 at here
				}
			} // end of *.aaa || *.aaa*
			else if( startpart.charAt(0)!='*' && endpart.charAt(1)=='*' )
			{    //eg  aa*.*
				int  startid = startpart.indexOf('*');
				if ( startid != -1 )
				startpart = startpart.substring(0,startid);

				if ( curr_index == -1 )
				{
					for (i=0; (i < sArr.length) &&
						((lst = new File(curr_path + sArr[i]) ).isDirectory() || (sArr[i].toUpperCase().indexOf(startpart)==-1) ); i++)
						;
					if (i < sArr.length )
					{
						String  parts = sArr[i].substring(0,startpart.length() );

						curr_fname = sArr[i];
						curr_index = i + 1;
					}
					else
					curr_fname = ""; //no file on a directroy
				}
				else
				{
					for (; (curr_index < sArr.length) &&
						((lst = new File(curr_path + sArr[curr_index])).isDirectory() || (sArr[curr_index].toUpperCase().indexOf(startpart)==-1) ); )
						curr_index++;

					if (curr_index >= sArr.length)
						curr_fname = "";
					else
						curr_fname = sArr[curr_index++]; //curr_index added 1 at here
				}
			}  // end of aaa.*
			else if( startpart.charAt(0)!='*' && endpart.charAt(0)!='*' )
			{ //aaa.aa
				int startid = startpart.indexOf('*');
				if ( startid != -1)
					startpart = startpart.substring(0,startid);
				int endid = endpart.indexOf('*');
				if ( endid != -1 )
					endpart = endpart.substring(0,endid);

				if ( curr_index == -1 )
				{
					for (i = 0; (i < sArr.length) &&
						( (lst = new File(curr_path + sArr[i]) ).isDirectory() ||
						(sArr[i].toUpperCase().indexOf(endpart)==-1 || sArr[i].toUpperCase().indexOf(startpart)==-1 ) ); i++)
						;
					if (i < sArr.length )
					{
						curr_fname = sArr[i];
						curr_index = i + 1;
					}
					else
					curr_fname = ""; //no file on a directroy

				}
				else
				{
					for (; (curr_index < sArr.length) &&
						((lst = new File(curr_path + sArr[curr_index])).isDirectory() ||
						( (sArr[curr_index].toUpperCase().indexOf(startpart)==-1) || sArr[curr_index].toUpperCase().indexOf(endpart)==-1 )); )
						curr_index++;

					if (curr_index >= sArr.length)
						curr_fname = "";
					else
						curr_fname = sArr[curr_index++]; //curr_index added 1 at here
				}
			} // end of aaa.aa
			else
			throw new HpException(65, "Bad file name");
		}  //end of dot exist
		return curr_fname;
	}

	private static String getTruePath(String tmp)
	{
		String path = tmp;
		String curdir = System.getProperty("user.dir");
		String curdriver = curdir.substring(0,2);

        String  subs = ".." + FSEPARATOR;
        int     dotNum=0,fstSepNum=0;

    	if(path.length()>=2 && path.charAt(path.length()-1 )=='.' && path.charAt(path.length()-2)=='.' )
	    path = path + FSEPARATOR;

        if( path.compareTo(".." + FSEPARATOR)==0 )
            path = curdir + FSEPARATOR + path;
        else if(path.length()>=2 && path.charAt(1)!=':' && path.charAt(0)!=FSEPARATOR)
            path = curdriver + path;
        else if(path.length()>=2 && path.charAt(1)!=':' && path.charAt(0)==FSEPARATOR)
            path = curdriver + path.substring(1);
        else if( path.length()==1 && path.charAt(0)=='.' )
            path = curdir;

        if( path.substring(0,2).toUpperCase().compareTo(curdriver)!=0 )
            path = curdir;
        int     firstdot = path.indexOf(subs);
        String  fstpart=null, lastpart=null;
        if( firstdot!=-1 ){
            fstpart =  path.substring( 0,firstdot);
            lastpart = path.substring(firstdot);
        }

        if( fstpart!=null && lastpart!=null ){
            int     dot = lastpart.indexOf(subs);
            while( dot!=-1 ){
                dotNum++;
                if( lastpart.length()!=1)
                lastpart = lastpart.substring(dot+2);
                dot = lastpart.indexOf(subs);
                if( dot!=-1 && lastpart.charAt(1)!='.' ){
                dotNum--;
                lastpart = lastpart.substring(dot);
                dot = lastpart.indexOf(subs);
                }
            }
            String truepart = fstpart;
            int  fstsep = fstpart.indexOf(FSEPARATOR );
            while( fstsep!=-1 ){   // eg. /src/runtime/../.. =c:/
            fstSepNum++;
            if( fstpart.length()!=1 )
            fstpart = fstpart.substring(fstsep+1);
            fstsep = fstpart.indexOf(FSEPARATOR);
            }

            if( dotNum>=fstSepNum ){
                path = truepart;
            }
            else {
                String temppart = truepart;
                int    temp = 0, findNum = 0;
                int   truenum = 0;
                while (temp != -1) {
                    findNum++;
                    temp = temppart.indexOf(FSEPARATOR );
                    if (temppart.length() != 1) {
						temppart = temppart.substring(temp + 1);
						truenum += temp+1;
                    }
                    if (fstSepNum-dotNum == findNum) {
						if (path.charAt(path.length() - 1) == FSEPARATOR)
							path = truepart.substring(0, truenum);
						else
							path = truepart.substring(0,truenum-1);
						break;
                    }
                } //end of while
            }
		}
		return path;
	}

	public static String DIR$(String path, int flag) throws HpException
	{
		if (flag >= 0 && flag < 2)         flag = 0;
		else if (flag >= 2 && flag < 4)    flag = 2;
		else if (flag >= 4 && flag < 8)    flag = 4;
		else if (flag >= 8 && flag <16)    flag = 8;
		else if (flag >=16 && flag <64)    flag = 16;
		else  /*flag<0 || flag>63 */    throw new HpException(8, "Illegal function call");

		switch(flag) {
			case 0:
			case 2:
			case 4:
				return DIR$(path);

			case 16:
				File ff = new File(path);
				if (ff.isDirectory())
				{
					int fp = path.lastIndexOf(FSEPARATOR);
					if (fp != -1 && fp != path.length() - 1)
						path = path.substring(fp + 1);
					else if (fp == path.length() - 1)
						path = ".";
					return path;
				}
				else
					return DIR$(path);

			case 8:
				break;
		}
		return "";
	}

	public static Variant DIR(String path, int flag) throws HpException
	{
		return new VString(DIR$(path, flag));
	}

   /**
	* @Function: CurDir(drive)
	* The drive argument is a string expression that specifies an existing drive
	*
	* Returns the current path.
	*
	* Note: If no drive is specified or if drive is a zero-length
	* string (""), CurDir returns the path for the current drive.
	*/
	public static String CURDIR$() throws HpException {
		return System.getProperty("user.dir");
	}

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

	public static String CURDIR$(String driver) throws HpException
	{
		String  str = System.getProperty("user.dir").toLowerCase();
		String  result = null;

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

		if (System.getProperty("os.name").equalsIgnoreCase("windows Nt")    ||
			System.getProperty("os.name").equalsIgnoreCase("windows 95")    )
		{
			if (driver.length() != 0)
			{
				driver = driver.replace('/', FSEPARATOR );
				char firstChar = driver.charAt(0);
				if ( firstChar < 'a' || firstChar > 'z' )
					throw new HpException(5, "Invalid procedure call");
				else if (firstChar == str.charAt(0))
					result = str; //current disk
				else
				{
					// is not current disk
					String dstr = firstChar + ":" + FSEPARATOR;
					File f = new File(dstr);
					if( f.isDirectory()  )
						result = driver.substring(0, 1) + ":" + FSEPARATOR;
					else
						throw new HpException(68, "Device unavailable");
				}
			}
			else result = str;
		}
		else
		{
			if( driver.length()!=0 )
			{
				driver = driver.replace('\\', FSEPARATOR);

				File funix = new File(driver);
				if( funix.isDirectory() )
					result = driver;
				else if( funix.isFile() )
				{
					int  i = driver.lastIndexOf(FSEPARATOR);
					if( i==-1 )
						result = str; //curdir
					else
						result = driver.substring(0,i);
				}
				else
					throw new HpException(68, "Device unavailable");
			}
			else result = str;
		}

		Properties  props = System.getProperties();
		props.put("user.dir", result);
		return result;
	}

	public static Variant CURDIR(String driver) throws HpException {
		return new VString(CURDIR$(driver));
	}
}

⌨️ 快捷键说明

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