diff --git a/opentrafficshaper/utils.pm b/opentrafficshaper/utils.pm index 47ffc9fffed6b6a7b384b5fc26e8bea9acdec3df..894cda2c4e949472dac1bf6e8daf95b5931fcbc7 100644 --- a/opentrafficshaper/utils.pm +++ b/opentrafficshaper/utils.pm @@ -33,6 +33,8 @@ our (@ISA,@EXPORT,@EXPORT_OK); isUsername isIP isNumber + + booleanize ); @EXPORT_OK = qw( ); @@ -174,5 +176,38 @@ sub isNumber } +# Booleanize the variable depending on its contents +sub booleanize +{ + my $var = shift; + + + # Check if we're defined + if (!isVariable($var)) { + return undef; + } + + # If we're a number + if (my $val = isNumber($var)) { + if ($val == 0) { + return 0; + } else { + return 1; + } + } + + # Nuke whitespaces + $var =~ s/\s//g; + + # Allow true, on, set, enabled, 1 + if ($var =~ /^(?:true|on|set|enabled|1|yes)$/i) { + return 1; + } + + # Invalid or unknown + return 0; +} + + 1; # vim: ts=4