<commit_before>/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.commons.io.ini;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import lucee.commons.io.IOUtil;
import lucee.commons.io.res.Resource;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;

/**
 * read an ini file and allow to modifie and read the data
 */
public final class IniFile {

	private Map sections;
	private final Resource file;

	private static Map newMap() {
		return new LinkedHashMap();
	}

	/**
	 * Constructor for the IniFile object
	 *
	 * @param file ini FIle
	 * @throws IOException
	 */
	public IniFile(Resource file) throws IOException {
		this.file = file;
		sections = newMap();
		InputStream is = null;
		if (!file.exists()) file.createFile(false);
		try {
			load(is = file.getInputStream());
		}
		finally {
			IOUtil.close(is);
		}
	}

	public IniFile(InputStream is) throws IOException {
		sections = newMap();
		load(is);
		file = null;
	}

	/**
	 * Sets the KeyValue attribute of the IniFile object
	 *
	 * @param strSection the section to set
	 * @param key the key of the new value
	 * @param value the value to set
	 */
	public void setKeyValue(String strSection, String key, String value) {
		Map section = getSectionEL(strSection);
		if (section == null) {
			section = newMap();
			sections.put(strSection.toLowerCase(), section);
		}
		section.put(key.toLowerCase(), value);
	}

	/**
	 * Gets the Sections attribute of the IniFile object
	 *
	 * @return The Sections value
	 */
	public Map getSections() {
		return sections;
	}

	/**
	 * Gets the Section attribute of the IniFile object
	 *
	 * @param strSection section name to get
	 * @return The Section value
	 * @throws IOException
	 */
	public Map getSection(String strSection) throws IOException {
		Object o = sections.get(strSection.toLowerCase());
		if (o == null) throw new IOException("section with name " + strSection + " does not exist");
		return (Map) o;
	}

	/**
	 * Gets the Section attribute of the IniFile object, return null if section not exist
	 *
	 * @param strSection section name to get
	 * @return The Section value
	 */
	public Map getSectionEL(String strSection) {
		Object o = sections.get(strSection.toLowerCase());
		if (o == null) return null;
		return (Map) o;
	}

	/**
	 * Gets the NullOrEmpty attribute of the IniFile object
	 *
	 * @param section section to check
	 * @param key key to check
	 * @return is empty or not
	 */
	public boolean isNullOrEmpty(String section, String key) {
		String value = getKeyValueEL(section, key);
		return (value == null || value.length() == 0);
	}

	/**
	 * Gets the KeyValue attribute of the IniFile object
	 *
	 * @param strSection section to get
	 * @param key key to get
	 * @return matching alue
	 * @throws IOException
	 */
	public String getKeyValue(String strSection, String key) throws IOException {
		Object o = getSection(strSection).get(key.toLowerCase());
		if (o == null) throw new IOException("key " + key + " doesn't exist in section " + strSection);
		return (String) o;

	}

	/**
	 * Gets the KeyValue attribute of the IniFile object, if not exist return null
	 *
	 * @param strSection section to get
	 * @param key key to get
	 * @return matching alue
	 */
	public String getKeyValueEL(String strSection, String key) {
		Map map = getSectionEL(strSection);
		if (map == null) return null;
		Object o = map.get(key.toLowerCase());
		if (o == null) return null;
		return (String) o;

	}

	/**
	 * loads the ini file
	 * <commit_msg>format error messages #3<commit_after>/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.commons.io.ini;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import lucee.commons.io.IOUtil;
import lucee.commons.io.res.Resource;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;

/**
 * read an ini file and allow to modifie and read the data
 */
public final class IniFile {

	private Map sections;
	private final Resource file;

	private static Map newMap() {
		return new LinkedHashMap();
	}

	/**
	 * Constructor for the IniFile object
	 *
	 * @param file ini FIle
	 * @throws IOException
	 */
	public IniFile(Resource file) throws IOException {
		this.file = file;
		sections = newMap();
		InputStream is = null;
		if (!file.exists()) file.createFile(false);
		try {
			load(is = file.getInputStream());
		}
		finally {
			IOUtil.close(is);
		}
	}

	public IniFile(InputStream is) throws IOException {
		sections = newMap();
		load(is);
		file = null;
	}

	/**
	 * Sets the KeyValue attribute of the IniFile object
	 *
	 * @param strSection the section to set
	 * @param key the key of the new value
	 * @param value the value to set
	 */
	public void setKeyValue(String strSection, String key, String value) {
		Map section = getSectionEL(strSection);
		if (section == null) {
			section = newMap();
			sections.put(strSection.toLowerCase(), section);
		}
		section.put(key.toLowerCase(), value);
	}

	/**
	 * Gets the Sections attribute of the IniFile object
	 *
	 * @return The Sections value
	 */
	public Map getSections() {
		return sections;
	}

	/**
	 * Gets the Section attribute of the IniFile object
	 *
	 * @param strSection section name to get
	 * @return The Section value
	 * @throws IOException
	 */
	public Map getSection(String strSection) throws IOException {
		Object o = sections.get(strSection.toLowerCase());
		if (o == null) throw new IOException("Section with name [" + strSection + "] does not exist");
		return (Map) o;
	}

	/**
	 * Gets the Section attribute of the IniFile object, return null if section not exist
	 *
	 * @param strSection section name to get
	 * @return The Section value
	 */
	public Map getSectionEL(String strSection) {
		Object o = sections.get(strSection.toLowerCase());
		if (o == null) return null;
		return (Map) o;
	}

	/**
	 * Gets the NullOrEmpty attribute of the IniFile object
	 *
	 * @param section section to check
	 * @param key key to check
	 * @return is empty or not
	 */
	public boolean isNullOrEmpty(String section, String key) {
		String value = getKeyValueEL(section, key);
		return (value == null || value.length() == 0);
	}

	/**
	 * Gets the KeyValue attribute of the IniFile object
	 *
	 * @param strSection section to get
	 * @param key key to get
	 * @return matching alue
	 * @throws IOException
	 */
	public String getKeyValue(String strSection, String key) throws IOException {
		Object o = getSection(strSection).get(key.toLowerCase());
		if (o == null) throw new IOException("Key [" + key + "] doesn't exist in section [" + strSection + "]");
		return (String) o;

	}

	/**
	 * Gets the KeyValue attribute of the IniFile object, if not exist return null
	 *
	 * @param strSection section to get
	 * @param key key to get
	 * @return matching alue
	 */
	public String getKeyValueEL(String strSection, String key) {
		Map map = getSectionEL(strSection);
		if (map == null) return null;
		Object o = map.get(key.toLowerCase());
		if (o == null) return null;
		return (String) o;

	}

	/**
	 * loads the ini file
	 * 
{'fraction_non_alphanumeric': 0.10207300585849481, 'fraction_numerical': 0.0016899504281207751, 'mean_word_length': 3.5523076923076924, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6266486', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2576, 'n_words': 1007}