Created
January 28, 2018 20:14
-
-
Save lpgauth/7a35dde5374f855d7e50acd0392f22c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
-mode(compile). | |
%% public | |
main([]) -> | |
bert_to_bert2("./priv/bertconfs/*/*.bert"); | |
main(Wildcard) -> | |
bert_to_bert2(Wildcard). | |
%% private | |
bert_to_bert2(Wildcard) -> | |
lists:foreach(fun (Filename) -> | |
Tables = read_file(Filename), | |
Path = filename:dirname(Filename), | |
lists:foreach(fun ({Table, Terms}) -> | |
write_file(filename(Path, Table), Terms) | |
end, Tables) | |
end, filelib:wildcard(Wildcard)). | |
encode_varint(I) -> | |
encode_varint(I, []). | |
encode_varint(I, Acc) when I =< 16#7f -> | |
lists:reverse([I | Acc]); | |
encode_varint(I, Acc) -> | |
LastSevenBits = (I - ((I bsr 7) bsl 7)), | |
OtherBits = (I bsr 7), | |
NewBit = LastSevenBits bor 16#80, | |
encode_varint(OtherBits, [NewBit | Acc]). | |
filename(Path, Table) -> | |
Path ++ "/" ++ atom_to_list(Table) ++ ".bert2". | |
read_file(Filename) -> | |
{ok, File} = file:read_file(Filename), | |
binary_to_term(File). | |
write_file(Filename, Terms) -> | |
{ok, File} = file:open(Filename, [raw, write]), | |
[write_line(File, Term) || Term <- Terms], | |
file:close(File). | |
write_line(File, Term) -> | |
Bin = term_to_binary(Term), | |
file:write(File, [encode_varint(size(Bin)), Bin]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment