/parse_file.erl

http://github.com/matpalm/median · Erlang · 74 lines · 52 code · 21 blank · 1 comment · 0 complexity · 90c7144f5db8492824acdcbac7f0a0fb MD5 · raw file

  1. -module(parse_file).
  2. -export([to_list/1, to_dict/1, to_dict_from_binary/1, to_dict_b/1, test/1]).
  3. to_list(Filename) ->
  4. {ok,B} = file:read_file(Filename),
  5. to_list(B, []).
  6. to_list(<<>>, List) ->
  7. lists:reverse(List);
  8. to_list(Binary, List) ->
  9. { ReducedBinary, Line } = next_line_b(Binary),
  10. Int = list_to_integer(Line),
  11. to_list(ReducedBinary, [Int|List]).
  12. to_dict_from_binary(Filename) ->
  13. {ok,B} = file:read_file(Filename),
  14. binary_to_term(B).
  15. to_dict(Filename) ->
  16. {ok,File} = file:open(Filename,read),
  17. to_dict(File, dict:new(), next_line(File)).
  18. to_dict(File,Freqs,eof) ->
  19. file:close(File),
  20. Freqs;
  21. to_dict(File,Freqs,Line) ->
  22. Value = line_to_int(Line),
  23. to_dict(File, dict:update_counter(Value,1,Freqs), next_line(File)).
  24. next_line(File) ->
  25. io:get_line(File,'').
  26. line_to_int(Line) ->
  27. Line_without_cr = lists:sublist(Line, length(Line)-1),
  28. list_to_integer(Line_without_cr).
  29. to_dict_b(InFilename) ->
  30. {ok,B} = file:read_file(InFilename),
  31. to_dict_b(InFilename,B,dict:new()).
  32. to_dict_b(_InFilename, <<>>, Dict) ->
  33. Dict;
  34. to_dict_b(InFilename, Binary, Dict) ->
  35. { ReducedBinary, Line } = next_line_b(Binary),
  36. Int = list_to_integer(Line),
  37. NewDict = dict:update_counter(Int,1,Dict),
  38. to_dict_b(InFilename, ReducedBinary, NewDict).
  39. next_line_b(Binary) ->
  40. next_line_b(Binary, []).
  41. next_line_b(<<>>, _Collected) ->
  42. ignore_last_line_if_didnt_end_in_newline;
  43. next_line_b(<<"\n",Rest/binary>>, Collected) ->
  44. { Rest, binary_to_list(list_to_binary(lists:reverse(Collected))) }; % black magic voodoo line
  45. next_line_b(<<C:1/binary,Rest/binary>>, Collected) ->
  46. next_line_b(Rest, [C|Collected]).
  47. % time testing
  48. test([Call,File]) ->
  49. Start = now(),
  50. apply(?MODULE, list_to_atom(Call), [File]),
  51. Seconds = timer:now_diff(now(),Start) / 1000 / 1000,
  52. io:format("~s time ~w sec\n",[Call, Seconds]),
  53. init:stop().