| 1 | package EC::Utilities; | ||
| 2 | |||
| 3 | my $VERSION = 0.01; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | use Exporter (); | ||
| 7 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||
| 8 | |||
| 9 | @ISA = qw (Exporter); | ||
| 10 | |||
| 11 | @EXPORT = qw (inc_path expand_path verify_path content | ||
| 12 | content_as_str strexist strfill); | ||
| 13 | |||
| 14 | sub tkversion { | ||
| 15 | require Tk; | ||
| 16 | return ${Tk::VERSION}; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub inc_path { | ||
| 20 | my ($filename) = $_[0]; | ||
| 21 | foreach (@INC) { | ||
| 22 | return "$_/$filename" if -f "$_/$filename"; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | # prepend $HOME directory to path name in place of ~ | ||
| 27 | sub expand_path { | ||
| 28 | my ($s) = $_[0]; | ||
| 29 | if( $s =~ /^\~/ ) { | ||
| 30 | $s =~ s/~//; | ||
| 31 | $s = $ENV{'HOME'}."/$s"; | ||
| 32 | } | ||
| 33 | $s =~ s/\/\//\//g; | ||
| 34 | return $s; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub verify_path { | ||
| 38 | my ($path) = @_; | ||
| 39 | if ((not -d $path) and (not -f $path)) { | ||
| 40 | carp "Verify_path(): Path $path not found: $!\n"; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | sub content { | ||
| 45 | my ($file) = @_; | ||
| 46 | my ($l, @contents); | ||
| 47 | eval { | ||
| 48 | open FILE, $file or | ||
| 49 | carp "Couldn't open $file: ".$!."\n"; | ||
| 50 | while (defined ($l=<FILE>)) { | ||
| 51 | chomp $l; | ||
| 52 | push @contents, ($l); | ||
| 53 | } | ||
| 54 | close FILE; | ||
| 55 | }; | ||
| 56 | return @contents; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub content_as_str { return join "\n", &content (@_) } | ||
| 60 | |||
| 61 | sub strexist { return defined $_[0] and length $_[0] } | ||
| 62 | |||
| 63 | sub strfill { | ||
| 64 | my ($s, $length) = @_; | ||
| 65 | |||
| 66 | if ( length ($s) > $length) { | ||
| 67 | $s = substr $s, 0, $length; | ||
| 68 | } elsif ( length ($s) < $length ) { | ||
| 69 | $s .= ' ' x ($length - length ($s)); | ||
| 70 | } | ||
| 71 | return $s; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |