ConfigSorted.pm 2.24 KB
Newer Older
1
2
3
4
#-----------------------------------------------------------------------
# Fault InJection Instrumenter (FIJI)
# https://embsys.technikum-wien.at/projects/vecs/fiji
#
5
6
7
8
# The creation of this file has been supported by the publicly funded
# R&D project Josef Ressel Center for Verification of Embedded Computing
# Systems (VECS) managed by the Christian Doppler Gesellschaft (CDG).
#
9
10
11
# Authors:
# Christian Fibich <fibich@technikum-wien.at>
# Stefan Tauner <tauner@technikum-wien.at>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# This module is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the LICENSE file for more details.
#-----------------------------------------------------------------------

## @file ConfigSorted.pm
# @brief Contains class \ref FIJI::ConfigSorted

## @class FIJI::ConfigSorted
# @brief Variation of Config::Simple that produces sorted sections and entries within a section

Christian Fibich's avatar
Christian Fibich committed
29
30
package FIJI::ConfigSorted;

31
32
33
use strict;
use warnings;

34
use Config::Simple;
35
use Sort::Naturally;
36

Christian Fibich's avatar
Christian Fibich committed
37
38
39
40
# Set version information
use vars qw($VERSION);
$VERSION = '0.1';

41
use base qw(Config::Simple);
Christian Fibich's avatar
Christian Fibich committed
42
43
44
45
46
47
48

# generates a writable string
sub as_string {
    my $self = shift;

    my $syntax = $self->{_SYNTAX} or die "'_SYNTAX' is not defined";
    my $sub_syntax = $self->{_SUB_SYNTAX} || '';
Christian Fibich's avatar
Christian Fibich committed
49
50
51
    my $currtime   = localtime;
    my $STRING     = undef;
    if ($syntax eq 'ini') {
Christian Fibich's avatar
Christian Fibich committed
52
53
        $STRING .= "; FIJI::ConfigSorted $VERSION\n";
        $STRING .= "; $currtime\n\n";
54
        for my $block_name (nsort(keys %{$self->{_DATA}})) {
Christian Fibich's avatar
Christian Fibich committed
55
            my $key_values = $self->{_DATA}->{$block_name};
Christian Fibich's avatar
Christian Fibich committed
56
            unless ($sub_syntax eq 'simple-ini') {
Christian Fibich's avatar
Christian Fibich committed
57
58
59
60
                $STRING .= sprintf("[%s]\n", $block_name);
            }
            for my $key (sort keys %{$key_values}) {
                my $value = $key_values->{$key};
61
                my $values = join($self->SUPER::WRITE_DELIM, map { $self->SUPER::quote_values($_) } @$value);
Christian Fibich's avatar
Christian Fibich committed
62
                $STRING .= sprintf("%s=%s\n", $key, $values);
Christian Fibich's avatar
Christian Fibich committed
63
64
65
66
67
68
69
70
71
            }
            $STRING .= "\n";
        }
    }
    $STRING .= "\n";
    return $STRING;
}

1;