aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/test/small_SUITE_data/src/file_open_encoding.erl
blob: 4f1268eba83762b328a2860e6b11d77404be010b (plain) (blame)
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
%%-----------------------------------------------------------------------
%% Program that gave erroneous warnings due to missing information about
%% the {encoding, latin1 | unicode | utf8 | ...} option of file:open/3.
%%-----------------------------------------------------------------------
-module(file_open_encoding).

-export([parse/1]).

-type proplist() :: [{atom(), any()}].

-spec parse(string()) -> proplist().
parse(FileName) ->
  {ok, IoDevice} = file:open(FileName, [read, binary, {encoding, utf8}]),
  do_parse(IoDevice, []).

do_parse(IoDevice, ResultSoFar) ->
  case io:get_line(IoDevice, "") of
    eof ->
      file:close(IoDevice),
      ResultSoFar;
    <<"--"/utf8, _Comment/binary>> ->
      do_parse(IoDevice, ResultSoFar);
    Line ->
      [Key, Value] = binary:split(Line, [<<": ">>, <<"\n">>], [global, trim]),
      do_parse(IoDevice, [{binary_to_atom(Key, utf8), Value} | ResultSoFar])
 end.