mergenedconns.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #
  2. # Edits NED files to merge two one-way connections to one.
  3. # It looks for the following pattern:
  4. #
  5. # a.ggg$o --> b.ggg$i;
  6. # a.ggg$i <-- b.ggg$o;
  7. #
  8. # and replaces it with
  9. #
  10. # a.ggg <--> b.ggg;
  11. #
  12. # Handles channels, gate++/gate[i], submod-to-parent connections etc.
  13. #
  14. $verbose = 1;
  15. $listfile = $ARGV[0];
  16. die "no listfile specified" if ($listfile eq '');
  17. # parse listfile
  18. print "reading $listfile...\n" if ($verbose);
  19. open(INFILE, $listfile) || die "cannot open $listfile";
  20. @fnames = ();
  21. while (<INFILE>) {
  22. chomp;
  23. s/\r$//; # cygwin/mingw perl does not do CR/LF translation
  24. push(@fnames,$_);
  25. }
  26. #print join(' ', @fnames);
  27. foreach $fname (@fnames)
  28. {
  29. print "reading $fname...\n" if ($verbose);
  30. $txt = readfile($fname);
  31. # if this is a network (simplification: we check if file contains at least one network)
  32. if ($txt =~ /^ *network\b/m)
  33. {
  34. # replace "in" gate with "pppg$i", "ethIn" with "ethg$i", etc
  35. $txt =~ s/\.in\b/.pppg\$i/g;
  36. $txt =~ s/\.out\b/.pppg\$o/g;
  37. $txt =~ s/\.ethIn\b/.ethg\$i/g;
  38. $txt =~ s/\.ethOut\b/.ethg\$o/g;
  39. }
  40. # Then merge $i/$o connection pairs
  41. $gateo = '(\w+(\[.*?\])?\.)?\w+\$o(\+\+)?(\[.*?\])?'; # 4 nested subexprs
  42. $gatei = '(\w+(\[.*?\])?\.)?\w+\$i(\+\+)?(\[.*?\])?';
  43. $rightarrow = ' *--> *(\w* *({.*?} *)?--> *)?'; # 2 nested subexprs
  44. $leftarrow = ' *<-- *(\w* *({.*?} *)?<-- *)?';
  45. while ($txt =~ /(${gateo})(${rightarrow})(${gatei});/m ||
  46. $txt =~ /(${gatei})(${rightarrow})(${gatei});/m ||
  47. $txt =~ /(${gateo})(${rightarrow})(${gateo});/m ||
  48. $txt =~ /(${gatei})(${leftarrow})(${gatei});/m ||
  49. $txt =~ /(${gateo})(${leftarrow})(${gateo});/m)
  50. {
  51. $leftg = $1;
  52. $chan = $6;
  53. $rightg = $9;
  54. mergeconn($leftg, $chan, $rightg);
  55. }
  56. $txt =~ s/--TBD-->/-->/g;
  57. $txt =~ s/<--TBD--/<--/g;
  58. writefile($fname, $txt);
  59. #writefile("$fname.new", $txt);
  60. }
  61. sub mergeconn ()
  62. {
  63. my $leftg = shift;
  64. my $chan = shift;
  65. my $rightg = shift;
  66. #print "found $& ==> $leftg; $chan; $rightg\n";
  67. $conntext = "$leftg$chan$rightg";
  68. $replacement = "$leftg$chan$rightg";
  69. $replacement =~ s/-->/<-->/g;
  70. $replacement =~ s/\$[io]//g;
  71. $leftginv = $leftg;
  72. $chaninv = $chan;
  73. $rightginv = $rightg;
  74. $leftginv =~ s/\$i/\$X/;
  75. $leftginv =~ s/\$o/\$i/;
  76. $leftginv =~ s/\$X/\$o/;
  77. $rightginv =~ s/\$i/\$X/;
  78. $rightginv =~ s/\$o/\$i/;
  79. $rightginv =~ s/\$X/\$o/;
  80. $chaninv =~ s/-->/-X-/g;
  81. $chaninv =~ s/<--/-->/g;
  82. $chaninv =~ s/-X-/<--/g;
  83. $delete1 = "$leftginv$chaninv$rightginv;";
  84. $delete2 = "$rightginv$chan$leftginv;";
  85. print " $conntext TO: $replacement\n";
  86. #print " delete: $delete1\n";
  87. #print " delete: $delete2\n";
  88. # if we find the opposite direction too, go ahead
  89. if ($txt =~ /\Q$delete1\E/ || $txt =~ /\Q$delete2\E/) {
  90. print " REPLACED!\n";
  91. $txt =~ s/\Q$conntext\E/$replacement/g;
  92. $txt =~ s/ *\Q$delete1\E *\n//gs;
  93. $txt =~ s/ *\Q$delete2\E *\n//gs;
  94. } else {
  95. print " SKIPPED (opposite direction not found, or has different channel)\n";
  96. $failconntext = $conntext;
  97. $failconntext =~ s/-->/--TBD-->/g;
  98. $failconntext =~ s/<--/<--TBD--/g;
  99. $txt =~ s/\Q$conntext\E/$failconntext/g;
  100. }
  101. }
  102. sub readfile ()
  103. {
  104. my $fname = shift;
  105. my $content;
  106. open FILE, "$fname" || die "cannot open $fname";
  107. read(FILE, $content, 1000000);
  108. close FILE;
  109. $content;
  110. }
  111. sub writefile ()
  112. {
  113. my $fname = shift;
  114. my $content = shift;
  115. open FILE, ">$fname" || die "cannot open $fname for write";
  116. print FILE $content;
  117. close FILE;
  118. }