package HeaderDoc::Utilities;
use strict;
use vars qw(@ISA @EXPORT $VERSION);
use Carp;
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(findRelativePath safeName getAPINameAndDisc convertCharsForFileMaker
printArray printHash updateHashFromConfigFiles getHashFromConfigFile);
$VERSION = 1.00;
my $pathSeparator;
my $isMacOS;
BEGIN {
if ($^O =~ /MacOS/i) {
$pathSeparator = ":";
$isMacOS = 1;
} else {
$pathSeparator = "/";
$isMacOS = 0;
}
}
sub findRelativePath {
my ($fromMe, $toMe) = @_;
my @fromMeParts = split (/$pathSeparator/, $fromMe);
my @toMeParts = split (/$pathSeparator/, $toMe);
my $localDebug = 0;
print "fromMe --> |$fromMe|\n" if $localDebug;
print "toMe --> |$toMe|\n" if $localDebug;
my $i = 0;
while (($fromMeParts[$i] eq $toMeParts[$i]) && ($i < $ print "$i\n" if $localDebug;
$i++;
}
@fromMeParts = splice (@fromMeParts, $i);
@toMeParts = splice (@toMeParts, $i);
my $numFromMeParts = @fromMeParts; my $relPart = "../" x ($numFromMeParts - 1);
my $relPath = $relPart.join("/", @toMeParts);
return $relPath;
}
sub safeName {
my ($filename) = @_;
my $returnedName="";
my $safeLimit;
my $macFileLengthLimit = 31;
my $longestExtension = 5;
my $partLength;
my $nameLength;
$safeLimit = ($macFileLengthLimit - $longestExtension);
$partLength = int (($safeLimit/2)-1);
$filename =~ tr/a-zA-Z0-9./_/cs;
$nameLength = length($filename);
if ($nameLength > $safeLimit) {
my $safeName = $filename;
$safeName =~ s/^(.{$partLength}).*(.{$partLength})$/$1_$2/;
$returnedName = $safeName;
} else {
$returnedName = $filename;
}
return $returnedName;
}
sub getAPINameAndDisc {
my $line = shift;
my ($name, $disc, $operator);
$line =~ s/^\s+//;
($name, $disc) = split (/\s/, $line, 2);
if ($name =~ /operator/) { ($operator, $name, $disc) = split (/\s/, $line, 3);
$name = $operator." ".$name;
}
return ($name, $disc);
}
sub convertCharsForFileMaker {
my $line = shift;
$line =~ s/\t/chr(198)/eg;
$line =~ s/\n/chr(194)/eg;
return $line;
}
sub updateHashFromConfigFiles {
my $configHashRef = shift;
my $fileArrayRef = shift;
foreach my $file (@{$fileArrayRef}) {
my %hash = &getHashFromConfigFile($file);
%{$configHashRef} = (%{$configHashRef}, %hash); }
return %{$configHashRef};
}
sub getHashFromConfigFile {
my $configFile = shift;
my %hash;
my $localDebug = 0;
my @lines;
if ((-e $configFile) && (-f $configFile)) {
open(INFILE, "<$configFile") || die "Can't open $configFile.\n";
@lines = <INFILE>;
close INFILE;
} else {
print "No configuration file found at $configFile\n" if ($localDebug);
return;
}
foreach my $line (@lines) {
if ($line =~/^ chomp $line;
my ($key, $value) = split (/\s*=>\s*/, $line);
if ((defined($key)) && (length($key))){
$hash{$key} = $value;
}
}
undef @lines;
return %hash;
}
sub printArray {
my (@theArray) = @_;
my $i= 0;
my $length = @theArray;
while ($i < $length) {
print ("\t$theArray[$i++]\n");
}
}
sub printHash {
my (%theHash) = @_;
print ("Printing contents of hash:\n");
foreach my $keyword (keys(%theHash)) {
print ("$keyword => $theHash{$keyword}\n");
}
print("-----------------------------------\n\n");
}
1;
__END__