File: fix_unicode_lang.escript

package info (click to toggle)
wings3d 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,404 kB
  • sloc: erlang: 132,169; ansic: 3,779; lisp: 1,434; sh: 805; makefile: 794; cpp: 244; xml: 50
file content (28 lines) | stat: -rwxr-xr-x 780 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env escript
%% -*- erlang -*-

-mode(compile).

main(Files) ->
    [check_file(File) || File <- Files],
    ok.

check_file(File) ->
    case file:consult(File) of
	{ok, _Content} -> ignore;
	{error, _Reason} ->
	    io:format("Fixing ~s~n~n",[File]),
	    {ok, Bin} = file:read_file(File),
	    [Line1|Rest] = binary:split(Bin, [<<"\n">>]),
	    Header = add_encoding(binary_to_list(Line1)),
	    Utf8 = unicode:characters_to_binary(Rest, latin1, utf8),
	    ok = file:write_file(File, [Header|Utf8]),
	    ok
    end.

add_encoding([$%|Rest]) -> add_encoding(Rest);
add_encoding([$  |Rest]) -> add_encoding(Rest);
add_encoding("-*- " ++ L1) ->
    "mode:erlang" ++ _ = L1, %% Assert
    <<"%% -*- coding:utf-8; "/utf8,
      (list_to_binary(L1))/binary, "\n"/utf8>>.