1 |
#!/usr/bin/env perl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
sub usage { |
7 |
return "Usage: perl arch2xml <arches>\n\n" |
8 |
." arches the root directory of the arch files you want to put into XML\n"; |
9 |
} |
10 |
|
11 |
die usage unless @ARGV; |
12 |
|
13 |
my $root = $ARGV[0]; |
14 |
opendir TMP, $root or die "$root: $!\n"; |
15 |
closedir TMP; |
16 |
|
17 |
#-------------------------------------------------- |
18 |
# use File::Find; |
19 |
# |
20 |
# my @arches = (); |
21 |
# |
22 |
# sub recurse { |
23 |
# my ($top_level_dir) = @_; |
24 |
# finddepth(\&isarc, $top_level_dir); |
25 |
# } |
26 |
# |
27 |
# sub isarc { |
28 |
# if (-f && /\.arc$/) { |
29 |
# push @arches, $File::Find::name, chomp $/; |
30 |
# } |
31 |
# } |
32 |
#-------------------------------------------------- |
33 |
|
34 |
sub arch2xml { |
35 |
# recurse '.'; |
36 |
my ($root) = @_; |
37 |
my @arches = split /\n/, `find $root -type f -name \*.arc`; |
38 |
|
39 |
my $xml = ""; |
40 |
for my $file (@arches) { |
41 |
my @arch = do { open my $fh, "<$file" or die "$file: $!"; <$fh> }; |
42 |
|
43 |
my $msg = ""; |
44 |
my $ismsg = 0; |
45 |
my $anim = ""; |
46 |
my $isanim = 0; |
47 |
|
48 |
for my $line (@arch) { |
49 |
chomp $line; |
50 |
|
51 |
next if ($line =~ /^#/ or $line =~ /^(\s+)?$/); |
52 |
|
53 |
if ($line eq "endmsg") { |
54 |
$xml .= "\t\t<msg>$msg</msg>\n"; |
55 |
$msg = ""; |
56 |
$ismsg = 0; |
57 |
} |
58 |
if ($ismsg) { |
59 |
$msg .= "$line "; |
60 |
next; |
61 |
} |
62 |
|
63 |
if ($line eq "mina") { |
64 |
$xml .= "\t\t<anim>\n\t\t$anim</anim>\n"; |
65 |
$anim = ""; |
66 |
$isanim = 0; |
67 |
} |
68 |
if ($isanim) { |
69 |
$anim .= "\t<face>$line</face>\n\t\t"; |
70 |
next; |
71 |
} |
72 |
|
73 |
if ($line =~ /[Oo]bject/) { |
74 |
$line =~ s/^[Oo]bject //; |
75 |
$xml .= "\t<object>\n"; |
76 |
$xml .= "\t\t<arch>$line</arch>\n"; |
77 |
} elsif ($line =~ /^end$/) { |
78 |
$xml .= "\t</object>\n"; |
79 |
} elsif ($line eq "msg") { |
80 |
$ismsg = 1; |
81 |
} elsif ($line eq "anim") { |
82 |
$isanim = 1; |
83 |
} elsif ($line eq "More") { |
84 |
# handle "More" |
85 |
} elsif ($line eq "mina") { |
86 |
# handled above |
87 |
} elsif ($line eq "endmsg") { |
88 |
# handled above |
89 |
} else { |
90 |
my ($tag, $data) = split / /, $line, 2; |
91 |
$xml .= "\t\t<$tag>$data</$tag>\n"; |
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
return $xml; |
97 |
} |
98 |
|
99 |
print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
100 |
print "<?xml-stylesheet type=\"text/xsl\" href=\"arches.xsl\"?>\n"; |
101 |
print "<arches>\n"; |
102 |
print arch2xml $root; |
103 |
print "</arches>\n"; |