xref: /trunk/main/l10ntools/scripts/keyidGen.pl (revision 7e90fac2)
1:
2eval 'exec perl -S $0 ${1+"$@"}'
3    if 0;
4#**************************************************************
5#
6#  Licensed to the Apache Software Foundation (ASF) under one
7#  or more contributor license agreements.  See the NOTICE file
8#  distributed with this work for additional information
9#  regarding copyright ownership.  The ASF licenses this file
10#  to you under the Apache License, Version 2.0 (the
11#  "License"); you may not use this file except in compliance
12#  with the License.  You may obtain a copy of the License at
13#
14#    http://www.apache.org/licenses/LICENSE-2.0
15#
16#  Unless required by applicable law or agreed to in writing,
17#  software distributed under the License is distributed on an
18#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19#  KIND, either express or implied.  See the License for the
20#  specific language governing permissions and limitations
21#  under the License.
22#
23#**************************************************************
24
25
26#
27# add keyids to sdf file
28#
29
30use Compress::Zlib();
31
32print "\nkeyidGen version 1.0 \n\n";
33
34my ( $infile,$outfile,$dbimport );
35get_options();
36
37print_help() if ( !defined $infile || $help );
38exit 1 if ( !defined $infile );
39if ( ! defined $outfile )
40{
41    $outfile = $infile;
42    $outfile =~ s/\.sdf$//i;
43    $outfile .= "_KeyID.sdf";
44}
45
46$collisions = 0;
47%hashcodes = ();
48$count = 0;
49print "writing to $outfile\n";
50open INFILE,"<$infile" || die "could not open $infile $! $^E\n";
51open OUTFILE,">$outfile" || die "could not open $outfile $! $^E\n";
52
53while ( <INFILE> )
54{
55    $line = $_;
56    chomp $line;
57    $hash = 0;
58    if ( $line =~ /^([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)$/ )
59    {
60        $string="$1 $2 $4 $5 $6 $7 $8";
61        $hashp = makeID( $string );
62
63        if ( defined ($hashcodes{ $hashp } ) )
64        {
65            $collisions ++ unless $hashcodes{ $hashp } eq $string;
66        }
67        $hashcodes{ $hashp } = $string;
68        $count++;
69        if ( $dbimport )
70        {
71            my ( $pre, $post, $old );
72            $pre = "$1\t$2\t";
73            $post = "\t$4\t$5\t$6\t$7\t$8\t$9\t$10\t$11\t$12\t$13\t$14\t$15\n";
74            $old = $3;
75            $old =~ s/;{0,1}keyid:......;{0,1}//;
76            $old =~ s/^0$//;
77            if ( $old ne "" ) { $old .= ";"; }
78            print OUTFILE "$pre${old}keyid:$hashp$post";
79        }
80        else
81        {
82            print OUTFILE "$1\t$2\t$3\t$4\t$5\t$6\t$7\t$8\t$9\t$10\t".makekidstr($hashp,$11)."\t".makekidstr($hashp,$12)."\t$13\t".makekidstr($hashp,$14)."\t$15\n";
83        }
84    }
85}
86print "$count entries\n";
87print "$collisions collisions\n";
88
89close INFILE;
90close OUTFILE;
91
92sub makeID
93{
94	my ( $String ) = shift;
95	my ( $hash );
96	# hardcoded to prevent windows installer to choke on bad directoryname :-((
97	if ( $String eq "scp2 source\\ooo\\directory_ooo.ulf LngText STR_DIR_KAPITEL   " )
98	{
99		return "keyid1";
100	}
101
102    $hash = Compress::Zlib::crc32( $String, undef );
103    return makenumber( $hash );
104}
105
106sub makenumber
107{
108    $h = shift;
109    #                  1         2         3         4
110    #         1234567890123456789012345678901234567890
111    $symbols="0123456789abcdefghijklmnopqrstuvwxyz+-[=]";
112    $order = length($symbols);
113    $result = "";
114    while ( length( $result ) < 6 )
115    {
116        $result .= substr( $symbols, ($h % $order), 1 );
117        $h = int( $h / $order );
118    }
119    die "makenumber failed because number is too big (this cannot be so this is a strange error)" if $h > 0;
120
121    return reverse $result;
122}
123
124
125sub makekidstr
126{
127    $kid = shift;
128    $str = shift;
129
130    if ( $str ne "" )
131    {
132        # special handling for strings starting with font descriptions like {&Tahoma8} (win system integration)
133        if ( $str =~ s/^(\{\&[^\}]+\})// )
134        {
135            return "$1$kid‖$str";
136        }
137        else
138        {
139            return "$kid‖$str";
140        }
141    }
142    else
143    {
144        return "";
145    }
146#    return "default";
147}
148
149sub print_help
150{
151    print "\n\n";
152    print "keyidGen 0.5 for sdf files\n";
153    print "--------------------------\n";
154    print "Usage:\n";
155    print "keyidGen <infile> [<outfile>] [-dbimport]\n";
156    print "                   add keyids to the entries and write them to a file with\n";
157    print "                   _KeyID added to the name\n";
158    print "   -dbimport       Add KeyID to a new column instead of to the strings.\n";
159    print "                   This is needed to import the IDs into tha database.\n";
160    print "\n\n";
161}
162
163
164sub get_options {
165	my ($arg,$has_infile);
166
167	while ($arg = shift @ARGV) {
168		$arg =~ /^-dbimport$/  and $dbimport = 1 and next;
169		$arg =~ /^-help$/  and $help = 1 and next; #show help
170
171		if ( !$has_infile )
172		{
173		    $infile = $arg;
174		    $has_infile = 1;
175		}
176		else
177		{
178		    $outfile = $arg;
179		}
180	}
181}
182