PageRenderTime 62ms CodeModel.GetById 48ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/commands/dir.java

#
Java | 117 lines | 93 code | 18 blank | 6 comment | 13 complexity | bcdd9e428a91c1fc749e6e0a1a72240a MD5 | raw file
  1package bsh.commands;
  2
  3import java.io.*;
  4import bsh.*;
  5import java.util.Date;
  6import java.util.Vector;
  7import java.util.GregorianCalendar;
  8import java.util.Calendar;
  9
 10/**
 11	This is an example of a bsh command written in Java for speed.
 12*/
 13public class dir 
 14{
 15	static final String [] months = { "Jan", "Feb", "Mar", "Apr", 
 16		"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 17
 18	public static String usage() {
 19		return "usage: dir( String dir )\n       dir()";
 20	}
 21
 22	public static void invoke( Interpreter env, NameSpace namespace ) {
 23		//String dir = getCWD( namespace );
 24		String dir = ".";
 25		invoke( env, namespace, dir );
 26	}
 27
 28	public static void invoke( 
 29		Interpreter env, NameSpace namespace, String dir ) 
 30	{
 31		File file;
 32		try {
 33			file =  env.pathToFile( dir );
 34		} catch (IOException e ) {
 35			env.println("error reading path: "+e);
 36			return;
 37		}
 38
 39		if ( !file.exists() || !file.canRead() ) {
 40			env.println( "Can't read " + file );
 41			return;
 42		}
 43		if ( !file.isDirectory() )  {
 44			env.println("'"+dir+"' is not a directory");
 45		}
 46
 47		String [] files = file.list();
 48		files = bubbleSort(files);
 49
 50		for( int i=0; i< files.length; i++ ) {
 51			File f = new File( dir + File.separator + files[i] );
 52			StringBuffer sb = new StringBuffer();
 53			sb.append( f.canRead() ? "r": "-" );
 54			sb.append( f.canWrite() ? "w": "-" );
 55			sb.append( "_" );
 56			sb.append( " ");
 57
 58			Date d = new Date(f.lastModified());
 59			GregorianCalendar c = new GregorianCalendar();
 60			c.setTime(d);
 61			int day	= c.get(Calendar.DAY_OF_MONTH);
 62			sb.append( months[ c.get(Calendar.MONTH) ] + " " + day );
 63			if ( day < 10 ) 
 64				sb.append(" ");
 65
 66			sb.append(" ");
 67
 68			// hack to get fixed length 'length' field
 69			int fieldlen = 8;
 70			StringBuffer len = new StringBuffer();
 71			for(int j=0; j<fieldlen; j++)
 72				len.append(" ");
 73			len.insert(0, f.length());
 74			len.setLength(fieldlen);
 75			// hack to move the spaces to the front
 76			int si = len.toString().indexOf(" ");
 77			if ( si != -1 ) {
 78				String pad = len.toString().substring(si);
 79				len.setLength(si);
 80				len.insert(0, pad);
 81			}
 82			
 83			sb.append( len );
 84
 85			sb.append( " " + f.getName() );
 86			if ( f.isDirectory() ) 
 87				sb.append("/");
 88
 89			env.println( sb.toString() );
 90		}
 91	}
 92
 93	public static String [] bubbleSort( String [] in ) {
 94		Vector v = new Vector();
 95		for(int i=0; i<in.length; i++)
 96			v.addElement(in[i]);
 97
 98		int n = v.size();
 99		boolean swap = true;
100		while ( swap ) {
101			swap = false;
102			for(int i=0; i<(n-1); i++)
103				if ( ((String)v.elementAt(i)).compareTo(
104						((String)v.elementAt(i+1)) ) > 0 ) {
105					String tmp = (String)v.elementAt(i+1);
106					v.removeElementAt( i+1 );
107					v.insertElementAt( tmp, i );
108					swap = true;
109				}
110		}
111
112		String [] out = new String [ n ];
113		v.copyInto(out);
114		return out;
115	}
116}
117