Mercurial > bins
comparison tools/bins_txt2xml @ 0:a84c32f131df 1.1.29
Import vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Wed, 15 Oct 2008 23:28:56 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a84c32f131df |
---|---|
1 #!/usr/bin/perl -w | |
2 | |
3 | |
4 use strict; | |
5 | |
6 my $verbose=1; | |
7 | |
8 my $bins_edit="bins_edit"; | |
9 | |
10 sub readTxt{ | |
11 my $file=shift; | |
12 my %hash; | |
13 | |
14 if (! open (FILE, $file)) { | |
15 print "Warning: cannot open file '$file' for reading ($!), skiping."; | |
16 return 1; | |
17 } | |
18 | |
19 my $tag; | |
20 my $value=""; | |
21 LINE: while (<FILE>) { | |
22 chomp; | |
23 next LINE if /^#/; #discard comments | |
24 next LINE if /^\s*$/; #ignore total whitespace | |
25 s/^\s+//; | |
26 s/\s+$//; | |
27 print " Reading line '$_'\n" if ($verbose >= 3); | |
28 if ($tag) { | |
29 if (m%</$tag>%) { | |
30 print " Found end tag </$tag>, " if ($verbose >= 2); | |
31 chomp($value); | |
32 $hash{$tag} = $value; | |
33 print "value is '$hash{$tag}'\n" if ($verbose >= 2); | |
34 $tag = ""; | |
35 $value = ""; | |
36 } else { | |
37 $value .= $_."\n"; | |
38 } | |
39 } elsif (m/<\w+>/) { | |
40 $tag = $_; | |
41 $tag =~ s/<(\w+)>/$1/; | |
42 print " Found begin tag <$tag>\n" if ($verbose >= 2); | |
43 } | |
44 } | |
45 close (FILE) || bail ("can't close $file ($!)"); | |
46 return %hash; | |
47 } | |
48 | |
49 | |
50 sub processFile{ | |
51 my $file = shift; | |
52 my $album = shift; # -a if album, empty string otherwise | |
53 | |
54 my %hash = readTxt($file); | |
55 | |
56 my $commandLine; | |
57 if ($file =~ m%/album.txt$%) { | |
58 $file =~ s%/album.txt$%/.%; | |
59 $commandLine="$bins_edit --html --album "; | |
60 } else { | |
61 $file =~ s/.txt$/.xml/; | |
62 $commandLine="$bins_edit --html "; | |
63 } | |
64 $commandLine .= "-v " foreach (2..$verbose); | |
65 my $tagName; | |
66 my $tagValue; | |
67 while ( ($tagName, $tagValue) = each(%hash) ) { | |
68 $tagValue =~ s/\'/\'\\\'\'/g; | |
69 $commandLine .= "-g $tagName='$tagValue' " | |
70 } | |
71 $commandLine .= "$file"; | |
72 | |
73 print " Executing «$commandLine»" if ($verbose >= 3); | |
74 system($commandLine); | |
75 } | |
76 | |
77 sub main { | |
78 my $dir = shift; | |
79 | |
80 print "Processing directory '$dir'...\n" if ($verbose >= 1); | |
81 | |
82 if (!opendir(DIR, "$dir")) { | |
83 print ("Warning: can't open dir $dir: $!\n"); | |
84 return; | |
85 } | |
86 my @files = grep { !/^\./ } readdir(DIR); | |
87 closedir DIR; | |
88 | |
89 foreach my $file (@files) { | |
90 if (-d $dir."/".$file) { | |
91 main ($dir."/".$file) | |
92 } elsif ($file =~ m/^album.txt$/) { | |
93 processFile($dir."/".$file, "-a"); | |
94 } elsif ($file =~ m/.txt$/) { | |
95 processFile($dir."/".$file, ""); | |
96 } | |
97 } | |
98 } | |
99 | |
100 main($ARGV[0]); |