<commit_before><?php
require_once __DIR__ . "/PiniSection.class.php";
require_once __DIR__ . "/PiniProperty.class.php";

class Pini
{
	/**
	 * @var string The filename which should be used by default in load() and save() if not specified
	 */
	private $filename;
	/**
	 * @var array An array containing all PiniSection instances
	 */
	public $sections;
	/**
	 * @var string The name of the currently parsing section
	 */
	private $currentSection;
	/**
	 * @var array The content of the currently reading comment block
	 */
	private $commentBlock;

	public function __construct($filename = null)
	{
		$this->filename = $filename;

		$this->init();
		$this->load();
	}

	private function init()
	{
		$this->commentBlock = array();
		$this->sections = array();
	}

	/**
	 * Parse the given line.
	 *
	 * @param string $line The line to parse
	 */
	private function parseLine($line)
	{
		$line = trim($line);

		// Ignore empty lines
		if (!$line)
		{
			return;
		}

		// This line is a comment -> skip it
		if ($line[0] == ";")
		{
			$this->commentBlock[] = substr($line, 1);
			return;
		}

		// Parse a section in format "[name]"
		if ($line[0] == "[" and $line[strlen($line) - 1] == "]")
		{
			$this->currentSection = new PiniSection(substr($line, 1, strlen($line) - 2), $this->commentBlock);
			$this->addSection($this->currentSection);

			$this->commentBlock = array();
			return;
		}

		// Parse the line in format "key = value"
		list($key, $value) = explode("=", $line, 2);

		$key = trim($key);
		$value = trim($value);

		// No section defined yet -> Create a new default section without a name
		if (!$this->currentSection)
		{
			$this->currentSection = new PiniSection();
			$this->addSection($this->currentSection);
		}

		// The key of key-array pairs end with "[]" (e.g. value[])
		if (substr($key, -2) == "[]")
		{
			// This is a key-array pair (a key with multiple values)
			$key = substr($key, 0, -2);

			$property = $this->currentSection->getProperty($key);
			if (!$property)
			{
				$property = new PiniProperty($key, array());
				$this->currentSection->addProperty($property);
			}

			$property->value[] = $value;
		}
		else
		{
			// This is a normal key-value pair
			$property = $this->currentSection->getProperty($key);
			if (!$property)
			{
				$property = new PiniProperty($key);
				$this->currentSection->addProperty($property);
			}

			$property->value = $value;
		}

		$property->comment = $this->commentBlock;
		$this->commentBlock = array();
	}

	/**
	 * Add the given section.
	 *
	 * @param PiniSection $section The instance of the section
	 */
	public function addSection(PiniSection $section)
	{
		$this->sections[$section->name] = $section;
	}

	/**
	 * Get the specified property of the specified section.
	 *
	 * @param string $section The name of the section from which the property should be retrieved
	 * @param string $key The name of the property which should be retrieved
	 *
	 * @return null|PiniProperty The property or null if not found
	 */
	public function getProperty($section, $key)
	{
		if (!isset($this->sections[$section]))
		{
			return null;
		}

		/**
		 * @var $sectionInstance PiniSection<commit_msg>Added getSection method to retrieve a section without checking it's availability first<commit_after><?php
require_once __DIR__ . "/PiniSection.class.php";
require_once __DIR__ . "/PiniProperty.class.php";

class Pini
{
	/**
	 * @var string The filename which should be used by default in load() and save() if not specified
	 */
	private $filename;
	/**
	 * @var array An array containing all PiniSection instances
	 */
	public $sections;
	/**
	 * @var string The name of the currently parsing section
	 */
	private $currentSection;
	/**
	 * @var array The content of the currently reading comment block
	 */
	private $commentBlock;

	public function __construct($filename = null)
	{
		$this->filename = $filename;

		$this->init();
		$this->load();
	}

	private function init()
	{
		$this->commentBlock = array();
		$this->sections = array();
	}

	/**
	 * Parse the given line.
	 *
	 * @param string $line The line to parse
	 */
	private function parseLine($line)
	{
		$line = trim($line);

		// Ignore empty lines
		if (!$line)
		{
			return;
		}

		// This line is a comment -> skip it
		if ($line[0] == ";")
		{
			$this->commentBlock[] = substr($line, 1);
			return;
		}

		// Parse a section in format "[name]"
		if ($line[0] == "[" and $line[strlen($line) - 1] == "]")
		{
			$this->currentSection = new PiniSection(substr($line, 1, strlen($line) - 2), $this->commentBlock);
			$this->addSection($this->currentSection);

			$this->commentBlock = array();
			return;
		}

		// Parse the line in format "key = value"
		list($key, $value) = explode("=", $line, 2);

		$key = trim($key);
		$value = trim($value);

		// No section defined yet -> Create a new default section without a name
		if (!$this->currentSection)
		{
			$this->currentSection = new PiniSection();
			$this->addSection($this->currentSection);
		}

		// The key of key-array pairs end with "[]" (e.g. value[])
		if (substr($key, -2) == "[]")
		{
			// This is a key-array pair (a key with multiple values)
			$key = substr($key, 0, -2);

			$property = $this->currentSection->getProperty($key);
			if (!$property)
			{
				$property = new PiniProperty($key, array());
				$this->currentSection->addProperty($property);
			}

			$property->value[] = $value;
		}
		else
		{
			// This is a normal key-value pair
			$property = $this->currentSection->getProperty($key);
			if (!$property)
			{
				$property = new PiniProperty($key);
				$this->currentSection->addProperty($property);
			}

			$property->value = $value;
		}

		$property->comment = $this->commentBlock;
		$this->commentBlock = array();
	}

	/**
	 * Add the given section.
	 *
	 * @param PiniSection $section The instance of the section
	 */
	public function addSection(PiniSection $section)
	{
		$this->sections[$section->name] = $section;
	}

	/**
	 * Get the instance of the specified section.
	 *
	 * @param string $name The name of the section to retrieve
	 * @return null|PiniSection The section or null if not found
	 */
	public function getSection($name)
	{
		if (!isset($this->sections[$name]))
		{
			return null;
		}

		return $this->sections[$name];
	}

	/**
	 * Get the specified property of the specified section.
	 *
	 * @param string $section The name of the section from which the property should be retrieved
	 * @param string $key The name of the property which should be retrieved
	 *
	 * @return null|PiniProperty The property or null if not found
	 */
	public function getProperty($section, $key)
	{
		if (!isset($this->sections[$section]))
		{
			return null;
		}

		/**
		 * @var $sectionInstance PiniSection
{'fraction_non_alphanumeric': 0.15504451038575667, 'fraction_numerical': 0.002967359050445104, 'mean_word_length': 3.2637571157495255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4688278', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2092, 'n_words': 641}