Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 3 | %%% |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | -module(thrift). |
| 8 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 9 | -export([start/0, stop/0, config/1, config/2]). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 10 | |
| 11 | -include("thrift.hrl"). |
| 12 | |
| 13 | %%% |
| 14 | %%% behavior definition |
| 15 | %%% |
| 16 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 17 | start() -> |
| 18 | application:start(thrift). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 19 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 20 | stop() -> |
| 21 | application:stop(thrift). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 22 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 23 | %%% |
| 24 | %%% configuration |
| 25 | %%% |
| 26 | |
| 27 | %% go to the global file by default |
| 28 | config(Path) -> |
| 29 | config(Path, {file, ?CONFIG_FILE}). |
| 30 | |
| 31 | config(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 |
| 47 | config1(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 |
| 58 | config1([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; |
| 68 | config1([], Item) -> {value, Item}; |
| 69 | config1(Item, Source) -> config1([Item], Source). |