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