forked from eugenehr/erlyconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_src.pl
76 lines (63 loc) · 2.01 KB
/
gen_src.pl
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
my $argsize = scalar @ARGV;
if($argsize < 2) {
die "Usage: $0 source-file atom\n";
}
my $input = shift @ARGV;
open (my $INPUT, "<", $input) or die "Could not open $input for reading";
my %map;
while(my $line = <$INPUT>) {
chomp $line;
if ($line =~ /^0[xX](\S+)\s+0[xX](\S+)/) {
if (hex("0x$1") != hex("0x$2")) {
$map{$1} = $2;
}
}
}
close $INPUT;
my $atom = shift @ARGV;
my $output = "src/$atom.erl";
open (my $OUTPUT, ">", $output) or die "Could not open $output for writing";
my $today = strftime "%Y-%m-%d", localtime;
my $version = strftime "%Y%m%d", localtime;
print $OUTPUT "%% THIS FILE WAS AUTOMATICALLY GENERATED BY $0\n";
print $OUTPUT "%% FROM $input AT $today\n";
print $OUTPUT "-module($atom).\n";
print $OUTPUT "-vsn($version).\n";
print $OUTPUT "-export([to_unicode/1, from_unicode/1]).\n\n";
print $OUTPUT "%% Public functions\n";
foreach my $src (sort keys %map) {
# if (hex("0x$src") == hex("0x$map{$src}")) {
# print $OUTPUT "% ";
# }
print $OUTPUT "to_unicode(16#$src) -> 16#$map{$src};\n";
}
print $OUTPUT <<L1;
to_unicode(List) when is_list(List) -> [to_unicode(C) || C <- List];
to_unicode(Bin) when is_binary(Bin) -> bin_to_unicode(Bin, <<>>);
to_unicode(Other) -> Other.
L1
foreach my $src (sort keys %map) {
# if (hex("0x$src") == hex("0x$map{$src}")) {
# print $OUTPUT "% ";
# }
print $OUTPUT "from_unicode(16#$map{$src}) -> 16#$src;\n";
}
print $OUTPUT <<L2;
from_unicode(List) when is_list(List) -> [from_unicode(C) || C <- List];
from_unicode(Bin) when is_binary(Bin) -> bin_from_unicode(Bin, <<>>);
from_unicode(Other) -> Other.
%% Private functions
bin_to_unicode(<<>>, Bin) -> Bin;
bin_to_unicode(<<C, Rest/binary>>, Acc) ->
U = to_unicode(C),
bin_to_unicode(Rest, <<Acc/binary, U/utf8>>).
bin_from_unicode(<<>>, Bin) -> Bin;
bin_from_unicode(<<U/utf8, Rest/binary>>, Acc) ->
C = from_unicode(U),
bin_from_unicode(Rest, <<Acc/binary, C>>).
L2
close $OUTPUT;