#!/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);
#$allStates = $stateFile.".crosstate"; #list of state floats recorded
$stateFile .= ".state";

#debug
#print "$stateFile \n";

# 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;
    for ($i=0; $i<$num_comps;$i++) {
      if ($final[$i] eq "u") {
        $MustInit[$i] = 1;
      }
    }
  } else {
    unlink $stateFile;
    for ($i = 0; $i < $num_comps; $i++) {
      $final[$i] = "u";  #initial values mark as unknown
      $MustInit[$i] = 1;  #tested in the compiled code
    }
  }  
  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;
