Stripping trailing space from configuration values
This commit is contained in:
parent
7b7fac78b3
commit
bb601b5fa6
33
ConfigMicro.pm
Normal file → Executable file
33
ConfigMicro.pm
Normal file → Executable file
@ -39,7 +39,7 @@
|
||||
# <pre>{ "sectionA" => { "keyA" => "valueA",
|
||||
# "keyB" => "valueB" },
|
||||
# "sectionB" => { "keyA" => "valueC",
|
||||
# "keyC" => "valueD" }
|
||||
# "keyC" => "valueD" }
|
||||
# }</pre>
|
||||
package ConfigMicro;
|
||||
|
||||
@ -59,7 +59,7 @@ BEGIN {
|
||||
|
||||
## @cmethod $ new(%args)
|
||||
# Create a new ConfigMicro object. This creates an object that provides functions
|
||||
# for loading and saving configurations, and pulling config data from a database.
|
||||
# for loading and saving configurations, and pulling config data from a database.
|
||||
# Meaningful options for this are:
|
||||
# filename - The name of the configuration file to read initial settings from. This
|
||||
# is optional, and if not specified you will get an empty object back.
|
||||
@ -101,7 +101,7 @@ sub read {
|
||||
my $filename = shift or return set_error("No file name provided");
|
||||
|
||||
# The current section, default it to '_' in case there is no leading [section]
|
||||
my $section = "_";
|
||||
my $section = "_";
|
||||
|
||||
# TODO: should this return the whole name? Possibly a security issue here
|
||||
return set_error("Failed to open '$filename': $!")
|
||||
@ -126,8 +126,9 @@ sub read {
|
||||
|
||||
# Handle attributes without quoted values - # or ; at any point will mark comments
|
||||
} elsif($line =~ /^\s*([\w\-]+)\s*=\s*([^\#;]+)/ ) {
|
||||
$self -> {$section} -> {$1} = $2;
|
||||
|
||||
my $key = $1;
|
||||
$self -> {$section} -> {$key} = $2;
|
||||
$self -> {$section} -> {$key} =~ s/^\s*(.*?)\s*$/$1/;
|
||||
# bad input...
|
||||
} else {
|
||||
close(CFILE);
|
||||
@ -142,11 +143,11 @@ sub read {
|
||||
|
||||
|
||||
## @method $ text_config(@skip)
|
||||
# Create a text version of the configuration stored in this ConfigMicro object.
|
||||
# Create a text version of the configuration stored in this ConfigMicro object.
|
||||
# This creates a string representation of the configuration suitable for writing to
|
||||
# an ini file or otherwise printing.
|
||||
# an ini file or otherwise printing.
|
||||
#
|
||||
# @param skip If you specify one or more section names, the sections will not be
|
||||
# @param skip If you specify one or more section names, the sections will not be
|
||||
# added to the string generated by this function.
|
||||
# @return A string representation of this ConfigMicro's config settings.
|
||||
sub text_config {
|
||||
@ -158,12 +159,12 @@ sub text_config {
|
||||
foreach $key (sort(keys(%$self))) {
|
||||
# Skip the internal settings
|
||||
next if($key eq "__privdata");
|
||||
|
||||
|
||||
# If we have any sections to skip, and the key is one of the ones to skip... skip!
|
||||
next if(scalar(@skip) && grep($key, @skip));
|
||||
|
||||
next if(scalar(@skip) && grep($key, @skip));
|
||||
|
||||
# Otherwise, we want to start a new section. Entries in the '_' section go out
|
||||
# with no section header.
|
||||
# with no section header.
|
||||
$result .= "[$key]\n" if($key ne "_");
|
||||
|
||||
# write out all the key/value pairs in the current section
|
||||
@ -183,7 +184,7 @@ sub text_config {
|
||||
# @param filename The file to save the configuration to.
|
||||
# @param skip An optional list of names of sections to ignore when writing the
|
||||
# configuration.
|
||||
# @return true if the configuration was saved successfully, false if a problem
|
||||
# @return true if the configuration was saved successfully, false if a problem
|
||||
# occurred.
|
||||
sub write {
|
||||
my $self = shift;
|
||||
@ -232,7 +233,7 @@ sub load_db_config {
|
||||
while($row = $confh -> fetchrow_hashref()) {
|
||||
$self -> {"config"} -> {$row -> {$name}} = $row -> {$value};
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -255,7 +256,7 @@ sub save_db_config {
|
||||
my $value = shift || "value";
|
||||
|
||||
my $confh = $dbh -> prepare("UPDATE $table SET `$value` = ? WHERE `$name` = ?");
|
||||
|
||||
|
||||
foreach my $key (keys(%{$self -> {"config"}})) {
|
||||
$confh -> execute($self -> {"config"} -> {$key}, $key)
|
||||
or return set_error("Unable to execute UPDATE query - ".$dbh -> errstr);
|
||||
@ -283,7 +284,7 @@ sub set_db_config {
|
||||
my $value = shift;
|
||||
my $namecol = shift || "name";
|
||||
my $valuecol = shift || "value";
|
||||
|
||||
|
||||
my $confh = $dbh -> prepare("UPDATE $table SET `$valuecol` = ? WHERE `$namecol` = ?");
|
||||
$confh -> execute($value, $name)
|
||||
or return set_error("Unable to execute UPDATE query - ".$dbh -> errstr);
|
||||
|
Loading…
x
Reference in New Issue
Block a user