dbm_index_dot.pl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/perl -w
  2. #
  3. #Author: Ruan Jue
  4. #
  5. use strict;
  6. use DB_File;
  7. my $dot_file = shift or die("Usage: $0 <dot_file>\n");
  8. die("$dot_file.dbm already exists!!!") if(-e "$dot_file.dbm");
  9. open(IN, "<", $dot_file) or die;
  10. my %hash;
  11. tie %hash, 'DB_File', "$dot_file.dbm", O_RDWR | O_CREAT, 0644, $DB_HASH or die "Cannot open $dot_file.dbm: $!";
  12. my %nodes = ();
  13. my %link = ();
  14. while(<IN>){
  15. s/^\s+//;
  16. s/\s+$//;
  17. if(/^rankdir\s*=\s*(\S+)/){
  18. $hash{"rankdir"} = $1;
  19. next;
  20. }
  21. my $desc = '';
  22. while(1){
  23. if(/\s*(\[[^]]+\]);?$/){
  24. $_ = substr($_, 0, length($_) - length($1));
  25. $desc .= $1;
  26. s/\s+$//;
  27. } else {
  28. last;
  29. }
  30. }
  31. my @ts = split;
  32. if(@ts == 1 and length $desc){
  33. $nodes{$ts[0]} = $desc;
  34. } elsif(@ts >= 3 and ($ts[1] eq '->' or $ts[1] eq '-')){
  35. my ($lnk1, $lnk2) = ("", "");
  36. if($ts[0]=~/^(\S+?):(\S+)$/){ $ts[0] = $1; $lnk1 = $2; }
  37. if($ts[2]=~/^(\S+?):(\S+)$/){ $ts[2] = $1; $lnk2 = $2; }
  38. push(@{$link{$ts[0]}{$ts[2]}}, [$desc, $lnk1, $lnk2]);
  39. $link{$ts[2]}{$ts[0]} = [] unless(defined $link{$ts[2]}{$ts[0]});
  40. }
  41. }
  42. close IN;
  43. foreach my $n1 (keys %nodes){
  44. $hash{$n1} = $nodes{$n1};
  45. }
  46. foreach my $n1 (keys %link){
  47. my $hx = $link{$n1};
  48. my $str = (defined $hash{$n1})? $hash{$n1} : "";
  49. foreach my $n2 (keys %{$hx}){
  50. my $ls = $hx->{$n2};
  51. $str .= "\nN\t$n2";
  52. foreach my $lk (@{$ls}){
  53. $str .= "\n" . join("\t", "L", @{$lk});
  54. }
  55. }
  56. $hash{$n1} = $str;
  57. }
  58. untie %hash;
  59. 1;