blob: 5dc898762e80b99666720dc6417372891ccd613a [file] [log] [blame]
Christopher Piro5b3a8f72007-08-01 22:27:37 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
Christopher Piro68940292007-10-02 00:35:12 +00003%%%
Christopher Piro5b3a8f72007-08-01 22:27:37 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
7-module(thrift).
8
Christopher Piro68940292007-10-02 00:35:12 +00009-export([start/0, stop/0, config/1, config/2]).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000010
11-include("thrift.hrl").
12
13%%%
14%%% behavior definition
15%%%
16
Christopher Piro68940292007-10-02 00:35:12 +000017start() ->
18 application:start(thrift).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000019
Christopher Piro68940292007-10-02 00:35:12 +000020stop() ->
21 application:stop(thrift).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000022
Christopher Piro68940292007-10-02 00:35:12 +000023%%%
24%%% configuration
25%%%
26
27%% go to the global file by default
28config(Path) ->
29 config(Path, {file, ?CONFIG_FILE}).
30
31config(Path, Source) ->
32 case config1(Path, Source) of
33 {error, file_error, R} ->
34 ?ERROR("error opening config ~p: ~p", [Source, R]),
35 false;
36 {error, bad_item, Tuple} ->
37 ?ERROR("malformed config item ~p found at ~p in ~p", [Tuple, Path, Source]),
38 false;
39 {error, not_found} ->
40 ?ERROR("config item ~p not found in ~p", [Path, Source]),
41 false;
42 {value, V} ->
43 {value, V}
44 end.
45
46%% go to a file
47config1(Path, {file, File}) ->
48 case file:consult(File) of
49 {error, R={_,_,_}} ->
50 {error, file_error, file:format_error(R)};
51 {error, Posix} ->
52 {error, file_error, Posix};
53 {ok, List} when is_list(List) ->
54 config1(Path, List)
55 end;
56
57%% go through a list from a file or a sublist
58config1([P|Ps], List) when is_list(List) ->
59 case lists:keysearch(P, 1, List) of
60 {value, Tuple} when size(Tuple) == 2 ->
61 List1 = element(2, Tuple), %% either another list or, if Ps is [], the item itself
62 config1(Ps, List1);
63 {value, Tuple} ->
64 {error, bad_item, Tuple};
65 false ->
66 {error, not_found}
67 end;
68config1([], Item) -> {value, Item};
69config1(Item, Source) -> config1([Item], Source).