hpstmtmisc.java

来自「一个简单的visio程序。」· Java 代码 · 共 375 行

JAVA
375
字号
/**
 * @(#)HpStmtMisc.java	98/06/25
 *
 * Copyright 1997-1998 by Halcyon Software Inc.,
 * All rights reserved.
 *
 * This program instructs miscellaneous Statements, they are:
 *	Randomize(number),	Beep(),
 *  RmDir(path),        ChDir(path),
 *  ChDrive(driver),    MkDir(path).
 *
 * @Version IB4J1.3
 */

package HPCore.stdstmt;

import HPCore.Exception.*;
import HECore.stddata.*;
import HPCore.stdfunc.*;

import java.io.File;
import java.util.*;

public class HpStmtMisc {
   /**
	* @Statement: Randomize(long)
	* Initializes the random-number generator.
	*/
	public static void RANDOMIZE(double  varnumber) throws HpException {
		Random rd = new Random();
		int  number = (int)varnumber;
		rd.setSeed(number);
	}

	public static void RANDOMIZE(short  varnumber) throws HpException {
		RANDOMIZE((double)varnumber);
	}

	public static void RANDOMIZE(int  varnumber) throws HpException {
		RANDOMIZE((double)varnumber);
	}

	public static void RANDOMIZE(HByte  varnumber) throws HpException {
		RANDOMIZE(varnumber.dblValue());
	}

	public static void RANDOMIZE(float  varnumber) throws HpException {
		RANDOMIZE((double)varnumber);
	}

	public static void RANDOMIZE(boolean  varnumber) throws HpException {
		if (varnumber)
			RANDOMIZE(-1);
		RANDOMIZE(0);
	}

	public static void RANDOMIZE(String  varnumber) throws HpException {
		if (!HpFuncMisc.isNumOfStr(varnumber))
			throw new HpException(13, "Type mismatch");
		RANDOMIZE(format.strToDbl(varnumber));
	}

	public static void RANDOMIZE(fixedString  varnumber) throws HpException {
		RANDOMIZE(varnumber.strValue());
	}

	public static void RANDOMIZE(VNull  varnumber) throws HpException {
		throw new HpException(94, "Invalid use of null");
	}

	public static void RANDOMIZE(VObject  varnumber) throws HpException {
		throw new HpException(13, "Type mismatch");
	}

	public static void RANDOMIZE(HDate  varnumber) throws HpException {
		RANDOMIZE(varnumber.dblValue());
	}

	public static void RANDOMIZE(HCurr  varnumber) throws HpException {
		RANDOMIZE(varnumber.dblValue());
	}

	public static void RANDOMIZE(Variant  varnumber) throws HpException
	{
		if (varnumber.getType() == Variant.V_NULL)
			throw new HpException(94, "Invalid use of null");
		if (varnumber.getType() == Variant.V_EMPTY)
			throw new HpException(13, "Type mismatch");

		Random rd = new Random();
		int  number = varnumber.lngValue();
		rd.setSeed(number);
	}

	public static void RANDOMIZE() throws HpException
	{
		Random    rd = new Random();
		Date      dt = new Date();
		long       time;
		time = dt.getTime();
		rd.setSeed(time);
	}

   /**
	* @Statement: Beep
	* Sounds a tone through the computer's speaker.
	*/
	public static void BEEP() throws HpException {
		java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
		toolkit.beep();
	}

	// DIR Part.
	static final    char FSEPARATOR = System.getProperty("file.separator").charAt(0);

   /**
	* @Statement: RmDir(path)
	* Removes an existing directory
	*/
	public static void RMDIR(String path) throws HpException
	{
		if (path.length() == 0)
			throw new HpException(8,"Illegal function call");

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

		File newpath = new File(path);
		if (!newpath.isDirectory())
		{
			String str = System.getProperty("user.dir");
			path = str + FSEPARATOR + path;
			newpath = new File(path);
		}
		if (!newpath.delete())
			throw new HpException(75,"Path or File access error");
	}

