From: Ian Beckwith Date: Tue, 19 Oct 2010 15:55:23 +0000 (+0100) Subject: Path::Node: make type an int not a string X-Git-Tag: debian/1.0-1~26 X-Git-Url: http://erislabs.net/gitweb/?p=id3fs.git;a=commitdiff_plain;h=13e50dce4c98b33208f8d5b191e940c46433f61a Path::Node: make type an int not a string --- diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index dc5ea7e..0c1260d 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -99,7 +99,7 @@ sub dirents } elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) { - if($state==$STATE_TAG && $self->at("tag") && + if($state==$STATE_TAG && $self->at($TYPE_TAG) && $self->{db}->tag_has_values($self->tail()->id())) { @dents=$self->tags(); @@ -116,7 +116,7 @@ sub dirents elsif($state==$STATE_BOOLEAN) { my $parent=$self->tail(); - unless($self->is("boolean", $parent) && + unless($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT") { @dents=("NOT"); @@ -142,6 +142,7 @@ sub dirents return(\@dents, \@fents); } +# State Machine Of Doom sub parse { my($self)=@_; @@ -176,12 +177,12 @@ sub parse elsif($name eq "NOT") { $root_not=1; - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); $self->state($STATE_BOOLEAN); } else { - $tag=ID3FS::Path::Node->new($self->{db},"tag", $name); + $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -198,11 +199,11 @@ sub parse { my $tag=$self->tail(); # print "SM: TAG/TAGVAL($state): $name\n"; - if($state==$STATE_TAG && $self->is("tag", $tag) && + if($state==$STATE_TAG && $self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id())) { # print "Parsing: parent: $tag->id()\n"; - my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id()); + my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id()); if(defined($tagval)) { $self->state($STATE_TAGVAL); @@ -225,16 +226,16 @@ sub parse elsif($name eq "AND") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } elsif($name eq "OR") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } else { - my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name); + my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -251,7 +252,7 @@ sub parse # print "SM: BOOLEAN: $name\n"; my $parent=$self->tail(); my $allownot=1; - if($self->is("boolean", $parent) && + if($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT") { $allownot=0; @@ -259,11 +260,11 @@ sub parse if($allownot && $name eq "NOT") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } else { - my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name); + my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -289,7 +290,7 @@ sub parse } else { - my $album=ID3FS::Path::Node->new($self->{db}, "album", $name); + my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name); if($album) { push(@{$self->{elements}}, $album); @@ -304,7 +305,7 @@ sub parse elsif($state==$STATE_TRACKLIST) { # print "SM: TRACKLIST: $name\n"; - my $track=ID3FS::Path::Node->new($self->{db}, "file", $name); + my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name); if($track) { push(@{$self->{elements}}, $track); @@ -333,7 +334,7 @@ sub parse } else { - my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name); + my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -354,7 +355,7 @@ sub parse # remove trailing boolean my @elements=@{$self->{elements}}; - while(@elements && $self->is("boolean", $elements[$#elements])) + while(@elements && $self->is($TYPE_BOOL, $elements[$#elements])) { pop @elements; } @@ -388,7 +389,7 @@ sub elements_to_tree return undef unless(@$elements); my ($left, $right, $op)=(undef, undef, undef); my $thing=pop @$elements; - if($self->is("boolean", $thing)) + if($self->is($TYPE_BOOL, $thing)) { $right=$self->elements_to_tree($elements); if($thing->{name} ne "NOT") @@ -410,16 +411,16 @@ sub sort_elements # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n"; while(my $thing = shift @input) { - if($self->is("tag", $thing)) + if($self->is($TYPE_TAG, $thing)) { # Handle tag values by dropping parent - if(@input && $self->is("tag", $input[0])) + if(@input && $self->is($TYPE_TAG, $input[0])) { $thing=shift @input; } push(@output, $thing); } - elsif($self->is("boolean", $thing)) + elsif($self->is($TYPE_BOOL, $thing)) { # bool while(@opstack && @@ -449,7 +450,7 @@ sub expecting_values { my($self)=@_; my $tail=$self->tail(); - if($self->is("tag", $tail)) + if($self->is($TYPE_TAG, $tail)) { return($self->{db}->tag_has_values($tail->id())); } @@ -459,7 +460,7 @@ sub trailing_tag_id { my($self)=@_; my $tail=$self->tail(); - if($self->is("tag", $tail)) + if($self->is($TYPE_TAG, $tail)) { return($tail->id()); } @@ -470,7 +471,7 @@ sub trailing_tag_parent { my($self)=@_; my $tail=$self->tail(); - if($self->is("tag", $tail)) + if($self->is($TYPE_TAG, $tail)) { return($tail->{parents_id}); } @@ -494,7 +495,7 @@ sub is my($self, $type, $thing)=@_; return 0 unless($thing); return 0 unless($thing->type()); - return 1 if($type eq $thing->type()); + return 1 if($type == $thing->type()); return 0; } @@ -589,7 +590,7 @@ sub albums my($self)=@_; my @ids=(); my $tail=$self->tail(); - if($self->is("artist", $tail)) + if($self->is($TYPE_ARTIST, $tail)) { return $self->artist_albums($tail->id()); } @@ -649,15 +650,15 @@ sub tracks { my($self)=@_; my $tail=$self->tail(); - if($self->is("artist", $tail)) + if($self->is($TYPE_ARTIST, $tail)) { return $self->artist_tracks($tail->id()); } - elsif($self->is("album", $tail)) + elsif($self->is($TYPE_ALBUM, $tail)) { my $artist_id=0; my $artist=$self->tail_parent(); - if($self->is("artist", $artist)) + if($self->is($TYPE_ARTIST, $artist)) { # should always happen $artist_id=$artist->id(); @@ -681,7 +682,7 @@ sub filename { my($self, $mountpoint)=@_; my $tail=$self->tail(); - if($self->is("file", $tail)) + if($self->is($TYPE_FILE, $tail)) { my $id=$tail->id(); my $sql=("SELECT paths.name, files.name FROM files\n" . @@ -801,12 +802,12 @@ sub in_or my($self)=@_; my $tail=$self->tail(); return 0 unless($tail); - return 0 unless($tail->type() eq "boolean"); + return 0 unless($tail->type() == $TYPE_BOOL); return 1 if($tail->name() eq "OR"); return 0 unless($tail->name() eq "NOT"); my $parent=$self->tail_parent(); return 0 unless($parent); - return 0 unless($parent->type() eq "boolean"); + return 0 unless($parent->type() == $TYPE_BOOL); return 1 if($parent->name() eq "OR"); return 0; } diff --git a/lib/ID3FS/Path/Node.pm b/lib/ID3FS/Path/Node.pm index 3775d38..eb703c2 100644 --- a/lib/ID3FS/Path/Node.pm +++ b/lib/ID3FS/Path/Node.pm @@ -19,6 +19,12 @@ package ID3FS::Path::Node; use strict; use warnings; +require Exporter; +use vars qw(@ISA @EXPORT $TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE); +@ISA=qw(Exporter); +@EXPORT=qw($TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE); +($TYPE_BOOL, $TYPE_TAG, $TYPE_ARTIST, $TYPE_ALBUM, $TYPE_FILE)=(1..5); + sub new { my $proto=shift; @@ -30,10 +36,13 @@ sub new $self->{type}=shift; $self->{name}=shift; $self->{parents_id}=shift; - if($self->{type} ne "boolean") + if($self->{type} != $TYPE_BOOL) { - my $table=ucfirst($self->{type}); - $table .= "s" unless($table=~/s$/); + my $table=''; + if ($self->{type} == $TYPE_TAG) { $table="tags"; } + elsif($self->{type} == $TYPE_ARTIST) { $table="artists"; } + elsif($self->{type} == $TYPE_ALBUM) { $table="albums"; } + elsif($self->{type} == $TYPE_FILE) { $table="files"; } $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id}); return undef unless(defined($self->{id})); } @@ -69,7 +78,7 @@ sub to_sql @outjoins = @joins = ("INNER"); } - if($self->type() ne "boolean") + if($self->type() != $TYPE_BOOL) { my $cnt=scalar(@joins)+1; $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'"; @@ -156,7 +165,7 @@ sub to_sql sub used_tags { my($self)=@_; - if($self->type() eq "boolean") + if($self->type() == $TYPE_BOOL) { my @used=(); push(@used, $self->left()->used_tags()) if($self->left());