view tools/bins_txt2xml @ 10:f962189bab94

Added tag 1.1.29.e2 for changeset 0f248ad86f9f
author Peter Gervai <grin@grin.hu>
date Thu, 16 Oct 2008 00:16:50 +0200
parents a84c32f131df
children
line wrap: on
line source

#!/usr/bin/perl -w


use strict;

my $verbose=1;

my $bins_edit="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]);