summaryrefslogtreecommitdiffstats
path: root/common/Varspaceval.pm
blob: 4df1e6754c708e4d275cc18490dba7059208b534 (plain)
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
#!/usr/bin/perl -w

# Copyright (C) 2002,2003 Mandrakesoft 
#
# Author: Philippe Hetroy, phetroy@mandrakesoft.com
#
# $Id: Varspaceval.pm,v 1.5 2004-01-22 20:24:48 tvignaud Exp $
# Module for loding and committing informations in a VAR = value file type

package MDK::Wizard::Varspaceval;
#use lib('./');
use strict;
use Data::Dumper;
use MDK::Common;

# Get all useful content of the config file
# Return a hash containg the key and the value
# WARNING : in the conf file, an empty value is returnes as a spaced value (mandatory because of XML compatibility)
sub get {
    my ($_self, $file) = @_;
    my %l;

    foreach (cat_($file)) {

        my ($v, $val, $val2) =
          /^\s*                 # leading space
           (\w+)\s*              # variable
           (
               "(.*)"        # double-quoted text
             | '(.*)'        # single-quoted text
	     | [^'"\s]*         # normal text
           )
           \s*$                 # end of line
          /x;
         no warnings;
         $l{$v} = defined $val2 ? $val2 : $val;
    }

    %l;
}

# Commits changes in conf files and ifconfig
sub commit {
    my ($_self, $file, $hash) = @_;

    my $output = "";
    if (-r $file) {

	foreach (cat_($file)) {
	    my ($pre, $key, $eq, $val, $rest) = /(^\s*)(\w+)(\s*"*'*)([^'"\s]*)(.*)/x;

            if (!defined $key) {
                $output .= $_;
                next;
            }
            next if !exists $hash->{$key};    #Elt has been removed
            no warnings;
            $val = delete $hash->{$key};
            my $out_val = $pre . $key . $eq . $val .  $rest;
            $output .= defined $val ? $out_val . "\n" : $out_val;
        }
            #appending added parameters
            foreach (keys %$hash) {
                $output .= $_ . " " . $hash->{$_} . "\n";
            }

    } else {    #the file does not exist
	print STDERR "File $file will be created\n";
	# FIXME: why "=" here but " " just above?
	foreach (keys %$hash) {
	    $output .= $_ . "=" . $hash->{$_} . "\n";
	}
    }

     # outputing the new conf
     output($file, $output);
}

1;