package Net;

##############################
# .net files

our $net_sep = "/";
our $ref_sep = "";

sub net_parse(@) {
    my @line = @_;

    my %pin2net;
    my %net2pin;
    my %pfx;

    my $line = "";
    for (@line) {
	chomp;
	if (m|^(.*)\\$|) {
	    $line .= $1;
	    next;
	}
	$line .= $_;
	#print $line, "\n";

	my @fld = split(" ", $line);
	my $net = shift @fld;

	#print "Net: $net\n";
	#print "aa: ", join(" ", @fld), "\n";

	if (m|^(.*$net_sep)[^$net_sep]*$|) {
	    my $pfx = $1;
	    $pfx{$pfx} = 1;
	} else {
	    $pfx{''} = 1;
	}

	$net2pin{$net} = [ @fld ];
	for my $pin (@fld) {
	    if (!defined($pin2net{$pin})) {
		$pin2net{$pin} = [];
	    }
	    push @{$pin2net{$pin}}, $net;
	}
	$line = "";
    }

    (\%pin2net, \%net2pin, \%pfx);
}

sub sort_pin($$) {
    my $a = shift;
    my $b = shift;

    $a =~ m/^(.*)-(\d+)$/;
    my $ard = $1;
    my $apn = $2;
    $b =~ m/^(.*)-(\d+)$/;
    my $brd = $1;
    my $bpn = $2;

    if ($ard eq $brd) {
	return $apn <=> $bpn;
    }
    $ard cmp $brd
}

1;
