package SFNFile;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(read_sfnfile);

sub read_sfnfile {
	my $filename = shift();
	my %file;
	my @figures;
	my %figure;
	open FILE, $filename or die $!;
	my @s = <FILE>;
#	while (<FILE>) {
	foreach(@s) {
#		print $s;
		chomp;
		next if /^\#/;										# Ignore comments
		if (/^(FILE.*?|DEFSTRING):(.+)/) {
			$file{lc($1)} = $2;
			next;
		}													# FileHeader...
		unless ($_ =~ /\w/) {								# blank line
			if (keys %figure) {								# end of a record?
				push @figures, {%figure};
				%figure=();
			}
			next;
		}
		if (/^([A-Z]+\d*):(.+)/) {$figure{lc($1)} = $2;}	# Header...
		else {push @{$figure{steps}}, $_;}					# or step?
	}
	close FILE;
	push @figures, {%figure} if (%figure);					# remaining record
	$file{figures} = \@figures;
	%file;
}

1;

