#
# Blake McNulty 02/02/2005
# Subroutine to reformat stored procedure paramater input fields into a
# fixed length field.  All spaces after initial space are replaced by
# an "_".  This is necessary for use in Oracle Stored Procedures with 
# compound "like" comparisons within the "where" clause.
#

sub sp_fmt {

  $fval  = @_[0];
  $fsize = @_[1];
  $fsp1;
  $fsp2;
  $idx;
  $fout;  
  $ffmt;  
  $fmax = 50;

# print "<pre>IN:",$fval,":";

  $fval = substr($fval.$SP,0,$fsize);
  $fout = $fval;

  $ftmp = $fval;
  $ftmp =~ s/ //gi;
#print "<pre>HELLO:",$ftmp,":",$fval,":";
  if ($ftmp eq '%' && substr($fval,0,1) eq '%') { 
      $ftmp = substr($fval.$SP,0,$fsize);
      $ftmp =~ s/%/_/gi; 
      $ftmp =~ s/ /_/gi; 
      $fout = $ftmp;
      goto NEXT1;
     }

  $fsp1  = index($fval,' ');
  $fsp2  = index($fval,'%');

# print "<pre>SP:",$fsp1,":";
# print "<pre>SP:",$fsp2,":";

  if ($fsp1 ne -1) { 
      if ($fsp2 gt $fsp1) { $fsp1 = $fsp2; }

      $fsp1++;
      $fout = substr($fval,0,$fsp1);

      if ($fsp1 eq $fsize) {   # First Space is Last Position of Field
          $fsp1--;
          $fout = substr($fval,0,$fsp1);
          $fout = $fout."%";
         goto NEXT1;
         }

#print "<pre>IN:",$fval,":";
      while ($fsp1 ne $fsize) {
#print "<PRE> $fsp1-X:",substr($fval,$fsp1,1),":X";
         if (substr($fval,$fsp1,1) eq ' ') {
            $fout = $fout."%";    
            }
         else {
            $fout = $fout.substr($fval,$fsp1,1);
            }
         $fsp1++;
         $idx++;
         if ($idx eq $fmax) { goto NEXT1; }   # Limit Maximum Size of Input
        }
     }

NEXT1:

  return($fout);
 
} 

# Required or will cause errors (dont know why)
1;
