qse/ase/awk/StdAwk.java

845 lines
17 KiB
Java
Raw Normal View History

2006-11-24 13:25:12 +00:00
/*
2007-02-01 07:24:00 +00:00
* $Id: StdAwk.java,v 1.17 2007-02-01 07:23:59 bacon Exp $
2006-11-24 13:25:12 +00:00
*/
package ase.awk;
import java.io.*;
public abstract class StdAwk extends Awk
{
2007-01-23 14:23:18 +00:00
private Reader src_in = null;
private Writer src_out = null;
2006-11-24 15:37:29 +00:00
2006-11-25 15:51:57 +00:00
private String[] sin = null;
private int sin_no = 0;
2007-01-23 14:23:18 +00:00
private String[] sout = null;
private int sout_no = 0;
2006-11-25 15:51:57 +00:00
private String[] cin = null;
private int cin_no = 0;
private String[] cout = null;
private int cout_no = 0;
2006-11-24 13:25:12 +00:00
2006-12-02 16:26:29 +00:00
private long seed;
private java.util.Random random;
2007-01-23 14:23:18 +00:00
private static Reader stdin = null;
private static Writer stdout = null;
static
{
stdin = new BufferedReader (
new InputStreamReader (System.in));
stdout = new BufferedWriter (
new OutputStreamWriter (System.out));
}
2006-11-24 13:25:12 +00:00
public StdAwk () throws Exception
{
super ();
2006-12-02 16:26:29 +00:00
seed = System.currentTimeMillis();
random = new java.util.Random (seed);
2006-11-25 15:51:57 +00:00
}
2006-11-24 13:25:12 +00:00
2006-11-26 15:55:44 +00:00
/* == major methods == */
2006-11-25 15:51:57 +00:00
public void parse () throws Exception
{
2007-01-23 14:23:18 +00:00
sin = sourceInputNames (); sin_no = 0;
sout = sourceOutputNames (); sout_no = 0;
2006-11-25 15:51:57 +00:00
super.parse ();
}
2006-11-24 15:37:29 +00:00
2007-02-01 07:24:00 +00:00
public void run (String main, String[] args) throws Exception
2006-11-25 15:51:57 +00:00
{
2007-01-23 14:23:18 +00:00
cin = consoleInputNames (); cin_no = 0;
cout = consoleOutputNames (); cout_no = 0;
2007-02-01 07:24:00 +00:00
super.run (main, args);
}
public void run (String main) throws Exception
{
run (main, null);
}
public void run (String[] args) throws Exception
{
run (null, args);
2007-01-31 09:31:29 +00:00
}
public void run () throws Exception
{
2007-02-01 07:24:00 +00:00
run (null, null);
2006-11-24 13:25:12 +00:00
}
2007-02-01 07:24:00 +00:00
2006-11-26 15:55:44 +00:00
/* == source code names == */
2007-01-23 14:23:18 +00:00
protected abstract String[] sourceInputNames ();
protected String[] sourceOutputNames () { return null; }
2006-11-24 15:37:29 +00:00
2006-11-26 15:55:44 +00:00
/* == console names == */
2007-01-23 14:23:18 +00:00
protected abstract String[] consoleInputNames ();
protected abstract String[] consoleOutputNames ();
2006-11-24 13:25:12 +00:00
2006-11-26 15:55:44 +00:00
/* == source code == */
protected int openSource (int mode)
2006-11-24 15:37:29 +00:00
{
if (mode == SOURCE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr;
2006-11-25 15:51:57 +00:00
sin_no = 0;
2007-01-23 14:23:18 +00:00
if (sin == null || sin_no >= sin.length) return 0;
isr = get_stream_reader (sin[sin_no]);
2006-11-25 15:51:57 +00:00
if (isr == null) return -1;
src_in = isr;
sin_no++;
2006-11-24 15:37:29 +00:00
return 1;
}
else if (mode == SOURCE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
sout_no = 0;
// when sout is null, the source output is treated
// as if the operation has been successful with
// the writeSource and closeSource method.
2006-11-25 15:51:57 +00:00
if (sout == null) return 1;
2007-01-23 14:23:18 +00:00
if (sout_no >= sin.length) return 0;
osw = get_stream_writer (sout[sout_no]);
2006-11-25 15:51:57 +00:00
if (osw == null) return -1;
2007-01-23 14:23:18 +00:00
2006-11-25 15:51:57 +00:00
src_out = osw;
2007-01-23 14:23:18 +00:00
sout_no++;
2006-11-24 15:37:29 +00:00
return 1;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int closeSource (int mode)
2006-11-24 15:37:29 +00:00
{
if (mode == SOURCE_READ)
{
2007-01-23 14:23:18 +00:00
if (src_in != StdAwk.stdin)
{
try { src_in.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 15:37:29 +00:00
return 0;
}
else if (mode == SOURCE_WRITE)
{
2006-11-25 15:51:57 +00:00
if (src_out == null) return 0;
2007-01-23 14:23:18 +00:00
if (src_out != StdAwk.stdout)
{
try { src_out.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 15:37:29 +00:00
return 0;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int readSource (char[] buf, int len)
2006-11-24 15:37:29 +00:00
{
2006-11-25 15:51:57 +00:00
try {
2006-11-26 15:55:44 +00:00
int n = src_in.read (buf, 0, len);
2006-11-25 15:51:57 +00:00
while (n == -1)
{
2007-01-23 14:23:18 +00:00
Reader isr;
if (sin == null || sin_no >= sin.length) return 0;
2006-11-25 15:51:57 +00:00
2007-01-23 14:23:18 +00:00
isr = get_stream_reader (sin[sin_no]);
2006-11-25 15:51:57 +00:00
if (isr == null) return -1;
2007-01-23 14:23:18 +00:00
if (src_in != StdAwk.stdin)
{
try { src_in.close (); }
catch (IOException ec) { /* ignore */ }
}
2006-11-25 15:51:57 +00:00
src_in = isr;
sin_no++;
n = src_in.read (buf, 0, len);
}
return n;
}
catch (IOException e)
{
return -1;
}
2006-11-24 15:37:29 +00:00
}
2006-11-26 15:55:44 +00:00
protected int writeSource (char[] buf, int len)
2006-11-24 15:37:29 +00:00
{
2006-11-25 15:51:57 +00:00
if (src_out == null) return len;
2007-01-23 14:23:18 +00:00
// only the first file is used at the moment.
// this is because the write message doesn't indicate
// the end of the output stream.
2006-11-25 15:51:57 +00:00
try { src_out.write (buf, 0, len); }
2006-11-24 15:37:29 +00:00
catch (IOException e) { return -1; }
2007-01-23 14:23:18 +00:00
2006-11-24 15:37:29 +00:00
return len;
}
2006-11-26 15:55:44 +00:00
/* == console interface == */
protected int openConsole (Extio extio)
2006-11-24 13:25:12 +00:00
{
2007-01-31 08:23:59 +00:00
//System.err.println ("[openConsole called.... name: " + extio.getName() + " mode: " + extio.getMode());
2006-11-24 13:25:12 +00:00
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
2007-01-23 14:23:18 +00:00
/*InputStream*/Reader isr;
2006-11-25 15:51:57 +00:00
cin_no = 0;
2007-01-23 14:23:18 +00:00
if (cin == null || cin_no >= cin.length) return 0;
isr = get_stream_reader (cin[cin_no]);
2006-11-24 13:25:12 +00:00
if (isr == null) return -1;
extio.setHandle (isr);
2007-01-23 14:23:18 +00:00
try { setConsoleInputName (extio, cin[cin_no]); }
catch (Exception e) { return -1; }
2006-11-24 13:25:12 +00:00
cin_no++;
return 1;
}
else if (mode == Extio.MODE_CONSOLE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
2006-11-25 15:51:57 +00:00
cout_no = 0;
2006-11-24 13:25:12 +00:00
2007-01-23 14:23:18 +00:00
if (cout == null || cout_no >= cout.length) return 0;
2007-01-31 09:31:29 +00:00
2007-01-23 14:23:18 +00:00
osw = get_stream_writer (cout[cout_no]);
2006-11-24 13:25:12 +00:00
if (osw == null) return -1;
extio.setHandle (osw);
2007-01-23 14:23:18 +00:00
try { setConsoleOutputName (extio, cout[cout_no]); }
catch (Exception e) { return -1; }
2006-11-24 13:25:12 +00:00
cout_no++;
return 1;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int closeConsole (Extio extio)
2006-11-24 13:25:12 +00:00
{
2007-01-31 08:23:59 +00:00
//System.err.println ("[closeConsole called.... name: " + extio.getName() + " mode: " + extio.getMode());
2006-11-24 13:25:12 +00:00
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr = (Reader)extio.getHandle ();
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 13:25:12 +00:00
return 0;
}
else if (mode == Extio.MODE_CONSOLE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw = (Writer)extio.getHandle ();
2006-11-24 13:25:12 +00:00
/* TODO: selective close the stream...
* system.out should not be closed??? */
2007-01-23 14:23:18 +00:00
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 13:25:12 +00:00
return 0;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int readConsole (Extio extio, char[] buf, int len)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr, tmp;
2006-11-24 13:25:12 +00:00
int n;
2007-01-23 14:23:18 +00:00
isr = (Reader)extio.getHandle ();
2006-11-24 13:25:12 +00:00
try { n = isr.read (buf, 0, len); }
catch (IOException e) { return -1; }
while (n == -1)
{
2007-01-23 14:23:18 +00:00
if (cin == null || cin_no >= cin.length) return 0;
tmp = get_stream_reader (cin[cin_no]);
2006-11-24 13:25:12 +00:00
if (tmp == null) return -1;
2007-01-23 14:23:18 +00:00
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { /* ignore */ }
}
2006-11-24 13:25:12 +00:00
extio.setHandle (tmp);
2007-01-23 14:23:18 +00:00
try { setConsoleInputName (extio, cin[cin_no]); }
catch (Exception e) { return -1; }
isr = (Reader)extio.getHandle ();
2006-11-24 13:25:12 +00:00
cin_no++;
try { n = isr.read (buf, 0, len); }
catch (IOException e) { return -1; }
}
return n;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int writeConsole (Extio extio, char[] buf, int len)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode ();
2007-01-21 13:21:14 +00:00
//System.err.println ("[writeConsole called name: " + extio.getName() + " mode: " + extio.getMode());
2006-11-24 13:25:12 +00:00
if (mode == Extio.MODE_CONSOLE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle ();
// as the write operation below doesn't
// indicate if it has reached the end, console
// can't be switched here unlike in read_console
2006-11-24 13:25:12 +00:00
try { osw.write (buf, 0, len); osw.flush (); }
catch (IOException e) { return -1; }
return len;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int flushConsole (Extio extio)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle ();
2006-11-24 15:07:18 +00:00
try { osw.flush (); }
catch (IOException e) { return -1; }
2007-01-23 14:23:18 +00:00
2006-11-24 15:07:18 +00:00
return 0;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int nextConsole (Extio extio)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr, tmp;
2006-11-24 13:25:12 +00:00
2007-01-23 14:23:18 +00:00
isr = (Reader)extio.getHandle ();
2006-11-24 13:25:12 +00:00
2007-01-23 14:23:18 +00:00
if (cin == null || cin_no >= cin.length) return 0;
tmp = get_stream_reader (cin[cin_no]);
2006-11-24 13:25:12 +00:00
if (tmp == null) return -1;
2007-01-23 14:23:18 +00:00
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { /* ignore */ }
}
2006-11-24 13:25:12 +00:00
extio.setHandle (tmp);
2007-01-23 14:23:18 +00:00
try { setConsoleInputName (extio, cin[cin_no]); }
catch (Exception e) { return -1; }
2006-11-24 13:25:12 +00:00
cin_no++;
return 1;
}
else if (mode == Extio.MODE_CONSOLE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw, tmp;
2006-11-24 13:25:12 +00:00
2007-01-23 14:23:18 +00:00
osw = (Writer)extio.getHandle ();
2006-11-24 13:25:12 +00:00
2007-01-23 14:23:18 +00:00
if (cout == null || cout_no >= cout.length) return 0;
tmp = get_stream_writer (cout[cout_no]);
2006-11-24 13:25:12 +00:00
if (tmp == null) return -1;
2007-01-23 14:23:18 +00:00
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { /* ignore */ }
}
2006-11-24 13:25:12 +00:00
extio.setHandle (tmp);
2007-01-23 14:23:18 +00:00
try { setConsoleOutputName (extio, cout[cout_no]); }
catch (Exception e) { return -1;}
2006-11-24 13:25:12 +00:00
cout_no++;
return 1;
}
return -1;
}
2007-01-23 14:23:18 +00:00
private Reader get_stream_reader (String name)
2006-11-24 13:25:12 +00:00
{
2007-01-23 14:23:18 +00:00
Reader isr;
2006-11-24 13:25:12 +00:00
if (name == null || name.length() == 0)
{
2007-01-23 14:23:18 +00:00
//FileInputStream fis;
//fis = new FileInputStream (FileDescriptor.in);
//isr = new BufferedReader (new InputStreamReader (fis));
//isr = new BufferedReader (
// new InputStreamReader (System.in));
isr = StdAwk.stdin;
2006-11-24 13:25:12 +00:00
}
else
{
FileInputStream fis;
try { fis = new FileInputStream (name); }
catch (IOException e) { return null; }
2007-01-23 14:23:18 +00:00
isr = new BufferedReader(new InputStreamReader (fis));
2006-11-24 13:25:12 +00:00
}
return isr;
}
2007-01-23 14:23:18 +00:00
private Writer get_stream_writer (String name)
2006-11-24 13:25:12 +00:00
{
2007-01-23 14:23:18 +00:00
Writer osw;
2006-11-24 13:25:12 +00:00
if (name == null || name.length() == 0)
{
2007-01-23 14:23:18 +00:00
//FileOutputStream fos;
//fos = new FileOutputStream (FileDescriptor.out);
//osw = new BufferedWriter (new OutputStreamWriter (fos));
//osw = new BufferedWriter(
// new OutputStreamWriter (System.out));
osw = StdAwk.stdout;
2006-11-24 13:25:12 +00:00
}
else
{
FileOutputStream fos;
try { fos = new FileOutputStream (name); }
catch (IOException e) { return null; }
2007-01-23 14:23:18 +00:00
osw = new BufferedWriter (new OutputStreamWriter (fos));
2006-11-24 13:25:12 +00:00
}
return osw;
}
2006-11-26 15:55:44 +00:00
/* == file interface == */
protected int openFile (Extio extio)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_FILE_READ)
{
FileInputStream fis;
2007-01-23 14:23:18 +00:00
Reader isr;
2006-11-24 13:25:12 +00:00
try { fis = new FileInputStream (extio.getName()); }
catch (IOException e) { return -1; }
2007-01-23 14:23:18 +00:00
isr = new BufferedReader (new InputStreamReader (fis));
2006-11-24 13:25:12 +00:00
extio.setHandle (isr);
return 1;
}
else if (mode == Extio.MODE_FILE_WRITE)
{
FileOutputStream fos;
2007-01-23 14:23:18 +00:00
Writer osw;
2006-11-24 13:25:12 +00:00
try { fos = new FileOutputStream (extio.getName()); }
catch (IOException e) { return -1; }
2007-01-23 14:23:18 +00:00
osw = new BufferedWriter (new OutputStreamWriter (fos));
2006-11-24 13:25:12 +00:00
extio.setHandle (osw);
return 1;
}
else if (mode == Extio.MODE_FILE_APPEND)
{
FileOutputStream fos;
2007-01-23 14:23:18 +00:00
Writer osw;
2006-11-24 13:25:12 +00:00
try { fos = new FileOutputStream (extio.getName(), true); }
catch (IOException e) { return -1; }
2007-01-23 14:23:18 +00:00
osw = new BufferedWriter (new OutputStreamWriter (fos));
2006-11-24 13:25:12 +00:00
extio.setHandle (osw);
return 1;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int closeFile (Extio extio)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_FILE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr;
isr = (Reader)extio.getHandle();
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 13:25:12 +00:00
return 0;
}
else if (mode == Extio.MODE_FILE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle();
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 13:25:12 +00:00
return 0;
}
else if (mode == Extio.MODE_FILE_APPEND)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle();
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 13:25:12 +00:00
return 0;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int readFile (Extio extio, char[] buf, int len)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_FILE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr;
isr = (Reader)extio.getHandle();
2006-11-24 13:25:12 +00:00
try
{
len = isr.read (buf, 0, len);
if (len == -1) len = 0;
}
catch (IOException e) { len = -1; }
return len;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int writeFile (Extio extio, char[] buf, int len)
2006-11-24 13:25:12 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_FILE_WRITE ||
mode == Extio.MODE_FILE_APPEND)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle();
2006-11-24 13:25:12 +00:00
try { osw.write (buf, 0, len); }
catch (IOException e) { len = -1; }
return len;
}
return -1;
}
2006-11-24 15:07:18 +00:00
2006-11-26 15:55:44 +00:00
protected int flushFile (Extio extio)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode ();
if (mode == Extio.MODE_FILE_WRITE ||
mode == Extio.MODE_FILE_APPEND)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle ();
2006-11-24 15:07:18 +00:00
try { osw.flush (); }
catch (IOException e) { return -1; }
return 0;
}
return -1;
}
2006-11-26 15:55:44 +00:00
/* == pipe interface == */
protected int openPipe (Extio extio)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_PIPE_READ)
{
Process proc;
2007-01-23 14:23:18 +00:00
Reader isr;
2006-11-24 15:07:18 +00:00
2006-12-03 06:53:25 +00:00
try { proc = popen (extio.getName()); }
2006-11-24 15:07:18 +00:00
catch (IOException e) { return -1; }
2006-12-03 06:53:25 +00:00
2007-01-23 14:23:18 +00:00
isr = new BufferedReader (new InputStreamReader (proc.getInputStream()));
2006-11-24 15:07:18 +00:00
extio.setHandle (isr);
return 1;
}
else if (mode == Extio.MODE_PIPE_WRITE)
{
Process proc;
2007-01-23 14:23:18 +00:00
Writer osw;
2006-11-24 15:07:18 +00:00
2006-12-03 06:53:25 +00:00
try { proc = popen (extio.getName()); }
2006-11-24 15:07:18 +00:00
catch (IOException e) { return -1; }
2006-12-03 06:53:25 +00:00
2007-01-23 14:23:18 +00:00
osw = new BufferedWriter (new OutputStreamWriter (proc.getOutputStream()));
2006-11-24 15:07:18 +00:00
extio.setHandle (osw);
return 1;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int closePipe (Extio extio)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_PIPE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr;
isr = (Reader)extio.getHandle();
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 15:07:18 +00:00
return 0;
}
else if (mode == Extio.MODE_PIPE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle();
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { return -1; }
}
2006-11-24 15:07:18 +00:00
return 0;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int readPipe (Extio extio, char[] buf, int len)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_PIPE_READ)
{
2007-01-23 14:23:18 +00:00
Reader isr;
isr = (Reader)extio.getHandle();
2006-11-24 15:07:18 +00:00
try
{
len = isr.read (buf, 0, len);
if (len == -1) len = 0;
}
catch (IOException e) { len = -1; }
return len;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int writePipe (Extio extio, char[] buf, int len)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode();
if (mode == Extio.MODE_PIPE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle();
2006-11-24 15:07:18 +00:00
try { osw.write (buf, 0, len); }
catch (IOException e) { len = -1; }
return len;
}
return -1;
}
2006-11-26 15:55:44 +00:00
protected int flushPipe (Extio extio)
2006-11-24 15:07:18 +00:00
{
int mode = extio.getMode ();
if (mode == Extio.MODE_PIPE_WRITE)
{
2007-01-23 14:23:18 +00:00
Writer osw;
osw = (Writer)extio.getHandle ();
2006-11-24 15:07:18 +00:00
try { osw.flush (); }
catch (IOException e) { return -1; }
return 0;
}
return -1;
}
2006-12-02 16:26:29 +00:00
/* == arithmetic built-in functions */
2007-01-24 14:21:30 +00:00
public Object sin (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
return new Double (Math.sin(x));
}
2007-01-24 14:21:30 +00:00
public Object cos (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
return new Double (Math.cos(x));
}
2007-01-24 14:21:30 +00:00
public Object tan (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
return new Double (Math.tan(x));
}
2007-01-24 14:21:30 +00:00
public Object atan2 (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double y = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (runid, args[1]);
return new Double (Math.atan2(y,x));
}
2007-01-24 14:21:30 +00:00
public Object log (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
return new Double (Math.log(x));
}
2007-01-24 14:21:30 +00:00
public Object exp (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
return new Double (Math.exp(x));
}
2007-01-24 14:21:30 +00:00
public Object sqrt (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
return new Double (Math.sqrt(x));
}
public Object rand (long runid, Object[] args)
{
return new Double (random.nextDouble ());
}
2007-01-24 14:21:30 +00:00
public Object srand (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
long prev_seed = seed;
seed = (args == null || args.length == 0)?
System.currentTimeMillis ():
builtinFunctionArgumentToLong (runid, args[0]);
random.setSeed (seed);
return new Long (prev_seed);
}
/* miscellaneous built-in functions */
2007-01-24 14:21:30 +00:00
public Object system (long runid, Object[] args) throws Exception
2006-12-02 16:26:29 +00:00
{
String str = builtinFunctionArgumentToString (runid, args[0]);
Process proc = null;
int n = 0;
str = builtinFunctionArgumentToString (runid, args[0]);
2006-12-03 06:53:25 +00:00
try { proc = popen (str); }
2006-12-02 16:26:29 +00:00
catch (IOException e) { n = -1; }
if (proc != null)
{
2006-12-03 06:53:25 +00:00
InputStream is;
byte[] buf = new byte[1024];
is = proc.getInputStream();
// TODO; better error handling... program execution.. io redirection???
try { while (is.read (buf) != -1) ; }
catch (IOException e) { n = -1; };
2006-12-02 16:26:29 +00:00
try { n = proc.waitFor (); }
catch (InterruptedException e)
{
proc.destroy ();
n = -1;
}
}
return new Long (n);
}
2006-12-03 06:53:25 +00:00
/* == utility functions == */
private Process popen (String command) throws IOException
{
String full;
/* TODO: consider OS names and versions */
full = System.getenv ("ComSpec");
if (full != null)
{
full = full + " /c " + command;
}
else
{
full = System.getenv ("SHELL");
if (full != null)
{
full = "/bin/sh -c \"" + command + "\"";
}
else full = command;
}
return Runtime.getRuntime().exec (full);
}
2006-11-24 13:25:12 +00:00
}