Skip to content

Commit

Permalink
Test and correct filelib and filename
Browse files Browse the repository at this point in the history
  • Loading branch information
bufflig committed Dec 3, 2010
1 parent 6ea8348 commit 159d79b
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 18 deletions.
3 changes: 3 additions & 0 deletions lib/kernel/test/file_name_SUITE.erl
Expand Up @@ -507,6 +507,9 @@ check_very_icky(Mod) ->
end,
?line {NumOK,NumNOK} = filelib:fold_files(".",".*",true,fun(_F,{N,M}) when is_list(_F) -> io:format("~ts~n",[_F]),{N+1,M}; (_F,{N,M}) -> io:format("~p~n",[_F]),{N,M+1} end,{0,0}),
?line ok = filelib:fold_files(".",[1076,1089,1072,124,46,42],true,fun(_F,_) -> ok end,false),
?line SF3 = unicode:characters_to_binary("åäösubfil3",file:native_name_encoding()),
?line Sorted = lists:sort([SF3,<<"åäösubfil2">>]),
?line Sorted = lists:sort(filelib:wildcard("*",<<"åäösubdir2">>)),
ok
catch
throw:need_unicode_mode ->
Expand Down
28 changes: 22 additions & 6 deletions lib/stdlib/src/filelib.erl
Expand Up @@ -47,14 +47,14 @@ wildcard(Pattern) when is_list(Pattern) ->
?HANDLE_ERROR(do_wildcard(Pattern, file)).

-spec wildcard(file:name(), file:name() | atom()) -> [file:filename()].
wildcard(Pattern, Cwd) when is_list(Pattern), is_list(Cwd) ->
wildcard(Pattern, Cwd) when is_list(Pattern), (is_list(Cwd) or is_binary(Cwd)) ->
?HANDLE_ERROR(do_wildcard(Pattern, Cwd, file));
wildcard(Pattern, Mod) when is_list(Pattern), is_atom(Mod) ->
?HANDLE_ERROR(do_wildcard(Pattern, Mod)).

-spec wildcard(file:name(), file:name(), atom()) -> [file:filename()].
wildcard(Pattern, Cwd, Mod)
when is_list(Pattern), is_list(Cwd), is_atom(Mod) ->
when is_list(Pattern), (is_list(Cwd) or is_binary(Cwd)), is_atom(Mod) ->
?HANDLE_ERROR(do_wildcard(Pattern, Cwd, Mod)).

-spec is_dir(file:name()) -> boolean().
Expand Down Expand Up @@ -118,7 +118,7 @@ do_wildcard_comp({compiled_wildcard,{exists,File}}, Mod) ->
do_wildcard_comp({compiled_wildcard,[Base|Rest]}, Mod) ->
do_wildcard_1([Base], Rest, Mod).

do_wildcard(Pattern, Cwd, Mod) when is_list(Pattern), is_list(Cwd) ->
do_wildcard(Pattern, Cwd, Mod) when is_list(Pattern), (is_list(Cwd) or is_binary(Cwd)) ->
do_wildcard_comp(do_compile_wildcard(Pattern), Cwd, Mod).

do_wildcard_comp({compiled_wildcard,{exists,File}}, Cwd, Mod) ->
Expand All @@ -127,9 +127,18 @@ do_wildcard_comp({compiled_wildcard,{exists,File}}, Cwd, Mod) ->
_ -> []
end;
do_wildcard_comp({compiled_wildcard,[current|Rest]}, Cwd0, Mod) ->
Cwd = filename:join([Cwd0]), %Slash away redundant slashes.
PrefixLen = length(Cwd)+1,
[lists:nthtail(PrefixLen, N) || N <- do_wildcard_1([Cwd], Rest, Mod)];
{Cwd,PrefixLen} = case filename:join([Cwd0]) of
Bin when is_binary(Bin) -> {Bin,byte_size(Bin)+1};
Other -> {Other,length(Other)+1}
end, %Slash away redundant slashes.
[
if
is_binary(N) ->
<<_:PrefixLen/binary,Res/binary>> = N,
Res;
true ->
lists:nthtail(PrefixLen, N)
end || N <- do_wildcard_1([Cwd], Rest, Mod)];
do_wildcard_comp({compiled_wildcard,[Base|Rest]}, _Cwd, Mod) ->
do_wildcard_1([Base], Rest, Mod).

Expand Down Expand Up @@ -276,6 +285,13 @@ do_wildcard_3(Base, [Pattern|Rest], Result, Mod) ->
do_wildcard_3(Base, [], Result, _Mod) ->
[Base|Result].

wildcard_4(Pattern, [File|Rest], Base, Result) when is_binary(File) ->
case wildcard_5(Pattern, binary_to_list(File)) of
true ->
wildcard_4(Pattern, Rest, Base, [join(Base, File)|Result]);
false ->
wildcard_4(Pattern, Rest, Base, Result)
end;
wildcard_4(Pattern, [File|Rest], Base, Result) ->
case wildcard_5(Pattern, File) of
true ->
Expand Down
28 changes: 19 additions & 9 deletions lib/stdlib/src/filename.erl
Expand Up @@ -151,10 +151,10 @@ win_basenameb(O) ->
basenameb(O,[<<"/">>,<<"\\">>]).
basenameb(Bin,Sep) ->
Parts = [ X || X <- binary:split(Bin,Sep,[global]),
X =:= <<>> ],
X =/= <<>> ],
if
Parts =:= [] ->
[];
<<>>;
true ->
lists:last(Parts)
end.
Expand Down Expand Up @@ -201,17 +201,19 @@ basename(Name, Ext) when is_list(Name), is_binary(Ext) ->
basename(filename_string_to_binary(Name),Ext);
basename(Name, Ext) when is_binary(Name), is_binary(Ext) ->
BName = basename(Name),
LAll = byte_size(Name),
LN = byte_size(BName),
LE = byte_size(Ext),
case LN - LE of
Neg when Neg < 0 ->
BName;
Pos ->
case BName of
<<Part:Pos/binary,Ext/binary>> ->
StartLen = LAll - Pos - LE,
case Name of
<<_:StartLen/binary,Part:Pos/binary,Ext/binary>> ->
Part;
Other ->
Other
_Other ->
BName
end
end;

