% Licensed under the Apache License, Version 2.0 (the "License"); you may not % use this file except in compliance with the License. You may obtain a copy of % the License at % % http://www.apache.org/licenses/LICENSE-2.0 % % Unless required by applicable law or agreed to in writing, software % distributed under the License is distributed on an "AS IS" BASIS, WITHOUT % WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the % License for the specific language governing permissions and limitations under % the License. -module(cap_util). -export([implode/2]). -export([abs_pathname/1,abs_pathname/2, trim/1, ascii_lower/1]). -export([encodeBase64/1, decodeBase64/1, encodeBase64Url/1, decodeBase64Url/1, to_hex/1,parse_term/1, dict_find/3]). -export([file_read_size/1]). -export([to_binary/1, to_integer/1, to_list/1]). -include("capricorn.hrl"). -include_lib("kernel/include/file.hrl"). to_hex([]) -> []; to_hex(Bin) when is_binary(Bin) -> to_hex(binary_to_list(Bin)); to_hex([H|T]) -> [to_digit(H div 16), to_digit(H rem 16) | to_hex(T)]. to_digit(N) when N < 10 -> $0 + N; to_digit(N) -> $a + N-10. parse_term(Bin) when is_binary(Bin) -> parse_term(binary_to_list(Bin)); parse_term(List) -> {ok, Tokens, _} = erl_scan:string(List ++ "."), erl_parse:parse_term(Tokens). % given a pathname "../foo/bar/" it gives back the fully qualified % absolute pathname. abs_pathname(" " ++ Filename) -> % strip leading whitspace abs_pathname(Filename); abs_pathname([$/ |_]=Filename) -> Filename; abs_pathname(Filename) -> {ok, Cwd} = file:get_cwd(), {Filename2, Args} = separate_cmd_args(Filename, ""), abs_pathname(Filename2, Cwd) ++ Args. abs_pathname(Filename, Dir) -> Name = filename:absname(Filename, Dir ++ "/"), OutFilename = filename:join(fix_path_list(filename:split(Name), [])), % If the filename is a dir (last char slash, put back end slash case string:right(Filename,1) of "/" -> OutFilename ++ "/"; "\\" -> OutFilename ++ "/"; _Else-> OutFilename end. % if this as an executable with arguments, seperate out the arguments % ""./foo\ bar.sh -baz=blah" -> {"./foo\ bar.sh", " -baz=blah"} separate_cmd_args("", CmdAcc) -> {lists:reverse(CmdAcc), ""}; separate_cmd_args("\\ " ++ Rest, CmdAcc) -> % handle skipped value separate_cmd_args(Rest, " \\" ++ CmdAcc); separate_cmd_args(" " ++ Rest, CmdAcc) -> {lists:reverse(CmdAcc), " " ++ Rest}; separate_cmd_args([Char|Rest], CmdAcc) -> separate_cmd_args(Rest, [Char | CmdAcc]). % lowercases string bytes that are the ascii characters A-Z. % All other characters/bytes are ignored. ascii_lower(String) -> ascii_lower(String, []). ascii_lower([], Acc) -> lists:reverse(Acc); ascii_lower([Char | RestString], Acc) when Char >= $A, Char =< $B -> ascii_lower(RestString, [Char + ($a-$A) | Acc]); ascii_lower([Char | RestString], Acc) -> ascii_lower(RestString, [Char | Acc]). % Is a character whitespace? is_whitespace($\s) -> true; is_whitespace($\t) -> true; is_whitespace($\n) -> true; is_whitespace($\r) -> true; is_whitespace(_Else) -> false. % removes leading and trailing whitespace from a string trim(String) -> String2 = lists:dropwhile(fun is_whitespace/1, String), lists:reverse(lists:dropwhile(fun is_whitespace/1, lists:reverse(String2))). % takes a heirarchical list of dirs and removes the dots ".", double dots % ".." and the corresponding parent dirs. fix_path_list([], Acc) -> lists:reverse(Acc); fix_path_list([".."|Rest], [_PrevAcc|RestAcc]) -> fix_path_list(Rest, RestAcc); fix_path_list(["."|Rest], Acc) -> fix_path_list(Rest, Acc); fix_path_list([Dir | Rest], Acc) -> fix_path_list(Rest, [Dir | Acc]). implode(List, Sep) -> implode(List, Sep, []). implode([], _Sep, Acc) -> lists:flatten(lists:reverse(Acc)); implode([H], Sep, Acc) -> implode([], Sep, [H|Acc]); implode([H|T], Sep, Acc) -> implode(T, Sep, [Sep,H|Acc]). %%% Purpose : Base 64 encoding and decoding. %%% Copied from ssl_base_64 to avoid using the %%% erlang ssl library -define(st(X,A), ((X-A+256) div 256)). %% A PEM encoding consists of characters A-Z, a-z, 0-9, +, / and %% =. Each character encodes a 6 bits value from 0 to 63 (A = 0, / = %% 63); = is a padding character. %% %% %% encode64(Bytes|Binary) -> binary %% %% Take 3 bytes a time (3 x 8 = 24 bits), and make 4 characters out of %% them (4 x 6 = 24 bits). %% encodeBase64(Bs) when is_list(Bs) -> encodeBase64(iolist_to_binary(Bs), <<>>); encodeBase64(Bs) -> encodeBase64(Bs, <<>>). encodeBase64(<>, Acc) -> <> = B, encodeBase64(Bs, <>); encodeBase64(<>, Acc) -> <> = <>, <>; encodeBase64(<>, Acc) -> <> = <>, <>; encodeBase64(<<>>, Acc) -> Acc. encodeBase64Url(Bs) when is_list(Bs) -> encodeBase64Url(list_to_binary(Bs), <<>>); encodeBase64Url(Bs) -> encodeBase64Url(Bs, <<>>). encodeBase64Url(<>, Acc) -> <> = B, encodeBase64Url(Bs, <>); encodeBase64Url(<>, Acc) -> <> = <>, <>; encodeBase64Url(<>, Acc) -> <> = <>, <>; encodeBase64Url(<<>>, Acc) -> Acc. %% %% decodeBase64(BinaryChars) -> Binary %% decodeBase64(Cs) when is_list(Cs) -> decodeBase64(list_to_binary(Cs)); decodeBase64(Cs) -> decode1(Cs, <<>>). decode1(<>, Acc) -> <> = <<(dec(C1)):6, (dec(C2)):6, 0:12>>, <>; decode1(<>, Acc) -> <> = <<(dec(C1)):6, (dec(C2)):6, (dec(C3)):6, (dec(0)):6>>, <>; decode1(<>, Acc) -> Bin = <>, decode1(Cs, Bin); decode1(<<>>, Acc) -> Acc. decodeBase64Url(Cs) when is_list(Cs) -> decodeBase64Url(list_to_binary(Cs)); decodeBase64Url(Cs) -> decode1Url(Cs, <<>>). decode1Url(<>, Acc) -> <> = <<(decUrl(C1)):6, (decUrl(C2)):6, 0:12>>, <>; decode1Url(<>, Acc) -> <> = <<(decUrl(C1)):6, (decUrl(C2)):6, (decUrl(C3)):6, (decUrl(0)):6>>, <>; decode1Url(<>, Acc) -> Bin = <>, decode1Url(Cs, Bin); decode1Url(<<>>, Acc) -> Acc. %% enc/1 and dec/1 %% %% Mapping: 0-25 -> A-Z, 26-51 -> a-z, 52-61 -> 0-9, 62 -> +, 63 -> / %% enc(C) -> 65 + C + 6*?st(C,26) - 75*?st(C,52) -15*?st(C,62) + 3*?st(C,63). dec(C) -> 62*?st(C,43) + ?st(C,47) + (C-59)*?st(C,48) - 69*?st(C,65) - 6*?st(C,97). %% encUrl/1 and decUrl/1 %% %% Mapping: 0-25 -> A-Z, 26-51 -> a-z, 52-61 -> 0-9, 62 -> -, 63 -> _ %% encUrl(C) -> 65 + C + 6*?st(C,26) - 75*?st(C,52) -13*?st(C,62) + 49*?st(C,63). decUrl(C) -> 62*?st(C,45) + (C-58)*?st(C,48) - 69*?st(C,65) + 33*?st(C,95) - 39*?st(C,97). dict_find(Key, Dict, DefaultValue) -> case dict:find(Key, Dict) of {ok, Value} -> Value; error -> DefaultValue end. file_read_size(FileName) -> case file:read_file_info(FileName) of {ok, FileInfo} -> FileInfo#file_info.size; Error -> Error end. to_binary(V) when is_binary(V) -> V; to_binary(V) when is_list(V) -> try list_to_binary(V) catch _ -> list_to_binary(io_lib:format("~p", [V])) end; to_binary(V) when is_atom(V) -> list_to_binary(atom_to_list(V)); to_binary(V) -> list_to_binary(io_lib:format("~p", [V])). to_integer(V) when is_integer(V) -> V; to_integer(V) when is_list(V) -> erlang:list_to_integer(V); to_integer(V) when is_binary(V) -> erlang:list_to_integer(binary_to_list(V)). to_list(V) when is_list(V) -> V; to_list(V) when is_binary(V) -> binary_to_list(V); to_list(V) when is_atom(V) -> atom_to_list(V); to_list(V) -> lists:flatten(io_lib:format("~p", [V])).