Debian Codegraph @

solenv/bin/modules/par2script/module.pm

raw
1 #
2 # This file is part of the LibreOffice project.
3 #
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/.
7 #
8 # This file incorporates work covered by the following license notice:
9 #
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 .
17 #
18
19 package par2script::module;
20
21 use par2script::converter;
22 use par2script::exiter;
23
24 ###########################################
25 # Removing undefined gids
26 # from modules
27 ###########################################
28
29 sub remove_from_modules
30 {
31 my ($gid, $item) = @_;
32
33 my $counter = 0;
34
35 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
36 my $searchkey = $par2script::globals::searchkeys{$item};
37
38 my $allmodules = $par2script::globals::definitions{'Module'};
39
40 my $onemodule;
41 foreach $onemodule (keys %{$allmodules})
42 {
43 if (( exists($allmodules->{$onemodule}->{$searchkey}) ) && ( $allmodules->{$onemodule}->{$searchkey} =~ /\b$gid\b/ ))
44 {
45 my $infoline = "WARNING: Removing $gid because of missing definition\n";
46 # print $infoline;
47 push(@par2script::globals::logfileinfo, $infoline);
48
49 $allmodules->{$onemodule}->{$searchkey} =~ s/\b$gid\b//;
50 $allmodules->{$onemodule}->{$searchkey} =~ s/\,\s*\,/\,/;
51 $allmodules->{$onemodule}->{$searchkey} =~ s/\(\s*\,\s*/\(/;
52 $allmodules->{$onemodule}->{$searchkey} =~ s/\s*\,\s*\)/\)/;
53
54 if (( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\,\s*\)/ ) ||
55 ( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\)/ ))
56 {
57 delete($allmodules->{$onemodule}->{$searchkey});
58 }
59
60 $counter++;
61 }
62 }
63
64 return $counter;
65 }
66
67 ###########################################
68 # Removing undefined gids automatically
69 # from modules
70 ###########################################
71
72 sub remove_undefined_gids_from_modules
73 {
74 # If assigned gids for "File", "Directory" or "Unixlink" are not defined,
75 # they are automatically removed from the module
76
77 foreach $item ( @par2script::globals::items_assigned_at_modules )
78 {
79 my $assignedgids = $par2script::globals::assignedgids{$item};
80 my $definedgids = $par2script::globals::definitions{$item};
81
82 my $gid;
83 foreach $gid ( keys %{$assignedgids} )
84 {
85 if ( ! exists( $definedgids->{$gid} ))
86 {
87 # deleting entry in module definition
88 my $number_of_removals = remove_from_modules($gid, $item);
89 # decreasing counter in assignments
90 if ( $assignedgids->{$gid} > $number_of_removals ) { $assignedgids->{$gid} = $assignedgids->{$gid} - $number_of_removals; }
91 else { delete($assignedgids->{$gid}); }
92 }
93 }
94 }
95 }
96
97 ############################################
98 # Getting the gid of the root module. The
99 # root module has no ParentID or an empty
100 # ParentID.
101 ############################################
102
103 sub get_rootmodule_gid
104 {
105 my $rootgid = "";
106 my $foundroot = 0;
107
108 my $allmodules = $par2script::globals::definitions{'Module'};
109
110 my $modulegid = "";
111 foreach $modulegid (keys %{$allmodules} )
112 {
113 # print "Module $modulegid\n";
114 # my $content = "";
115 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
116 # print "End\n";
117 # print "\n";
118
119 if (( ! exists($allmodules->{$modulegid}->{'ParentID'})) || ( $allmodules->{$modulegid}->{'ParentID'} eq "" ))
120 {
121 if ( $foundroot ) { par2script::exiter::exit_program("ERROR: More than one Root module. Only one module without ParentID or with empty ParentID allowed ($rootgid and $modulegid).", "get_rootmodule_gid"); }
122 $rootgid = $modulegid;
123 $foundroot = 1;
124 }
125 }
126
127 if ( ! $foundroot ) { par2script::exiter::exit_program("ERROR: Could not find Root module. Did not find module without ParentID or with empty ParentID.", "get_rootmodule_gid"); }
128
129 return $rootgid;
130 }
131
132 ####################################
133 # Adding defined items without
134 # assignment to the root module.
135 ####################################
136
137 sub add_to_root_module
138 {
139 # If defined gids for "File", "Directory" or "Unixlink" are not assigned,
140 # they are automatically assigned to the root module
141
142 my $rootmodulegid = get_rootmodule_gid();
143
144 my $item;
145 foreach $item ( @par2script::globals::items_assigned_at_modules )
146 {
147 my $assignedgids = $par2script::globals::assignedgids{$item};
148 my $definedgids = $par2script::globals::definitions{$item};
149
150 my $gidstring = "";
151
152 # Perhaps there are already items assigned to the root
153 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
154 my $modulekey = $par2script::globals::searchkeys{$item};
155 if ( exists($par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey}) )
156 {
157 $gidstring = $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey};
158 $gidstring =~ s/\(//;
159 $gidstring =~ s/\)//;
160 }
161
162 my $gid;
163 foreach $gid ( keys %{$definedgids} )
164 {
165 if ( ! exists( $assignedgids->{$gid} ))
166 {
167 if ( $gidstring eq "" )
168 {
169 $gidstring = $gid;
170 }
171 else
172 {
173 $gidstring = "$gidstring,$gid";
174 }
175
176 $assignedgids->{$gid} = 1;
177 }
178 }
179
180 if ( $gidstring ne "" )
181 {
182 $gidstring = "\($gidstring\)";
183 $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey} = $gidstring;
184 }
185 }
186 }
187
188 ###################################################
189 # Including \n in a very long string
190 ###################################################
191
192 sub include_linebreaks
193 {
194 my ($allgidstring) = @_;
195
196 my $newline = "";
197 my $newlength = 0;
198
199 $allgidstring =~ s/\(//;
200 $allgidstring =~ s/\)//;
201
202 my $allgids = par2script::converter::convert_stringlist_into_array_2($allgidstring, ",");
203
204 if ( $#{$allgids} > -1 )
205 {
206 my $onegid;
207 foreach $onegid ( @{$allgids} )
208 {
209 $newline = "$newline$onegid,";
210 $newlength = $newlength + length($onegid) + 1; # +1 for the comma
211
212 if ( $newlength > 80 )
213 {
214 $newline = $newline . "\n\t\t\t\t";
215 $newlength = 0;
216 }
217 }
218 }
219
220 $newline =~ s/,\s*$//;
221 $newline = "($newline)";
222
223 return $newline;
224 }
225
226 ###################################################
227 # Shorten the lines that belong to modules, if
228 # the length of the line is greater 100
229 ###################################################
230
231 sub shorten_lines_at_modules
232 {
233 my $item;
234 foreach $item ( @par2script::globals::items_assigned_at_modules )
235 {
236 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "shorten_lines_at_modules"); }
237 my $searchkey = $par2script::globals::searchkeys{$item};
238
239 my $allmodules = $par2script::globals::definitions{'Module'};
240
241 my $onemodule;
242 foreach $onemodule (keys %{$allmodules})
243 {
244 if (( exists($allmodules->{$onemodule}->{$searchkey}) ) &&
245 ( length($allmodules->{$onemodule}->{$searchkey}) > 100 ))
246 {
247 # including "\n\t\t\t\t"
248 my $newstring = include_linebreaks($allmodules->{$onemodule}->{$searchkey});
249 $allmodules->{$onemodule}->{$searchkey} = $newstring;
250 }
251 }
252 }
253 }
254
255 1;