tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / solenv / bin / modules / par2script / work.pm
blobcc1f1e4d5607d5385eda7e0fe2ce50589fa10245
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package par2script::work;
22 use par2script::globals;
23 use par2script::remover;
25 ############################################
26 # par2script working module
27 ############################################
29 sub analyze_comma_separated_list
31 my ($list, $listref) = @_; # second parameter is optional
33 my @list = ();
34 my $locallistref;
36 if (!( $listref )) { $locallistref = \@list; }
37 else { $locallistref = $listref; }
39 par2script::remover::remove_leading_and_ending_comma(\$list);
40 par2script::remover::remove_leading_and_ending_whitespaces(\$list);
42 while ( $list =~ /^\s*(.*?)\s*\,\s*(.*)\s*$/ )
44 my $oneentry = $1;
45 $list = $2;
46 par2script::remover::remove_leading_and_ending_whitespaces(\$oneentry);
47 push(@{$locallistref}, $oneentry);
50 # the last entry
52 par2script::remover::remove_leading_and_ending_whitespaces(\$list);
53 push(@{$locallistref}, $list);
55 return $locallistref;
58 ############################################
59 # setting list of include paths
60 ############################################
62 sub setincludes
64 my ($list) = @_;
66 # input is the comma separated list of include paths
68 my $includes = analyze_comma_separated_list($list);
70 return $includes;
73 ############################################
74 # setting list of all par files
75 ############################################
77 sub setparfiles
79 my ($filename) = @_;
81 # input is the name of the list file
82 my $filecontent = par2script::files::read_file($filename);
84 my @parfiles = ();
85 my $parfilesref = \@parfiles;
87 foreach ( @{$filecontent} ) { $parfilesref = analyze_comma_separated_list($_, $parfilesref); }
89 return $parfilesref;
92 ############################################
93 # finding the correct include path
94 # for the par files
95 ############################################
97 sub make_complete_paths_for_parfiles
99 my ($parfiles, $includes) = @_;
101 my $oneparfile;
103 foreach $oneparfile ( @{$parfiles} )
105 my $foundparfile = 0;
106 my $includepath;
108 foreach $includepath ( @{$includes} )
110 my $parfile = "$includepath/$oneparfile";
112 if ( -f $parfile )
114 $foundparfile = 1;
115 $oneparfile = $parfile;
116 last;
120 if ( ! $foundparfile )
122 die "ERROR: Could not find parfile ${$parfiles}[$i] in includes paths: $par2script::globals::includepathlist !\n";
127 ######################################################
128 # collecting one special item in the par files and
129 # including it into the "definitions" hash
130 ######################################################
132 sub collect_definitions
134 my ($parfilecontent) = @_;
136 my $multidefinitionerror = 0;
137 my @multidefinitiongids = ();
139 my %itemhash;
141 # create empty item hashes
142 foreach $oneitem ( @par2script::globals::allitems ) {
143 my %items;
144 $par2script::globals::definitions{$oneitem} = \%items;
147 for ( my $i = 0; $i <= $#{$parfilecontent}; $i++ )
149 my $line = ${$parfilecontent}[$i];
150 my $oneitem, $gid;
152 $line =~ /^\s*$/ && next; # skip blank lines
154 # lines should be well formed:
155 if ($line =~ m/^\s*(\w+)\s+(\w+)\s*$/)
157 $oneitem = $1;
158 $gid = $2;
159 } else {
160 chomp ($line);
161 my $invalid = $line;
162 $invalid =~ s/[\s\w]*//g;
163 par2script::exiter::exit_program("ERROR: malformed par file, invalid character '$invalid', expecting <token> <gid> but saw '$line'", "test_par_syntax");
165 # print STDERR "line '$line' -> '$oneitem' '$gid'\n";
167 # hunt badness variously
168 if ( ! defined $par2script::globals::definitions{$oneitem} )
170 par2script::exiter::exit_program("ERROR: invalid scp2 fragment item type '$oneitem' in line: '$line'", "test_par_syntax");
173 # no hyphen allowed in gids -> cannot happen here because (\w+) is required for gids
174 if ( $gid =~ /-/ ) { par2script::exiter::exit_program("ERROR: No hyphen allowed in global id: $gid", "test_of_hyphen"); }
176 my %oneitemhash;
178 while (! ( ${$parfilecontent}[$i] =~ /^\s*End\s*$/i ) )
180 if ( ${$parfilecontent}[$i] =~ /^\s*(.+?)\s*\=\s*(.+?)\s*\;\s*$/ ) # only oneliner!
182 $itemkey = $1;
183 $itemvalue = $2;
185 if ( $oneitem eq "Directory" ) { if ( $itemkey =~ "DosName" ) { $itemkey =~ s/DosName/HostName/; } }
186 if (( $oneitem eq "Directory" ) || ( $oneitem eq "File" ) || ( $oneitem eq "Unixlink" )) { if ( $itemvalue eq "PD_PROGDIR" ) { $itemvalue = "PREDEFINED_PROGDIR"; }}
187 if (( $itemkey eq "Styles" ) && ( $itemvalue =~ /^\s*(\w+)(\s*\;\s*)$/ )) { $itemvalue = "($1)$2"; }
188 elsif ( $itemkey eq "Files" ) # filter out empty file records, as they mess up assignment to modules
190 $itemvalue =~ /^\(([^)]*)\)$/;
191 $itemvalue = '(' . join( ',', grep( !/^$/, split( ',', $1 ) ) ) . ')';
194 $oneitemhash{$itemkey} = $itemvalue;
196 $i++;
199 # test of uniqueness
200 if ( defined ($par2script::globals::definitions{$oneitem}->{$gid}) )
202 $multidefinitionerror = 1;
203 push(@multidefinitiongids, $gid);
206 $par2script::globals::definitions{$oneitem}->{$gid} = \%oneitemhash;
209 if ( $multidefinitionerror ) { par2script::exiter::multidefinitionerror(\@multidefinitiongids); }
211 # foreach $key (keys %par2script::globals::definitions)
213 # print "Key: $key \n";
215 # foreach $key (keys %{$par2script::globals::definitions{$key}})
217 # print "\t$key \n";
222 ######################################################
223 # Filling content into the script
224 ######################################################
226 sub put_oneitem_into_script
228 my ( $script, $item, $itemhash, $itemkey ) = @_;
230 push(@{$script}, "$item $itemkey\n" );
231 my $content = "";
232 foreach $content (sort keys %{$itemhash->{$itemkey}}) { push(@{$script}, "\t$content = $itemhash->{$itemkey}->{$content};\n" ); }
233 push(@{$script}, "End\n" );
234 push(@{$script}, "\n" );
237 ######################################################
238 # Creating the script
239 ######################################################
241 sub create_script
243 my @script = ();
244 my $oneitem;
246 foreach $oneitem ( @par2script::globals::allitems )
248 if ( exists($par2script::globals::definitions{$oneitem}) )
250 if ( $oneitem eq "Shortcut" ) { next; } # "Shortcuts" after "Files"
252 if (( $oneitem eq "Module" ) || ( $oneitem eq "Directory" )) { write_sorted_items(\@script, $oneitem); }
253 else { write_unsorted_items(\@script, $oneitem); }
257 return \@script;
260 ######################################################
261 # Adding script content for the unsorted items
262 ######################################################
264 sub write_unsorted_items
266 my ( $script, $oneitem ) = @_;
268 my $itemhash = $par2script::globals::definitions{$oneitem};
270 my $itemkey = "";
271 foreach $itemkey (sort keys %{$itemhash})
273 put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey);
275 # special handling for Shortcuts after Files
276 if (( $oneitem eq "File" ) && ( exists($par2script::globals::definitions{"Shortcut"}) ))
278 my $shortcutkey;
279 foreach $shortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} )
281 if ( $par2script::globals::definitions{"Shortcut"}->{$shortcutkey}->{'FileID'} eq $itemkey )
283 put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $shortcutkey);
285 # and Shortcut to Shortcut also
286 my $internshortcutkey;
287 foreach $internshortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} )
289 if ( $par2script::globals::definitions{"Shortcut"}->{$internshortcutkey}->{'ShortcutID'} eq $shortcutkey )
291 put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $internshortcutkey);
300 ######################################################
301 # Collecting all children of a specified parent
302 ######################################################
304 sub collect_children
306 my ( $itemhash, $parent, $order ) = @_;
308 my $item;
309 foreach $item ( sort keys %{$itemhash} )
311 if ( $itemhash->{$item}->{'ParentID'} eq $parent )
313 push(@{$order}, $item);
314 my $newparent = $item;
315 collect_children($itemhash, $newparent, $order);
320 ######################################################
321 # Adding script content for the sorted items
322 ######################################################
324 sub write_sorted_items
326 my ( $script, $oneitem ) = @_;
328 my $itemhash = $par2script::globals::definitions{$oneitem};
330 my @itemorder = ();
331 my @startparents = ();
333 if ( $oneitem eq "Module" ) { push(@startparents, ""); }
334 elsif ( $oneitem eq "Directory" ) { push(@startparents, "PREDEFINED_PROGDIR"); }
335 else { die "ERROR: No root parent defined for item type $oneitem !\n"; }
337 # supporting more than one toplevel item
338 my $parent;
339 foreach $parent ( @startparents ) { collect_children($itemhash, $parent, \@itemorder); }
341 my $itemkey;
342 foreach $itemkey ( @itemorder ) { put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey); }
345 #######################################################################
346 # Collecting all assigned gids of the type "item" from the modules
347 # in the par files. Using a hash!
348 #######################################################################
350 sub collect_assigned_gids
352 my $allmodules = $par2script::globals::definitions{'Module'};
354 my $item;
355 foreach $item ( @par2script::globals::items_assigned_at_modules )
357 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "collect_assigned_gids"); }
359 my $searchkey = $par2script::globals::searchkeys{$item};
361 my %assignitems = ();
362 my $modulegid = "";
364 foreach $modulegid (keys %{$allmodules} )
366 # print "Module $modulegid\n";
367 # my $content = "";
368 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
369 # print "End\n";
370 # print "\n";
372 if ( exists($allmodules->{$modulegid}->{$searchkey}) )
374 my $list = $allmodules->{$modulegid}->{$searchkey};
375 if ( $list =~ /^\s*\((.*?)\)\s*(.*?)\s*$/ ) { $list = $1; }
376 else { par2script::exiter::exit_program("ERROR: Invalid module list: $list", "collect_assigned_gids"); }
377 my $allassigneditems = par2script::converter::convert_stringlist_into_array_2($list, ",");
379 my $gid;
380 foreach $gid ( @{$allassigneditems} )
382 if ( exists($assignitems{$gid}) ) { $assignitems{$gid} = $assignitems{$gid} + 1; }
383 else { $assignitems{$gid} = 1; }
388 $par2script::globals::assignedgids{$item} = \%assignitems;
392 ##################################################
393 # Collecting the content of all par files.
394 # Then the files do not need to be opened twice.
395 ##################################################
397 sub read_all_parfiles
399 my ($parfiles) = @_;
401 my @parfilecontent = ();
402 my $parfilename;
404 foreach $parfilename ( @{$parfiles} )
406 my $parfile = par2script::files::read_file($parfilename);
407 foreach ( @{$parfile} ) { push(@parfilecontent, $_); }
408 push(@parfilecontent, "\n");
411 return \@parfilecontent;