Skip to content

Instantly share code, notes, and snippets.

@kgadek
Last active October 6, 2015 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgadek/2921556 to your computer and use it in GitHub Desktop.
Save kgadek/2921556 to your computer and use it in GitHub Desktop.
Erlang: extract *.ini file from *.tgx package and parse it in 12 LOC
parse_ini(Filename) ->
Str = string:tokens(os:cmd(["tar JxOf ", Filename, " settings.conf"]), "\n"),
parse_ini(Str, default, []).
parse_ini([], _Group, Res) -> Res;
parse_ini([Head|Tail], Group, Res) ->
[F|R] = Str = string:strip(Head),
case F of
$# -> parse_ini(Tail, Group, Res);
$[ -> parse_ini(Tail, string:strip(R, both, $]), Res);
_ -> [K|V] = string:tokens(Str, "="),
parse_ini(Tail, Group, [{{Group, K}, string:join(V,"=")}|Res])
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment