annotate tools/bins_txt2xml @ 9:0f248ad86f9f 1.1.29.e2

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