package WebGUI::Macro::HRF;

# Klaus Hertle <online@hertle.net>                   17-Nov-2004

=head1 LEGAL

-------------------------------------------------------------------

 This module is free software; you can redistribute it and/or 
 modify it under the terms of the GNU General Public License 
 version 2 as published by the Free Software Foundation and 
 included to WebGUI.
 Please read the legal notices (docs/legal.txt) and the 
 license (docs/license.txt) that came with the WebGUI 
 distribution before using this software.

-------------------------------------------------------------------

=cut


use strict;
our $VERSION = '1.0.0';
use WebGUI::Macro;
use WebGUI::Utility;


=head1 NAME

Package WebGUI::Macro::HRF

=head1 SYNOPSIS

^HRF(byte-value);

^HRF(byte-value,digits-after-decimal);


=head1 INSTALLATION

To enable this macro copy this file to your WebGUI macro directory 
[WebGUI_ROOT]/lib/WebGUI/Macro and add the line

 HRF=>HRF, \ 

to your WebGUI conf file.

When using ModPerl restart your webserver.


=head1 UNINSTALLATION

To uninstall this macro remove the line

 HRF=>HRF, \ 

from your WebGUI conf file and delete this file from your WebGUI macro directory 
[WebGUI_ROOT]/lib/WebGUI/Macro.

When using ModPerl restart your webserver.



=head1 DESCRIPTION

This macro returns an given byte value in human readable format. 
You can pass up to two parameters to the macro: byte value and digits after decimal. 
The first parameter represents the byte value to be transformed. This parameter is mandatory.
The second parameter specifies the digits after the decimal point. The default is 0. This parameter is optional.



=head1 EXAMPLES

^Size(12); reports 12 B.

^Size(1024); reports 1 kB.

^Size(1048576); reports 1 MB.

^Size(1183741824);   reports 1 GB

^Size(1183741824,1); reports 1.1 GB

^Size(1183741824,2); reports 1.10 GB

^Size(1183741824,3); reports 1.102 GB

=cut


#-------------------------------------------------------------------

#
# transform to human readable format
#

sub _hrf {
	my $byte = shift;
        my $dig = shift;
	my $hrf;

	if ($byte >= 1073741824) {
		$hrf = round($byte/1073741824,$dig);
		$hrf .= '&nbsp;GB';
	} elsif ($byte >= 1048576) {
		$hrf = round($byte/1048576,$dig);
                $hrf .= '&nbsp;MB';
	} elsif ($byte >= 1024) {
		$hrf = round($byte/1024,$dig);
		$hrf .= '&nbsp;kB';
	} else {
		$hrf = $byte.'&nbsp;B';
	}         

	return $hrf;

}


#-------------------------------------------------------------------

#
# Main macro routine
#


sub process {my ( $output, $byte_input, $digits );

	my $params = shift;
        
        my ($byte_input,$digits) = WebGUI::Macro::getParams($params);
	
        my $options = scalar WebGUI::Macro::getParams($params);
	return "<b>Error</b>: no value passed to macro <b>HRF</b>." unless defined $byte_input;
        $digits = 0 unless defined $digits;     # digits after decimal - default: 0
        
        if ($options > 2) {
	  return "<b>Error</b>: more than two parameters passed to macro <b>HRF</b>.";
        } else {

	  # transform into human friendly format
          $output = _hrf($byte_input,$digits);

	  return $output;
	}
}


=head1 AUTHOR

Klaus Hertle <online@hertle.net>

=cut


1;
