[m-rev.] For review: Java implementation of library/dir.m

James Goddard goddardjames at yahoo.com
Wed Jan 14 12:37:12 AEDT 2004


> In general, modules should only be imported in either the interface 
> or the implementation section, not both.  Modules which are imported
> in the interface are automatically visible in the implementation.

Ok, I've moved all the common imports to be just in the interface.

> If the imports don't fit on a single line, please use multiple
> import_module declarations rather than a single one line-wrapped;
> this makes them much easier to grep for.

Done.  I've also moved each import to it's own line.

> Hmm... that looks like it has a bug:
> when does the directory actually get created?

A very good question =)  I've added that call now.

> Also, you should define a Java version of dir__make_directory,
> using java.io.File.mkdirs().

Ok, I wasn't sure whether to do that, since there was already a mercury
predicate for it.

> > +		java.util.LinkedList ll = new java.util.LinkedList();
> > +		for (int i = 0; i < fileList.length; i++) {
> > +			ll.add(fileList[i]);
> > +		}
> 
> Why not use java.utils.Arrays.asList(fileList)?

Thanks! I was looking for something like that.. Relative diff below.

James

-----------------------------------------------------------------------------
diff -u library/dir.m library/dir.m
--- library/dir.m       13 Jan 2004 05:16:05 -0000
+++ library/dir.m       14 Jan 2004 01:28:40 -0000
@@ -23,7 +23,10 @@
 :- module dir.
 :- interface.

-:- import_module string, bool, io, list.
+:- import_module bool.
+:- import_module io.
+:- import_module list.
+:- import_module string.

        % predicates to isolate system dependencies

@@ -229,8 +232,12 @@

 :- implementation.

-:- import_module io, char, enum, exception, int, list, require, string,
-               std_util.
+:- import_module char.
+:- import_module enum.
+:- import_module exception.
+:- import_module int.
+:- import_module require.
+:- import_module std_util.

 dir__directory_separator = (if have_win32 then ('\\') else ('/')).
 :- pragma foreign_proc("C#", dir__directory_separator = (Sep::out),
@@ -820,6 +827,32 @@
     }
 }").

+% Java has a similar library function java.io.File.mkdirs()
+:- pragma foreign_proc("Java",
+       dir__make_directory(DirName::in, Res::out, _IO0::di, _IO::uo),
+       [may_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+       try{
+               java.io.File dir = new java.io.File(DirName);
+               if (dir.isFile()) {
+                       throw new java.lang.RuntimeException(
+                                       ""a file with that name already"" +
+                                       "" exists"");
+               }
+               if (dir.isDirectory()) {
+                       Res = check_dir_accessibility_4_p_0(DirName);
+               } else {
+                       if (!dir.mkdirs()) {
+                               throw new java.lang.RuntimeException(
+                                               ""make_directory failed"");
+                       }
+                       Res = make_mkdir_res_ok_0_f_0();
+               }
+       } catch (java.lang.Exception e) {
+               Res = make_mkdir_res_error_4_p_0(e);
+       }
+").
+
 :- pred can_implement_make_directory is semidet.

 can_implement_make_directory :- semidet_fail.
@@ -951,6 +984,11 @@
                                        new java.io.IOException(
                                        ""directory already exists""));
                } else {
+                       if (!newDir.mkdir()) {
+                               throw new java.lang.RuntimeException(
+                                               ""make_single_directory"" +
+                                               "" failed"");
+                       }
                        Result = make_mkdir_res_ok_0_f_0();
                }
        } catch (java.lang.Exception e) {
@@ -1322,18 +1360,9 @@
        try {
                java.lang.String[] fileList =
                                (new java.io.File(DirName)).list();
+               java.util.List list = java.util.Arrays.asList(fileList);

-               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);
-               }
+               Result = read_first_entry_4_p_0(list.iterator());
        } catch (java.lang.Exception e) {
                Result = make_dir_open_result_error_4_p_0(e);
        }


http://personals.yahoo.com.au - Yahoo! Personals
New people, new possibilities. FREE for a limited time.
--------------------------------------------------------------------------
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