CAD-Firemen

 view release on metacpan or  search on metacpan

lib/CAD/Firemen/Common.pm  view on Meta::CPAN

}

 sub printBlock {
  my $text = untaint(shift);
  my $indent = untaint(shift);
  my $color = untaint(shift);

  if(!defined($text)){
    return;
  }
  if(!defined($indent)){
    $indent = 0;
  }
  if(!defined($color) or ($color eq "")){
    $color = "RESET";
  }
  # -2 is the linebreak
  my $terminalWidth = _terminalWidth() - 2;
  my $textWidth = $terminalWidth - $indent;

  # remove all linebreaks
  $text =~ s/[\n\r]/ /gs;

  my $start = 0;
  my $end = $textWidth;
  while($start < length($text)){
    my $line = strip(substr($text, $start, $end));
    my $max = $terminalWidth;
    if($textWidth > length($line)){
      $max = length($line) + $indent;
    }
    printColored(sprintf("%". $max ."s", $line), $color);
    print "\n";
    $start += $end;
    # $end is the number of returned characters
    if($start + $end > length($text)){
      $end = length($text) - $start;
    }
  }
}

sub buildStatistic {
  my $label = shift;
  my $value = shift;
  my $max = shift;
  my $result = "";

  if(!defined($label)){
    return $result;
  }
  if(!defined($value)){
    return $result;
  }
  if(!defined($max) || ($max == 0)){
    return $result;
  }

  my $terminalWidth = _terminalWidth() - 2;
  my $relValue = sprintf("%.0f", $value / $max * 100);
  $label .= " [";
  # - 6 is the percent itself (e.g.: " 69%, ")
  my $valueLen = $terminalWidth - length($label) - 1 - 6 - length($value);
  if($valueLen > 100){
    $valueLen = 100;
  }
  my $signs = floor($valueLen * $relValue / 100);
  my $space = $valueLen - $signs;
  $result = "[";
  for(my $i = 0; $i < $signs; $i++){
    $result .= "=";
  }
  $result .= " ". sprintf("%". $space ."s %3s%%, %s", ("", $relValue, $value));
  return $result;
}

sub getInstallationPath {
  my $result = "";
  my @tempPaths = ($ENV{'PATH'} =~ m/;([^;]+(?:proe|creo)[^;]+);/gi);
  my @paths = ();
  # add the paths from config
  my $config = loadSettings();
  if(defined($config)){
    if(exists($config->{"paths"})){
      foreach my $dir (@{$config->{"paths"}}){
        push(@paths, $dir);
      }
    }
  }

  # add path from ENV{PATH}, if not already done
  for(my $i = 0; $i < scalar(@tempPaths); $i++){
    if($tempPaths[$i] =~ m/([\W\w]+)(?:\\|\/)mech(?:\\|\/)bin/i){
      $tempPaths[$i] = $1;
    }
    elsif($tempPaths[$i] =~ m/([\W\w]+)(?:\\|\/)Parametric{0,1}(?:\\|\/)bin$/i){
      $tempPaths[$i] = $1;

      my $alreadyInserted = 0;
      foreach my $existing (@paths){
        if(index($existing, $tempPaths[$i]) != -1){
          $alreadyInserted = 1;
          last;
        }
      }
      if($alreadyInserted){
        $tempPaths[$i] = "";
      }
      else{
        # only search for common files, if path is not already added!
        print "Searching for Common Files directory of installation ". $tempPaths[$i] ."\n";
        my @commonFilesDirectories = File::Find::Rule->name("Common Files")->in($tempPaths[$i]);
        if(scalar(@commonFilesDirectories) == 1){
          testPassed("  Found Common Files for ". $tempPaths[$i]);
          $tempPaths[$i] = $commonFilesDirectories[0];
        }
        else{
          testFailed("  Found Common Files for ". $tempPaths[$i]);
        }
      }
    }
    elsif($tempPaths[$i] =~ m/([\W\w]+)(?:\\|\/)bin$/i){

lib/CAD/Firemen/Common.pm  view on Meta::CPAN

  return $terminalWidth;
}

1;

__END__

=pod

=head1 NAME

CAD::Firemen::Common - Shared functions used by other scripts from the Firemen module.

=head1 VERSION

version 0.7.2

=head1 METHODS

=head2 strip

Strips out whitespaces at the beginning and the end of the given string.
It also removes double whitespaces.

=head2 untaint

to untaint the string, it strip outs any escape sequences (without \n), to make the string more secure (taint mode)

=head2 print2ColsRightAligned

Prints the string within the first parameter on the far left of the screen.
The second paremeter is printed on the far right of the screen in the color
of optional third parameter. See Term::ANSIColor for the names of the colors.

=head2 testPassed

Prints the content of the first parameter on the far left screen side
and "PASSED" in green on the far right.

=head2 testFailed

Prints the content of the first parameter on the far right side
and "FAILED" in red on the far right.

=head2 maxLength

Returns the lenght of the longest string within the
given array as first parameter.

=head2 printColored

Prints the given text in the given color. The main reason to use this function is to
use Win32::Console within this module.

=head2 printBlock

Prints a text block with an specified indentation.

=head2 buildStatistic

Builds a bar of = to display a percentage value of the ratio between $value and $max.

=head2 getInstallationPath

Method parses $ENV{PATH} and tries to filter out all Firemen related paths.
Afterwards, if more than one is found, the user can select which one he wants
to use. This one is returned than.

The returned path DOES NOT ends with a slash!

=head2 getInstallationConfigCdb

Uses getInstallationConfigPath() to return the full path to the related config.cdb.
You may specify the installation path to get the related config.pro. If not given, it uses
getInstallationPath() to guess or ask one.

=head2 getInstallationConfigPro

Uses getInstallationConfigPath() to return the full path to the related config.pro.
You may specify the installation path to get the related config.pro. If not given, it uses
getInstallationPath() to guess or ask one.

=head2 sharedDir

Returns the path to the shared directory where all modules and scripts of this
distribution places their files.

If it does not exists, it creates it.

=head2 installationId

Compuates an installation identifier out of the creo installation path.
This ID is used e. g. to create the database name.

=head2 dbConnect

Creates a connection to the database and returns the reference to the DBI object
or 0 if an error occurs. If the database does not exists an empty database file
is created.

If you want to insert data, make sure that you use the commit function, since
AutoCommit is disabled.

The database layout is described in fm_create_help.

=head2 loadSettings

Loads the settings from config file and returns a reference to the hash.

Most possible settings are explained at CAD::Firemen (Use perldoc CAD::Firemen).

=head2 saveSettings

Saves the Hash, which reference is given into the config file.

=head2 cleanSvn

Method to delete all .svn directories borrowed from http://snipplr.com/view/27050/ with small change (introduced rmtree)

=head2 _settingsFile
FOR INTERNAL USE ONLY!



( run in 1.516 second using v1.01-cache-2.11-cpan-5b529ec07f3 )