You are here

function Config_File::parse_contents in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/smarty/Config_File.class.php \Config_File::parse_contents()

parse the source of a configuration file manually.

Parameters

string $contents the file-contents to parse:

2 calls to Config_File::parse_contents()
Config_File::load_file in includes/moodle/lib/smarty/Config_File.class.php
Load a configuration file manually.
Config_File::set_file_contents in includes/moodle/lib/smarty/Config_File.class.php
Store the contents of a file manually.

File

includes/moodle/lib/smarty/Config_File.class.php, line 266

Class

Config_File
Config file reading class @package Smarty

Code

function parse_contents($contents) {
  if ($this->fix_newlines) {

    // fix mac/dos formatted newlines
    $contents = preg_replace('!\\r\\n?!', "\n", $contents);
  }
  $config_data = array();
  $config_data['sections'] = array();
  $config_data['vars'] = array();

  /* reference to fill with data */
  $vars =& $config_data['vars'];

  /* parse file line by line */
  preg_match_all('!^.*\\r?\\n?!m', $contents, $match);
  $lines = $match[0];
  for ($i = 0, $count = count($lines); $i < $count; $i++) {
    $line = $lines[$i];
    if (empty($line)) {
      continue;
    }
    if ($line[0] == '[' && preg_match('!^\\[(.*?)\\]!', $line, $match)) {

      /* section found */
      if ($match[1][0] == '.') {

        /* hidden section */
        if ($this->read_hidden) {
          $section_name = substr($match[1], 1);
        }
        else {

          /* break reference to $vars to ignore hidden section */
          unset($vars);
          $vars = array();
          continue;
        }
      }
      else {
        $section_name = $match[1];
      }
      if (!isset($config_data['sections'][$section_name])) {
        $config_data['sections'][$section_name] = array(
          'vars' => array(),
        );
      }
      $vars =& $config_data['sections'][$section_name]['vars'];
      continue;
    }
    if (preg_match('/^\\s*(\\.?\\w+)\\s*=\\s*(.*)/s', $line, $match)) {

      /* variable found */
      $var_name = rtrim($match[1]);
      if (strpos($match[2], '"""') === 0) {

        /* handle multiline-value */
        $lines[$i] = substr($match[2], 3);
        $var_value = '';
        while ($i < $count) {
          if (($pos = strpos($lines[$i], '"""')) === false) {
            $var_value .= $lines[$i++];
          }
          else {

            /* end of multiline-value */
            $var_value .= substr($lines[$i], 0, $pos);
            break;
          }
        }
        $booleanize = false;
      }
      else {

        /* handle simple value */
        $var_value = preg_replace('/^([\'"])(.*)\\1$/', '\\2', rtrim($match[2]));
        $booleanize = $this->booleanize;
      }
      $this
        ->_set_config_var($vars, $var_name, $var_value, $booleanize);
    }

    /* else unparsable line / means it is a comment / means ignore it */
  }
  return $config_data;
}