From dfc0469a184ae978f40cac9df20d399fdeca99a3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 17 Jul 2010 15:49:23 +0200 Subject: Read JID and server name from config file --- src/core/ephraim_config.erl | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/core/ephraim_config.erl (limited to 'src/core/ephraim_config.erl') diff --git a/src/core/ephraim_config.erl b/src/core/ephraim_config.erl new file mode 100644 index 0000000..6c49351 --- /dev/null +++ b/src/core/ephraim_config.erl @@ -0,0 +1,55 @@ +-module(ephraim_config). +-compile([debug_info, export_all]). + +-spec init() -> ok. +init() -> + register(ephraim_config, self()), + ephraim ! {self(), started}, + + {ok, Terms} = file:consult(config_path() ++ "/ephraim.cfg"), + Config = dict:from_list(lists:map(fun(Term) -> {element(1, Term), Term} end, Terms)), + + loop(Config). + + +-spec config_path() -> string(). +config_path() -> + Path = case os:getenv("XDG_CONFIG_HOME") of + false -> + os:getenv("HOME") ++ "/.config"; + ConfigHome -> + ConfigHome + end ++ "/ephraim", + case file:read_file_info(Path) of + {ok, _} -> + ok; + {error, _} -> + file:make_dir(Path) + end, + Path. + + +-spec get(atom()) -> tuple() | error. +get(Key) -> + ephraim_config ! {self(), get, Key}, + receive + {config, ok, Key, Value} -> + Value; + {config, error, Key} -> + error + end. + +-spec loop(dict()) -> ok. +loop(Config) -> + receive + stop -> + ok; + {From, get, Key} -> + case dict:find(Key, Config) of + {ok, Value} -> + From ! {config, ok, Key, Value}; + error -> + From ! {config, error, Key} + end, + loop(Config) + end. -- cgit v1.2.3