![]() |
![]() |
7 Einträge, 1 Seite |
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
#/usr/bin/perl
use strict;
use warnings;
return 1;
sub read_ini
{
# reads an ini-file
# returns a hash containing the key-value pairs
my @content;
my %lines;
my $file=shift; # filename
my $name=shift; # name of target topic
open(IN,"<$file") or die "$!";
@content=<IN>;
close(IN);
my $i=0;
my $n=0;
for (@content)
{
$i++ if /\[.*?\]/io;
$n=$i if /\[$name\]/io;
$lines{"$1"}="$2" if /^(.*?)\=(.*)$/i and $i==$n;
last if $i>$n;
}
return %lines;
}
sub change_ini
{
# changes key-value pairs
# returns true when done
my @content;
my %lines;
my $file=shift; # filename
my $name=shift; # name of target topic
my $key=shift; # target key
my $value=shift; # new value
open(IN,"<$file") or die "$!";
@content=<IN>;
close(IN);
my $i=0;
my $n=0;
for (@content)
{
$i++ if /\[.*?\]/io;
$n=$i if /\[$name\]/io;
s/(^$key\=)(.*)$/$1$value/i, last if /(^$key\=)(.*)$/i and $i==$n;
}
open(IN,">$file") or die "$!";
for(@content)
{
print IN $_;
}
close(IN);
return 1;
}
sub ins_item_ini
{
# inserts a new key-value pair
# returns true when done
my @content;
my $file=shift; # filename
my $name=shift; # name of topic
my $new_kv=shift; # something like admin=123456
open(IN,"<$file") or die "$!";
@content=<IN>;
close(IN);
my $i=0;
my $n=0;
open(IN,">$file") or die "$!";
for(@content)
{
$i++ if /\[.*?\]/io;
$n=$i if /\[$name\]/io and $n ne '-';
if($n eq $i and $n ne '-')
{
print IN "$new_kv\n";
print IN $_;
$n='-';
}
else
{
print IN $_;
}
}
close(IN);
return 1;
}
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
#!/C:/Perl/bin/perl.exe
use strict;
use warnings;
return 1;
sub read_ini
{
my @content;
my %lines;
my $file=shift;
my $name=shift;
open(IN,"<$file") or die "$!";
undef $_;
@content=<IN>;
close(IN);
my $i=0;
my $n=0;
for (@content)
{
$i++ if /\[.*?\]/i;
$n=$i if /\[$name\]/i;
$lines{"$1"}="$2" if /^(.*?)\=(.*)$/i and $i==$n;
}
return %lines;
}
sub change_ini
{
my @content;
my %lines;
my $file=shift;
my $name=shift;
my $key=shift;
my $value=shift;
open(IN,"<$file") or die "$!";
@content=<IN>;
close(IN);
my $i=0;
my $n=0;
for (@content)
{
$i++ if /\[.*?\]/io;
$n=$i if /\[$name\]/io;
s/(^$key\=)(.*)$/$1$value/i, last if /(^$key\=)(.*)$/i and $i==$n;
}
open(IN,">$file") or die "$!";
for(@content)
{
print IN $_;
}
close(IN);
}
sub insert_item
{
my @content;
my $file=shift;
my $name=shift;
my $new_kv=shift;
open(IN,"<$file") or die "$!";
@content=<IN>;
close(IN);
my $i=0;
my $n=0;
open(IN,">$file") or die "$!";
for(@content)
{
$i++ if /\[.*?\]/io;
$n=$i if /\[$name\]/io and $n ne '-';
if($n eq $i and $n ne '-')
{
print IN $_;
print IN "$new_kv\n";
$n='-';
}
else
{
print IN $_;
}
}
close(IN);
}
sub insert_topic
{
my $file=shift;
my $name=shift;
open(IN,">>$file") or die "$!";
print IN "[$name]\n";
close(IN);
}
1
2
3
4
5
6
7
8
9
10
[Private]
Nordlicht=1234
[Documention]
all=*
webmask=local
[Stats]
Admin=QzeCat96
Root=PakYupTon
http-put=1
http-delete=1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/C:/Perl/bin/perl.exe
use strict;
use warnings;
require 'ini.pl';
# Ein neues Wertepaar einfügen
insert_item('director.ini','Private','user2=4567');
# ein Wertepaar verändern
change_ini('director.ini','Private','Nordlicht','abcd');
# ein neues topic einfügen
insert_topic('director.ini','Admin');
# Konfiguration in einem topic auslesen
my %config = read_ini('director.ini','Stats');
while(my ($k,$v) = each(%config))
{
#und etwas damit tun...
print "$k - $v\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
[Private]
user2=4567
Nordlicht=abcd
[Documention]
all=*
webmask=local
[Stats]
Admin=QzeCat96
Root=PakYupTon
http-put=1
http-delete=1
[Admin]
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
package Token;
use Moose;
has 'type' => ( is => 'rw' );
has 'value' => ( is => 'rw' );
package Tokenizer;
use Moose;
has 'token' => (
isa => 'ArrayRef',
default => sub { [] },
is => 'rw',
);
sub tokenize {
my $self = shift;
my $stream = shift || '';
TOKEN: {
$self->add_token('SECTION', $1) if $stream =~ /\G\[(\w+)\]/gcx;
$self->add_token('VARIABLE', $1) if $stream =~ /\G(\w+)\s*=/gcx;
$self->add_token('VALUE', $1) if $stream =~ /\G(\w+)/gcx;
redo TOKEN if $stream =~ /\G\s+/gcx;
};
}
sub add_token {
my $self = shift;
my ($t, $v) = @_;
push @{$self->token}, Token->new( type => $t, value => $v );
}
package main;
my $d;
{
local($/);
$d = <DATA>;
}
my $t = Tokenizer->new;
$t->tokenize($d);
warn Dumper $t;
my %config;
my ($cur_section, $cur_variable, $cur_value);
for my $token (@{$t->token}) {
$cur_section = $token->value if $token->type eq 'SECTION';
die unless $cur_section;
$cur_variable = $token->value if $token->type eq 'VARIABLE';
$cur_value = $token->type eq 'VALUE' ? $token->value : undef;
push @{$config{$cur_section}->{$cur_variable}}, $cur_value
if (defined $cur_value and defined $cur_variable);
}
warn Dumper \%config;
__DATA__
[foo]
bar = buz
qiz = 42 17 23
[languages]
favorite = perl python ruby
disliked = C Java
![]() |
![]() |
7 Einträge, 1 Seite |