[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

gEDA-cvs: CVS update: refdes_renum



  User: sdb     
  Date: 07/04/14 20:42:23

  Modified:    .        refdes_renum
  Log:
  Added --gentle flag to refdes_renum, which won't renumber refdeses
  
  which already have a number.
  
  
  
  
  Revision  Changes    Path
  1.6       +290 -78   eda/geda/gaf/utils/scripts/refdes_renum
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: refdes_renum
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/utils/scripts/refdes_renum,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- refdes_renum	14 Apr 2007 03:16:01 -0000	1.5
  +++ refdes_renum	15 Apr 2007 00:42:22 -0000	1.6
  @@ -1,6 +1,6 @@
   #!/usr/bin/perl -w
   #
  -# $Id: refdes_renum,v 1.5 2007/04/14 03:16:01 danmc Exp $
  +# $Id: refdes_renum,v 1.6 2007/04/15 00:42:22 sdb Exp $
   #
   # Copyright (C) 2003 Dan McMahill
   #
  @@ -37,90 +37,241 @@
   $Getopt::Long::autoabbrev=0;
   
   my $clear; # reset all refdes
  -
   &GetOptions(("help" => \&usage, 
   	     "nocopy" => \$nocopy,
   	     "pgskip:100" => \$pgskip,
   	     "verbose" => \$verbose,
   	     "version" => \&version,
                "clear"   => \$clear,
  +	     "gentle" => \$gentle,
   	     ));
   
   usage() if $Getopt::Long::error;
   
  -# set the default increment for sheets
  -unless( $pgskip ) { $pgskip = 0; }
   
  -usage() unless @ARGV;
  +# By default pgskip is empty.  But if the user gives
  +# the --pgskip option with no value, then assign pgskip
  +# to 100.  
  +if (defined($pgskip) && ($pgskip == 0)) {  
  +    $pgskip = 100; 
  +}
   
   
  +# Print usage string in event user didn't call out any args
  +usage() unless @ARGV;
   
  -# make sure the input schematic exists and we can read it
  +# Make sure the input schematic exists and we can read it.
  +# Also count number of files to open.
   $i=0;
   while(@ARGV) {
     $fname[$i]=shift(@ARGV);
  -  die "Schematic file $fname[$i] does not exist or can not be read"
  +  die_sdb(1, "Schematic file $fname[$i] does not exist or can not be read")
       unless -r $fname[$i];
     $i++;
   }
  +$filecnt = $i;  # number of files to open
  +if($verbose) {print "Found $filecnt files to process\n";}
  +
  +# To refdes an entire design, we normally just loop through all files
  +# and process each one in order.  However if --gentle option is set, 
  +# we need to make two passes through all files.  First to create a hash
  +# of the highest refdeses used, and second to update unnumbered 
  +# refdeses.  The hash is used to know where to start the refdes numbering.
  +#
  +# It gets more complicated.  If --pgskip is not set, then we only need
  +# to find the highest refdes overall.  But if --pgskip is set, then 
  +# we need to know the highest refdes on each *page*.  This is accomplished
  +# on the first scan by creating the variable $highdev{$i, $pre} when --pgskip
  +# is defined, but using the variable $highdev{$pre} when it is not.
  +#
  +if (defined($gentle)) {  # First scan through design to build hash
  +    for($i=0; $i < $filecnt; $i++) {   # $i is the file index.
  +	print "Scanning input file #".($i+1).": $fname[$i]\n";
  +	open(NETLIST,"$fname[$i]") or die_sdb(1, "Can't open $fname[$i]: $!\n");
  +
  +	while($line = <NETLIST>) {  # Iterate over lines found in .sch file.
  +	    unless( $line =~ /^refdes/) {
  +		next;
  +	    }
  +	    # Found refdes line.  Eat the newline.
  +	    $line =~ s/\n//;
   
  -$filecnt = $i;
  +	    # Now extract component prefix and suffix.
  +	    if($verbose) {print ("Matching $line ....\n");}
  +	    $line =~ /refdes=([a-zA-Z_]+)(\d+|\??)/i;
  +	    my $pre = $1;
  +	    my $suf = $2 ? $2 : "?";
  +	    if($verbose) {print "Refdes line \"$line\" has pre=$pre suf=$suf\n";}
  +
  +	    # Now put highest refdes found into $highdev hash.
  +	    if (defined($pgskip)) {
  +		if ($suf eq "?") {
  +		    # Part has no pre-existing refdes.
  +		    # Skip to next part.
  +		    next;
  +		} elsif (!defined($highdev{$i, $pre})) { 
  +                    # refdes is not cached, but refdes exists on part.  
  +		    # Create new cache entry
  +		    if ($suf < $pgskip) {
  +			# if pgskip=100, on pg 1, and suf = 23, set highdev=123.
  +			$highdev{$i, $pre} = ($i+1)*$pgskip + $suf;
  +		    } elsif ( ( ($i+1)*$pgskip < $suf) && 
  +			      ( ($i+2)*$pgskip > $suf) ) { 
  +			# if pgskip=100, we're on pg 2, and suf = 204, set highdev=204.
  +			$highdev{$i, $pre} = $suf;
  +		    } else {
  +			# I don't know what to do!
  +			die_sdb(3, "Encountered refdes incompatable with --pgskip setting.  Refdes=$pre$suf.\n");
  +		    }
   
  -for($i=0; $i < $filecnt; $i++) {
  -  print "Processing input file #",$i+1,": $fname[$i]\n";
  -  open(NETLIST,"$fname[$i]") or die "Can't open $fname[$i]: $!\n";
  +		} elsif ($highdev{$i, $pre} < $suf) {
  +		    # part has refdes and refdes is higher than cached
  +                    # value.  Store this new value as highest value.
  +		    $highdev{$i, $pre} = $suf;
  +		} elsif ($highdev{$i, $pre} >= $suf) {
  +		    # part has refdes, but refdes is lower than cached
  +                    # high value.  Leave it alone.
  +		    next;
  +		} else {
  +		    # Should never get here
  +		    die_sdb(4, "Invalid refdes with --pgskip set!  Exiting....\n");
  +		}
  +
  +		if($verbose) {
  +		    print "On page ".($i+1).", caching highest refdes $pre$highdev{$i, $pre}\n";
  +		}
  +	    } else {
  +		if ($suf eq "?") {
  +		    # Part has no pre-existing refdes.
  +		    next;
  +		} elsif (!defined($highdev{$pre}) ) { 
  +                    # refdes is not cached, and refdes exists on part.  Create new
  +                    # cache entry
  +		    $highdev{$pre} = $suf;
  +		} elsif ($highdev{$pre} < $suf) {
  +		    # part has refdes and refdes is higher than cached
  +                    # value.  Store this new value as highest value.
  +		    $highdev{$pre} = $suf;
  +		} elsif ($highdev{$pre} >= $suf) {
  +		    # part has refdes, but refdes is lower than cached
  +                    # high value.  Leave it alone.
  +		    next;
  +		} else {
  +		    # Should never get here
  +		    die_sdb(4, "Invalid refdes!  Exiting....\n");
  +		}
  +		if($verbose) {
  +		    print "Caching highest refdes $pre$highdev{$pre}\n";
  +		}
  +	    }
  +	    
  +	}  # while($line = <NETLIST>)
  +	close(NETLIST);
  +    }  # for($i=0; $i < $filecnt; $i++)
  +} # if (defined($gentle))
  +
  +
  +# OK, now we can read through the netlist file again, assign refdeses,
  +# and write the output file.
  +for($i=0; $i < $filecnt; $i++) {   # $i is the file index.
  +    print "Now processing input file #".($i+1).": $fname[$i]\n";
  +    open(NETLIST,"$fname[$i]") or die_sdb(1, "Can't open $fname[$i]: $!\n");
   
     # open output netlist
     $outfname="$fname[$i].renum";
  -  open(OUTNET,">$outfname") or die "Can't open $outfname: $!\n";
  +    open(OUTNET,">$outfname") or die_sdb(2, "Can't open $outfname: $!\n");
   
  +    # Iterate over lines found in .sch file.
     while($line = <NETLIST>) {
       unless( $line =~ /^refdes/) {
         print OUTNET $line;
         next;
       }
  -    # eat the newline
  +
  +	# Found refdes line.  Eat the newline.
       $line =~ s/\n//;
   
  -    # for lines like "refdes=CR37" pick out the "CR" part.
  -    # There's probably a more concise way, but the perl book
  -    # is at work right now.
  -    $pre = $line;
  -    $pre =~ s/^refdes=//;
  -    $pre =~ s/([0-9\?])*$//;
  -    my $suf = $1;
  -    print "Refdes line \"$line\" has pre=\"$pre\"m suf=$suf\n" if($verbose);
  +	# Now extract component prefix and suffix.
  +	if($verbose) {print ("Processing $line ....\n");}
  +	$line =~ /refdes=([a-zA-Z_]+)(\d+|\??)/i;
  +	my $pre = $1;
  +	my $suf = $2 ? $2 : "?";
  +	if($verbose) {print "Refdes line \"$line\" has pre=$pre suf=$suf\n";}
  +
  +	# Now finally update refdes
       if ($clear) {
  +	    # Just overwrite all refdeses upon clear
  +	    if($verbose) {print ("Clearing refdes=$pre$suf\n");}
   	print OUTNET "refdes=$pre?\n";
  +	} elsif (defined($pgskip) && defined($gentle)) {
  +	    # If highdev exists, then start devcnt there, otherwise, start
  +	    # devcnt at correct value for each page.
  +	    if (!defined($devcnt{$i, $pre}) && defined($highdev{$i, $pre})) {
  +		$devcnt{$i, $pre} = $highdev{$i, $pre};
  +	    } elsif (!defined($devcnt{$i, $pre}) && !defined($highdev{$i, $pre})) {
  +		$devcnt{$i, $pre} = ($i+1)*$pgskip ;
  +	    }
  +	    
  +	    if ($suf eq "?") {
  +		$devcnt{$i, $pre}++;
  +		if ($devcnt{$i, $pre} >= ($i+2)*$pgskip) {
  +		    print STDERR "ERROR:  You are numbering more than $pgskip elements with\n";
  +		    print STDERR "prefix $pre on this sheet.  You will need to either\n";
  +		    print STDERR "reduce this number or specify a larger step with the\n";
  +		    print STDERR "--pgskip argument.\n";
  +		    die_sdb(3, "");
  +		}
  +		print "Renumbering $line to $pre$devcnt{$i, $pre}\n" if($verbose);
  +		print OUTNET "refdes=$pre$devcnt{$i, $pre}\n";
       } else {
  -	# if we're skipping numbers, then start at 100 for page 1
  -	# and we'll jump to 200 for page 2, etc.
  -        if( ! $devcnt{$pre} ) { $devcnt{$pre} = $pgskip ? ($i+1)*$pgskip : 0; }
  +		print "Leaving line=$line alone\n" if($verbose);
  +		print OUTNET "refdes=$pre$suf\n";
  +	    }
  +
  +	} elsif (defined($pgskip) && !defined($gentle)) {
  +	    if (!defined($devcnt{$i, $pre})) {
  +		$devcnt{$i, $pre} = ($i+1)*$pgskip ;
  +	    }
  +	    $devcnt{$i, $pre}++;
  +	    if ($devcnt{$i, $pre} >= ($i+2)*$pgskip) {
  +		print STDERR "ERROR:  You are numbering more than $pgskip elements with\n";
  +		print STDERR "prefix $pre on this sheet.  You will need to either\n";
  +		print STDERR "reduce this number or specify a larger step with the\n";
  +		print STDERR "--pgskip argument.\n";
  +		die_sdb(3, "");
  +	    }
  +	    print "Renumbering $line to $pre$devcnt{$i, $pre}\n" if($verbose);
  +	    print OUTNET "refdes=$pre$devcnt{$i, $pre}\n";
  +
  +	} elsif (!defined($pgskip) && defined($gentle)) {
  +	    if (!defined($devcnt{$pre}) && defined($highdev{$pre})) {
  +		$devcnt{$pre} = $highdev{$pre};
  +	    } elsif (!defined($devcnt{$pre}) && !defined($highdev{$pre})) {
  +		$devcnt{$pre} = 0;
  +	    }
  +	    if ($suf eq "?") {
           $devcnt{$pre}++;
           print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
           print OUTNET "refdes=$pre$devcnt{$pre}\n";
  +	    } else {
  +		print "Leaving line=$line alone\n" if($verbose);
  +		print OUTNET "refdes=$pre$suf\n";
       }
  +	} elsif (!defined($pgskip) && !defined($gentle)) {
  +	    if (!defined($devcnt{$pre})) {
  +		$devcnt{$pre} = 0 ;
     }
  +	    $devcnt{$pre}++;
  +	    print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
  +	    print OUTNET "refdes=$pre$devcnt{$pre}\n";
  +
  +	} else {
  +	    die_sdb(4, "Invalid state when trying to update refdes!  Exiting.....\n");
  +	}
  +    }  # while($line = <NETLIST>)
     close(NETLIST);
     close(OUTNET);
   
  -  # Bump each elements starting refdes up to the next 100 (or whatever has
  -  # been specified with pgskip) to help identify what schematic page a
  -  # component is on by the refdes.
  -  if( $pgskip ) {
  -      foreach $dev (keys %devcnt) {
  -	  my $new_cnt = ($i + 2)*$pgskip;
  -	  if( $new_cnt < $devcnt{$dev} ) {
  -	      print STDERR "ERROR:  You have more than $pgskip elements with\n";
  -	      print STDERR "prefix $dev on this sheet.  You will need to either\n";
  -	      print STDERR "reduce this number or specify a larger step with the\n";
  -	      print STDERR "--pgskip argument.\n";
  -	      exit( 1 );
  -	  }
  -	  $devcnt{$dev} = $new_cnt;
  -	  print "Incremented \"$dev\" counter to $devcnt{$dev}\n" if($verbose);
  -      }
  -  }
   
     # make this an option to not overwrite the original
     if( $nocopy ) {
  @@ -131,7 +282,7 @@
     }
   }
   
  -exit;
  +exit(0);
   
   
   #######################################################################
  @@ -172,17 +323,30 @@
     print "                with 201, etc  Specifying a value gives the step between pages.\n";
     print "                For example --pgskip 10 will start with 11, 21, 31, etc.\n";
     print "\n";
  +  print "    --gentle    If given, this flag tells refdes_renum to leave any refdeses\n";
  +  print "                alone if they already have numbers.  Use this option to number\n";
  +  print "                new components in a schematic which has already been numbered.\n";
  +  print "\n";
     print "    --verbose   Enables verbose output.\n";
     print "\n";
     print "    --version   Shows the version of this program.\n";
     print "\n\n";
  -  print "Example:\n\n";
  +  print "Return codes:\n\n";
  +  print "\t$pname returns the following codes to the shell upon completion:\n";
  +  print "\n";
  +  print "          0   Program ran successfully.\n";
  +  print "          1   Error opening or reading input file.\n";
  +  print "          2   Error opening or writing output file.\n";
  +  print "          3   Too many components for --pgskip setting.\n";
  +  print "          4   Internal error (program bug encountered).\n";
  +  print "\n";
  +  print "Usage examples:\n\n";
     print "\t$pname mysch.sch\n";
     print "\t$pname --pgskip pg1.sch pg2.sch pg3.sch\n";
     print "\n\n";
     print "$pname was written by Dan McMahill <dmcmahill\@netbsd.org>\n";
     print "\n\n";
  -  exit;
  +  exit(0);
   }
   
   #---------------------------------
  @@ -192,7 +356,7 @@
   #---------------------------------
   
   sub version {
  -  open(PROG,"$0") or die "Could not open \"$0\" to find version\n\n";
  +  open(PROG,"$0") or die_sdb(4, "Could not open \"$0\" to find version\n\n");
     my $pname = $0;
     $pname =~ s/.*\///g;
   
  @@ -202,10 +366,62 @@
         $version = $words[3];
         $date = $words[4];
         print "$pname ($0):  Version $version, $date\n";
  -      exit;
  +      exit(0);
       }
     }
  -  print "Could not determine version of \"$0\"\n\n";
  -  exit;
  +  die_sdb(4, "Could not determine version of \"$0\"\n\n");
  +}
  +
  +#-------------------------------------------
  +# die_sdb(rc, string)
  +#
  +# Local version of die.  Sets return code
  +# for shell, then calls die(string)
  +#--------------------------------------------
  +sub die_sdb {
  +    my $rc = $_[0];
  +    my $string = $_[1];
  +    $! = $rc;
  +    die($string);
   }
   
  +# ----------------------------------------------
  +#
  +# Change Log
  +#
  +# $Log: refdes_renum,v $
  +# Revision 1.6  2007/04/15 00:42:22  sdb
  +# Added --gentle flag to refdes_renum, which won't renumber refdeses
  +# which already have a number.
  +#
  +# Revision 1.5  4.9.2007 sdb.
  +# Added --gentle flag, and include logic to handle case
  +# where user mixes --gentle with --pgskip.
  +#
  +# Revision 1.4  2007/02/24 18:43:17  pcjc2
  +# Merge changes to date from noscreen branch.
  +#
  +# Revision 1.2.6.1  2007/02/11 23:59:10  pcjc2
  +# Sync with trunc
  +#
  +# Revision 1.3  2007/02/10 20:46:17  sdb
  +# Added --clear option to clear refdeses.  (* jcl *)
  +#
  +# Revision 1.2  2005/12/21 00:09:56  danmc
  +# - Fix a bug where when using the --pgskip option, components which were
  +#   present on page n, but not on pages n+1 through n+j, and present again
  +#   on page n+j+1 got numbered in a strange way.  For example, J101 on page
  +#   1, no connectors on page 2, J201 on page 3 instead of J301.  Noted by
  +#   Stuart Brorson.
  +#
  +# - While here allow the user to change the default increment from 100 to whatever
  +#   they want.
  +#
  +# - Also fix a bug where exactly 101 components with the same refdes prefix
  +#   would cause a duplicate refdes on the next page.
  +#
  +# Revision 1.1  2003/02/21 03:21:12  ahvezda
  +# Added scripts/refdes_renum written by Dan McMahill
  +#
  +#
  +#
  
  
  


_______________________________________________
geda-cvs mailing list
geda-cvs@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-cvs