[glibmm/gmmproc-refactor] Add some section types and section manager.



commit 630c8898e76cb320facae635ea044d2efd693d85
Author: Krzesimir Nowak <qdlacz gmail com>
Date:   Wed Nov 30 18:11:46 2011 +0100

    Add some section types and section manager.

 tools/pm/Common/SectionManager.pm       |  305 +++++++++++++++++++++++++++++++
 tools/pm/Common/Sections/Conditional.pm |  100 ++++++++++
 tools/pm/Common/Sections/Entries.pm     |   70 +++++++
 tools/pm/Common/Sections/Section.pm     |   62 +++++++
 4 files changed, 537 insertions(+), 0 deletions(-)
---
diff --git a/tools/pm/Common/SectionManager.pm b/tools/pm/Common/SectionManager.pm
new file mode 100644
index 0000000..0cd7616
--- /dev/null
+++ b/tools/pm/Common/SectionManager.pm
@@ -0,0 +1,305 @@
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+# gmmproc - Common::WrapParser module
+#
+# Copyright 2011 glibmm development team
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+#
+
+package Common::SectionManager;
+
+use strict;
+use warnings;
+use feature ':5.10';
+
+use Common::Sections::Entries;
+use Common::Sections::Section;
+use Common::Sections::Conditional;
+use constant
+{
+  'SECTION_H' => 'SECTION_MAIN_H',
+  'SECTION_CC' => 'SECTION_MAIN_CC',
+  'SECTION_P_H' => 'SECTION_MAIN_P_H',
+  'SECTION_DEV_NULL' => 'SECTION_MAIN_DEV_NULL',
+  'VARIABLE_UNKNOWN' => 'NO_SUCH_VARIABLE_FOR_NOW'
+};
+
+sub _get_section ($$)
+{
+  my ($self, $section_name) = @_;
+  my $all_sections = $self->{'all_sections'};
+
+  unless (exists $all_sections->{$section_name})
+  {
+    $all_sections->{$section_name} = Common::Sections::Section->new ($section_name);
+  }
+
+  return $all_sections->{$section_name}
+}
+
+sub _get_conditional ($$)
+{
+  my ($self, $conditional_name) = @_;
+  my $conditionals = $self->{'conditionals'};
+
+  unless (exists $conditionals->{$conditional_name})
+  {
+    $conditionals->{$conditional_name} = Common::Sections::Conditional->new ($conditional_name, VARIABLE_UNKNOWN);
+  }
+
+  return $conditionals->{$conditional_name};
+}
+
+sub _append_stuff_to_entries ($$$$)
+{
+  my ($self, $type, $stuff, $entries) = @_;
+
+  given ($type)
+  {
+    when (Common::Sections::Entries::STRING ())
+    {
+      $entries->append_string ($stuff);
+    }
+    when (Common::Sections::Entries::SECTION ())
+    {
+      my $section = $self->_get_section ($stuff);
+
+      $entries->append_section ($section);
+    }
+    when (Common::Sections::Entries::CONDITIONAL ())
+    {
+      my $conditional = $self->_get_conditional ($stuff);
+
+      $entries->append_conditional ($conditional);
+    }
+    default
+    {
+      print STDERR 'Wrong type of entry.' . "\n";
+      exit 1;
+    }
+  }
+}
+
+sub _append_stuff_to_section ($$$$)
+{
+  my ($self, $type, $stuff, $section_name) = @_;
+  my $section = $self->_get_section ($section_name);
+  my $entries = $section->get_entries;
+
+  $self->append_stuff_to_entries ($type, $stuff, $entries);
+}
+
+sub _append_stuff_to_conditional ($$$$$)
+{
+  my ($self, $type, $stuff, $conditional_name, $bool) = @_;
+
+  if ($bool)
+  {
+    $bool = Common::Sections::Entries::TRUE ();
+  }
+  else
+  {
+    $bool = Common::Sections::Entries::FALSE ();
+  }
+
+  my $conditional = $self->_get_conditional ($conditional_name);
+  my $entries = $conditional->get_entries ($bool);
+
+  $self->append_stuff_to_entries ($type, $stuff, $entries);
+}
+
+sub new ($)
+{
+  my ($type) = @_;
+  my $class = (ref ($type) or $type or 'Common::SectionManager');
+  my $main_h_section = Common::Sections::Section->new (SECTION_H);
+  my $main_cc_section = Common::Sections::Section->new (SECTION_CC);
+  my $main_p_h_section = Common::Sections::Section->new (SECTION_P_H);
+  my $main_dev_null_section = Common::Sections::Section->new (SECTION_DEV_NULL);
+  my $self =
+  {
+    'toplevel_sections' =>
+    {
+      $main_h_section->get_name => $main_h_section,
+      $main_cc_section->get_name => $main_cc_section,
+      $main_p_h_section->get_name => $main_p_h_section,
+      $main_dev_null_section->get_name => $main_dev_null_section
+    },
+    'all_sections' =>
+    {
+      $main_h_section->get_name => $main_h_section,
+      $main_cc_section->get_name => $main_cc_section,
+      $main_p_h_section->get_name => $main_p_h_section,
+      $main_dev_null_section->get_name => $main_dev_null_section
+    },
+    'conditionals' => {},
+    'variables' => {}
+  };
+
+  return bless $self, $class;
+}
+
+sub get_variable ($$)
+{
+  my ($self, $name) = @_;
+  my $variables = $self->{'variables'};
+
+  unless (exists $variables->{$name})
+  {
+    $variables->{$name} = Common::Sections::Conditional::FALSE ();
+  }
+
+  return $variables->{$name};
+}
+
+sub set_variable ($$$)
+{
+  my ($self, $name, $value) = @_;
+  my $variables = $self->{'variables'};
+
+  if ($value)
+  {
+    $variables->{$name} = Common::Sections::Conditional::TRUE ();
+  }
+  else
+  {
+    $variables->{$name} = Common::Sections::Conditional::FALSE ();
+  }
+}
+
+##
+## string, section name
+##
+sub append_string_to_section ($$$)
+{
+  shift->_append_stuff_to_section (Common::Sections::Entries::STRING (), shift, shift);
+}
+
+##
+## section name, section name
+##
+sub append_section_to_section ($$$)
+{
+  shift->_append_stuff_to_section (Common::Sections::Entries::SECTION (), shift, shift);
+}
+
+##
+## conditional name, section name
+##
+sub append_conditional_to_section ($$$)
+{
+  shift->_append_stuff_to_section (Common::Sections::Entries::CONDITIONAL (), shift, shift);
+}
+
+##
+## string, conditional name, bool value
+##
+sub append_string_to_conditional ($$$$)
+{
+  shift->_append_stuff_to_conditional (Common::Sections::Entries::STRING (), shift, shift, shift);
+}
+
+##
+## section name, conditional name, bool value
+##
+sub append_section_to_conditional ($$$$)
+{
+  shift->_append_stuff_to_conditional (Common::Sections::Entries::SECTION (), shift, shift, shift);
+}
+
+##
+## conditional name, conditional name, bool value
+##
+sub append_conditional_to_conditional ($$$$)
+{
+  shift->_append_stuff_to_conditional (Common::Sections::Entries::CONDITIONAL (), shift, shift, shift);
+}
+
+sub set_variable_for_conditional ($$$)
+{
+  my ($self, $variable_name, $conditional_name) = @_;
+  my $conditional = $self->_get_conditional ($conditional_name);
+
+  $conditional->set_variable_name ($variable_name);
+}
+
+sub write_main_section_to_file ($$$)
+{
+  my ($self, $section_name, $file_name) = @_;
+  my $toplevel_sections = $self->{'toplevel_sections'};
+
+  unless (exists $toplevel_sections->{$section_name})
+  {
+    print STDERR 'No such toplevel section: `' . $section_name . '\'.';
+    exit 1;
+  }
+
+  my $fd = IO::File->new ($file_name, 'w');
+
+  unless (defined $fd)
+  {
+    print STDERR 'Could not open file `' . $file_name . '\' for writing.' . "\n";
+    exit 1;
+  }
+
+  my $section = $toplevel_sections->{$section_name};
+  my $entries = $section->get_entries;
+
+  for (my $iter = 0; $iter < @{$entries}; ++$iter)
+  {
+    my $pair = $entries->[$iter];
+    my $type = $pair->[0];
+    my $entry = $pair->[1];
+
+    given ($type)
+    {
+      when (Common::Sections::Entries::STRING ())
+      {
+        $fd->print ($entry);
+      }
+      when (Common::Sections::Entries::SECTION ())
+      {
+        my $new_entries = $entry->get_entries;
+
+        if (@{$new_entries})
+        {
+          splice @{$entries}, $iter, 1, @{$new_entries};
+          $entry->clear;
+          redo;
+        }
+      }
+      when (Common::Sections::Entries::CONDITIONAL ())
+      {
+        my $new_entries = $entry->get_entries ($self->get_variable ($entry->get_variable_name));
+
+        if (@{$new_entries})
+        {
+          splice @{$entries}, $iter, 1, @{$new_entries};
+          $entry->clear;
+          redo;
+        }
+      }
+      default
+      {
+        print STDERR 'Unknown type of entry in section.' . "\n";
+        exit 1;
+      }
+    }
+  }
+  $section->clear;
+  $fd->close;
+}
+
+1; # indicate proper module load.
diff --git a/tools/pm/Common/Sections/Conditional.pm b/tools/pm/Common/Sections/Conditional.pm
new file mode 100644
index 0000000..694d699
--- /dev/null
+++ b/tools/pm/Common/Sections/Conditional.pm
@@ -0,0 +1,100 @@
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+# gmmproc - Common::Sections::Conditional module
+#
+# Copyright 2011 glibmm development team
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+#
+
+package Common::Sections::Conditional;
+
+use strict;
+use warnings;
+use feature ':5.10';
+
+use constant
+{
+  'FALSE' => 0,
+  'TRUE' => 1
+};
+
+sub new ($$$)
+{
+  my ($type, $name, $bool_variable_name) = @_;
+  my $class = (ref ($type) or $type or 'Common::Sections::Conditional');
+  my $self =
+  {
+    'name' => $name,
+    'false_entries' => Common::Sections::Entries->new,
+    'true_entries' => Common::Sections::Entries->new,
+    'bool_variable_name' => $bool_variable_name
+  };
+
+  return bless $self, $class;
+}
+
+sub get_name ($)
+{
+  my ($self) = @_;
+
+  return $self->{'name'};
+}
+
+sub set_variable_name ($$)
+{
+  my ($self, $bool_variable_name) = @_;
+
+  $self->{'bool_variable_name'} = $bool_variable_name;
+}
+
+sub get_variable_name ($)
+{
+  my ($self) = @_;
+
+  return $self->{'bool_variable_name'};
+}
+
+sub get_entries ($$)
+{
+  my ($self, $which) = @_;
+
+  given ($which)
+  {
+    when (FALSE)
+    {
+      return $self->{'false_entries'};
+    }
+    when (TRUE)
+    {
+      return $self->{'true_entries'};
+    }
+    default
+    {
+      # TODO: throw an error.
+      print STDERR 'Unknown value for conditional, use Common::Sections::Conditional::{TRUE,FALSE}' . "\n";
+      exit 1;
+    }
+  }
+}
+
+sub clear ($)
+{
+  my ($self) = @_;
+
+  $self->{'false_entries'} = Common::Sections::Entries->new;
+  $self->{'true_entries'} = Common::Sections::Entries->new;
+}
+
+1; #indicate proper module load.
diff --git a/tools/pm/Common/Sections/Entries.pm b/tools/pm/Common/Sections/Entries.pm
new file mode 100644
index 0000000..b9250b0
--- /dev/null
+++ b/tools/pm/Common/Sections/Entries.pm
@@ -0,0 +1,70 @@
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+# gmmproc - Common::Sections::Entries module
+#
+# Copyright 2011 glibmm development team
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+#
+
+package Common::Sections::Entries;
+
+use strict;
+use warnings;
+use constant
+{
+  'STRING' => 0,
+  'SECTION' => 1,
+  'CONDITIONAL' => 2
+};
+
+sub new ($)
+{
+  my ($type) = @_;
+  my $class = (ref ($type) or $type or 'Common::Sections::Entries');
+  my $self = [];
+
+  return bless $self, $class;
+}
+
+sub append_string ($$)
+{
+  my ($self, $string) = @_;
+
+  push @{$self}, [STRING, $string];
+}
+
+sub append_section ($$)
+{
+  my ($self, $section) = @_;
+
+  push @{$self}, [SECTION, $section];
+}
+
+sub append_conditional ($$)
+{
+  my ($self, $conditional) = @_;
+
+  push @{$self}, [CONDITIONAL, $conditional];
+}
+
+sub get_copy ($)
+{
+  my ($self) = @_;
+  my @copy = (@{$self});
+
+  return \ copy;
+}
+
+1; #indicate proper module load.
diff --git a/tools/pm/Common/Sections/Section.pm b/tools/pm/Common/Sections/Section.pm
new file mode 100644
index 0000000..7409c34
--- /dev/null
+++ b/tools/pm/Common/Sections/Section.pm
@@ -0,0 +1,62 @@
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+# gmmproc - Common::WrapParser module
+#
+# Copyright 2011 glibmm development team
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+#
+
+package Common::Sections::Section;
+
+use strict;
+use warnings;
+
+use Common::Sections::Entries;
+
+sub new ($$)
+{
+  my ($type, $name) = @_;
+  my $class = (ref ($type) or $type or 'Common::Sections::Section');
+  my $self =
+  {
+    'name' => $name,
+    'entries' => Common::Sections::Entries->new
+  };
+
+  return bless $self, $class;
+}
+
+sub get_name ($)
+{
+  my ($self) = @_;
+
+  return $self->{'name'};
+}
+
+sub get_entries ($)
+{
+  my ($self) = @_;
+
+  return $self->{'entries'};
+}
+
+sub clear ($)
+{
+  my ($self) = @_;
+
+  $self->{'entries'} = Common::Sections::Entries->new;
+}
+
+1; #indicate proper module load.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]