fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / doc / seperate.pl
blob900e8a3cca9819212bfe0082e0f922b66bab4c3d
1 #!/usr/bin/perl
3 $filename = $ARGV[0];
5 $filename =~ /^(.*)\.([^.]*)$/;
6 $basename = $1;
7 $extension = $2;
9 print "Processing $filename...\n";
11 $head = "";
13 open(fi, "<$filename");
15 while(<fi>) {
16 last if(/snip:chapter/);
17 $head .= $_;
20 # chapters
21 my $nr=1;
22 my @chapters;
23 my @names;
24 /!!!"(.*)"!!!/;$name = $1;
25 while(<fi>) {
26 my $chapter = "";
27 # one chapter
28 while(1) {
29 if(/snip:\/chapter/) {
30 while(<fi>) {
31 last if(!/^\s*$/);
33 last;
35 $chapter .= $_;
36 $_ = <fi>;
38 $names[$nr] = $name;
39 $chapters[$nr++] = $chapter;
41 /!!!"(.*)"!!!/;$name = $1;
42 if(!/snip:chapter/) {
43 last;
47 while($_) {
48 $tail .= $_;
49 $_ = <fi>;
52 close(fi);
54 my $num = $nr;
55 $nr=1;
56 for($nr=1;$nr<$num;$nr++) {
57 $h2 = $head;
58 $t2 = $tail;
59 my $filename = "$basename.$nr.$extension";
60 print "Creating $filename...\n";
61 for($n=1;$n<$num;$n++) {
62 if($nr == $n) {
63 $h2 =~ s/chapterlink$n/<a href="$basename.$n.$extension">&gt;$names[$n]&lt;<\/a>/;
64 } else {
65 $h2 =~ s/chapterlink$n/<a href="$basename.$n.$extension">$names[$n]<\/a>/;
69 if($nr > 1) {
70 $prev = $names[$nr-1];
71 $aprev = $basename.".".($nr-1).".".$extension;
72 $t2 =~ s/__prevlink__/<a href="$aprev">Previous:&nbsp;$prev<\/a>/;
73 } else {
74 $t2 =~ s/__prevlink__/&nbsp;/;
76 if($nr < $num-1) {
77 $next= $names[$nr+1];
78 $anext = $basename.".".($nr+1).".".$extension;
80 $t2 =~ s/__nextlink__/<a href="$anext">Next:&nbsp;$next<\/a>/;
81 } else {
82 $t2 =~ s/__nextlink__/&nbsp;/;
84 $t2 =~ s/__currentchapter__/$names[$nr]/;
86 open(fi, ">$filename");
87 print fi "$h2\n$chapters[$nr]\n$t2";
88 close(fi);