OSDN Git Service

add support for dynamic variable handling.(**,--)
authorpanda <panda>
Mon, 10 Mar 2003 11:32:23 +0000 (20:32 +0900)
committerpanda <panda>
Mon, 10 Mar 2003 11:32:23 +0000 (20:32 +0900)
config.php

index 059bb56..f7e205a 100644 (file)
@@ -2,7 +2,7 @@
 /////////////////////////////////////////////////
 // PukiWiki - Yet another WikiWikiWeb clone.
 //
-// $Id: config.php,v 1.1 2003/03/07 06:45:26 panda Exp $
+// $Id: config.php,v 1.2 2003/03/10 11:32:23 panda Exp $
 //
 /*
  * ¥×¥é¥°¥¤¥ó¤ÎÀßÄê¤òPukiWiki¤Î¥Ú¡¼¥¸¤Ëµ­½Ò¤¹¤ë
@@ -47,26 +47,62 @@ class Config
        function read()
        {
                $this->objs = array();
-               $title = '';
-               $obj = &$this->get_object($title);
+               $obj = &new ConfigTable();
                foreach (get_source($this->page) as $line)
                {
-                       if ($line != '' and $line{0} == '|' and preg_match('/^\|(.+)\|\s*$/',$line,$matches))
+                       if ($line == '')
                        {
-                               $obj->add_value(explode('|',$matches[1]));
+                               continue;
+                       }
+                       
+                       $head = $line{0};
+                       $level = strspn($line,$head);
+                       
+                       if ($level > 3)
+                       {
+                               $obj->add_line($line);
+                               continue;
+                       }
+                       
+                       if ($head == '*')
+                       {
+                               if ($level == 1)
+                               {
+                                       $this->objs[$obj->title] = &$obj;
+                                       $obj = &new ConfigTable();
+                                       $obj->add_line($line);
+                               }
+                               else
+                               {
+                                       if (!is_a($obj,'ConfigTable_Direct'))
+                                       {
+                                               $obj = &new ConfigTable_Direct($obj->after);
+                                       }
+                                       $obj->set_key($line);
+                               }
                        }
-                       else if ($line != '' and $line{0} == '*')
+                       else if ($head == '-' and $level > 1)
                        {
-                               $level = strspn($line,'*');
-                               $title = trim(substr($line,$level));
-                               $obj = &$this->get_object($title,$level);
+                               if (!is_a($obj,'ConfigTable_Direct'))
+                               {
+                                       $obj = &new ConfigTable_Direct($obj->after);
+                               }
+                               $obj->add_value($line);
+                       }
+                       else if ($head == '|' and preg_match('/^\|(.+)\|\s*$/',$line,$matches))
+                       {
+                               if (!is_a($obj,'ConfigTable_Sequential'))
+                               {
+                                       $obj = &new ConfigTable_Sequential($obj->after);
+                               }
+                               $obj->add_value(explode('|',$matches[1]));
                        }
                        else
                        {
                                $obj->add_line($line);
                        }
                }
-               $this->objs[$title] = &$obj;
+               $this->objs[$obj->title] = &$obj;
        }
        // ÇÛÎó¤ò¼èÆÀ¤¹¤ë
        function &get($title)
@@ -87,11 +123,11 @@ class Config
                $obj->values[] = $value;
        }
        // ¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤Ê¤¤¤È¤­¤Ïºî¤ë)
-       function &get_object($title,$level=1)
+       function &get_object($title)
        {
                if (!array_key_exists($title,$this->objs))
                {
-                       $this->objs[$title] = &new ConfigTable(str_repeat('*',$level).$title."\n");
+                       $this->objs[$title] = &new ConfigTable(array('*'.trim($title)."\n"));
                }
                return $this->objs[$title];
        }
@@ -114,29 +150,44 @@ class Config
 //ÇÛÎóÃͤòÊÝ»ý¤¹¤ë¥¯¥é¥¹
 class ConfigTable
 {
+       // ¥Æ¡¼¥Ö¥ë¤Î̾Á°
+       var $title = '';
+       // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
+       var $before = array();
        // ¼èÆÀ¤·¤¿ÃͤÎÇÛÎó
        var $values = array();
        // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
-       var $line;
+       var $after = array();
        
-       function ConfigTable($title)
+       function ConfigTable($lines=NULL)
        {
-               $this->line = $title;
+               if ($lines !== NULL)
+               {
+                       $this->title = trim(substr($lines[0],strspn($lines[0],'*')));
+                       $this->before = $lines;
+               }
        }
+       // ÀâÌÀ¤ÎÄɲÃ
+       function add_line($line)
+       {
+               $this->after[] = $line;
+       }
+       function toString()
+       {
+               return join('',$this->before).join('',$this->after);
+       }
+}
+class ConfigTable_Sequential extends ConfigTable
+{
        // ¹Ô¤ÎÄɲÃ
        function add_value($value)
        {
                $this->values[] = (count($value) == 1) ? $value[0] : $value;
        }
-       // ÀâÌÀ¤ÎÄɲÃ
-       function add_line($line)
-       {
-               $this->line .= $line;
-       }
        // ½ñ¼°²½
        function toString()
        {
-               $retval = $this->line;
+               $retval = join('',$this->before);
                if (is_array($this->values))
                {
                        foreach ($this->values as $value)
@@ -145,9 +196,53 @@ class ConfigTable
                                $retval .= "|$value|\n";
                        }
                }
-               $retval .= "\n"; // ¶õ¹Ô :)
-               
-               return $retval;
+               return $retval.join('',$this->after);
+       }
+}
+class ConfigTable_Direct extends ConfigTable
+{
+       // ¼èÆÀ¤·¤¿¥­¡¼¤ÎÇÛÎó¡£½é´ü²½»þ¤Ë»ÈÍѤ¹¤ë¡£
+       var $_keys = array();
+       
+       // ¥­¡¼¤ÎÀßÄê
+       function set_key($line)
+       {
+               $level = strspn($line,'*');
+               $this->_keys[$level] = trim(substr($line,$level));
+       }
+       // ¹Ô¤ÎÄɲÃ
+       function add_value($line)
+       {
+               $level = strspn($line,'-');
+               $arr = &$this->values;
+               for ($n = 2; $n <= $level; $n++)
+               {
+                       $arr = &$arr[$this->_keys[$n]];
+               }
+               $arr[] = trim(substr($line,$level));
+       }
+       // ½ñ¼°²½
+       function toString($values = NULL,$level = 2)
+       {
+               $retval = join('',$this->before);
+               if ($values == NULL)
+               {
+                       $retval = parent::toString();
+                       $values = &$this->values;
+               }
+               foreach ($values as $key=>$value)
+               {
+                       if (is_array($value))
+                       {
+                               $retval .= str_repeat('*',$level).$key."\n";
+                               $retval .= $this->toString($value,$level + 1);
+                       }
+                       else
+                       {
+                               $retval .= str_repeat('-',$level).$value."\n";
+                       }
+               }
+               return $retval.join('',$this->after);
        }
 }
 ?>