summaryrefslogtreecommitdiff
path: root/hw/sortsheets.pl
blob: e16f05ffa3c804e544e0ec0d172182bc2f81356c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl
$MAGIC = "Xsheetnumber";

sub usage
{
	print STDERR "usage: $0 [-q] [-xy|-yx] [top-sheet.sch]\n";
	exit(1);
}


while ($ARGV[0] =~ /^-/) {
	if ($ARGV[0] eq "-q") {
		$query = 1;
	} elsif ($ARGV[0] eq "-xy") {
		$xy = 1;
	} elsif ($ARGV[0] eq "-yx") {
		$yx = 1;
	} else {
		&usage;
	}
	shift @ARGV;
}

$where = "head";
$sheet = 1;
while (<>) {
	if (/^\$Sheet/) {
		$where = "sheet";
		$tmp = $_;
		$sheet++;
		next;
	}
	if ($where eq "head") {
		$head .= $_;
		next;
	}
	if ($where ne "sheet") {
		$tail .= $_;
		next;
	}
	if (/^\$EndSheet/) {
		$where = "outside";
		$tmp .= $_;

		if ($xy || $yx) {
			push(@x, $x);
			push(@y, $y);
			push(@unsorted, $tmp);
			next;
		}

		die "file name not found" unless $tmp =~ /^F1\s+"([^"]+)"/ms;
		$file = $1;
		open(FILE, $file) || die "$file: $!";

		undef $explicit;
		while (<FILE>) {
			next unless /^Comment4\s+"$MAGIC=([1-9]\d*)"/;
			$explicit = $1;
			last;
		}
		close FILE;

		if (!defined $explicit) {
			warn "$file: sheet $sheet has no explicit tag\n";
			$explicit = $sheet;
		}
		die "$file: sheet number must be 2 or higher\n"
		    if $explicit < 2;
		die "$file: collision on number $explicit\n"
		    if defined $sorted[$explicit];
		$sorted[$explicit] = $tmp;
		$was[$explicit] = $sheet;
		next;
	}
	if (/^S\s+(\d+)\s+(\d+)/) {
		$x = $1;
		$y = $2;
	}
	$tmp .= $_;
}


sub map
{
	local ($a, $b) = @_;

	return $x[$a] == $x[$b] ? $y[$a] <=> $y[$b] : $x[$a] <=> $x[$b]
	    if $xy;
	return $y[$a] == $y[$b] ? $x[$a] <=> $x[$b] : $y[$a] <=> $y[$b];
}


if ($xy || $yx) {
	my @i = sort({ &map($a, $b) } ( 0 .. $#unsorted ));

	for (0..$#i) {
#print STDERR "$_ <- $i[$_]  $x[$i[$_]] $y[$i[$_]]\n";
		$sorted[$_ + 2] = $unsorted[$i[$_]];
		$was[$_ + 2] = $i[$_] + 2;
	}
}

print $head || die "write: $!" unless $query;
for ($i = 2; $i <= $#sorted; $i++) {
	if (!defined $sorted[$i]) {
		warn "sheet #$i is missing in sequence\n";
		next;
	}
	if ($was[$i] != $i) {
		exit(1) if $query;
		warn "sheet $was[$i] is now $i\n" unless $was[$i] == $i;
	}
	print $sorted[$i] || die "write: $!" unless $query;
}
exit(0) if $query;
print $tail || die "write: $!";
close(STDOUT) || die "write: $!";