package n8ur;

# John R. Ackermann N8UR   (jra@febo.com)
# Sun Jan  30 09:45:03 2005
#
# n8ur - my useful functions
# version 0.3
#
# Copyright 2005 by John R. Ackermann  N8UR (jra@febo.com)
# Licensed under the GPL version 2 or later; see the file COPYING
# included with this distribution.  I request, but do not require, that
# any modifications that correct bugs or errors, or increase the program's
# functionality, be sent via email to the author at the address above.

use strict;
use warnings;
use diagnostics;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION     = '0.30';
@ISA         = qw(Exporter);
@EXPORT      = ();
@EXPORT_OK   = qw(trim collapse squash lower_case upper_case
		 round parse_value is_number dec2bin
		 dec2bin_no_leading_zeroes bin2dec bin2hex);

sub trim {
	$_ = shift;
	s/\n/ /sg;		# convert newlines to spaces
	s/\r/ /sg;		# convert carriage returns to spaces
	s/\000/ /sg;		# convert nulls to spaces
	s/^\s+//sg;		# trim leading spaces
	s/\s+$//sg;		# trim trailing spaces
	return $_;
}

sub collapse {
	$_ = shift;
	s/\s+/ /sg;		# collapse multiple spaces to just one
	return $_;
}

sub squash {
	$_ = shift;
	s/\s//sg;		# remove all spaces
	return $_;
}

sub lower_case {
	$_ = shift;
	$_ =~ tr [A-Z] [a-z];
	return $_;
}

sub upper_case {
	$_ = shift;
	$_ =~ tr [a-z] [A-Z];
	return $_;
}

sub round {
	my($places) = shift;
        my($number) = shift;
	my($rounded);
	if ($number < 0) {
		$rounded = int(($number*10**$places) -.5
			* ($number <=> 0) )/10**$places;
	}
	else {
		$rounded = int(($number*10**$places) +.5
			* ($number <=> 0) )/10**$places;
	}

	return $rounded
        };

sub parse_value {
	# splits input into alpha prefix, numeric value, and alpha suffix
	# first split is when a digit, or "+", "-", or "." is encountered
	# second split is at first alpha after the number
	my($val) = shift;
	my $prefix = "";
	my $value = "";
	my $suffix = "";
	my $j = 0;
	my $end = 0;

	# get rid of any embedded spaces
	$val = squash($val);

	until ( (substr($val,$j,1) =~ /[\d+-\.]/) || ($j == length($val)) ) {
		$prefix .= substr($val,$j,1);
		$j++;
		$end = $j;
		}

	if ($end > 1) {
		$val = substr($val,$end);
		}

	$j = 0;
	$end = 0;
	until ( (substr($val,$j,1) =~ /[a-z]/i) || ($j == length($val)) ) {
		$j++;
		$end = $j;
		}

	$value = substr($val,0,$end);
	$suffix = substr($val,$end);

	return $prefix,$value,$suffix;
}

sub is_number {
	# returns true if input is a decimal number
    	$_ = shift;
	if ( /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/ ) {
		return 1;
		}
	else {
		return 0;
		}
}

sub dec2bin {
	my $str = unpack("B32", pack("N", shift));
	return $str;
}

sub dec2bin_no_leading_zeroes {
	my $str = unpack("B32", pack("N", shift));
	$str =~ s/^0+(?=\d)//;
	return $str;
}

sub bin2dec {
	return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

sub bin2hex {
	return hex(unpack("N", pack("B32", substr("0" x 32 . shift, -32))));
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

n8ur - Perl extension for blah blah blah

=head1 SYNOPSIS

	use n8ur;
	$string = trim($input)
	$string = collapse($input)
	$string = squash($input)
	$string = lower_case($input)
	$string = upper_case($input)
	$num = round($num_places,$input)
	($prefix,$value,$suffix) = parse_value($string)
	$boolean = is_number($input)
	$string = dec2bin($input)
	$string = dec2bin_no_leading_zeroes($input)
	$num = bin2dec($input)
	$num = bin2hex($input)
  

=head1 DESCRIPTION

Some useful functions I use in my programs.

=head2 EXPORT

None by default.

=head1 SEE ALSO

=head1 AUTHOR

John Ackermann   N8UR, jra@febo.com

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2005 by John Ackermann N8UR (jra@febo.com)

This program may be copied, modified, distributed and used for 
any legal purpose provided that (a) the copyright notice above as well
as these terms are retained on all copies; (b) any modifications that 
correct bugs or errors, or increase the program's functionality, are 
sent via email to the author at the address above; and (c) such 
modifications are made subject to these license terms.

=cut
