2006-10-24 06:06:16 +00:00
|
|
|
/*
|
2006-11-23 03:31:58 +00:00
|
|
|
* $Id: Awk.java,v 1.6 2006-11-23 03:31:58 bacon Exp $
|
2006-10-24 06:06:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package ase.test.awk;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
public class Awk extends ase.awk.Awk
|
|
|
|
{
|
|
|
|
private FileReader insrc;
|
|
|
|
private FileWriter outsrc;
|
|
|
|
|
2006-11-22 15:12:04 +00:00
|
|
|
private String[] cin;
|
|
|
|
private int cin_no;
|
|
|
|
|
|
|
|
private String[] cout;
|
|
|
|
private int cout_no;
|
|
|
|
|
2006-10-24 06:06:16 +00:00
|
|
|
public Awk () throws ase.awk.Exception
|
|
|
|
{
|
|
|
|
super ();
|
2006-11-22 15:12:04 +00:00
|
|
|
|
|
|
|
cin = new String[3];
|
|
|
|
cin[0] = "c1.txt";
|
|
|
|
cin[1] = "c2.txt";
|
|
|
|
cin[2] = "c3.txt";
|
|
|
|
cin_no = 0;
|
|
|
|
|
2006-11-23 03:31:58 +00:00
|
|
|
cout = new String[3];
|
|
|
|
cout[0] = "c4.txt";
|
|
|
|
cout[1] = "c5.txt";
|
|
|
|
cout[2] = "";
|
2006-11-22 15:12:04 +00:00
|
|
|
cout_no = 0;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected int open_source (int mode)
|
|
|
|
{
|
|
|
|
if (mode == SOURCE_READ)
|
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
try { insrc = new FileReader ("t.awk"); }
|
2006-10-24 06:06:16 +00:00
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (mode == SOURCE_WRITE)
|
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
try { outsrc = new FileWriter ("t.out"); }
|
2006-10-24 06:06:16 +00:00
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int close_source (int mode)
|
|
|
|
{
|
|
|
|
if (mode == SOURCE_READ)
|
|
|
|
{
|
|
|
|
try { insrc.close (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (mode == SOURCE_WRITE)
|
|
|
|
{
|
|
|
|
try { outsrc.close (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int read_source (char[] buf, int len)
|
|
|
|
{
|
|
|
|
try { return insrc.read (buf, 0, len); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int write_source (char[] buf, int len)
|
|
|
|
{
|
|
|
|
try { outsrc.write (buf, 0, len); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-11-22 15:12:04 +00:00
|
|
|
/* ===== console ===== */
|
2006-11-21 15:06:51 +00:00
|
|
|
protected int open_console (ase.awk.Extio extio)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
System.err.println ("[open_console called.... name: " + extio.getName() + " mode: " + extio.getMode());
|
|
|
|
|
|
|
|
int mode = extio.getMode ();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_CONSOLE_READ)
|
|
|
|
{
|
2006-11-22 15:12:04 +00:00
|
|
|
InputStreamReader isr;
|
|
|
|
if (cin_no >= cin.length) return 0;
|
|
|
|
isr = get_input_stream (cin[cin_no]);
|
|
|
|
if (isr == null) return -1;
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
extio.setHandle (isr);
|
2006-11-23 03:31:58 +00:00
|
|
|
setConsoleName (extio, cin[cin_no]);
|
2006-11-22 15:12:04 +00:00
|
|
|
|
|
|
|
cin_no++;
|
2006-11-21 15:06:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE)
|
|
|
|
{
|
2006-11-22 15:12:04 +00:00
|
|
|
OutputStreamWriter osw;
|
|
|
|
|
|
|
|
if (cout_no >= cout.length) return 0;
|
2006-11-23 03:31:58 +00:00
|
|
|
osw = get_output_stream (cout[cout_no]);
|
|
|
|
if (osw == null) return -1;
|
2006-11-22 15:12:04 +00:00
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
extio.setHandle (osw);
|
2006-11-23 03:31:58 +00:00
|
|
|
setOutputConsoleName (extio, cout[cout_no]);
|
|
|
|
|
|
|
|
cout_no++;
|
2006-11-21 15:06:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
protected int close_console (ase.awk.Extio extio)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
System.err.println ("[close_console called.... name: " + extio.getName() + " mode: " + extio.getMode());
|
|
|
|
|
|
|
|
int mode = extio.getMode ();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_CONSOLE_READ)
|
|
|
|
{
|
|
|
|
InputStreamReader isr = (InputStreamReader)extio.getHandle ();
|
|
|
|
try { isr.close (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE)
|
|
|
|
{
|
|
|
|
OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle ();
|
|
|
|
//try { osw.close (); }
|
|
|
|
//catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
protected int read_console (ase.awk.Extio extio, char[] buf, int len)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
int mode = extio.getMode ();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_CONSOLE_READ)
|
|
|
|
{
|
2006-11-22 15:12:04 +00:00
|
|
|
InputStreamReader isr, tmp;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
isr = (InputStreamReader)extio.getHandle ();
|
|
|
|
|
|
|
|
try { n = isr.read (buf, 0, len); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
|
|
|
|
while (n == -1)
|
|
|
|
{
|
|
|
|
if (cin_no >= cin.length) return 0;
|
|
|
|
tmp = get_input_stream (cin[cin_no]);
|
|
|
|
if (tmp == null) return -1;
|
|
|
|
|
|
|
|
try { isr.close (); }
|
|
|
|
catch (IOException e) { /* ignore */ }
|
|
|
|
|
|
|
|
extio.setHandle (tmp);
|
2006-11-23 03:31:58 +00:00
|
|
|
setConsoleName (extio, cin[cin_no]);
|
2006-11-22 15:12:04 +00:00
|
|
|
isr = (InputStreamReader)extio.getHandle ();
|
|
|
|
cin_no++;
|
|
|
|
|
|
|
|
try { n = isr.read (buf, 0, len); }
|
|
|
|
catch (IOException e) { return -1; }
|
2006-11-21 15:06:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-22 15:12:04 +00:00
|
|
|
return n;
|
2006-11-21 15:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
protected int write_console (ase.awk.Extio extio, char[] buf, int len)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
int mode = extio.getMode ();
|
|
|
|
|
2006-11-22 15:12:04 +00:00
|
|
|
if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE)
|
2006-11-21 15:06:51 +00:00
|
|
|
{
|
|
|
|
OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle ();
|
2006-11-23 03:31:58 +00:00
|
|
|
// as the write operation below doesn't indicate
|
|
|
|
// if it has reached the end, console can't be
|
|
|
|
// switched here unlike read_console.
|
2006-11-21 15:06:51 +00:00
|
|
|
try { osw.write (buf, 0, len); osw.flush (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2006-11-22 15:12:04 +00:00
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-22 05:58:26 +00:00
|
|
|
protected int next_console (ase.awk.Extio extio)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-22 15:12:04 +00:00
|
|
|
int mode = extio.getMode ();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_CONSOLE_READ)
|
|
|
|
{
|
|
|
|
InputStreamReader isr, tmp;
|
|
|
|
|
|
|
|
isr = (InputStreamReader)extio.getHandle ();
|
|
|
|
|
|
|
|
if (cin_no >= cin.length) return 0;
|
|
|
|
tmp = get_input_stream (cin[cin_no]);
|
|
|
|
if (tmp == null) return -1;
|
|
|
|
|
|
|
|
try { isr.close (); }
|
|
|
|
catch (IOException e) { /* ignore */ }
|
|
|
|
|
|
|
|
extio.setHandle (tmp);
|
2006-11-23 03:31:58 +00:00
|
|
|
setConsoleName (extio, cin[cin_no]);
|
2006-11-22 15:12:04 +00:00
|
|
|
|
|
|
|
cin_no++;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-11-23 03:31:58 +00:00
|
|
|
else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE)
|
|
|
|
{
|
|
|
|
OutputStreamWriter osw, tmp;
|
|
|
|
|
|
|
|
osw = (OutputStreamWriter)extio.getHandle ();
|
|
|
|
|
|
|
|
if (cout_no >= cout.length) return 0;
|
|
|
|
tmp = get_output_stream (cout[cout_no]);
|
|
|
|
if (tmp == null) return -1;
|
|
|
|
|
|
|
|
try { osw.close (); }
|
|
|
|
catch (IOException e) { /* ignore */ }
|
|
|
|
|
|
|
|
extio.setHandle (tmp);
|
|
|
|
setOutputConsoleName (extio, cout[cout_no]);
|
|
|
|
|
|
|
|
cout_no++;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-11-22 15:12:04 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private InputStreamReader get_input_stream (String name)
|
|
|
|
{
|
|
|
|
InputStreamReader isr;
|
|
|
|
|
|
|
|
if (name == null || name.length() == 0)
|
|
|
|
{
|
|
|
|
isr = new InputStreamReader (System.in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FileInputStream fis;
|
|
|
|
try { fis = new FileInputStream (name); }
|
|
|
|
catch (IOException e) { return null; }
|
|
|
|
isr = new InputStreamReader (fis);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isr;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-23 03:31:58 +00:00
|
|
|
private OutputStreamWriter get_output_stream (String name)
|
|
|
|
{
|
|
|
|
OutputStreamWriter osw;
|
|
|
|
|
|
|
|
if (name == null || name.length() == 0)
|
|
|
|
{
|
|
|
|
osw = new OutputStreamWriter (System.out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FileOutputStream fos;
|
|
|
|
try { fos = new FileOutputStream (name); }
|
|
|
|
catch (IOException e) { return null; }
|
|
|
|
osw = new OutputStreamWriter (fos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return osw;
|
|
|
|
}
|
|
|
|
|
2006-11-22 15:12:04 +00:00
|
|
|
/* ===== file ===== */
|
2006-10-24 06:06:16 +00:00
|
|
|
public int open_file (ase.awk.Extio extio)
|
|
|
|
{
|
2006-11-22 02:57:52 +00:00
|
|
|
int mode = extio.getMode();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_FILE_READ)
|
|
|
|
{
|
|
|
|
FileInputStream fis;
|
|
|
|
InputStreamReader isr;
|
|
|
|
|
|
|
|
try { fis = new FileInputStream (extio.getName()); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
|
|
|
|
isr = new InputStreamReader (fis);
|
|
|
|
extio.setHandle (isr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (mode == ase.awk.Extio.MODE_FILE_WRITE)
|
|
|
|
{
|
|
|
|
FileOutputStream fos;
|
|
|
|
OutputStreamWriter osw;
|
|
|
|
|
|
|
|
try { fos = new FileOutputStream (extio.getName()); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
|
|
|
|
osw = new OutputStreamWriter (fos);
|
|
|
|
extio.setHandle (osw);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (mode == ase.awk.Extio.MODE_FILE_APPEND)
|
|
|
|
{
|
|
|
|
FileOutputStream fos;
|
|
|
|
OutputStreamWriter osw;
|
|
|
|
|
|
|
|
try { fos = new FileOutputStream (extio.getName(), true); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
|
|
|
|
osw = new OutputStreamWriter (fos);
|
|
|
|
extio.setHandle (osw);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
public int close_file (ase.awk.Extio extio)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-22 02:57:52 +00:00
|
|
|
int mode = extio.getMode();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_FILE_READ)
|
|
|
|
{
|
|
|
|
InputStreamReader isr;
|
|
|
|
isr = (InputStreamReader)extio.getHandle();
|
|
|
|
try { isr.close (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (mode == ase.awk.Extio.MODE_FILE_WRITE)
|
|
|
|
{
|
|
|
|
OutputStreamWriter osw;
|
|
|
|
osw = (OutputStreamWriter)extio.getHandle();
|
|
|
|
try { osw.close (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (mode == ase.awk.Extio.MODE_FILE_APPEND)
|
|
|
|
{
|
|
|
|
OutputStreamWriter osw;
|
|
|
|
osw = (OutputStreamWriter)extio.getHandle();
|
|
|
|
try { osw.close (); }
|
|
|
|
catch (IOException e) { return -1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
protected int read_file (ase.awk.Extio extio, char[] buf, int len)
|
2006-10-24 06:06:16 +00:00
|
|
|
{
|
2006-11-22 02:57:52 +00:00
|
|
|
int mode = extio.getMode();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_FILE_READ)
|
|
|
|
{
|
|
|
|
InputStreamReader isr;
|
|
|
|
isr = (InputStreamReader)extio.getHandle();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
len = isr.read (buf, 0, len);
|
|
|
|
if (len == -1) len = 0;
|
|
|
|
}
|
|
|
|
catch (IOException e) { len = -1; }
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
protected int write_file (ase.awk.Extio extio, char[] buf, int len)
|
|
|
|
{
|
2006-11-22 02:57:52 +00:00
|
|
|
int mode = extio.getMode();
|
|
|
|
|
|
|
|
if (mode == ase.awk.Extio.MODE_FILE_WRITE ||
|
|
|
|
mode == ase.awk.Extio.MODE_FILE_APPEND)
|
|
|
|
{
|
|
|
|
OutputStreamWriter osw;
|
|
|
|
osw = (OutputStreamWriter)extio.getHandle();
|
|
|
|
try { osw.write (buf, 0, len); }
|
|
|
|
catch (IOException e) { len = -1; }
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
2006-10-24 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main (String[] args)
|
|
|
|
{
|
|
|
|
Awk awk = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
awk = new Awk ();
|
|
|
|
awk.parse ();
|
|
|
|
awk.run ();
|
|
|
|
}
|
|
|
|
catch (ase.awk.Exception e)
|
|
|
|
{
|
|
|
|
System.out.println ("ase.awk.Exception - " + e.getMessage());
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (awk != null) awk.close ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|