View Full Version : Parser
digitalsea
06-30-03, 11:53 AM
Hey everyone,
I'm needing a script that can parse a file size (like 35 MB) through a form and convert that into bytes. This also needs to work with KBs and weird capitalization of MB and KB (ex: kB, Kb, kb, KB).
Any ideas or code that would work?
Fahim Ferdous
07-21-03, 10:00 PM
You want the size of the file in bytes?
digitalsea
07-21-03, 10:06 PM
Yeah, but since it took so long for someone to respond, I just did it myself:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
$query = $ENV{'QUERY_STRING'};
($name, $value) = split(/==/, $query);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
my @sizes = ($FORM{size});
#size of different data types as log2
#ie, a kilobyte (k) is 2^10 bytes
my %mag = ( b => 0, #bytes
k => 10, #kilo
m => 20, #mega
g => 30, #giga
t => 40, #tera
);
for(@sizes)
{
#zero or more numbers, maybe a decimal, one or more numbers
#maybe whitespace, then one of our magnitude hash keys
if(/(\d*\.?\d+)\s*?([bkmgt])/i)
{
#number of units ($1) * the size (2^ ) of that unit ($2)
print $1*(2**$mag{lc $2}) . " bytes!\n";
}
}
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.