diff options
Diffstat (limited to 'utils/idna-derived-props-gen.pl')
-rw-r--r-- | utils/idna-derived-props-gen.pl | 128 |
1 files changed, 107 insertions, 21 deletions
diff --git a/utils/idna-derived-props-gen.pl b/utils/idna-derived-props-gen.pl index 515f62a40..a9e9b4b53 100644 --- a/utils/idna-derived-props-gen.pl +++ b/utils/idna-derived-props-gen.pl @@ -16,7 +16,88 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -print <<HEADER; +use strict; + +use Getopt::Long (); +use Fcntl qw( O_CREAT O_EXCL O_WRONLY O_APPEND O_RDONLY O_WRONLY ); + +use constant GETOPT_OPTS => qw( auto_abbrev no_getopt_compat bundling ); +use constant GETOPT_SPEC => + qw( output|o=s + properties|p=s + joining|j=s + help|h|? ); + +# default option values: +my %opt = qw(properties "idna-tables-properties.csv" joining "DerivedJoiningType.txt"); + +sub usage +{ + my @fmt = map { s/::$//; $_ } keys(%{$::{'msgfmt::'}}); + print(STDERR <<TXT ); +usage: + $0 [-o output-file] -p properties-file -j joining-file + + output-file : defaults to standard output +TXT + exit(1); +} + +sub output_stream +{ + if( $opt{output} ) + { + my $ofh; + + sysopen( $ofh, $opt{output}, O_CREAT|O_EXCL|O_APPEND|O_WRONLY ) || + die( "$0: Failed to open output file $opt{output}: $!\n" ); + + return $ofh; + } + + return \*STDOUT; +} + +sub input_stream +{ + my $stream = $_[0]; + + if( $opt{$stream} ) + { + my $ifh; + + sysopen( $ifh, $opt{$stream}, O_RDONLY ) || + die( "$0: Failed to open input file $stream: $!\n" ); + + return $ifh; + } + die( "$0: No input file for $stream"); +} + +sub main +{ + my $output; + my $properties; + my $joining; + my $opt_ok; + + # option parsing: + Getopt::Long::Configure( GETOPT_OPTS ); + $opt_ok = Getopt::Long::GetOptions( \%opt, GETOPT_SPEC ); + + # double check the options are sane (and we weren't asked for the help) + if( !$opt_ok || $opt{help} ) + { + usage(); + } + + # open the appropriate files + $properties = input_stream("properties"); + $joining = input_stream("joining"); + $output = output_stream(); + + + print { $output } <<HEADER; /* This file is generated by idna-derived-props-gen.pl * DO NOT EDIT BY HAND */ @@ -53,19 +134,21 @@ typedef struct idna_table { idna_table idna_derived[] = { HEADER -open(CSVFILE, "idna-tables-5.2.0-properties.csv"); -$line = <CSVFILE>; # discard header line -while($line = <CSVFILE>) { - @items = split(/\,/, $line); - @codepoints = split(/-/, $items[0]); - if($#codepoints == 0) { $codepoints[1] = $codepoints[0]; } - print "\t{ 0x" . $codepoints[0] . ", 0x" . $codepoints[1] . ", .p.property = IDNA_P_" . $items[1] . " },\n"; -} + my $line = <$properties>; # discard header line + + while($line = <$properties>) { + my @items = split(/\,/, $line); + my @codepoints = split(/-/, $items[0]); + if($#codepoints == 0) { + $codepoints[1] = $codepoints[0]; + } + print { $output } "\t{ 0x" . $codepoints[0] . ", 0x" . $codepoints[1] . ", .p.property = IDNA_P_" . $items[1] . " },\n"; + } -close(CSVFILE); + close($properties); -print <<HEADER; + print { $output } <<HEADER; { 0, 0, .p.property = 0} }; @@ -73,24 +156,27 @@ idna_table idna_joiningtype[] = { HEADER -open(TXTFILE, "DerivedJoiningType.txt"); - -while($line = <TXTFILE>) { + while($line = <$joining>) { chop($line); if(substr($line, 0, 1) eq '#') {next;} if(length($line) == 0) {next;} - @items = split(/;/, $line); - @codepoints = split(/\./, $items[0]); - if($#codepoints == 0) { $codepoints[2] = $codepoints[0]; } - print "\t{ 0x" . $codepoints[0] . ", 0x" . $codepoints[2] . ", .p.jt = IDNA_UNICODE_JT_" . substr($items[1], 1, 1) . " },\n"; -} + my @items = split(/;/, $line); + my @codepoints = split(/\./, $items[0]); + if($#codepoints == 0) { + $codepoints[2] = $codepoints[0]; + } + print { $output } "\t{ 0x" . $codepoints[0] . ", 0x" . $codepoints[2] . ", .p.jt = IDNA_UNICODE_JT_" . substr($items[1], 1, 1) . " },\n"; + } -close(TXTFILE); + close($joining); -print <<HEADER; + print { $output } <<HEADER; { 0, 0, .p.jt = 0} }; #endif HEADER +} + +main(); |