opt
/
kaspersky
/
kav4fs
/
lib
/
perl
/
KL
➕ New
📤 Upload
✎ Editing:
IniFile.pm
← Back
package KL::IniFile; use strict; my $INI_ERROR_TEXT = ''; # Ini file error message sub get_error { return $INI_ERROR_TEXT ? $INI_ERROR_TEXT : undef; } # Load some data from the configuration (ini-like) file sub load { if ( !open FILE, "<$_[0]" ) { $INI_ERROR_TEXT = "unable to read file $_[0]: $!"; return; } my $content = join ("", <FILE>); close FILE; # just in case $content =~ s/\r//g; my ($sname, $sdata) = ("&%HEADER%&", ""); my (%inicontent); foreach my $line (split ("\n", $content)) { # section header if ( $line =~ /^\s*\[(.*?)\]\s*/ ) { if ( $sname ne "&%HEADER%&" ) { $inicontent{"sections"}{$sname} = $sdata; push @{$inicontent{"order"}}, $sname; } else { $inicontent{"header"} = $sdata; } ($sname, $sdata) = ($1, ""); next; } $sdata .= $line . "\n"; } if ( $sname ne "&%HEADER%&" ) { $inicontent{"sections"}{$sname} = $sdata; push @{$inicontent{"order"}}, $sname; } else { $inicontent{"header"} = $sdata; } # no sections if ( !$inicontent{"order"} || !@{$inicontent{"order"}} ) { $INI_ERROR_TEXT = "no sections found in file $_[0]"; return; } return %inicontent; } sub save { my $filename = shift; my %inidata = @_; if ( !open FILE, ">$filename" ) { $INI_ERROR_TEXT = "unable to write file $filename: $!"; return; } print FILE "$inidata{header}" if $inidata{"header"}; foreach my $name (@{$inidata{"order"}}) { print FILE "[$name]\n$inidata{sections}->{$name}"; print FILE "\n" if $inidata{"sections"}->{$name} !~ /\n\n$/; } close FILE; return 1; } # Get ini file value sub get_value { my ($ref, $section, $key) = @_; my $undefpattern = "^$key\\s*=\\s*\$"; my $pattern = "^$key\\s*=\\s*(.+?)\$"; return undef if !$ref->{"sections"}->{$section} || $ref->{"sections"}->{$section} =~ /$undefpattern/m || $ref->{"sections"}->{$section} !~ /$pattern/m; return $1; } # Set ini file value sub set_value { my ($ref, $section, %data) = @_; # add the section if not exist if ( !$ref->{"sections"}->{$section} ) { push @{$ref->{"order"}}, $section; $ref->{"sections"}->{$section} = ""; } # search through the content foreach my $key (keys %data) { my $value = defined $data{$key} ? $data{$key} : ""; $ref->{"sections"}->{$section} =~ m/^\s*$key\s*=\s*/m and $ref->{"sections"}->{$section} =~ s/^\s*$key\s*=.*?\n/$key=$value\n/m or $ref->{"sections"}->{$section} = "$key=$value\n$ref->{sections}->{$section}"; } } # Delete the whole section, or ini file key=value sub remove_value { my ($ref, $section, @keys) = @_; return if !$ref->{"sections"}->{$section}; foreach my $key (@keys) { $ref->{"sections"}->{$section} =~ s/^\s*$key\s*=\s*.*\n?//gm; } # add delete the section if empty if ( !@keys || $ref->{"sections"}->{$section} =~ /^\s*$/s ) { delete $ref->{"sections"}->{$section}; @{$ref->{"order"}} = grep { $_ ne $section } @{$ref->{"order"}}; } } # Modify some data in the configuration (ini-like) file sub update { my ($filename, $section, %data) = @_; # read inifile my %inidata = load ($filename); return if !%inidata; set_value (\%inidata, $section, %data); return save ($filename, %inidata); } 1;
💾 Save Changes
Cancel
📤 Upload File
×
Select File
Upload
Cancel
➕ Create New
×
Type
📄 File
📁 Folder
Name
Create
Cancel
✎ Rename Item
×
Current Name
New Name
Rename
Cancel
🔐 Change Permissions
×
Target File
Permission (e.g., 0755, 0644)
0755
0644
0777
Apply
Cancel