summaryrefslogtreecommitdiff
path: root/lib/SkinFile.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2010-01-24 14:21:26 +0100
committerYorhel <git@yorhel.nl>2010-01-24 14:22:46 +0100
commitb04c46baec8aef93d21898ef7f52b9d6d2442ce3 (patch)
treeca68ca3d952b4dc84ef21bac844c97b1523d323a /lib/SkinFile.pm
parent9b7a44bee4f47dca175be2f38bc516aa34e14104 (diff)
SkinFile: Abstracted access to the skin configuration files
And updated skingen.pl and vndb.pl to make use of this abstraction.
Diffstat (limited to 'lib/SkinFile.pm')
-rw-r--r--lib/SkinFile.pm73
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/SkinFile.pm b/lib/SkinFile.pm
new file mode 100644
index 00000000..b8d0c4fb
--- /dev/null
+++ b/lib/SkinFile.pm
@@ -0,0 +1,73 @@
+
+package SkinFile;
+
+use strict;
+use warnings;
+use Fcntl 'LOCK_SH', 'SEEK_SET';
+
+
+sub new {
+ my($class, $root, $open) = @_;
+ my $self = bless { root => $root }, $class;
+ $self->open($open) if $open;
+ return $self;
+}
+
+
+sub list {
+ return map /\/([^\/]+)\/conf/?$1:(), glob "$_[0]{root}/*/conf";
+}
+
+
+sub open {
+ my($self, $dir, $force) = @_;
+ return if $self->{"s_$dir"} && !$force;
+ my %o;
+ open my $F, '<:utf8', "$self->{root}/$dir/conf" or die $!;
+ flock $F, LOCK_SH or die $!;
+ seek $F, 0, SEEK_SET or die $!;
+ while(<$F>) {
+ chomp;
+ s/\r//g;
+ s{[\t\s]*//.+$}{};
+ next if !/^([a-z0-9]+)[\t\s]+(.+)$/;
+ $o{$1} = $2;
+ }
+ close $F;
+ $self->{"s_$dir"} = \%o;
+ $self->{opened} = $dir;
+}
+
+
+sub get {
+ my($self, $dir, $var) = @_;
+ $self->open($dir) if defined $var;
+ $var = $dir if !defined $var;
+ $var ? $self->{"s_$self->{opened}"}{$var} : keys %{$self->{"s_$self->{opened}"}};
+}
+
+
+1;
+
+
+__END__
+
+=pod
+
+=head1 NAME
+
+SkinFile - Simple object oriented interface to parsing skin configuration files
+
+=head1 USAGE
+
+ use SkinFile;
+ my $s = SkinFile->new($dir);
+ my @skins = $s->list;
+
+ $s->open($skins[0]);
+ my $name = $s->get('name');
+
+ # same as above, but in one function
+ my $name = $s->get($skins[0], 'name');
+
+