##-------------------------------------------------------------------------
## 
## gfimisc.pm -- 
## 
## Author          : Pierre Jaccard
## Created On      : 1999/08/03 17:02:52
## Last Modified By: Pierre Jaccard
## Last Modified On: 1999/08/10 16:29:14
## Update Count    : 70
## Directory       : /home/pego/pcd1/codas3c/gfi/lib/perl/
## Version         : 0.0
## Status          : Unknown
## --------------------------------------------------------------------- ++
## DESCRIPTION: 
## 
##    See separate doc.
## 
## --------------------------------------------------------------------- ++
## REVISIONS: 
## --------------------------------------------------------------------- ++
## CHANGES: 
##-------------------------------------------------------------------------

package gfimisc;

#------------------------------------------------------------------
# Modules:
#------------------------------------------------------------------

use strict;
use Carp;

#------------------------------------------------------------------
# Declaration:
#------------------------------------------------------------------

BEGIN {
  
  use Exporter ();
  use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);  
  $VERSION = 1.0;
  @ISA = qw(Exporter);

  #--- Exported stuff:
  @EXPORT = qw(&gfimisc_chk_line &gfimisc_split &gfimisc_get_data 
               &gfimisc_lst2arr);

  %EXPORT_TAGS = ();

  #--- Exported on demand:
  @EXPORT_OK = ();

}

use vars @EXPORT_OK;

#------------------------------------------------------------------
# Global variables:
#------------------------------------------------------------------

#------------------------------------------------------------------
# File private variables:
#------------------------------------------------------------------
    
#------------------------------------------------------------------
# gfimisc - Check input data line
#
# $data = gfimisc_chk_line($data)
#
# Check if input line is empty, has only white spaces or is a comment
# line. If this is so, return the empty string, else, return the input line
# with leading and trailing white spaces removed.
# ------------------------------------------------------------------

sub gfimisc_chk_line {

  my($data) = @_ ;

  chomp($data);
  $data =~ s/^\s*(.*?)\s*$/$1/g;
  return("") if($data eq "");
  return("") if($data =~ /^\%/);

  return($data);
};

#------------------------------------------------------------------
# gfimisc_split - Split input data line into an array
#
# @data = gfimisc_split($data, \@cols)
#
# Split input data line into an array, each element being separated by
# white spaces. Optionally, a reference to an array of column numbers to be
# kept can be given. Column numbering start from zero. Returns the array
# with data, or the empty array if the line is empty. Data in the output
# array are ordered in the same order as they are specified in the list of
# columns. 
# ------------------------------------------------------------------

sub gfimisc_split {

  my(@cols, @tmp, @data, $n, $c);

  my($data, $cols) = @_ ;

  #--- Process input columns:
  @cols = ();
  if($cols){
    @cols = @{$cols};
  }
  
  #--- Split the line:
  $n = @data = split(/\s+/, $data);

  return(@data) unless(@cols);

  #--- Filter columns:
  if(@cols){
    @tmp = ();
    foreach $c (@cols){
      push(@tmp, $c) if($c < $n);
    }
    @cols = @tmp;
  }

  return(@data[@cols]);
};

#------------------------------------------------------------------
# gfimisc_get_data - Get the next line with input data
#
# @data = gfimisc_get_data($data, \@cols)
#
# Check the input line if it is empty or a comment line and split it into
# columns. A list of column numbers can be given to filter out some of them
# or/and reorder them. If not specified, all columns are output in the
# order they appear in the input line.
#------------------------------------------------------------------

sub gfimisc_get_data {

  my(@data);

  my($data, $cols) = @_ ;

  #--- Check the input line:
  $data = gfimisc_chk_line($data);
  return(()) if($data eq "");
  
  #--- Split the line:
  @data = gfimisc_split($data, $cols);
  
  return(@data);
};

#------------------------------------------------------------------
# gfimisc_lst2arr - Process column option string
#
# @cols = gfimisc_lst2arr($cols)
#
# Splits the given column option string and returns the array with each
# specified column numbers. Numbers are decremented by one to be compatible
# with other finctions and Perl indexes.
#------------------------------------------------------------------

sub gfimisc_lst2arr {

  my(@cols, @fields, $f, $start, $step, $stop, $i);

  my($cols) = @_ ;

  @cols = ();

  @fields = split(/\,/, $cols);

  foreach $f (@fields){
    
    next unless($f);

    $start = $stop = $step = 0;
    ($start, $step, $stop) = split(/\:/, $f) if($f =~ /\:/);
    ($start, $stop)        = split(/\-/, $f) if($f =~ /\-/);
    $start = $f unless($start);
    $stop  = $f unless($stop);
    $step  = 1  unless($step);

    if($step > 0){
      for($i=$start; $i<=$stop; $i += $step){
        push(@cols, $i-1);
      }
    }
    if($step < 0){
      for($i=$start; $i>=$stop; $i += $step){
        push(@cols, $i-1);
      }
    }
    
  }

  return(@cols);
}

1;

  
