#!/usr/bin/perl -w $pageend=65; # should this be a variable from the input file ? #################################################### sub new_of { $m=shift; $m="00000" unless (defined($m)); open(OF, ">OUT_$of.$m.txt") or die("open $!"); @page=(); @page=(("")x $pageend); # empty string @slots=@freeslots; } #################################################### sub showpage { foreach $i (1 .. $pageend) { $page[$i]="" unless defined( $page[$i] ); printf(OF "%s\n", $page[$i]); } } #################################################### if (!defined($ARGV[0])) { print "Usage: to_labels.plx inputfilename\n"; exit(1); } $now=time; $of=sprintf("%s_%d_", $ARGV[0], $now); open(IN, "<$ARGV[0]") or die("cannot read $ARGV[0] - $!"); $state=""; while () { chomp; if ("" eq $state) { if (/^<(\w+)>$/) { $state=$1; next; } } if (/^<\/(\w+)>$/) { if ($1 eq $state) { $state=""; next; } } if ("geometry" eq $state) { if (/^xd=(\d+)$/) { die("repeated X-dimension") if (defined($xd)); $xd=$1; next; } if (/^yd=(\d+)$/) { die("repeated Y-dimension") if (defined($yd)); $yd=$1; next; } if (/^s=(\d+,\d+)$/) { push(@freeslots, $1); next; } die("bogus geometry ? $_"); } if ("addresses" eq $state) { # geometry needs to be defined first die ("insufficient geometry data") unless (defined($xd) && defined($yd)); die ("rejecting geometry data") unless (($xd>20) && ($yd>5)); new_of("00000") if (0 == @slots); @fields=split(/,/); # Insert this address into the page data at the next available slot # find the height and width of this address # find a vacant slot $xandy=$slots[0]; die("no vacant slots") unless (defined($xandy)); # if ($xandy =~ /^(\d+),(\d+)$/) { $x=$1; $y=$2; } else { die("invalid slot position"); } # choose the position of this address centered in the slot die("oversized address (vertical) - $fields[0]") if (@fields > $yd); $y += (($yd - @fields)/2); $flm=0; foreach (@fields) { $flm=length if (length > $flm); } die("oversized address (horizontal) - $fields[0]") if (@fields > $yd); $x += (($xd - $flm)/2); # foreach $_ (@fields) { s/^\s+//; s/\s+$//; $f=$_; $ending=""; # actually put the text on the page $lpy=length $page[$y]; if ($x < $lpy) { # XXX line too full # XXX rewrite currrent line and save any ending $beginning=substr( $page[$y], 0, ($x-1)); $ending=substr( $page[$y], $x+length($f) ); $page[$y]=$beginning; $lpy=length $page[$y]; } if ($x > ($lpy+1)) { $page[$y] .= (" "x($x-$lpy)); } $page[$y] .= $f; $page[$y] .= $ending if ($ending); $y++; } # mark that slot as taken - if the last one is taken showpage and newpage shift @slots; if (!@slots) { showpage(); new_of($.); } } } close(IN); showpage() unless ( (scalar @slots) == (scalar @freeslots)); # might be empty exit(0);