   /**
	* @Statement: ChDir(path)
	* Changes the current directory
	*/
	public static void CHDIR(String path) throws HpException
	{
		String   curdir = null, tempkey = null;

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

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

			File  newpath = new File(path);

			curdir = System.getProperty("user.dir").toLowerCase();
			char   firstchar = path.charAt(0);
			String curdriver = curdir.substring(0,3);

			if( System.getProperty("os.name").equalsIgnoreCase("Windows Nt") ||
				System.getProperty("os.name").equalsIgnoreCase("Windows 95")  )
			{
				if( newpath.isDirectory() )
				{
					String  subs = ".." + FSEPARATOR;
					int     dotNum=0,fstSepNum=0;

					if( path.indexOf(subs) != -1 )
						path = getTruePath( path );

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

					if( path.length()==1 && path.charAt( path.length()-1)==FSEPARATOR )
						path = curdriver;
					else if( (path.length()<3 && path.substring(0,2).equals(FSEPARATOR +".") ) ||
						( path.charAt(path.length()-1 )=='.' && path.charAt(path.length()-2)==FSEPARATOR ) )
						path = curdriver;
					else if( !path.substring(0,3).equals(curdriver) && path.charAt(1)==':' )
						path = curdir;
					else if( !path.substring(0,3).equals(curdriver) && path.charAt(0)!=FSEPARATOR  )
						path = curdriver + path;
					else if(!path.substring(0,3).equals( curdriver ) && path.charAt(0)==FSEPARATOR )
						path = curdriver.substring(0,2) + path;
				}
				else if (newpath.exists() && newpath.isAbsolute() && !newpath.isFile())
				{
					path = curdriver;
				}
				else if( newpath.exists() && curdriver.indexOf( firstchar)!=-1 )
				{
					path = curdir;
				}
				else
					throw new HpException(76, "Path not found");
			}
			else if (newpath.isFile())
			{
				int  saper = path.lastIndexOf(FSEPARATOR);
				if (saper == -1)
					path = curdir;
				else
					path = path.substring(0, saper);
			}
			else if (!newpath.isDirectory())
				throw new HpException(76, "Path not found");

			Properties props = System.getProperties();
			props.put("user.dir", path);
			System.setProperties(props);
		} catch(Exception err) {
			throw new HpException(76, "Path not found");
		}
	}

	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;
	}

   /**
	* @Statement: ChDrive(driver)
	* Changes the current drive
	*/
	public static void CHDRIVE(String driver) throws HpException
	{
		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.replace( '/', FSEPARATOR );
				String      driverstr = driver.substring(0, 1);
				driverstr = driverstr + ":" + FSEPARATOR ;
				File  f = new File(driverstr);
				if (f.exists() || f.isDirectory())
				{

					String curdir = System.getProperty("user.dir").toLowerCase();
					char firstChar = driverstr.charAt(0);

					if (firstChar != curdir.charAt(0))
					{
						Properties  props = System.getProperties();
						props.put("user.dir", driverstr);
					}
				}
				else
					throw new HpException(68 , "Device unavailable" );
			}
		}
	}

   /**
	* Statement: MkDir(path)
	* Creates a new directory
	*/
	public static void MKDIR(String mydir) throws HpException
	{
		if (mydir.length() != 0 &&
			(System.getProperty("os.name").equalsIgnoreCase("Windows Nt") ||
			System.getProperty("os.name").equalsIgnoreCase("Windows 95")  ) )
		{
			mydir = mydir.replace( '/', FSEPARATOR );
		}
		else if (mydir.length() != 0)
			mydir = mydir.replace('\\', FSEPARATOR);

		File  newpath = new File(mydir);

		if (!newpath.isDirectory() && (newpath.getPath()).indexOf(":" + FSEPARATOR )==-1  )
		{
			String str = System.getProperty("user.dir");
			mydir = str + FSEPARATOR + mydir;
			newpath = new File(mydir);
		}

		if (newpath.exists())
			throw new HpException( 75, "Path or File access error");
		else if (!newpath.mkdir())
		{
			if (!newpath.exists())
				throw new HpException( 75, "Path or File access error");
			else    
				throw new HpException( 76,"Path not found" );
		}
	}
}

⌨️ 快捷键说明

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