]> jspc29.x-matter.uni-frankfurt.de Git - daqtools.git/commitdiff
tiny improvements and fixes to xml-db2tex.pl
authorMichael Wiebusch <antiquark@gmx.net>
Fri, 4 Jul 2014 17:09:18 +0000 (19:09 +0200)
committerMichael Wiebusch <antiquark@gmx.net>
Fri, 4 Jul 2014 17:09:18 +0000 (19:09 +0200)
xml-db/xml-db2tex.pl

index 1e95b3bb8bb4cff3d63c5674c63fc41efef15b38..4ccfbc773c83aefaad0812e0d3db5164b1ab92a0 100755 (executable)
@@ -15,6 +15,8 @@ use File::Copy;
 
 my $opt;
 
+$opt->{style} = "altgray";
+
 Getopt::Long::Configure(qw(gnu_getopt));
 GetOptions(
            'help|h'      => \$opt->{help},
@@ -24,6 +26,7 @@ GetOptions(
            'entity|e=s'  => \$opt->{entity},
            'output|o=s'  => \$opt->{output},
            'pdf'         => \$opt->{pdf},
+           'style=s'      => \$opt->{style},
            'standalone'  => \$opt->{standalone}
           );
 
@@ -31,21 +34,23 @@ printHelpMessage() if $opt->{help};
 printHelpMessage() unless $opt->{entity} && $opt->{group};
 
 
-my $me = this->new();
+my $self = this->new();
+
+$self->{opt} = $opt; # assimilate options
 
-$me->setEntity($opt->{entity});
-# $me->{entityFile} = "/home/micha/mnt/55local1/htdocs/daqtools/xml-db/cache/CbController.entity";
-$me->{group} = $opt->{group};
-$me->{table}->{label}   = $opt->{label}||"tab:".$opt->{group};
-$me->{table}->{caption} = $opt->{caption}||"Registers in group ".$opt->{group};
-$me->produceTable();
+$self->setEntity($opt->{entity});
+# $self->{entityFile} = "/home/micha/mnt/55local1/htdocs/daqtools/xml-db/cache/CbController.entity";
+$self->{group} = $opt->{group};
+$self->{table}->{label}   = $opt->{label}||"tab:".$opt->{group};
+$self->{table}->{caption} = $opt->{caption}||"Registers in group ".$opt->{group};
+$self->produceTable();
 
 
-$me->writeTexFile($opt->{output}, $opt->{standalone} );
+$self->writeTexFile($opt->{output}, $opt->{standalone} );
 
 if ($opt->{pdf}){
   if ($opt->{output}){
-    $me->pdflatex($opt->{output});
+    $self->pdflatex($opt->{output});
   } else {
     die "\n\ncannot make pdf!\nno output file specified, use the -o <file.tex> argument!\n";
   }
@@ -71,6 +76,12 @@ Options:
   -c, --caption    caption of the table
   -l, --label      latex label of the table
   
+  --style          hline : separate registers by
+                     horizontal lines
+                   altgray : separate registers with 
+                     alternating gray and white boxes
+                     (default)
+  
   --standalone     generate standalone compilable latex file
   --pdf            compile directly to pdf
   
@@ -92,8 +103,9 @@ sub new {
   
   # default formatting of the table
   $self->{table}->{dataKeys} = [ 'name', 'addr', 'bits', 'description' ];
-  $self->{table}->{header} = [ 'register', 'addr', 'bits', 'description' ];
-  $self->{table}->{format} = '@{} l l l p{8cm} @{}';
+  $self->{table}->{header} = [ 'Register', 'Addr', 'Bits', 'Description' ];
+#   $self->{table}->{format} = '@{} l l l p{8cm} @{}';
+  $self->{table}->{format} = ' l l c p{8cm} ';
   
   $self  = {
     %$self,
@@ -126,7 +138,7 @@ sub produceTable {
     my $type = $node->{type};
     my $repeat = $node->{repeat} || 1;
     my $stepsize = $node->{stepsize}||0;
-    my $bits = "";
+    my $bits = " ";
     if ($type ne 'register'){
       my $start = $node->{start};
       my $stop = $node->{start}+$node->{bits}-1;
@@ -156,21 +168,56 @@ sub produceTable {
         $hexaddr = ''; # don't print addr it's already in the register
       }
       
-      push(@{$data},{%$node, name => $name_, addr => $hexaddr, bits => $bits, addr_uint => $addr_});
+      push(@{$data},{
+        %$node,
+        name => $name_,
+        addr => $hexaddr,
+        bits => $bits,
+        addr_uint => $addr_
+      });
     }
   }
 
-  @$data = sort { $a->{bits} cmp $b->{bits} } @$data; # bit fields in ascending order
+  @$data = sort {
+    # sort numerically by first number in the bits string
+    my $aa=-1;
+    my $bb=-1;
+    if($a->{bits} =~ m/^(\d+)/){
+      $aa=$1;
+    }
+    if($b->{bits} =~ m/^(\d+)/){
+      $bb=$1;
+    }
+    return $aa <=> $bb;
+  } @$data; # bit fields in ascending order
+  
   @$data = sort { $a->{addr_uint} cmp $b->{addr_uint} } @$data; # addresses in ascending order
 
 
+  # some more formatting
+  
   my $last_addr;
+  my $addr_counter=0;
   for my $item (@$data){
+    
     # make hline at each new register
     my $cur_addr = $item->{addr_uint};
     if($last_addr){
       if($last_addr != $cur_addr){
-        $self->{table}->addData(plain_code => '\hline');
+        if($self->{opt}->{style} eq "hline"){
+          $self->{table}->addData(plain_code => '\hline');
+        }
+        $addr_counter++;
+      }
+    }
+    
+    
+#     if($item->{type} eq 'register' || $item->{type} eq 'registerfield'){
+#       $self->{table}->addData(plain_code => '\rowcolor{lightgray}');
+#     }
+    if($self->{opt}->{style} eq "altgray"){
+      if($addr_counter % 2){
+        $self->{table}->addData(plain_code => '\rowcolor{light-gray}');
       }
     }
     $self->{table}->addData(%$item); # fill it with the sorted data
@@ -201,6 +248,9 @@ sub writeTexFile {
     \usepackage{longtable}
     \usepackage{geometry}
     \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=3cm,rmargin=3cm}
+    \usepackage[table]{xcolor}
+    
+    \definecolor{light-gray}{gray}{0.90}
     \begin{document}
     %;
   }
@@ -213,21 +263,21 @@ sub writeTexFile {
 
 sub pdflatex{
   my $self = shift;
-  my $output = shift||$me->{group};
+  my $output = shift||$self->{group};
   my $here = qx("pwd");
   $here =~ s/\n//g;
   my $directory = "/dev/shm/xml-db2tex".rand();
   unless(-e $directory or mkdir $directory) {
          die "Unable to create $directory\n";
   }
-  my $texfile = $me->{group}.".tex";
-  my $pdffile = $me->{group}.".pdf";
+  my $texfile = $self->{group}.".tex";
+  my $pdffile = $self->{group}.".pdf";
   
   $output =~ s/\.(tex|pdf)//;
   $output.= ".pdf";
   
   chdir $directory;
-  $me->writeTexFile($texfile, "standalone");
+  $self->writeTexFile($texfile, "standalone");
   system("pdflatex $texfile");
   system("pdflatex $texfile");
   copy("$directory/$pdffile","$here/$output");
@@ -320,8 +370,11 @@ sub addData {
 sub generateString {
   my $self = shift;
   my $str = '% remember to include the following latex packages:
-  % booktabs
-  % longtable'."\n";
+%\usepackage{booktabs}
+%\usepackage{longtable}
+%\usepackage[table]{xcolor}  
+%\definecolor{light-gray}{gray}{0.90}
+  '."\n";
   
   my $header;
   
@@ -331,6 +384,7 @@ sub generateString {
     $header= "  ".join(" & ",map { '\textbf{'.$_.'}' } @{$self->{dataKeys}}).' \\\\'."\n";
   }
   
+  
   $str .= '\begin{longtable}'."\n";
   $str .="{".($self->{format}||"")."}\n";