#!/usr/bin/perl -w -X

#Keeps a state file whose lines are tuples of all states 
# (just one value if a real component).
use component;

#$stateFile = &component::baseName($0);  #NG for all platforms
$stateFile = "SystemCode.state";

#print "$stateFile \n";  #debug

# Arguments: number of components
# Returns: list with floating point states for each component in the order previously written
#  Stateless components are not included
sub loadStates {
  my ($num_comps) = @_; 
  my ($i);
  my @final = (); #one for each possible component
  
  if (open (STATE, "<$stateFile")) { # if file exists, not first time
    chop($und = <STATE>); #previous value
    @final = split(" ",$und);
    close STATE;
  } else {
    for ($i = 0; $i < $num_comps; $i++) {
      $comp[$i]->run($x);
      $final[$i] = $comp[$i]->getState;
    }
  }  
  return @final;
}

# Arguments: states for all components in the system in the order they are to be written
# Returns: nothing
sub saveStates {
  my @states = @_;
  my ($i);
  open STATE, ">$stateFile";
  for ($i = 0; $i < scalar(@states); $i++) {
    chomp($states[$i]);
    print STATE "$states[$i] ";
  }
  print STATE "\n";
  close STATE;
  return;
}
return 1;
