Upload 2.0.2
[physicsfs] / extras / makecasefoldhashtable.pl
1 #!/usr/bin/perl -w
2
3 use warnings;
4 use strict;
5
6 print <<__EOF__;
7 /*
8  * This file is part of PhysicsFS (http://icculus.org/physfs/)
9  *
10  * This data generated by physfs/extras/makecasefoldhashtable.pl ...
11  * Do not manually edit this file!
12  *
13  * Please see the file LICENSE.txt in the source's root directory.
14  */
15
16 #ifndef __PHYSICSFS_INTERNAL__
17 #error Do not include this header from your applications.
18 #endif
19
20 __EOF__
21
22
23 my @foldPairs;
24
25 for (my $i = 0; $i < 256; $i++) {
26     $foldPairs[$i] = '';
27 }
28
29 open(FH,'<','casefolding.txt') or die("failed to open casefolding.txt: $!\n");
30 while (<FH>) {
31     chomp;
32     # strip comments from textfile...
33     s/\#.*\Z//;
34
35     # strip whitespace...
36     s/\A\s+//;
37     s/\s+\Z//;
38
39     next if not /\A([a-fA-F0-9]+)\;\s*(.)\;\s*(.+)\;/;
40     my ($code, $status, $mapping) = ($1, $2, $3);
41     my $hexxed = hex($code);
42     my $hashed = (($hexxed ^ ($hexxed >> 8)) & 0xFF);
43     #print("// code '$code'   status '$status'   mapping '$mapping'\n");
44     #print("// hexxed '$hexxed'  hashed '$hashed'\n");
45
46     if (($status eq 'C') or ($status eq 'F')) {
47         my ($map1, $map2, $map3) = ('0000', '0000', '0000');
48         $map1 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
49         $map2 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
50         $map3 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
51         die("mapping space too small for '$code'\n") if ($mapping ne '');
52         $foldPairs[$hashed] .= "    { 0x$code, 0x$map1, 0x$map2, 0x$map3 },\n";
53     }
54 }
55 close(FH);
56
57 for (my $i = 0; $i < 256; $i++) {
58     $foldPairs[$i] =~ s/,\n\Z//;
59     my $str = $foldPairs[$i];
60     next if $str eq '';
61     my $num = '000' . $i;
62     $num =~ s/\A.*?(\d\d\d)\Z/$1/;
63     my $sym = "case_fold_${num}";
64     print("static const CaseFoldMapping ${sym}[] = {\n$str\n};\n\n");
65 }
66
67 print("\nstatic const CaseFoldHashBucket case_fold_hash[256] = {\n");
68
69 for (my $i = 0; $i < 256; $i++) {
70     my $str = $foldPairs[$i];
71     if ($str eq '') {
72         print("    { 0, NULL },\n");
73     } else {
74         my $num = '000' . $i;
75         $num =~ s/\A.*?(\d\d\d)\Z/$1/;
76         my $sym = "case_fold_${num}";
77         print("    { __PHYSFS_ARRAYLEN($sym), $sym },\n");
78     }
79 }
80 print("};\n\n");
81
82 exit 0;
83
84 # end of makecashfoldhashtable.pl ...
85