Thread Datei lesen und schreiben (4 answers)
Opened by Gast at 2009-01-28 22:25

Gast Gast
 2009-01-29 18:04
#118515 #118515
Was sagt ihr zu meinem Konfigurationsfileskript?

Code (perl): (dl )
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
#!/usr/bin/perl
use warnings;
use strict;


my $file;
my %hash = ();
my $key = '';
my %kon = ();
my @kontrolle = ();
my $count = 0;


while ( defined ( my $line = <DATA> ) ) {
        chomp ( $line );
        if ( $line =~ /^\s*#/ || $line =~ /^\s*$/ ) {
                next;
        }
        elsif ( $line =~ /^\s*Datei\s*=\s*'(.*)'/ ) {
                if ( $file ) {
                        write_to_file ( $file );
                        %hash = ();
                }
                $file = $1;
                if ( ! -f $file ) {
                        open ( my $fh_w, '>>', $file ) or die "$file $!";
                        close ( $fh_w );
                }
                open ( my $fh, '<', $file ) or die "$file $!";
                while ( <$fh> ) {
                        chomp;
                        next if $_ =~ /^\s*#/ || $_ =~ /^\s*$/;
                        if ( /^\s*\[([^\]]+)\]/ ) {
                                $key = $1;
                                next;
                        }
                        my ( $k, $v ) = split /\s*=\s*/;
                        ${$hash{$key}}{$k} = $v;
                }
                close ( $fh );
        } else {
                if ( $line =~ /^\s*\[([^\]]+)\]/ ) {
                        $key = $1;
                        next;
                }
                my ( $k, $v ) = split /\s*=\s*/, $line;
                ${$hash{$key}}{$k} = $v;
                $kon{$k}++;
        }
}

write_to_file ( $file );

sub write_to_file {
        my $file = shift;
        open ( my $fh_write, '>', $file ) or die "$file $!";
                        foreach my $key ( sort keys %hash ) {
                                print $fh_write "[$key]\n";
                                foreach my $k ( sort keys %{$hash{$key}} ) {
                                        foreach ( keys %kon ) {
                                                push ( @kontrolle, [ $file, $key, $k, ${$hash{$key}}{$k} ] ) if $_ eq $k;
                                        }
                                        print $fh_write $k, "=", ${$hash{$key}}{$k}, "\n";
                                }
                        }
        close ( $fh_write );
}

my $dat = '0';
foreach ( sort { $a->[0] cmp $b->[0] || $a->[2] cmp $b->[2] } @kontrolle ) {
        print "\n" if $dat ne $_->[0];
        print "$_->[0]\t: $_->[2]=$_->[3] [$_->[1]] \n";
        $dat = $_->[0];
}


__DATA__
Datei = '/home/manfred/.kde/share/config/kdesktoprc'
[FMSettings]
ShowFileTips=false
[ScreenSaver]
Enabled=false

Datei = '/home/manfred/.kde/share/config/konquerorrc'
[FMSettings]
ShowFileTips=false
[Trash]
ConfirmTrash=false


View full thread Datei lesen und schreiben