Загрузка данных


###############################################################################

sub load_config_files
{
    my ($cfgFile) = @_;

    # Load input file
    my @cfg = &load_file($cfgFile);

    # Output status
    printf("Parsing ...\n");

    # Log original input files
    &log_array('Input file(s)', \@cfg);

    # Do substitutions
    my $maxLoop = 10;
    while (&do_substitutions(\@cfg) != 0)
    {
        $maxLoop--;
        if ($maxLoop == 0)
        {
            print "error: max recursive substitution reached\n";
            exit;
        }
    }

    # Remove dictionary lines
    @cfg = grep(!/^\w+:/, @cfg);

    # Expand API perms to all protocols
    foreach (@permTypes)
    {
		my $perm = $_;
		my $replace = '';

	    foreach (@protocols)
	    {
	    	$replace .= $_ . "=$perm ";
	    }
		chomp $replace;

	    s/api=$perm\b/$replace/g for @cfg;
	}	    

    # Replace standard perms
    s/perm=0\b/perm=0x0000/g for @cfg;
    s/perm=none\b/perm=0x0000/g for @cfg;
    s/perm=sec_r\b/perm=0x4400/g for @cfg;
    s/perm=secpriv_rx\b/perm=0x5000/g for @cfg;
    s/perm=secpriv_rw\b/perm=0x6000/g for @cfg;
    s/perm=secpriv_rwx\b/perm=0x7000/g for @cfg;
    s/perm=sec_rw\b/perm=0x6600/g for @cfg;
    s/perm=rw\b/perm=0x6666/g for @cfg;
    s/perm=sec_rwx\b/perm=0x7700/g for @cfg;
    s/perm=ro\b/perm=0x4444/g for @cfg;
    s/perm=nspriv_r\b/perm=0x7740/g for @cfg;
    s/perm=ns_r\b/perm=0x7744/g for @cfg;
    s/perm=nspriv_rw\b/perm=0x6660/g for @cfg;
    s/perm=nspriv_rwx\b/perm=0x7770/g for @cfg;
    s/perm=full\b/perm=0x7777/g for @cfg;

    # Expand SYS/FUSA
    s/^SYS\b/SYS_0/g for @cfg;
    s/^FUSA\b/FUSA_0/g for @cfg;

    # Remove duplicate words
    foreach (@cfg)
    {
        my (%before, @words, @new);

        @words = split(/ /, $_);
        @new = grep { ! $before{$_}++ } @words;
        $_ = join ' ', @new;
    }

    # Default resources to DEV_SM
    foreach my $line (@cfg)
    {
        foreach my $proto (@protocols)
        {
            my $search = uc $proto . '_';

            $line =~ s/\b($search\w+)\b/DEV_SM_$1/g;
        }
    }

    # Remove unused API permissions
    foreach my $line (@cfg)
    {
        foreach my $proto (@protocols)
        {
            my $search = uc $proto . '_';

            if (!($line =~ /_SM_$search/))
            {
                $line =~ s/\b$proto=\w+ //g;
            }
        }
    }

    # Remove perms if no TRDC
    foreach my $line (@cfg)
    {
        if (!(($line =~ /MBC_\w+=/) || ($line =~ /MRC_\w+=/)
            || ($line =~ /MDAC_\w+=/) || ($line =~ /^DOM\d*\b/)
            || ($line =~ /^LM\d*\b/)|| ($line =~ /^DEBUG\b/)))
        {
            $line =~ s/\bdid=\w+ //g;
        }
        if (!(($line =~ /MBC_\w+=/) || ($line =~ /MRC_\w+=/)))
        {
            $line =~ s/\bperm=\w+ //g;
        }
        if (!($line =~ /MDAC_\w+=/))
        {
            $line =~ s/\bsa=\w+ //g;
            $line =~ s/\bpa=\w+ //g;
        }
    }

    # Remove leading/trailing spaces
    s/^\s+|\s+$//g for @cfg;

    # Remove extra spaces
    s/\s+/ /g for @cfg;

    # Append end marker
    push @cfg, 'EOF';

    # Append space at end of every line
    s/$/ /g for @cfg;

    # Log parsed data
    &log_array('Parsed data', \@cfg);

    return @cfg; 
}

###############################################################################

sub load_file
{
    my ($fileName) = @_;
    my @contents;
    my $line = '';
    my $lineNum = 1;

    # Open file
    open my $in, '<', $fileName
        or die "error: failure to open: $fileName, $!";
    if ($verbose)
    {
        my $fn = fileparse($fileName);
        if ($verbose)
        {
            printf("Loading $fn ...\n");
        }
    }

    # Load into array
    while (<$in>)
    {
        # Remove CR/LF
        chomp $_;      
        $_ =~ s/\r//;

		# Get copyright
		if (/Copyright\s+[0-9]/)
		{
			if ($copyright eq '')
			{
				$copyright = $_;

			    # Replace hashs
                $copyright =~ s/^# /## /;
			    $copyright =~ s/#/*/g;
			}
		}

        # Remove comment
        $_ =~ s/#.*//;    

        # Continue line?
        if (/\\$/)
        {
            $_ =~ s/\\$//;
            $line .= $_ . ' ';
        }
        elsif (/^include/)
        {
            # Get fileName
            my($first, $rest) = split(/ /, $_, 2);

            # Load include file
            my @inc = &load_file(dirname($fileName) . '/' . $rest);

            # Append to contents
            push @contents, @inc;
        }
        else
        {
            $line .= $_;

            push @contents, $line . ' filename=' . $fileName
                . ' line=' . $lineNum;
            $line = '';
        }
        $lineNum++;
    }

    # Replace ,
    s/,/ /g for @contents;

    # Remove tabs
    s/\t/ /g for @contents;

    # Remove leading/trailing spaces
    s/^\s+|\s+$//g for @contents;

    # Remove extra spaces
    s/\s+/ /g for @contents;

    # Remove empty lines
    @contents = grep(/\S/, @contents);
    @contents = grep(!/^filename=/, @contents);

    # Remove comment lines
    @contents = grep(!/^#/, @contents);

    # Close file
    close($in);

    return @contents;
}