File: bins_txt2xml

package info (click to toggle)
bins 1.1.27-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,212 kB
  • ctags: 553
  • sloc: perl: 4,756; xml: 1,768; sh: 428; makefile: 149
file content (100 lines) | stat: -rwxr-xr-x 2,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl -w


use strict;

my $verbose=1;

my $bins_edit="~/src/bins/bins_edit";

sub readTxt{
  my $file=shift;
  my %hash;

  if (! open (FILE, $file)) {
    print "Warning: cannot open file '$file' for reading ($!), skiping.";
    return 1;
  }

  my $tag;
  my $value="";
 LINE: while (<FILE>) {
    chomp;
    next LINE if /^#/;          #discard comments
    next LINE if /^\s*$/;	#ignore total whitespace
    s/^\s+//;
    s/\s+$//;
    print "    Reading line '$_'\n" if ($verbose >= 3);
    if ($tag) {
      if (m%</$tag>%) {
	print "   Found end tag </$tag>, " if ($verbose >= 2);
	chomp($value);
	$hash{$tag} = $value;
	print "value is '$hash{$tag}'\n" if ($verbose >= 2);
	$tag   = "";
	$value = "";
      } else {
	$value .= $_."\n";
      }
    } elsif (m/<\w+>/) {
      $tag = $_;
      $tag =~ s/<(\w+)>/$1/;
      print "   Found begin tag <$tag>\n" if ($verbose >= 2);
    }
  }
  close (FILE) || bail ("can't close $file  ($!)");
  return %hash;
}


sub processFile{
  my $file = shift;
  my $album = shift; # -a if album, empty string otherwise

  my %hash = readTxt($file);

  my $commandLine;
  if ($file =~ m%/album.txt$%) {
    $file =~ s%/album.txt$%/.%;
    $commandLine="$bins_edit --html --album ";
  } else {
    $file =~ s/.txt$/.xml/;
    $commandLine="$bins_edit --html ";
  }
  $commandLine .= "-v " foreach (2..$verbose);
  my $tagName;
  my $tagValue;
  while ( ($tagName, $tagValue) = each(%hash) ) {
    $tagValue =~ s/\'/\'\\\'\'/g;
    $commandLine .= "-g $tagName='$tagValue' "
  }
  $commandLine .= "$file";

  print " Executing $commandLine" if ($verbose >= 3);
  system($commandLine);
}

sub main {
  my $dir = shift;

  print "Processing directory '$dir'...\n" if ($verbose >= 1);

  if (!opendir(DIR, "$dir")) {
    print ("Warning: can't open dir $dir: $!\n");
    return;
  }
  my @files = grep { !/^\./ } readdir(DIR);
  closedir DIR;

  foreach my $file (@files) {
    if (-d $dir."/".$file) {
      main ($dir."/".$file)
    } elsif ($file =~ m/^album.txt$/) {
      processFile($dir."/".$file, "-a");
    } elsif ($file =~ m/.txt$/) {
      processFile($dir."/".$file, "");
    }
  }
}

main($ARGV[0]);