From efbdfbe117939cd50d0252a210b9b7634de42bb4 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 9 May 2013 17:04:18 -0700 Subject: Basic file rename from rcl to rlx --- src/rlx_util.erl | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/rlx_util.erl (limited to 'src/rlx_util.erl') diff --git a/src/rlx_util.erl b/src/rlx_util.erl new file mode 100644 index 0000000..4afb26d --- /dev/null +++ b/src/rlx_util.erl @@ -0,0 +1,93 @@ +%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*- +%%% Copyright 2012 Erlware, LLC. All Rights Reserved. +%%% +%%% This file is provided to you 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. +%%%--------------------------------------------------------------------------- +%%% @author Eric Merritt +%%% @copyright (C) 2012 Erlware, LLC. +%%% +%%% @doc Trivial utility file to help handle common tasks +-module(rcl_util). + +-export([mkdir_p/1, + to_binary/1, + to_string/1, + is_error/1, + error_reason/1, + indent/1, + optional_to_string/1]). + +-define(ONE_LEVEL_INDENT, " "). +%%============================================================================ +%% types +%%============================================================================ + + +%%============================================================================ +%% API +%%============================================================================ +%% @doc Makes a directory including parent dirs if they are missing. +-spec mkdir_p(string()) -> ok | {error, Reason::file:posix()}. +mkdir_p(Path) -> + %% We are exploiting a feature of ensuredir that that creates all + %% directories up to the last element in the filename, then ignores + %% that last element. This way we ensure that the dir is created + %% and not have any worries about path names + DirName = filename:join([filename:absname(Path), "tmp"]), + filelib:ensure_dir(DirName). + +%% @doc ident to the level specified +-spec indent(non_neg_integer()) -> iolist(). +indent(Amount) when erlang:is_integer(Amount) -> + [?ONE_LEVEL_INDENT || _ <- lists:seq(1, Amount)]. + +-spec to_binary(iolist() | binary()) -> binary(). +to_binary(String) when erlang:is_list(String) -> + erlang:iolist_to_binary(String); +to_binary(Bin) when erlang:is_binary(Bin) -> + Bin. + +to_string(Binary) when erlang:is_binary(Binary) -> + erlang:binary_to_list(Binary); +to_string(Atom) when erlang:is_atom(Atom) -> + erlang:atom_to_list(Atom); +to_string(Else) when erlang:is_list(Else) -> + Else. + +%% @doc get the reason for a particular relcool error +-spec error_reason(relcool:error()) -> any(). +error_reason({error, {_, Reason}}) -> + Reason. +%% @doc check to see if the value is a relcool error +-spec is_error(relcool:error() | any()) -> boolean(). +is_error({error, _}) -> + true; +is_error(_) -> + false. + +%% @doc convert optional argument to empty string if undefined +optional_to_string(undefined) -> + ""; +optional_to_string(Value) when is_list(Value) -> + Value. + +%%%=================================================================== +%%% Test Functions +%%%=================================================================== + +-ifndef(NOTEST). +-include_lib("eunit/include/eunit.hrl"). + +-endif. -- cgit v1.2.3