Add size specification parsing

feature/node-rewrite
Sven Slootweg 12 years ago
parent 39408154fa
commit be7cc50582

@ -55,3 +55,5 @@ class ContainerTrafficRetrieveException extends ContainerException {}
class UnauthorizedException extends Exception {} class UnauthorizedException extends Exception {}
class InsufficientAccessLevelException extends Exception {} class InsufficientAccessLevelException extends Exception {}
class TemplateUnavailableException extends Exception {} class TemplateUnavailableException extends Exception {}
class ParsingException extends Exception {}

@ -37,4 +37,58 @@ function shrink_command($command)
return $command; return $command;
} }
function parse_size($size, $multiplier = 1024)
{
/* Note that this function will always assume bytes and the given multiplier, regardless of
* what is actually specified. */
if(preg_match("/(-?[0-9.,]+)\s*(([kKmMgGtTpPeEzZyY]?)([iI]?)([bB]?))/", $size, $matches))
{
$number = (float) $matches[1];
$unit = $matches[2];
$prefix = $matches[3];
$suffix = $matches[5];
if(empty($prefix))
{
/* Size is in bytes. */
return $number;
}
else
{
switch(strtolower($prefix))
{
case "y":
$number = $number * $multiplier;
case "z":
$number = $number * $multiplier;
case "e":
$number = $number * $multiplier;
case "p":
$number = $number * $multiplier;
case "t":
$number = $number * $multiplier;
case "g":
$number = $number * $multiplier;
case "m":
$number = $number * $multiplier;
case "k":
$number = $number * $multiplier;
break;
default:
throw new ParsingException("No valid unit was specified.");
}
return $number;
}
}
elseif(is_numeric($size))
{
return (int) $size;
}
else
{
throw new ParsingException("The given size specification could not be parsed.");
}
}
?> ?>

@ -39,7 +39,7 @@ test_iprange("0.0.0.0/1", "0.0.0.0", "127.255.255.255", 1, 4);
test_iprange("162.16.47.0/16", "162.16.0.0", "162.16.255.255", 16, 4); test_iprange("162.16.47.0/16", "162.16.0.0", "162.16.255.255", 16, 4);
test_iprange("192.168.1.0/27", "192.168.1.0", "192.168.1.31", 27, 4); test_iprange("192.168.1.0/27", "192.168.1.0", "192.168.1.31", 27, 4);
test_iprange("192.168.1.0/32", "192.168.1.0", "192.168.1.0", 32, 4); test_iprange("192.168.1.0/32", "192.168.1.0", "192.168.1.0", 32, 4);
test_iprange("192.168.1.0", "192.168.1.0", "192.168.1.0", 0, 4);*/ test_iprange("192.168.1.0", "192.168.1.0", "192.168.1.0", 0, 4);
$sContainer = new Container(0); $sContainer = new Container(0);
$sContainer->uHostname = "test6.cryto.net"; $sContainer->uHostname = "test6.cryto.net";
@ -59,4 +59,10 @@ $sContainer->uTotalTrafficLimit = 1000000000000;
$sContainer->InsertIntoDatabase(); $sContainer->InsertIntoDatabase();
$sContainer->Deploy(); $sContainer->Deploy();
*/
var_dump(
parse_size("15m"), parse_size("24 KB"), parse_size("51"),
parse_size("2 TiB"), parse_size("4.9GiB"), parse_size("0.75GB"),
parse_size("20gb", 1000), parse_size("14.6 TiB", 1000), parse_size("84YB")
);

Loading…
Cancel
Save