Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 29 lines (25 sloc) 0.873 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ./ebin -sasl errlog_type error -boot start_sasl

main([FileName]) ->
    case erlang:open_port({spawn, "iconv -l"}, [line]) of
        Port when is_port(Port) ->
            file:write_file(FileName, generate_header_file(gather_output()));
        Error ->
            throw(Error)
    end.

%% Private function
generate_header_file(Output) ->
    Encodings = lists:usort(lists:sort(string:tokens(Output, " "))),
    [H|T] = Encodings,
    Defines = lists:foldr(fun(E, Acc) ->
        Acc ++ ",\"" ++ E ++ "\"" end,
    "-define(SUPPORTED_ENCODINGS, [\"" ++ H ++ "\"", T),
    Defines ++ ", \"\"]).\n".

gather_output() ->
    gather_output([]).

gather_output(Accum) ->
    receive
        {_Port, {data, {eol, Data}}} -> gather_output([Data ++ " "|Accum])
        after 100 -> lists:flatten(lists:reverse(Accum))
    end.
Something went wrong with that request. Please try again.