Location: PHPKode > scripts > Def Setting Files > def-setting-files/class.deff.php
<?php
  class Deff
  {
    var $path;
    
    function Deff($path = "")
    {
      if (strlen($path) > 0)
      {
        $this->GetDefinitionsFromFile($path);
      }
    }
    
    function LoadDefinitionFile($path)
    {
      $fp = @fopen($path, "r");
      if ($fp !== false)
      {
        $this->path = $path;
        $content = "";
        while (!feof($fp))
        {
          $content .= fread($fp, 4096);
        }
        fclose($fp);
        return $this->LoadDefinitionData($content);
      }
      else
      {
        $this->path = "";
        return false;
      }
    }
    
    function LoadDefinitionData($data)
    {
      $data = preg_split("/\r?\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
      $ret = array();
      $group = "";
      for ($i = 0; $i < count($data); $i++)
      {
        $data[$i] = trim($data[$i]);
        if (preg_match("/^(.*?)\s*\{$/", $data[$i], $r))
        {
          $group = trim($r[1]);
        }
        elseif ($data[$i] == "}")
        {
          $group = "";
        }
        elseif (strlen($group) > 0)
        {
          if (preg_match("/^(.+?)\s*=\s*(.+?)$/", $data[$i], $r))
          {
            if ($r[2][0] == "[" && substr($r[2], -1) == "]")
            {
              $ret[$group][$r[1]] = $this->__StringToArray(substr($r[2], 1, -1));
            }
            else
            {
              $ret[$group][$r[1]] = $r[2];
            }
          }
        }
      }
      return $ret;
    }
    
    function SaveDefinition($data, $path = "")
    {
      if (strlen($path) > 0 && is_dir(dirname($path)))
      {
        return $this->__SaveDefinitionData($data, $path);
      }
      elseif (strlen($this->path) > 0)
      {
        return $this->__SaveDefinitionData($this->data, $path);
      }
      else
      {
        return false;
      }
    }
    
    function __SaveDefinitionData($data, $path)
    {
      $fp = @fopen($path, "w");
      if ($fp !== false)
      {
        foreach ($data as $groupname => $groupvalues)
        {
          fwrite($fp, $groupname . " {\n");
          foreach ($groupvalues as $name => $value)
          {
            fwrite($fp, "\t" . $name . "\t=\t");
            if (is_array($value))
            {
              fwrite($fp, $this->__ArrayToString($value) . "\n");
            }
            else
            {
              fwrite($fp, $value . "\n");
            }
          }
          fwrite($fp, "}\n");
        }
      }
      else
      {
        return false;
      }
    }
    
    function __ArrayToString(&$arr)
    {
      $ret = "";
      for ($i = 0; $i < count($arr); $i++)
      {
        if (strlen($ret) > 0)
        {
          $ret .= ";";
        }
        if (is_array($arr[$i]))
        {
          $ret .= $this->__ArrayToString($arr[$i]);
        }
        else
        {
          $ret .= $arr[$i];
        }
      }
      return "[" . $ret . "]";
    }
    
    function __StringToArray(&$str)
    {
      $ret = array();
      $p = 0;
      $pmax = strlen($str);
      while ($p < $pmax)
      {
        if (substr($str, $p, 1) == "[")
        {
          $c = 0;
          $p2 = $p + 1;
          while (true)
          {
            if (substr($str, $p2, 1) == "]" && $c == 0) {
              break;
            }
            if (substr($str, $p2, 1) == "]") $c--;
            elseif (substr($str, $p2, 1) == "[") $c++;
            $p2++;
          }
          $p++;
          $ret[] = $this->__StringToArray(substr($str, $p, $p2 - $p));
          $p = $p2 + 2;
        }
        else
        {
          $p2 = strpos($str, ";", $p);
          if ($p2 !== false)
          {
            $ret[] = substr($str, $p, $p2 - $p);
            $p = $p2 + 1;
          }
          else
          {
            $p2 = strpos($str, "]", $p);
            if ($p2 !== false) $ret[] = substr($str, $p, $p2 - $p);
            else $ret[] = substr($str, $p);
            $p = $pmax;
          }
        }
      }
      return $ret;
    }
  }
?>
Return current item: Def Setting Files