package INSTALL::component; use INSTALL::common; use INSTALL::ini; use INSTALL::global; my $pGlobals = \%INSTALL::global::variables; #============================================================================== # Miscelaneous / Helpers #============================================================================== sub encode_text { (@_ == 3) || die 'Invalid number of arguments.'; my ($text, $orig_char, $new_char) = @_; my $orig_char_pattern = quotemeta($orig_char); my $new_char_pattern = quotemeta($new_char); $text =~ s/$new_char_pattern/$new_char$new_char/g; $text =~ s/$orig_char_pattern/$new_char/g; return($text); } sub unencode_text { (@_ == 3) || die 'Invalid number of arguments.'; ($text, $orig_char, $new_char) = @_; my $orig_char_pattern = quotemeta($orig_char); my $new_char_pattern = quotemeta($new_char); $text =~ s/^$new_char_pattern([^$new_char_pattern])/$orig_char$1/g; $text =~ s/([^$new_char_pattern])$new_char_pattern([^$new_char_pattern])/$1$orig_char$2/g; $text =~ s/$new_char_pattern$new_char_pattern/$new_char/g; return($text); } sub pHash_to_text { (@_ == 1) || die 'Invalid number of arguments.'; my $pHash = shift @_; my @sections = map { my $key = $_; encode_text(encode_text($key,"=","~"),",",";") . "=" . encode_text($pHash->{$key},",",";"); } keys %{$pHash}; return(join(",",@sections)); } sub text_to_pHash { (@_ == 1) || die 'Invalid number of arguments.'; my $section_text = shift @_; my @sections = split(/\s*,\s*/,$section_text); my %hash; foreach (@sections) { /([^=]*)=(.*)/; $hash{unencode_text(unencode_text($1,",",";"),"=","~")} = encode_text($2,",",";"); } return(\%hash); } sub encode_name { (@_ == 1) || die 'Invalid number of arguments.'; my $name = shift @_; return(encode_text($name," ","_")); } sub unencode_name { (@_ == 1) || die 'Invalid number of arguments.'; my $name = shift @_; return(unencode_text($name," ","_")); } #------------------------------------------------------------------------------ #============================================================================== # Exposed interfaces #============================================================================== #------------------------------------------------------------------------------ # Returns non-zero on success. sub register { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # name N # installdir N # hash Y (@_ >= 2) || die 'Invalid number of arguments.'; my ($name, $installdir, %hash) = @_; if (not $name) { return error("Name parameter cannot be empty."); } my $pIni = INSTALL::ini::open($pGlobals->{chili_ini}); if (not $pIni) { return error("Could not update '$pGlobals->{chili_ini}'."); } my $pSection = INSTALL::ini::openSection($pIni, "$installdir"); if (not $pSection) { return error("Could not find section '$installdir'."); } my $key = "component_" . encode_name($name); if (exists $pSection->{$key}) { my $pCurHash = text_to_pHash($pSection->{$key}); foreach (keys %{$pCurHash}) { if (not exists $hash{$_}) { $hash{$_} = $pCurHash->{$_}; } } } $pSection->{$key} = pHash_to_text(\%hash); INSTALL::ini::save($pIni, $pGlobals->{chili_ini}); return(1); } # Returns non-zero on success. sub unregister { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # name N # installdir N (@_ == 2) || die 'Invalid number of arguments.'; my ($name, $installdir) = @_; # find name in section my $pIni = INSTALL::ini::open($pGlobals->{chili_ini}); if (not $pIni) { return error("Could not update '$pGlobals->{chili_ini}'."); } my $pSection = INSTALL::ini::openSection($pIni, "$installdir"); if (not $pSection) { return error("Could not find section '$installdir'."); } my $key = "component_" . encode_name($name); my $result = 0; if (exists $pSection->{$key}) { delete $pSection->{$key}; $result = 1; } INSTALL::ini::save($pIni, $pGlobals->{chili_ini}); return($result); } # Returns non-zero on success. sub list { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # installdir N (@_ == 1) || die 'Invalid number of arguments.'; my $installdir = shift@_; my $pIni = INSTALL::ini::open($pGlobals->{chili_ini}); if (not $pIni) { error("Could not update '$pGlobals->{chili_ini}'."); return @error; } my $pSection = INSTALL::ini::openSection($pIni, "$installdir"); if (not $pSection) { error("Could not find section '$installdir'."); return @error; } my @component_keys = grep { /component_/ } keys %{$pSection}; my @components = map { s/^component_//; unencode_name($_) } @component_keys; return(@components); } # Returns a reference to a hash which contains all of the values associated # with the specified component (or undef, on failure). sub open { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # name N # installdir N (@_ == 2) || die 'Invalid number of arguments.'; my ($name, $installdir) = @_; my $pIni = INSTALL::ini::open($pGlobals->{chili_ini}); if (!$pIni) { error("Could not update '$pGlobals->{chili_ini}'."); return @error; } my $pSection = INSTALL::ini::openSection($pIni, "$installdir"); if (!$pSection) { error("Could not find section '$installdir'."); return @error; } my $key = "component_" . encode_name($name); if (exists $pSection->{$key}) { return(text_to_pHash($pSection->{$key})); } else { return(undef); } } # Returns non-zero on success. It should be noted that the third parameter # (ie. ) is a pointer to a hash. Presumably this is the value returned # from open. sub save { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # name N # installdir N # pHash Y (@_ >= 2) || die 'Invalid number of arguments.'; my ($name,$installdir,$pHash) = @_; my $pIni = INSTALL::ini::open($pGlobals->{chili_ini}); if (!$pIni) { error("Could not update '$pGlobals->{chili_ini}'."); return @error; } my $pSection = INSTALL::ini::openSection($pIni, "$installdir"); if (!$pSection) { error("Could not find section '$installdir'."); return @error; } my $key = "component_" . encode_name($name); $pSection->{$key} = pHash_to_text($pHash); INSTALL::ini::save($pIni, $pGlobals->{chili_ini}); return(1); } # Returns non-zero on success. It should be noted that the third parameter # (ie. ) is an actual hash. This method is similar to save # with the exception that the current hash values are merged with the new # ones. Current values will be overwritten by new values of the same key. sub merge { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # name N # installdir N # hash Y (@_ > 2) || die 'Invalid number of arguments.'; my ($name,$installdir,%hash) = @_; my $pIni = INSTALL::ini::open($pGlobals->{chili_ini}); if (!$pIni) { error("Could not update '$pGlobals->{chili_ini}'."); return @error; } my $pSection = INSTALL::ini::openSection($pIni, "$installdir"); if (!$pSection) { error("Could not find section '$installdir'."); return @error; } my $key = "component_" . encode_name($name); if (exists $pSection->{$key}) { my $pCurHash = text_to_pHash($pSection->{$key}); foreach (keys %{$pCurHash}) { if (not exists $hash{$_}) { $hash{$_} = $pCurHash->{$_}; } } } $pSection->{$key} = pHash_to_text(\%hash); INSTALL::ini::save($pIni, $pGlobals->{chili_ini}); return(1); } 1;