~ubuntu-branches/ubuntu/trusty/geda-utils/trusty

« back to all changes in this revision

Viewing changes to scripts/refdes_renum

  • Committer: Bazaar Package Importer
  • Author(s): Hamish Moffatt
  • Date: 2005-03-15 23:04:53 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050315230453-x3x6qtw9qv17zbnf
Tags: 20050313-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
#
 
3
# $Id: refdes_renum,v 1.1 2003/02/21 03:21:12 ahvezda Exp $
 
4
#
 
5
# Copyright (C) 2003 Dan McMahill
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 
 
21
 
 
22
# This script is used to renumber devices in a gschem (part of gEDA)
 
23
# schematic.  This program works on single as well as multi-sheet 
 
24
# schematics.
 
25
#
 
26
# Usage:
 
27
#    renum.pl file1.sch [file2.sch [file3.sch ...]]
 
28
#
 
29
 
 
30
# for parsing input options
 
31
use Getopt::Long;
 
32
 
 
33
# for ceil function
 
34
use POSIX;
 
35
 
 
36
# don't allow -he to be interpreted as --help
 
37
$Getopt::Long::autoabbrev=0;
 
38
 
 
39
&GetOptions(("help" => \&usage, 
 
40
             "nocopy" => \$nocopy,
 
41
             "pgskip" => \$pkgskip,
 
42
             "verbose" => \$verbose,
 
43
             "version" => \&version
 
44
             ));
 
45
 
 
46
usage() if $Getopt::Long::error;
 
47
usage() unless @ARGV;
 
48
 
 
49
# make sure the input schematic exists and we can read it
 
50
$i=0;
 
51
while(@ARGV) {
 
52
  $fname[$i]=shift(@ARGV);
 
53
  die "Schematic file $fname[$i] does not exist or can not be read"
 
54
    unless -r $fname[$i];
 
55
  $i++;
 
56
}
 
57
 
 
58
$filecnt = $i;
 
59
 
 
60
for($i=0; $i < $filecnt; $i++) {
 
61
  print "Processing input file #",$i+1,": $fname[$i]\n";
 
62
  open(NETLIST,"$fname[$i]") or die "Can't open $fname[$i]: $!\n";
 
63
 
 
64
  # open output netlist
 
65
  $outfname="$fname[$i].renum";
 
66
  open(OUTNET,">$outfname") or die "Can't open $outfname: $!\n";
 
67
 
 
68
  while($line = <NETLIST>) {
 
69
    unless( $line =~ /^refdes/) {
 
70
      print OUTNET $line;
 
71
      next;
 
72
    }
 
73
    # eat the newline
 
74
    $line =~ s/\n//;
 
75
 
 
76
    # for lines like "refdes=CR37" pick out the "CR" part.
 
77
    # There's probably a more concise way, but the perl book
 
78
    # is at work right now.
 
79
    $pre = $line;
 
80
    $pre =~ s/^refdes=//;
 
81
    $pre =~ s/[0-9\?]*$//;
 
82
    print "Refdes line \"$line\" has pre=\"$pre\"\n" if($verbose);
 
83
 
 
84
    # if we're skipping numbers, then start at 100 for page 1
 
85
    # and we'll jump to 200 for page 2, etc.
 
86
    if( ! $devcnt{$pre} ) { $devcnt{$pre} = $pkgskip ? ($i+1)*100 : 0; }
 
87
    $devcnt{$pre}++;
 
88
    print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
 
89
    print OUTNET "refdes=$pre$devcnt{$pre}\n";
 
90
  }
 
91
  close(NETLIST);
 
92
  close(OUTNET);
 
93
 
 
94
  # round each element up to the next 100 to help identify what
 
95
  # schematic page a component is on by the refdes.
 
96
  if( $pkgskip ) {
 
97
      foreach $dev (keys %devcnt) {
 
98
          $devcnt{$dev} = 100*POSIX::ceil($devcnt{$dev}/100);
 
99
          print "Incremented \"$dev\" counter to $devcnt{$dev}\n" if($verbose);
 
100
      }
 
101
  }
 
102
 
 
103
  # make this an option to not overwrite the original
 
104
  if( $nocopy ) {
 
105
      print "Leaving page #",$i+1," output in $outfname\n";
 
106
      }
 
107
  else {
 
108
      system("mv $outfname $fname[$i]");
 
109
  }
 
110
}
 
111
 
 
112
exit;
 
113
 
 
114
 
 
115
#######################################################################
 
116
#
 
117
# Subroutines
 
118
#
 
119
#######################################################################
 
120
 
 
121
#---------------------------------
 
122
# usage()
 
123
#
 
124
# prints program usage
 
125
#---------------------------------
 
126
 
 
127
sub usage {
 
128
  my $pname = $0;
 
129
  $pname =~ s/.*\///g;
 
130
 
 
131
  print "Usage:\n\n";
 
132
  print "\t$pname [--nocopy] [--pgskip] file1 [file2 [file3 ... ] ]\n";
 
133
  print "\t$pname --help\n";
 
134
  print "\t$pname --version\n";
 
135
  print "\n";
 
136
  print "$pname reads a gschem schematic file or files and renumbers all reference\n";
 
137
  print "designators.  The reference designators are numbered starting with 1 and the\n";
 
138
  print "old schematic file is replaced by the modified schematic file.\n";
 
139
  print "\n";
 
140
  print "$pname accepts the following options:\n";
 
141
  print "\n";
 
142
  print "    --help      Displays this help message.\n";
 
143
  print "\n";
 
144
  print "    --nocopy    If given, this flag leaves the modified files in new files\n";
 
145
  print "                whose names are generated by appending a \".renum\" to the\n";
 
146
  print "                original file names.  The default is to overwrite the original.\n";
 
147
  print "\n";
 
148
  print "    --pgskip    When this flag is used, components on the first schematic sheet\n";
 
149
  print "                are numbered starting with 101.  On the second sheet, they start\n";
 
150
  print "                with 201, etc.  \n";
 
151
  print "\n";
 
152
  print "    --verbose   Enables verbose output.\n";
 
153
  print "\n";
 
154
  print "    --version   Shows the version of this program.\n";
 
155
  print "\n\n";
 
156
  print "$pname was written by Dan McMahill <dmcmahill\@netbsd.org>\n";
 
157
  print "\n\n";
 
158
  exit;
 
159
}
 
160
 
 
161
#---------------------------------
 
162
# version()
 
163
#
 
164
# prints program version
 
165
#---------------------------------
 
166
 
 
167
sub version {
 
168
  open(PROG,"$0") or die "Could not open \"$0\" to find version\n\n";
 
169
  my $pname = $0;
 
170
  $pname =~ s/.*\///g;
 
171
 
 
172
  while($line = <PROG>) {
 
173
    if( $line =~ /^#\s*\$Id.*\$/) {
 
174
      @words = split ' ',,$line;
 
175
      $version = $words[3];
 
176
      $date = $words[4];
 
177
      print "$pname ($0):  Version $version, $date\n";
 
178
      exit;
 
179
    }
 
180
  }
 
181
  print "Could not determine version of \"$0\"\n\n";
 
182
  exit;
 
183
}
 
184
 
 
185
# ----------------------------------------------
 
186
#
 
187
# Change Log
 
188
#
 
189
# $Log: refdes_renum,v $
 
190
# Revision 1.1  2003/02/21 03:21:12  ahvezda
 
191
# Added scripts/refdes_renum written by Dan McMahill
 
192
#
 
193
#
 
194
#