Expand Down Expand Up @@ -447,7 +449,7 @@ join1b(<<UcLetter, $:, Rest/binary>>, RelativeName, [], win32)
when is_integer(UcLetter), UcLetter >= $A, UcLetter =< $Z ->
join1b(Rest, RelativeName, [$:, UcLetter+$a-$A], win32);
join1b(<<$\\,Rest/binary>>, RelativeName, Result, win32) ->
join1b(<<$/,Rest>>, RelativeName, Result, win32);
join1b(<<$/,Rest/binary>>, RelativeName, Result, win32);
join1b(<<$/,Rest/binary>>, RelativeName, [$., $/|Result], OsType) ->
join1b(Rest, RelativeName, [$/|Result], OsType);
join1b(<<$/,Rest/binary>>, RelativeName, [$/|Result], OsType) ->
Expand Down Expand Up @@ -546,6 +548,8 @@ win32_pathtype(_) -> relative.
%% rootname("/jam.src/foo.erl") -> "/jam.src/foo"

-spec rootname(file:name()) -> file:filename().
rootname(Name) when is_binary(Name) ->
list_to_binary(rootname(binary_to_list(Name))); % No need to handle unicode, . is < 128
rootname(Name0) ->
Name = flatten(Name0),
rootname(Name, [], [], major_os_type()).
Expand Down Expand Up @@ -573,6 +577,12 @@ rootname([], Root, _Ext, _OsType) ->
%% rootname("/jam.src/foo.erl", ".erl") -> "/jam.src/foo"

-spec rootname(file:name(), file:name()) -> file:filename().
rootname(Name, Ext) when is_binary(Name), is_binary(Ext) ->
list_to_binary(rootname(binary_to_list(Name),binary_to_list(Ext)));
rootname(Name, Ext) when is_binary(Name) ->
rootname(Name,filename_string_to_binary(Ext));
rootname(Name, Ext) when is_binary(Ext) ->
rootname(filename_string_to_binary(Name),Ext);
rootname(Name0, Ext0) ->
Name = flatten(Name0),
Ext = flatten(Ext0),
Expand Down Expand Up @@ -639,7 +649,7 @@ win32_splitb(<<Slash,Rest/binary>>) when ((Slash =:= $\\) orelse (Slash =:= $/))
[<<$/>> | [ X || X <- L, X =/= <<>> ]];
win32_splitb(Name) ->
L = binary:split(Name,[<<"/">>,<<"\\">>],[global]),
[<<$/>> | [ X || X <- L, X =/= <<>> ]].
[ X || X <- L, X =/= <<>> ].


unix_split(Name) ->
Expand Down Expand Up @@ -900,7 +910,7 @@ do_flatten(Atom, Tail) when is_atom(Atom) ->
atom_to_list(Atom) ++ flatten(Tail).

filename_string_to_binary(List) ->
case unicode:characters_to_binary(List,unicode,file:native_name_encoding()) of
case unicode:characters_to_binary(flatten(List),unicode,file:native_name_encoding()) of
{error,_,_} ->
erlang:error(badarg);
Bin when is_binary(Bin) ->
Expand Down
7 changes: 6 additions & 1 deletion lib/stdlib/test/filelib_SUITE.erl
Expand Up @@ -53,8 +53,11 @@ wildcard_one(Config) when is_list(Config) ->

wildcard_two(Config) when is_list(Config) ->
?line Dir = filename:join(?config(priv_dir, Config), "wildcard_two"),
?line DirB = unicode:characters_to_binary(Dir, file:native_name_encoding()),
?line ok = file:make_dir(Dir),
?line do_wildcard_1(Dir, fun(Wc) -> filelib:wildcard(Wc, Dir) end),
?line do_wildcard_1(Dir, fun(Wc) -> io:format("~p~n",[{Wc,Dir, X = filelib:wildcard(Wc, Dir)}]),X end),
?line do_wildcard_1(Dir, fun(Wc) -> io:format("~p~n",[{Wc,DirB, X = filelib:wildcard(Wc, DirB)}]),
[unicode:characters_to_list(Y,file:native_name_encoding()) || Y <- X] end),
?line do_wildcard_1(Dir, fun(Wc) -> filelib:wildcard(Wc, Dir++"/") end),
case os:type() of
{win32,_} ->
Expand Down Expand Up @@ -253,5 +256,7 @@ ensure_dir_eexist(Config) when is_list(Config) ->
%% There already is a file with the name of the directory
%% we want to create.
?line NeedFile = filename:join(Name, "file"),
?line NeedFileB = filename:join(Name, <<"file">>),
?line {error, eexist} = filelib:ensure_dir(NeedFile),
?line {error, eexist} = filelib:ensure_dir(NeedFileB),
ok.

0 comments on commit 159d79b

Please sign in to comment.