[m-rev.] diff: fix dir.*fold2 under windows

Peter Ross pro at missioncriticalit.com
Fri Jun 5 15:39:23 AEST 2009


Hi,


===================================================================


Estimated hours taken: 1
Branches: main

Fix a problem where dir.{recursive_,}foldl2 doesn't handle directories
containing a trailing slash under windows.

library/dir.m:
	Remove the trailing slash of a dir name before calling the foldl
	dir predicates except when we are at the root directory.


Index: library/dir.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/dir.m,v
retrieving revision 1.48
diff -u -r1.48 dir.m
--- library/dir.m	28 Apr 2009 02:05:54 -0000	1.48
+++ library/dir.m	5 Jun 2009 05:18:58 -0000
@@ -1224,12 +1224,30 @@
 %-----------------------------------------------------------------------------%
 
 dir.foldl2(P, DirName, T, Res, !IO) :-
-    dir.foldl2_process_dir(no, P, DirName, [], no, no, _, T, Res, !IO).
+    dir.foldl2_process_dir(no, P, fixup_dirname(DirName), [], no, no, _, T, Res, !IO).
 
 dir.recursive_foldl2(P, DirName, FollowLinks, T, Res, !IO) :-
-    dir.foldl2_process_dir(no, P, DirName, [], yes, FollowLinks, _,
+    dir.foldl2_process_dir(no, P, fixup_dirname(DirName), [], yes, FollowLinks, _,
         T, Res, !IO).
 
+    %
+    % Under windows you cannot list the files of a directory if the directory
+    % name contains a trailing slash, except when the trailing slash indicates
+    % the root directory.
+    %
+    % This function removes the trailing slash, except when we are in the
+    % root directory.
+    %
+:- func fixup_dirname(string) = string.
+
+fixup_dirname(Dir0) = Dir :-
+    DirChars = canonicalize_path_chars(string.to_char_list(Dir0)),
+    ( is_root_directory(DirChars) ->
+        Dir = Dir0
+    ;
+        Dir = string.from_char_list(remove_trailing_dir_separator(DirChars))
+    ).
+
 :- pred dir.foldl2_process_dir(bool::in,
     dir.foldl_pred(T)::in(dir.foldl_pred), string::in,
     list(file_id)::in, bool::in, bool::in, bool::out, T::in,

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list