summaryrefslogtreecommitdiffstats
path: root/solenv/bin/modules/par2script/module.pm
blob: ef916650b42fe318a7107ec6abc998f5677bb7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#

package par2script::module;

use par2script::converter;
use par2script::exiter;

###########################################
# Removing undefined gids
# from modules
###########################################

sub remove_from_modules
{
    my ($gid, $item) = @_;

    my $counter = 0;

    if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
    my $searchkey = $par2script::globals::searchkeys{$item};

    my $allmodules = $par2script::globals::definitions{'Module'};

    my $onemodule;
    foreach $onemodule (keys %{$allmodules})
    {
        if (( exists($allmodules->{$onemodule}->{$searchkey}) ) && ( $allmodules->{$onemodule}->{$searchkey} =~ /\b$gid\b/ ))
        {
            my $infoline = "WARNING: Removing $gid because of missing definition\n";
            # print $infoline;
            push(@par2script::globals::logfileinfo, $infoline);

            $allmodules->{$onemodule}->{$searchkey} =~ s/\b$gid\b//;
            $allmodules->{$onemodule}->{$searchkey} =~ s/\,\s*\,/\,/;
            $allmodules->{$onemodule}->{$searchkey} =~ s/\(\s*\,\s*/\(/;
            $allmodules->{$onemodule}->{$searchkey} =~ s/\s*\,\s*\)/\)/;

            if (( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\,\s*\)/ ) ||
                ( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\)/ ))
            {
                delete($allmodules->{$onemodule}->{$searchkey});
            }

            $counter++;
        }
    }

    return $counter;
}

###########################################
# Removing undefined gids automatically
# from modules
###########################################

sub remove_undefined_gids_from_modules
{
    # If assigned gids for "File", "Directory" or "Unixlink" are not defined,
    # they are automatically removed from the module

    foreach $item ( @par2script::globals::items_assigned_at_modules )
    {
        my $assignedgids = $par2script::globals::assignedgids{$item};
        my $definedgids = $par2script::globals::definitions{$item};

        my $gid;
        foreach $gid ( keys %{$assignedgids} )
        {
            if ( ! exists( $definedgids->{$gid} ))
            {
                # deleting entry in module definition
                my $number_of_removals = remove_from_modules($gid, $item);
                # decreasing counter in assignments
                if ( $assignedgids->{$gid} > $number_of_removals ) { $assignedgids->{$gid} = $assignedgids->{$gid} - $number_of_removals; }
                else { delete($assignedgids->{$gid}); }
            }
        }
    }
}

############################################
# Getting the gid of the root module. The
# root module has no ParentID or an empty
# ParentID.
############################################

sub get_rootmodule_gid
{
    my $rootgid = "";
    my $foundroot = 0;

    my $allmodules = $par2script::globals::definitions{'Module'};

    my $modulegid = "";
    foreach $modulegid (keys %{$allmodules} )
    {
        # print "Module $modulegid\n";
        # my $content = "";
        # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
        # print "End\n";
        # print "\n";

        if (( ! exists($allmodules->{$modulegid}->{'ParentID'})) || ( $allmodules->{$modulegid}->{'ParentID'} eq "" ))
        {
            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"); }
            $rootgid = $modulegid;
            $foundroot = 1;
        }
    }

    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"); }

    return $rootgid;
}

####################################
# Adding defined items without
# assignment to the root module.
####################################

sub add_to_root_module
{
    # If defined gids for "File", "Directory" or "Unixlink" are not assigned,
    # they are automatically assigned to the root module

    my $rootmodulegid = get_rootmodule_gid();

    my $item;
    foreach $item ( @par2script::globals::items_assigned_at_modules )
    {
        my $assignedgids = $par2script::globals::assignedgids{$item};
        my $definedgids = $par2script::globals::definitions{$item};

        my $gidstring = "";

        # Perhaps there are already items assigned to the root
        if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
        my $modulekey = $par2script::globals::searchkeys{$item};
        if ( exists($par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey}) )
        {
            $gidstring = $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey};
            $gidstring =~ s/\(//;
            $gidstring =~ s/\)//;
        }

        my $gid;
        foreach $gid ( keys %{$definedgids} )
        {
            if ( ! exists( $assignedgids->{$gid} ))
            {
                if ( $gidstring eq "" )
                {
                    $gidstring = $gid;
                }
                else
                {
                    $gidstring = "$gidstring,$gid";
                }

                $assignedgids->{$gid} = 1;
            }
        }

        if ( $gidstring ne "" )
        {
            $gidstring = "\($gidstring\)";
            $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey} = $gidstring;
        }
    }
}

###################################################
# Including \n in a very long string
###################################################

sub include_linebreaks
{
    my ($allgidstring) = @_;

    my $newline = "";
    my $newlength = 0;

    $allgidstring =~ s/\(//;
    $allgidstring =~ s/\)//;

    my $allgids = par2script::converter::convert_stringlist_into_array_2($allgidstring, ",");

    if ( $#{$allgids} > -1 )
    {
        my $onegid;
        foreach $onegid ( @{$allgids} )
        {
            $newline = "$newline$onegid,";
            $newlength = $newlength + length($onegid) + 1; # +1 for the comma

            if ( $newlength > 80 )
            {
                $newline = $newline . "\n\t\t\t\t";
                $newlength = 0;
            }
        }
    }

    $newline =~ s/,\s*$//;
    $newline = "($newline)";

    return $newline;
}

###################################################
# Shorten the lines that belong to modules, if
# the length of the line is greater 100
###################################################

sub shorten_lines_at_modules
{
    my $item;
    foreach $item ( @par2script::globals::items_assigned_at_modules )
    {
        if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "shorten_lines_at_modules"); }
        my $searchkey = $par2script::globals::searchkeys{$item};

        my $allmodules = $par2script::globals::definitions{'Module'};

        my $onemodule;
        foreach $onemodule (keys %{$allmodules})
        {
            if (( exists($allmodules->{$onemodule}->{$searchkey}) ) &&
                ( length($allmodules->{$onemodule}->{$searchkey}) > 100 ))
            {
                # including "\n\t\t\t\t"
                my $newstring = include_linebreaks($allmodules->{$onemodule}->{$searchkey});
                $allmodules->{$onemodule}->{$searchkey} = $newstring;
            }
        }
    }
}

1;