diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-07-17 15:49:23 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-07-17 15:49:23 +0200 |
commit | dfc0469a184ae978f40cac9df20d399fdeca99a3 (patch) | |
tree | 5bc05b375bde27de3a9f27d62df058a4b7abfc60 /src/core/ephraim_config.erl | |
parent | b66c4b8549bc64aae8780a98851ea59850024a58 (diff) | |
download | ephraim-dfc0469a184ae978f40cac9df20d399fdeca99a3.tar ephraim-dfc0469a184ae978f40cac9df20d399fdeca99a3.zip |
Read JID and server name from config file
Diffstat (limited to 'src/core/ephraim_config.erl')
-rw-r--r-- | src/core/ephraim_config.erl | 55 |
1 files changed, 55 insertions, 0 deletions
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. |