diff --git a/ase/awk/Awk.java b/ase/awk/Awk.java index 230f2ba7..3f526184 100644 --- a/ase/awk/Awk.java +++ b/ase/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.7 2006-11-23 03:31:35 bacon Exp $ + * $Id: Awk.java,v 1.8 2006-11-24 13:20:48 bacon Exp $ */ package ase.awk; @@ -28,21 +28,23 @@ public abstract class Awk public native void parse () throws Exception; public native void run () throws Exception; private native void open () throws Exception; - private native int setconsolename (long run_id, String name); + private native int setfilename (long run_id, String name); + private native int setofilename (long run_id, String name); - public void setConsoleName (Extio extio, String name) //throws Exception + public void setInputConsoleName (Extio extio, String name) //throws Exception { /* TODO: setconsolename is not safe. for example, it can * crash the program if run_id is invalid. so this wrapper * needs to do some sanity check. */ //if (setconsolename (run_id, name) == -1) // throw new Exception ("cannot set the consle name"); - setconsolename (extio.getRunId(), name); + setfilename (extio.getRunId(), name); } public void setOutputConsoleName (Extio extio, String name) { // TODO: + setofilename (extio.getRunId(), name); } /* abstrace methods */ diff --git a/ase/awk/StdAwk.java b/ase/awk/StdAwk.java new file mode 100644 index 00000000..fa833c97 --- /dev/null +++ b/ase/awk/StdAwk.java @@ -0,0 +1,351 @@ +/* + * $Id: StdAwk.java,v 1.1 2006-11-24 13:20:48 bacon Exp $ + */ + +package ase.awk; + +import java.io.*; + +public abstract class StdAwk extends Awk +{ + private String[] cin; + private int cin_no; + private String[] cout; + private int cout_no; + + public StdAwk () throws Exception + { + super (); + + cin = getInputConsoleNames (); + cout = getOutputConsoleNames (); + cin_no = 0; + cout_no = 0; + } + + /* ===== standard console names ===== */ + protected abstract String[] getInputConsoleNames (); + protected abstract String[] getOutputConsoleNames (); + + /* ===== console ===== */ + protected int open_console (Extio extio) + { + System.err.println ("[open_console called.... name: " + extio.getName() + " mode: " + extio.getMode()); + + int mode = extio.getMode (); + + if (mode == Extio.MODE_CONSOLE_READ) + { + InputStreamReader isr; + if (cin_no >= cin.length) return 0; + isr = get_input_stream (cin[cin_no]); + if (isr == null) return -1; + + extio.setHandle (isr); + setInputConsoleName (extio, cin[cin_no]); + + cin_no++; + return 1; + } + else if (mode == Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw; + + if (cout_no >= cout.length) return 0; + osw = get_output_stream (cout[cout_no]); + if (osw == null) return -1; + + extio.setHandle (osw); + setOutputConsoleName (extio, cout[cout_no]); + + cout_no++; + return 1; + } + + return -1; + } + + protected int close_console (Extio extio) + { + System.err.println ("[close_console called.... name: " + extio.getName() + " mode: " + extio.getMode()); + + int mode = extio.getMode (); + + if (mode == Extio.MODE_CONSOLE_READ) + { + InputStreamReader isr = (InputStreamReader)extio.getHandle (); + try { isr.close (); } + catch (IOException e) { return -1; } + return 0; + } + else if (mode == Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); + /* TODO: selective close the stream... + * system.out should not be closed??? */ + try { osw.close (); } + catch (IOException e) { return -1; } + return 0; + } + + return -1; + } + + protected int read_console (Extio extio, char[] buf, int len) + { + int mode = extio.getMode (); + + if (mode == Extio.MODE_CONSOLE_READ) + { + 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); + setInputConsoleName (extio, cin[cin_no]); + isr = (InputStreamReader)extio.getHandle (); + cin_no++; + + try { n = isr.read (buf, 0, len); } + catch (IOException e) { return -1; } + } + + return n; + } + + return -1; + } + + protected int write_console (Extio extio, char[] buf, int len) + { + int mode = extio.getMode (); + + if (mode == Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); + // as the write operation below doesn't indicate + // if it has reached the end, console can't be + // switched here unlike read_console. + try { osw.write (buf, 0, len); osw.flush (); } + catch (IOException e) { return -1; } + + return len; + } + + return -1; + } + + protected int next_console (Extio extio) + { + int mode = extio.getMode (); + + if (mode == 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); + setInputConsoleName (extio, cin[cin_no]); + + cin_no++; + return 1; + } + else if (mode == 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; + + /* TODO: selective close the stream... + * system.out should not be closed??? */ + try { osw.close (); } + catch (IOException e) { /* ignore */ } + + extio.setHandle (tmp); + setOutputConsoleName (extio, cout[cout_no]); + + cout_no++; + return 1; + } + + 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; + } + + 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; + } + + /* ===== file ===== */ + public int open_file (Extio extio) + { + int mode = extio.getMode(); + + if (mode == 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 == 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 == 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; + } + + public int close_file (Extio extio) + { + int mode = extio.getMode(); + + if (mode == Extio.MODE_FILE_READ) + { + InputStreamReader isr; + isr = (InputStreamReader)extio.getHandle(); + try { isr.close (); } + catch (IOException e) { return -1; } + return 0; + } + else if (mode == Extio.MODE_FILE_WRITE) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle(); + try { osw.close (); } + catch (IOException e) { return -1; } + return 0; + } + else if (mode == Extio.MODE_FILE_APPEND) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle(); + try { osw.close (); } + catch (IOException e) { return -1; } + return 0; + } + + return -1; + } + + protected int read_file (Extio extio, char[] buf, int len) + { + int mode = extio.getMode(); + + if (mode == 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; + } + + return -1; + } + + protected int write_file (Extio extio, char[] buf, int len) + { + int mode = extio.getMode(); + + if (mode == Extio.MODE_FILE_WRITE || + mode == Extio.MODE_FILE_APPEND) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle(); + try { osw.write (buf, 0, len); } + catch (IOException e) { len = -1; } + return len; + } + + return -1; + } +} diff --git a/ase/awk/awk.h b/ase/awk/awk.h index c73fd223..78cab51a 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.150 2006-11-23 14:27:51 bacon Exp $ + * $Id: awk.h,v 1.151 2006-11-24 13:20:48 bacon Exp $ */ #ifndef _ASE_AWK_AWK_H_ @@ -396,7 +396,9 @@ ase_awk_val_t* ase_awk_getglobal (ase_awk_run_t* run, ase_size_t idx); int ase_awk_setglobal (ase_awk_run_t* run, ase_size_t idx, ase_awk_val_t* val); void ase_awk_setretval (ase_awk_run_t* run, ase_awk_val_t* val); -int ase_awk_setconsolename ( +int ase_awk_setfilename ( + ase_awk_run_t* run, const ase_char_t* name, ase_size_t len); +int ase_awk_setofilename ( ase_awk_run_t* run, const ase_char_t* name, ase_size_t len); int ase_awk_getrunerrnum (ase_awk_run_t* run); diff --git a/ase/awk/jni.c b/ase/awk/jni.c index 709e5933..f6f4bab2 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.22 2006-11-23 14:27:51 bacon Exp $ + * $Id: jni.c,v 1.23 2006-11-24 13:20:49 bacon Exp $ */ #include @@ -783,7 +783,7 @@ static ase_ssize_t __process_extio ( return -1; } -JNIEXPORT int JNICALL Java_ase_awk_Awk_setconsolename (JNIEnv* env, jobject obj, jlong run_id, jstring name) +JNIEXPORT int JNICALL Java_ase_awk_Awk_setfilename (JNIEnv* env, jobject obj, jlong run_id, jstring name) { ase_awk_run_t* run = (ase_awk_run_t*)run_id; const jchar* str; @@ -791,9 +791,22 @@ JNIEXPORT int JNICALL Java_ase_awk_Awk_setconsolename (JNIEnv* env, jobject obj, str = (*env)->GetStringChars (env, name, JNI_FALSE); len = (*env)->GetStringLength (env, name); - n = ase_awk_setconsolename (run, str, len); + n = ase_awk_setfilename (run, str, len); (*env)->ReleaseStringChars (env, name, str); return n; } +JNIEXPORT int JNICALL Java_ase_awk_Awk_setofilename (JNIEnv* env, jobject obj, jlong run_id, jstring name) +{ + ase_awk_run_t* run = (ase_awk_run_t*)run_id; + const jchar* str; + int len, n; + + str = (*env)->GetStringChars (env, name, JNI_FALSE); + len = (*env)->GetStringLength (env, name); + n = ase_awk_setofilename (run, str, len); + (*env)->ReleaseStringChars (env, name, str); + return n; +} + diff --git a/ase/awk/jni.def b/ase/awk/jni.def index efb83b6a..c5e2d377 100644 --- a/ase/awk/jni.def +++ b/ase/awk/jni.def @@ -5,5 +5,6 @@ EXPORTS Java_ase_awk_Awk_close Java_ase_awk_Awk_parse Java_ase_awk_Awk_run - Java_ase_awk_Awk_setconsolename + Java_ase_awk_Awk_setfilename + Java_ase_awk_Awk_setofilename diff --git a/ase/awk/jni.h b/ase/awk/jni.h index d3c7cb6d..2d69bc05 100644 --- a/ase/awk/jni.h +++ b/ase/awk/jni.h @@ -1,5 +1,5 @@ /* - * $Id: jni.h,v 1.8 2006-11-22 15:12:04 bacon Exp $ + * $Id: jni.h,v 1.9 2006-11-24 13:20:49 bacon Exp $ */ #ifndef _ASE_AWK_JNI_H_ @@ -15,7 +15,8 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv*, jobject); JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv*, jobject); JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv*, jobject); JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv*, jobject); -JNIEXPORT int JNICALL Java_ase_awk_Awk_setconsolename (JNIEnv*, jobject, jlong, jstring); +JNIEXPORT int JNICALL Java_ase_awk_Awk_setfilename (JNIEnv*, jobject, jlong, jstring); +JNIEXPORT int JNICALL Java_ase_awk_Awk_setofilename (JNIEnv*, jobject, jlong, jstring); #ifdef __cplusplus } diff --git a/ase/awk/makefile.bcc b/ase/awk/makefile.bcc index f101ff09..9921ef2d 100644 --- a/ase/awk/makefile.bcc +++ b/ase/awk/makefile.bcc @@ -3,7 +3,7 @@ OUT = aseawk C_SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c \ run.c rec.c val.c func.c misc.c extio.c rex.c JNI_SRCS = $(C_SRCS) jni.c -JAVA_SRCS = Awk.java Exception.java Extio.java +JAVA_SRCS = Exception.java Extio.java Awk.java StdAwk.java C_OBJS = $(C_SRCS:.c=.obj) JNI_OBJS = $(JNI_SRCS:.c=.obj) diff --git a/ase/awk/parse.c b/ase/awk/parse.c index a4d099b5..0b75ad85 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.206 2006-11-23 14:27:51 bacon Exp $ + * $Id: parse.c,v 1.207 2006-11-24 13:20:49 bacon Exp $ */ #include @@ -276,6 +276,7 @@ static struct __bvent __bvtab[] = { ASE_T("IGNORECASE"), 10, 0 }, { ASE_T("NF"), 2, 0 }, { ASE_T("NR"), 2, 0 }, + { ASE_T("OFILENAME"), 9, ASE_AWK_NEXTOFILE }, { ASE_T("OFMT"), 4, 0 }, { ASE_T("OFS"), 3, 0 }, { ASE_T("ORS"), 3, 0 }, diff --git a/ase/awk/run.c b/ase/awk/run.c index 8a609121..e8038840 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.283 2006-11-23 14:27:51 bacon Exp $ + * $Id: run.c,v 1.284 2006-11-24 13:20:49 bacon Exp $ */ #include @@ -474,7 +474,7 @@ void ase_awk_setretval (ase_awk_run_t* run, ase_awk_val_t* val) ase_awk_refupval (run, val); } -int ase_awk_setconsolename ( +int ase_awk_setfilename ( ase_awk_run_t* run, const ase_char_t* name, ase_size_t len) { ase_awk_val_t* tmp; @@ -498,6 +498,34 @@ int ase_awk_setconsolename ( return n; } +int ase_awk_setofilename ( + ase_awk_run_t* run, const ase_char_t* name, ase_size_t len) +{ + ase_awk_val_t* tmp; + int n; + + if (run->awk->option & ASE_AWK_NEXTOFILE) + { + if (len == 0) tmp = ase_awk_val_zls; + else + { + tmp = ase_awk_makestrval (run, name, len); + if (tmp == ASE_NULL) + { + run->errnum = ASE_AWK_ENOMEM; + return -1; + } + } + + ase_awk_refupval (run, tmp); + n = ase_awk_setglobal (run, ASE_AWK_GLOBAL_OFILENAME, tmp); + ase_awk_refdownval (run, tmp); + } + else n = 0; + + return n; +} + int ase_awk_getrunerrnum (ase_awk_run_t* run) { return run->errnum; @@ -1000,12 +1028,13 @@ static int __set_globals_to_default (ase_awk_run_t* run) static struct __gtab_t gtab[] = { - { ASE_AWK_GLOBAL_CONVFMT, DEFAULT_CONVFMT }, - { ASE_AWK_GLOBAL_FILENAME, ASE_NULL }, - { ASE_AWK_GLOBAL_OFMT, DEFAULT_OFMT }, - { ASE_AWK_GLOBAL_OFS, DEFAULT_OFS }, - { ASE_AWK_GLOBAL_ORS, DEFAULT_ORS }, - { ASE_AWK_GLOBAL_SUBSEP, DEFAULT_SUBSEP }, + { ASE_AWK_GLOBAL_CONVFMT, DEFAULT_CONVFMT }, + { ASE_AWK_GLOBAL_FILENAME, ASE_NULL }, + { ASE_AWK_GLOBAL_OFILENAME, ASE_NULL }, + { ASE_AWK_GLOBAL_OFMT, DEFAULT_OFMT }, + { ASE_AWK_GLOBAL_OFS, DEFAULT_OFS }, + { ASE_AWK_GLOBAL_ORS, DEFAULT_ORS }, + { ASE_AWK_GLOBAL_SUBSEP, DEFAULT_SUBSEP }, }; ase_awk_val_t* tmp; diff --git a/ase/awk/run.h b/ase/awk/run.h index 07a495c8..9e67785e 100644 --- a/ase/awk/run.h +++ b/ase/awk/run.h @@ -1,5 +1,5 @@ /* - * $Id: run.h,v 1.27 2006-11-23 14:27:52 bacon Exp $ + * $Id: run.h,v 1.28 2006-11-24 13:20:49 bacon Exp $ */ #ifndef _ASE_AWK_RUN_H_ @@ -90,6 +90,7 @@ enum ase_awk_global_id_t ASE_AWK_GLOBAL_IGNORECASE, ASE_AWK_GLOBAL_NF, ASE_AWK_GLOBAL_NR, + ASE_AWK_GLOBAL_OFILENAME, ASE_AWK_GLOBAL_OFMT, ASE_AWK_GLOBAL_OFS, ASE_AWK_GLOBAL_ORS, diff --git a/ase/test/awk/Awk.java b/ase/test/awk/Awk.java index 6e36df9a..34d3f6a9 100644 --- a/ase/test/awk/Awk.java +++ b/ase/test/awk/Awk.java @@ -1,12 +1,12 @@ /* - * $Id: Awk.java,v 1.6 2006-11-23 03:31:58 bacon Exp $ + * $Id: Awk.java,v 1.7 2006-11-24 13:25:12 bacon Exp $ */ package ase.test.awk; import java.io.*; -public class Awk extends ase.awk.Awk +public class Awk extends ase.awk.StdAwk { private FileReader insrc; private FileWriter outsrc; @@ -20,18 +20,24 @@ public class Awk extends ase.awk.Awk public Awk () throws ase.awk.Exception { super (); + } - cin = new String[3]; + protected String[] getInputConsoleNames () + { + String[] cin = new String[3]; cin[0] = "c1.txt"; cin[1] = "c2.txt"; cin[2] = "c3.txt"; - cin_no = 0; + return cin; + } - cout = new String[3]; + protected String[] getOutputConsoleNames () + { + String[] cout = new String[3]; cout[0] = "c4.txt"; cout[1] = "c5.txt"; cout[2] = ""; - cout_no = 0; + return cout; } protected int open_source (int mode) @@ -83,323 +89,6 @@ public class Awk extends ase.awk.Awk return len; } - /* ===== console ===== */ - protected int open_console (ase.awk.Extio extio) - { - System.err.println ("[open_console called.... name: " + extio.getName() + " mode: " + extio.getMode()); - - int mode = extio.getMode (); - - if (mode == ase.awk.Extio.MODE_CONSOLE_READ) - { - InputStreamReader isr; - if (cin_no >= cin.length) return 0; - isr = get_input_stream (cin[cin_no]); - if (isr == null) return -1; - - extio.setHandle (isr); - setConsoleName (extio, cin[cin_no]); - - cin_no++; - return 1; - } - else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) - { - OutputStreamWriter osw; - - if (cout_no >= cout.length) return 0; - osw = get_output_stream (cout[cout_no]); - if (osw == null) return -1; - - extio.setHandle (osw); - setOutputConsoleName (extio, cout[cout_no]); - - cout_no++; - return 1; - } - - return -1; - } - - protected int close_console (ase.awk.Extio extio) - { - 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; - } - - protected int read_console (ase.awk.Extio extio, char[] buf, int len) - { - int mode = extio.getMode (); - - if (mode == ase.awk.Extio.MODE_CONSOLE_READ) - { - 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); - setConsoleName (extio, cin[cin_no]); - isr = (InputStreamReader)extio.getHandle (); - cin_no++; - - try { n = isr.read (buf, 0, len); } - catch (IOException e) { return -1; } - } - - return n; - } - - return -1; - } - - protected int write_console (ase.awk.Extio extio, char[] buf, int len) - { - int mode = extio.getMode (); - - if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) - { - OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); - // as the write operation below doesn't indicate - // if it has reached the end, console can't be - // switched here unlike read_console. - try { osw.write (buf, 0, len); osw.flush (); } - catch (IOException e) { return -1; } - - return len; - } - - return -1; - } - - protected int next_console (ase.awk.Extio extio) - { - 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); - setConsoleName (extio, cin[cin_no]); - - cin_no++; - return 1; - } - 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; - } - - 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; - } - - 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; - } - - /* ===== file ===== */ - public int open_file (ase.awk.Extio extio) - { - 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; - } - - public int close_file (ase.awk.Extio extio) - { - 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; - } - - protected int read_file (ase.awk.Extio extio, char[] buf, int len) - { - 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; - } - - return -1; - } - protected int write_file (ase.awk.Extio extio, char[] buf, int len) - { - 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; - } - - return -1; - } - public static void main (String[] args) { Awk awk = null; diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index a0edc268..7e26eb1c 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.124 2006-11-23 14:31:59 bacon Exp $ + * $Id: awk.c,v 1.125 2006-11-24 13:25:12 bacon Exp $ */ #include @@ -537,7 +537,7 @@ static int open_extio_console (ase_awk_extio_t* epa) } __awk_dprintf (ASE_T(" console(r) - %s\n"), infiles[infile_no]); - if (ase_awk_setconsolename ( + if (ase_awk_setfilename ( epa->run, infiles[infile_no], ase_awk_strlen(infiles[infile_no])) == -1) {