[m-rev.] For review: Java implementation of library/dir.m
James Goddard
goddardjames at yahoo.com
Tue Jan 13 16:28:43 AEDT 2004
Estimated hours taken: 4
Branches: main
Implement some library procedures for the Java back end.
library/dir.m:
Updated the imports so both interface and implementation include
string and io.
Defined the dir__stream foreign_type as an java.util.Iterator.
Implemented the following procedures in Java:
dir__directory_separator/0
can_implement_make_directory/0
dir__make_single_directory_2/5
dir__open_2/4
dir__close_2/5
dir__read_entry_2/6
java/runtime/VA_PseudoTypeInfo_Struct8.java:
This new file is a workaround which allows dir.m to successfully
compile in grade Java.
Index: library/dir.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/dir.m,v
retrieving revision 1.20
diff -u -d -r1.20 dir.m
--- library/dir.m 11 Aug 2003 10:51:02 -0000 1.20
+++ library/dir.m 13 Jan 2004 05:16:05 -0000
@@ -23,7 +23,7 @@
:- module dir.
:- interface.
-:- import_module bool, io, list.
+:- import_module string, bool, io, list.
% predicates to isolate system dependencies
@@ -229,14 +229,21 @@
:- implementation.
-:- import_module char, enum, exception, int, list, require, string, std_util.
+:- import_module io, char, enum, exception, int, list, require, string,
+ std_util.
dir__directory_separator = (if have_win32 then ('\\') else ('/')).
:- pragma foreign_proc("C#", dir__directory_separator = (Sep::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Sep = System.IO.Path.DirectorySeparatorChar;
-").
+").
+
+:- pragma foreign_proc("Java", dir__directory_separator = (Sep::out),
+ [promise_pure, will_not_call_mercury, thread_safe],
+"
+ Sep = java.io.File.separatorChar;
+").
:- func dir__alt_directory_separator = char.
@@ -832,6 +839,11 @@
[will_not_call_mercury, promise_pure, thread_safe],
"SUCCESS_INDICATOR = true;"
).
+:- pragma foreign_proc("Java",
+ can_implement_make_directory,
+ [will_not_call_mercury, promise_pure, thread_safe],
+ "succeeded = true;"
+).
dir__make_single_directory(DirName, Result, !IO) :-
dir__make_single_directory_2(1, DirName, Result, !IO).
@@ -917,6 +929,35 @@
}
}").
+:- pragma foreign_proc("Java",
+ dir__make_single_directory_2(ErrorIfExists::in, DirName::in,
+ Result::out, _IO0::di, _IO::uo),
+ [may_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+ try {
+ java.io.File newDir = new java.io.File(DirName);
+ java.io.File parent = newDir.getParentFile();
+
+ if (parent == null) {
+ Result = make_mkdir_res_error_4_p_0(
+ new java.io.IOException(
+ ""can't create root directory""));
+ } else if (!parent.exists()) {
+ Result = make_mkdir_res_error_4_p_0(
+ new java.io.IOException(
+ ""parent directory does not exist""));
+ } else if (ErrorIfExists == 1 && newDir.exists()) {
+ Result = make_mkdir_res_error_4_p_0(
+ new java.io.IOException(
+ ""directory already exists""));
+ } else {
+ Result = make_mkdir_res_ok_0_f_0();
+ }
+ } catch (java.lang.Exception e) {
+ Result = make_mkdir_res_error_4_p_0(e);
+ }
+").
+
:- func dir__make_mkdir_res_ok = io__res.
:- pragma export((dir__make_mkdir_res_ok = out), "ML_make_mkdir_res_ok").
@@ -1169,6 +1210,7 @@
:- pragma foreign_type("C", dir__stream, "ML_DIR_STREAM").
:- pragma foreign_type("il", dir__stream,
"class [mscorlib]System.Collections.IEnumerator").
+:- pragma foreign_type("Java", dir__stream, "java.util.Iterator").
:- pred can_implement_dir_foldl is semidet.
@@ -1273,6 +1315,30 @@
}
}").
+:- pragma foreign_proc("Java",
+ dir__open_2(DirName::in, Result::out, _IO0::di, _IO::uo),
+ [may_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+ try {
+ java.lang.String[] fileList =
+ (new java.io.File(DirName)).list();
+
+ java.util.LinkedList ll = new java.util.LinkedList();
+ for (int i = 0; i < fileList.length; i++) {
+ ll.add(fileList[i]);
+ }
+
+ java.util.Iterator iterator = ll.listIterator(0);
+ if (!iterator.hasNext()) {
+ Result = make_dir_open_result_eof_0_f_0();
+ } else {
+ Result = read_first_entry_4_p_0(iterator);
+ }
+ } catch (java.lang.Exception e) {
+ Result = make_dir_open_result_error_4_p_0(e);
+ }
+").
+
:- pred dir__check_dir_readable(string, int, io__result({dir__stream, string}),
io__state, io__state).
:- mode dir__check_dir_readable(in, out, out, di, uo) is det.
@@ -1432,6 +1498,16 @@
Status = 1;
}").
+:- pragma foreign_proc("Java",
+ dir__close_2(_Dir::in, Status::out, Error::out, _IO0::di, _IO::uo),
+ [will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"{
+ /* Nothing to do. */
+ Error = null;
+ Status = 1;
+}").
+
+
:- pred dir__read_entry(dir__stream, io__result(string), io__state, io__state).
:- mode dir__read_entry(in, out, di, uo) is det.
@@ -1527,6 +1603,21 @@
Status = 0;
}
}").
+
+:- pragma foreign_proc("Java",
+ dir__read_entry_2(Dir::in, Status::out, Error::out, FileName::out,
+ _IO0::di, _IO::uo),
+ [will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+ if (Dir.hasNext()) {
+ FileName = (java.lang.String) Dir.next();
+ Status = 1;
+ } else {
+ FileName = null;
+ Status = -1;
+ }
+ Error = null;
+").
%-----------------------------------------------------------------------------%
Index: java/runtime/VA_PseudoTypeInfo_Struct8.java
===================================================================
RCS file: java/runtime/VA_PseudoTypeInfo_Struct8.java
diff -N java/runtime/VA_PseudoTypeInfo_Struct8.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ java/runtime/VA_PseudoTypeInfo_Struct8.java 13 Jan 2004 05:08:56 -0000
@@ -0,0 +1,16 @@
+//
+// Copyright (C) 2001-2003 The University of Melbourne.
+// This file may only be copied under the terms of the GNU Library General
+// Public License - see the file COPYING.LIB in the Mercury distribution.
+//
+
+package mercury.runtime;
+
+public class VA_PseudoTypeInfo_Struct8 extends PseudoTypeInfo {
+ public VA_PseudoTypeInfo_Struct8(
+ TypeCtorInfo_Struct type_ctor_info,
+ int arity,
+ Object[] args) {
+ // XXX stub only
+ }
+}
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list