1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# Copyright (C) 2002,2003 Mandrakesoft |
4 |
# |
5 |
# Author: Philippe Hetroy, phetroy@mandrakesoft.com |
6 |
# |
7 |
# $Id: Varspaceval.pm,v 1.5 2004-01-22 20:24:48 tvignaud Exp $ |
8 |
# Module for loding and committing informations in a VAR = value file type |
9 |
|
10 |
package MDK::Wizard::Varspaceval; |
11 |
use lib('./'); |
12 |
use strict; |
13 |
use Data::Dumper; |
14 |
|
15 |
# Get all useful content of the config file |
16 |
# Return a hash containg the key and the value |
17 |
# ATTENTION : in the conf file, an empty value is returnes as a spaced value (mandatory because of XML compatibility) |
18 |
sub get { |
19 |
my ($_self, $file) = @_; |
20 |
my %l; |
21 |
local *F; open F, $file or return; |
22 |
local $_; |
23 |
|
24 |
while (<F>) { |
25 |
|
26 |
my ($v, $val, $val2) = |
27 |
/^\s* # leading space |
28 |
(\w+)\s* # variable |
29 |
( |
30 |
"(.*)" # double-quoted text |
31 |
| '(.*)' # single-quoted text |
32 |
| [^'"\s]* # normal text |
33 |
) |
34 |
\s*$ # end of line |
35 |
/x; |
36 |
no warnings; |
37 |
$l{$v} = defined $val2 ? $val2 : $val; |
38 |
} |
39 |
|
40 |
%l; |
41 |
} |
42 |
|
43 |
# Commits changes in conf files and ifconfig |
44 |
sub commit { |
45 |
my ($_self, $file, $hash) = @_; |
46 |
local *F; |
47 |
|
48 |
my $output = ""; |
49 |
if (open(F, $file)) { |
50 |
local $_; |
51 |
|
52 |
while (<F>) { |
53 |
my ($pre, $key, $eq, $val, $rest) = /(^\s*)(\w+)(\s*"*'*)([^'"\s]*)(.*)/x; |
54 |
|
55 |
if (!defined $key) { |
56 |
$output .= $_; |
57 |
next; |
58 |
}; |
59 |
next if !exists $hash->{$key}; #Elt has been removed |
60 |
no warnings; |
61 |
$val = $hash->{$key}; |
62 |
delete $hash->{$key}; |
63 |
$output .= defined $val ? $pre . $key . $eq . $val . $rest . "\n" : $pre . $key . $eq . $val . $rest; |
64 |
# $output .= $pre . $key . $eq . $val . $rest . "\n"; |
65 |
} |
66 |
#appending added parameters |
67 |
foreach (keys %$hash) { |
68 |
$output .= $_ . " " . $hash->{$_} . "\n"; |
69 |
} |
70 |
|
71 |
} else { #the file does not exist |
72 |
print STDERR "File $file will be created\n"; |
73 |
foreach (keys %$hash) { |
74 |
$output .= defined $hash->{$_} ? $_ . "=" . $hash->{$_} . "\n" : $_ . "=\n"; |
75 |
} |
76 |
} |
77 |
|
78 |
#print $output; |
79 |
#print "\n------------------\n"; |
80 |
|
81 |
# outputing the new conf |
82 |
open(F, "> $file") or return; |
83 |
print F $output; |
84 |
close(F); |
85 |
} |
86 |
|
87 |
1; |