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