summaryrefslogtreecommitdiff
path: root/lib/Config/HAProxy/Iterator.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Config/HAProxy/Iterator.pm')
-rw-r--r--lib/Config/HAProxy/Iterator.pm69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/Config/HAProxy/Iterator.pm b/lib/Config/HAProxy/Iterator.pm
new file mode 100644
index 0000000..6b0208e
--- /dev/null
+++ b/lib/Config/HAProxy/Iterator.pm
@@ -0,0 +1,69 @@
+package Config::HAProxy::Iterator;
+use strict;
+use warnings;
+use Config::HAProxy::Node;
+use Carp;
+
+use constant {
+ NO_RECURSION => 0,
+ INORDER => 1,
+ POSTORDER => 2
+};
+
+sub new {
+ my $class = shift;
+ my $node = shift;
+ my $self = bless { }, $class;
+ if ($node->is_section) {
+ $self->{_list} = [ $node->tree() ];
+ } else {
+ $self->{_list} = [ $node ];
+ }
+ local %_ = @_;
+ if (defined($_{recursive})) {
+ $self->{_recursive} = $_{recursive};
+ } elsif ($_{inorder}) {
+ $self->{_recursive} = INORDER;
+ } elsif ($_{postorder}) {
+ $self->{_recursive} = POSTORDER;
+ } else {
+ $self->{_recursive} = NO_RECURSION;
+ }
+ return $self;
+}
+
+sub recursive { shift->{_recursive} }
+sub inorder { shift->{_recursive} == INORDER }
+sub postorder { shift->{_recursive} == POSTORDER }
+
+sub next {
+ my $self = shift;
+
+ if ($self->{_itr}) {
+ if (defined(my $v = $self->{_itr}->next())) {
+ return $v;
+ } else {
+ delete $self->{_itr};
+ return $self->{_cur} if $self->postorder;
+ }
+ }
+
+ if (defined($self->{_cur} = shift @{$self->{_list}})) {
+ if ($self->recursive && $self->{_cur}->is_section) {
+ $self->{_itr} = $self->{_cur}->iterator(recursive => $self->recursive);
+ if ($self->inorder) {
+ return $self->{_cur};
+ } else {
+ return $self->next();
+ }
+ }
+ }
+
+ return $self->{_cur};
+}
+
+1;
+
+
+
+

Return to:

Send suggestions and report system problems to the System administrator.