xref: /aoo41x/main/l10ntools/scripts/keyidGen.pl (revision cdf0e10c)
1:
2eval 'exec perl -S $0 ${1+"$@"}'
3    if 0;
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29#*************************************************************************
30#
31# add keyids to sdf file
32#
33
34use Compress::Zlib();
35
36print "\nkeyidGen version 1.0 \n\n";
37
38my ( $infile,$outfile,$dbimport );
39get_options();
40
41print_help() if ( !defined $infile || $help );
42exit 1 if ( !defined $infile );
43if ( ! defined $outfile )
44{
45    $outfile = $infile;
46    $outfile =~ s/\.sdf$//i;
47    $outfile .= "_KeyID.sdf";
48}
49
50$collisions = 0;
51%hashcodes = ();
52$count = 0;
53print "writing to $outfile\n";
54open INFILE,"<$infile" || die "could not open $infile $! $^E\n";
55open OUTFILE,">$outfile" || die "could not open $outfile $! $^E\n";
56
57while ( <INFILE> )
58{
59    $line = $_;
60    chomp $line;
61    $hash = 0;
62    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]*)$/ )
63    {
64        $string="$1 $2 $4 $5 $6 $7 $8";
65        $hashp = makeID( $string );
66
67        if ( defined ($hashcodes{ $hashp } ) )
68        {
69            $collisions ++ unless $hashcodes{ $hashp } eq $string;
70        }
71        $hashcodes{ $hashp } = $string;
72        $count++;
73        if ( $dbimport )
74        {
75            my ( $pre, $post, $old );
76            $pre = "$1\t$2\t";
77            $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";
78            $old = $3;
79            $old =~ s/;{0,1}keyid:......;{0,1}//;
80            $old =~ s/^0$//;
81            if ( $old ne "" ) { $old .= ";"; }
82            print OUTFILE "$pre${old}keyid:$hashp$post";
83        }
84        else
85        {
86            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";
87        }
88    }
89}
90print "$count entries\n";
91print "$collisions collisions\n";
92
93close INFILE;
94close OUTFILE;
95
96sub makeID
97{
98	my ( $String ) = shift;
99	my ( $hash );
100	# hardcoded to prevent windows installer to choke on bad directoryname :-((
101	if ( $String eq "scp2 source\\ooo\\directory_ooo.ulf LngText STR_DIR_KAPITEL   " )
102	{
103		return "keyid1";
104	}
105
106    $hash = Compress::Zlib::crc32( $String, undef );
107    return makenumber( $hash );
108}
109
110sub makenumber
111{
112    $h = shift;
113    #                  1         2         3         4
114    #         1234567890123456789012345678901234567890
115    $symbols="0123456789abcdefghijklmnopqrstuvwxyz+-[=]";
116    $order = length($symbols);
117    $result = "";
118    while ( length( $result ) < 6 )
119    {
120        $result .= substr( $symbols, ($h % $order), 1 );
121        $h = int( $h / $order );
122    }
123    die "makenumber failed because number is too big (this cannot be so this is a strange error)" if $h > 0;
124
125    return reverse $result;
126}
127
128
129sub makekidstr
130{
131    $kid = shift;
132    $str = shift;
133
134    if ( $str ne "" )
135    {
136        # special handling for strings starting with font descriptions like {&Tahoma8} (win system integration)
137        if ( $str =~ s/^(\{\&[^\}]+\})// )
138        {
139            return "$1$kid‖$str";
140        }
141        else
142        {
143            return "$kid‖$str";
144        }
145    }
146    else
147    {
148        return "";
149    }
150#    return "default";
151}
152
153sub print_help
154{
155    print "\n\n";
156    print "keyidGen 0.5 for sdf files\n";
157    print "--------------------------\n";
158    print "Usage:\n";
159    print "keyidGen <infile> [<outfile>] [-dbimport]\n";
160    print "                   add keyids to the entries and write them to a file with\n";
161    print "                   _KeyID added to the name\n";
162    print "   -dbimport       Add KeyID to a new column instead of to the strings.\n";
163    print "                   This is needed to import the IDs into tha database.\n";
164    print "\n\n";
165}
166
167
168sub get_options {
169	my ($arg,$has_infile);
170
171	while ($arg = shift @ARGV) {
172		$arg =~ /^-dbimport$/  and $dbimport = 1 and next;
173		$arg =~ /^-help$/  and $help = 1 and next; #show help
174
175		if ( !$has_infile )
176		{
177		    $infile = $arg;
178		    $has_infile = 1;
179		}
180		else
181		{
182		    $outfile = $arg;
183		}
184	}
185}
186