Read and write INI configuration files

Introduced classes

class IniError

ini :: IniError

Error for IniFile parsing
class IniFile

ini :: IniFile

Read and write INI configuration files
class IniSection

ini :: IniSection

A section in a IniFile

All class definitions

class IniError

ini $ IniError

Error for IniFile parsing
class IniFile

ini $ IniFile

Read and write INI configuration files
class IniSection

ini $ IniSection

A section in a IniFile
package_diagram ini::ini ini core core ini::ini->core config::config config config::config->ini::ini nitc::loader loader nitc::loader->ini::ini nitc::nitpm nitpm nitc::nitpm->ini::ini popcorn::pop_config pop_config popcorn::pop_config->config::config markdown::nitmd nitmd markdown::nitmd->config::config markdown2::nitmd nitmd markdown2::nitmd->config::config nlp::nlp_index nlp_index nlp::nlp_index->config::config nlp::nlp_server nlp_server nlp::nlp_server->config::config vsm::example_vsm example_vsm vsm::example_vsm->config::config popcorn::pop_config... ... popcorn::pop_config...->popcorn::pop_config markdown::nitmd... ... markdown::nitmd...->markdown::nitmd markdown2::nitmd... ... markdown2::nitmd...->markdown2::nitmd nlp::nlp_index... ... nlp::nlp_index...->nlp::nlp_index nlp::nlp_server... ... nlp::nlp_server...->nlp::nlp_server vsm::example_vsm... ... vsm::example_vsm...->vsm::example_vsm nitc::modelbuilder modelbuilder nitc::modelbuilder->nitc::loader nitc::modelbuilder... ... nitc::modelbuilder...->nitc::modelbuilder a_star-m a_star-m a_star-m->nitc::nitpm a_star-m... ... a_star-m...->a_star-m

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.

Children

module config

config :: config

Configuration options for nit tools and apps
module loader

nitc :: loader

Loading of Nit source files
module nitpm

nitc :: nitpm

Nit package manager command line interface

Descendants

module a_star-m

a_star-m

module abstract_compiler

nitc :: abstract_compiler

Abstract compiler
module actors_generation_phase

nitc :: actors_generation_phase

Generate a support module for each module that contain a class annotated with is actor
module actors_injection_phase

nitc :: actors_injection_phase

Injects model for the classes annotated with "is actor" so
module android

nitc :: android

Compile program for the Android platform
module android_annotations

nitc :: android_annotations

Additionnal annotations to gather metadata on Android projects
module annotation

nitc :: annotation

Management and utilities on annotations
module api

nitc :: api

Components required to build a web server about the nit model.
module api_auth

nitc :: api_auth

module api_base

nitc :: api_base

Base classes used by nitweb.
module api_docdown

nitc :: api_docdown

Nitdoc specific Markdown format handling for Nitweb
module api_feedback

nitc :: api_feedback

Feedback related features
module api_light

nitc :: api_light

Highlight and collect messages from a piece of code
module api_model

nitc :: api_model

module app_annotations

nitc :: app_annotations

Annotations to gather metadata on app.nit projects
module ast_metrics

nitc :: ast_metrics

Metrics about the nodes and identifiers in the AST
module astbuilder

nitc :: astbuilder

Instantiation and transformation of semantic nodes in the AST of expressions and statements
module auto_super_init

nitc :: auto_super_init

Computing of super-constructors that must be implicitly called at the begin of constructors.
module c

nitc :: c

Support for nesting C code within a Nit program using its FFI
module c_compiler_options

nitc :: c_compiler_options

Offers the annotations cflags and ldflags to specify
module catalog

nitc :: catalog

Basic catalog generator for Nit packages
module check_annotation

nitc :: check_annotation

Check that annotation present in the AST are either primitive or user-declared
module code_gen

nitc :: code_gen

Main frontend phases plus code generation phases
module commands_base

nitc :: commands_base

Documentation commands
module commands_catalog

nitc :: commands_catalog

Commands to retrieve Catalog related data
module commands_docdown

nitc :: commands_docdown

Doc down related queries
module commands_graph

nitc :: commands_graph

Graph commands
module commands_http

nitc :: commands_http

Initialize commands from HTTP requests
module commands_model

nitc :: commands_model

Doc commands about a Model or a MEntity
module commands_parser

nitc :: commands_parser

A parser that create DocCommand from a string
module commands_usage

nitc :: commands_usage

Commands about how mentities are used
module compilation

nitc :: compilation

The compilation module of the VirtualMachine
module compiler

nitc :: compiler

Compilation to C
module compiler_ffi

nitc :: compiler_ffi

Full FFI support for the compiler
module compiler_serialization

nitc :: compiler_serialization

Serialization support for the compiler
module contracts

nitc :: contracts

Module to build contract
module cpp

nitc :: cpp

Supports the use of the C++ language through the FFI
module deriving

nitc :: deriving

Injection of automatic method definitions for standard methods, based on the attributes of the classes
module detect_covariance

nitc :: detect_covariance

Detect the static usage of covariance in the code.
module detect_variance_constraints

nitc :: detect_variance_constraints

Collect metrics about detected variances constraints on formal types.
module div_by_zero

nitc :: div_by_zero

Detection of divisions by zero in obvious cases
module dynamic_loading_ffi

nitc :: dynamic_loading_ffi

Execute FFI code by creating and loading shared libraries
module emscripten

nitc :: emscripten

Compile to JavaScript using the Emscripten SDK
module example_vsm

vsm :: example_vsm

Example using a FileIndex
module explain_assert

nitc :: explain_assert

Explain failed assert to the console by modifying the AST.
module extern_classes

nitc :: extern_classes

Manages all extern classes and their associated foreign type.
module extra_java_files

nitc :: extra_java_files

Intro the annotation extra_java_files to compile extra java files
module ffi

nitc :: ffi

Full FFI support, independent of the compiler
module ffi_base

nitc :: ffi_base

Tools and utilities for implement FFI with different languages
module flow

nitc :: flow

Intraprocedural static flow.
module frontend

nitc :: frontend

Collect and orchestration of main frontend phases
module generate_hierarchies

nitc :: generate_hierarchies

Create dot files for various hierarchies of a model.
module global_compiler

nitc :: global_compiler

Global compilation of a Nit program
module header_dependency

nitc :: header_dependency

Tracks which modules has public header code that must be imported
module highlight

nitc :: highlight

Highlighting of Nit AST
module html_commands

nitc :: html_commands

Render commands results as HTML
module html_model

nitc :: html_model

Translate mentities to html blocks.
module htmlight

nitc :: htmlight

Highlighting of Nit AST with HTML
module i18n_phase

nitc :: i18n_phase

Basic support of internationalization through the generation of id-to-string tables
module inheritance_metrics

nitc :: inheritance_metrics

Collect metrics about inheritance usage
module interpreter

nitc :: interpreter

Interpretation of Nit programs
module ios

nitc :: ios

Compile programs for the iOS platform
module java

nitc :: java

FFI support for the Java language
module java_compiler

nitc :: java_compiler

Compile Nit code to Java code
module json_commands

nitc :: json_commands

Translate command results to json
module json_model

nitc :: json_model

Make model entities Serializable.
module light

nitc :: light

Light FFI support for the compiler
module light_c

nitc :: light_c

Support for nesting C code within a Nit program using its FFI
module light_ffi

nitc :: light_ffi

Light FFI support, independent of the compiler
module light_ffi_base

nitc :: light_ffi_base

Tools and utilities for implement FFI with different languages
module light_only

nitc :: light_only

Compiler support for the light FFI only, detects unsupported usage of callbacks
module loader

github :: loader

module local_var_init

nitc :: local_var_init

Verify that local variables are initialized before their usage
module mclasses_metrics

nitc :: mclasses_metrics

Collect common metrics about mclasses
module md_commands

nitc :: md_commands

Render commands results as Markdown
module memory_logger

nitc :: memory_logger

Extension to inject memory-tracing instrumentation in code generated by nitc.
module mendel_metrics

nitc :: mendel_metrics

The Mendel model helps to understand class hierarchies.
module metrics

nitc :: metrics

Various statistics about Nit models and programs
module metrics_base

nitc :: metrics_base

Helpers for various statistics tools.
module mixin

nitc :: mixin

Loading and additional module refinements at link-time.
module mmodules_metrics

nitc :: mmodules_metrics

Collect common metrics about modules
module model_collect

nitc :: model_collect

Collect things from the model.
module model_contract

nitc :: model_contract

The abstract concept of a contract in the model
module model_hyperdoc

nitc :: model_hyperdoc

Dump of Nit model into hypertext human-readable format.
module model_index

nitc :: model_index

Search things from the Model
module model_visitor

nitc :: model_visitor

Simple visitor framework for Nit models.
module modelize

nitc :: modelize

Create a model from nit source files
module modelize_class

nitc :: modelize_class

Analysis and verification of class definitions to instantiate model element
module modelize_property

nitc :: modelize_property

Analysis and verification of property definitions to instantiate model element
module naive_interpreter

nitc :: naive_interpreter

Interpretation of a Nit program directly on the AST
module nit

nitc :: nit

A naive Nit interpreter
module nitc

nitc :: nitc

A Nit compiler
module nitcatalog

nitc :: nitcatalog

Basic catalog generator for Nit packages
module nitdoc

nitc :: nitdoc

Generator of static API documentation for the Nit language
module nith

nitc :: nith

A ligHt Nit compiler
module nitj

nitc :: nitj

Compile Nit into Java code runnable on the Java Virtual Machine.
module nitlight

nitc :: nitlight

Tool that produces highlighting for Nit programs
module nitls

nitc :: nitls

Simple tool to list Nit source files
module nitmd

markdown :: nitmd

A Markdown parser for Nit.
module nitmd

markdown2 :: nitmd

A Markdown parser for Nit.
module nitmetrics

nitc :: nitmetrics

A program that collects various metrics on nit programs and libraries
module nitni

nitc :: nitni

Native interface related services (used underneath the FFI)
module nitni_base

nitc :: nitni_base

Native interface related services (used underneath the FFI)
module nitni_callbacks

nitc :: nitni_callbacks

nitni services related to callbacks (used underneath the FFI)
module nitpackage

nitc :: nitpackage

Helpful features about packages
module nitpick

nitc :: nitpick

A program that collect potential style and code issues
module nitpretty

nitc :: nitpretty

module nitrestful

nitc :: nitrestful

Tool generating boilerplate code linking RESTful actions to Nit methods
module nitsaf

nitc :: nitsaf

Nit Static Analysis Framework client example.
module nitserial

nitc :: nitserial

Serialization support compiler, a tool to support deserialization of live generic types
module nitsmells

nitc :: nitsmells

module nituml

nitc :: nituml

UML generator in dot format.
module nitunit

nitc :: nitunit

Testing tool.
module nitvm

nitc :: nitvm

The Nit virtual machine launcher
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
module nitx

nitc :: nitx

nitx, a command tool that displays useful data about Nit code
module nlp_index

nlp :: nlp_index

Example showing how to use a NLPFileIndex.
module no_warning

nitc :: no_warning

Fill toolcontext information about blacklisting of warnings.
module nullables_metrics

nitc :: nullables_metrics

Statistics about the usage of nullables
module objc

nitc :: objc

FFI support for Objective-C
module on_demand_compiler

nitc :: on_demand_compiler

Compiles extern code within a module to a static library, as needed
module parallelization_phase

nitc :: parallelization_phase

Phase generating threads for functions annotated with threaded annotation
module parse_annotations

nitc :: parse_annotations

Simple annotation parsing
module pkgconfig

nitc :: pkgconfig

Offers the PkgconfigPhase to use the external program "pkg-config" in order
module platform

nitc :: platform

Platform system, used to customize the behavior of the compiler.
module pop_config

popcorn :: pop_config

Configuration file and options for Popcorn apps
module pop_repos

popcorn :: pop_repos

Repositories for data management.
module poset_metrics

nitc :: poset_metrics

Metrics about the various posets of the model of a Nit program
module pretty

nitc :: pretty

Library used to pretty print Nit code.
module rapid_type_analysis

nitc :: rapid_type_analysis

Rapid type analysis on the AST
module readme_metrics

nitc :: readme_metrics

Collect common metrics about README files
module refinement_metrics

nitc :: refinement_metrics

Collect metrics about refinement usage
module regex_phase

nitc :: regex_phase

Check for error in regular expressions from string literals
module rta_metrics

nitc :: rta_metrics

Metrics from RTA
module saf

nitc :: saf

Nit Static Analysis Framework.
module saf_base

nitc :: saf_base

Static Analysis Framework base
module scope

nitc :: scope

Identification and scoping of local variables and labels.
module self_metrics

nitc :: self_metrics

Metrics about the usage of explicit and implicit self
module semantize

nitc :: semantize

Process bodies of methods in regard with the model.
module separate_compiler

nitc :: separate_compiler

Separate compilation of a Nit program
module separate_erasure_compiler

nitc :: separate_erasure_compiler

Separate compilation of a Nit program with generic type erasure
module serialization_code_gen_phase

nitc :: serialization_code_gen_phase

Phase generating methods (code) to serialize Nit objects
module serialization_model_phase

nitc :: serialization_model_phase

Phase generating methods (model-only) to serialize Nit objects
module ssa

nitc :: ssa

Single-Static Assignment algorithm from an AST
module static

nitc :: static

Nitdoc generation framework
module static_base

nitc :: static_base

Base entities shared by all the nitdoc code
module static_cards

nitc :: static_cards

Cards templates for the static documentation
module static_html

nitc :: static_html

Render documentation pages as HTML
module static_index

nitc :: static_index

Manage indexing of Nit model for Nitdoc QuickSearch.
module static_structure

nitc :: static_structure

Composes the pages of the static documentation
module static_types_metrics

nitc :: static_types_metrics

Metrics on the usage of explicit static types.
module tables_metrics

nitc :: tables_metrics

Metrics on table generation
module term

nitc :: term

module term_model

nitc :: term_model

Markdown templates for Nit model MEntities.
module test_astbuilder

nitc :: test_astbuilder

Program used to test the clone method of the astbuilder tool
module test_highlight

nitc :: test_highlight

Program used to test the Nit highlighter
module test_model_visitor

nitc :: test_model_visitor

Example of model_visitor
module test_neo

nitc :: test_neo

Test for neo model saving and loading.
module test_phase

nitc :: test_phase

Stub for loading a runing phases on a bunch of modules
module test_test_phase

nitc :: test_test_phase

Example of simple module that aims to do some specific work on nit programs.
module testing

nitc :: testing

Test unit generation and execution for Nit.
module testing_base

nitc :: testing_base

Base options for testing tools.
module testing_doc

nitc :: testing_doc

Testing from code comments.
module testing_gen

nitc :: testing_gen

Test Suites generation.
module testing_suite

nitc :: testing_suite

Testing from external files.
module transform

nitc :: transform

Thansformations that simplify the AST of expressions
module typing

nitc :: typing

Intraprocedural resolution of static types and OO-services
module uml

nitc :: uml

Group head module for UML generation services
module uml_base

nitc :: uml_base

Exposes the base class for UML generation of a Model
module uml_class

nitc :: uml_class

Provides facilities of exporting a Model to a UML class diagram
module uml_module

nitc :: uml_module

Services for generation of a UML package diagram based on a Model
module variables_numbering

nitc :: variables_numbering

Handle all numbering operations related to local variables in the Nit virtual machine
module vim_autocomplete

nitc :: vim_autocomplete

Generate files used by the Vim plugin to autocomplete with doc
module virtual_machine

nitc :: virtual_machine

Implementation of the Nit virtual machine
module vm

nitc :: vm

Entry point of all vm components
module vm_optimizations

nitc :: vm_optimizations

Optimization of the nitvm
module xcode_templates

nitc :: xcode_templates

Templates and other services to create XCode projects
# Read and write INI configuration files
module ini

import core
intrude import core::collection::hash_collection

# Read and write INI configuration files
#
# In an INI file, properties (or keys) are associated to values thanks to the
# equals symbol (`=`).
# Properties may be grouped into section marked between brackets (`[` and `]`).
#
# ~~~
# var ini_string = """
# ; Example INI
# key=value1
# [section1]
# key=value2
# [section2]
# key=value3
# """
# ~~~
#
# The main class, `IniFile`, can be created from an INI string and allows easy
# access to its content.
#
# ~~~
# # Read INI from string
# var ini = new IniFile.from_string(ini_string)
#
# # Check keys presence
# assert ini.has_key("key")
# assert ini.has_key("section1.key")
# assert not ini.has_key("not.found")
#
# # Access values
# assert ini["key"] == "value1"
# assert ini["section2.key"] == "value3"
# assert ini["not.found"] == null
#
# # Access sections
# assert ini.sections.length == 2
# assert ini.section("section1")["key"] == "value2"
# ~~~
#
# `IniFile` can also be used to create new INI files from scratch, or edit
# existing ones through its API.
#
# ~~~
# # Create a new INI file and write it to disk
# ini = new IniFile
# ini["key"] = "value1"
# ini["section1.key"] = "value2"
# ini["section2.key"] = "value3"
# ini.write_to_file("my_config.ini")
#
# # Load the INI file from disk
# ini = new IniFile.from_file("my_config.ini")
# assert ini["key"] == "value1"
# assert ini["section1.key"] == "value2"
# assert ini["section2.key"] == "value3"
#
# "my_config.ini".to_path.delete
# ~~~
class IniFile
	super Writable
	super HashMap[String, nullable String]

	# Create a IniFile from a `string` content
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key1=value1
	# [section1]
	# key2=value2
	# """)
	# assert ini["key1"] == "value1"
	# assert ini["section1.key2"] == "value2"
	# ~~~
	#
	# See also `stop_on_first_error` and `errors`.
	init from_string(string: String, stop_on_first_error: nullable Bool) do
		init stop_on_first_error or else false
		load_string(string)
	end

	# Create a IniFile from a `file` content
	#
	# ~~~
	# """
	# key1=value1
	# [section1]
	# key2=value2
	# """.write_to_file("my_config.ini")
	#
	# var ini = new IniFile.from_file("my_config.ini")
	# assert ini["key1"] == "value1"
	# assert ini["section1.key2"] == "value2"
	#
	# "my_config.ini".to_path.delete
	# ~~~
	#
	# See also `stop_on_first_error` and `errors`.
	init from_file(file: String, stop_on_first_error: nullable Bool) do
		init stop_on_first_error or else false
		load_file(file)
	end

	# Sections composing this IniFile
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# [section1]
	# key1=value1
	# [ section 2 ]
	# key2=value2
	# """)
	# assert ini.sections.length == 2
	# assert ini.sections.first.name == "section1"
	# assert ini.sections.last.name == "section 2"
	# ~~~
	var sections = new Array[IniSection]

	# Get a section by its `name`
	#
	# Returns `null` if the section is not found.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# [section1]
	# key1=value1
	# [section2]
	# key2=value2
	# """)
	# assert ini.section("section1") isa IniSection
	# assert ini.section("section2").name == "section2"
	# assert ini.section("not.found") == null
	# ~~~
	fun section(name: String): nullable IniSection do
		for section in sections do
			if section.name == name then return section
		end
		return null
	end

	# Does this file contains no properties and no sections?
	#
	# ~~~
	# var ini = new IniFile.from_string("")
	# assert ini.is_empty
	#
	# ini = new IniFile.from_string("""
	# key=value
	# """)
	# assert not ini.is_empty
	#
	# ini = new IniFile.from_string("""
	# [section]
	# """)
	# assert not ini.is_empty
	# ~~~
	redef fun is_empty do return super and sections.is_empty

	# Is there a property located at `key`?
	#
	# Returns `true` if the `key` is not found of if its associated value is `null`.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key=value1
	# [section1]
	# key=value2
	# [section2]
	# key=value3
	# """)
	# assert ini.has_key("key")
	# assert ini.has_key("section1.key")
	# assert ini.has_key("section2.key")
	# assert not ini.has_key("section1")
	# assert not ini.has_key("not.found")
	# ~~~
	redef fun has_key(key) do return self[key] != null

	# Get the value associated with a property (`key`)
	#
	# Returns `null` if the key is not found.
	# Section properties can be accessed with the `.` notation.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key=value1
	# [section1]
	# key=value2
	# [section2]
	# key=value3
	# """)
	# assert ini["key"] == "value1"
	# assert ini["section1.key"] == "value2"
	# assert ini["section2.key"] == "value3"
	# assert ini["section1"] == null
	# assert ini["not.found"] == null
	# ~~~
	redef fun [](key) do
		if key == null then return null
		key = key.to_s.trim

		# Look in root
		var node = node_at(key)
		if node != null then return node.value

		# Look in sections
		for section in sections do
			# Matched if the section name is a prefix of the key
			if not key.has_prefix(section.name) then continue
			var skey = key.substring(section.name.length + 1, key.length)
			if section.has_key(skey) then return section[skey]
		end
		return null
	end

	# Set the `value` for the property locaated at `key`
	#
	# ~~~
	# var ini = new IniFile
	# ini["key"] = "value1"
	# ini["section1.key"] = "value2"
	# ini["section2.key"] = "value3"
	#
	# assert ini["key"] == "value1"
	# assert ini["section1.key"] == "value2"
	# assert ini["section2.key"] == "value3"
	# assert ini.section("section1").name == "section1"
	# assert ini.section("section2")["key"] == "value3"
	# ~~~
	redef fun []=(key, value) do
		if value == null then return
		var parts = key.split_once_on(".")

		# No dot notation, store value in root
		if parts.length == 1 then
			super(key.trim, value.trim)
			return
		end

		# First part matches a section, store value in it
		var section = self.section(parts.first.trim)
		if section != null then
			section[parts.last.trim] = value.trim
			return
		end

		# No section matched, create a new one and store value in it
		section = new IniSection(parts.first.trim)
		section[parts.last.trim] = value.trim
		sections.add section
	end

	# Flatten `self` and its subsection in a `Map` of keys => values
	#
	# Properties from section are prefixed with their section names with the
	# dot (`.`) notation.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key=value1
	# [section]
	# key=value2
	# """)
	# assert ini.flatten.join(", ", ": ") == "key: value1, section.key: value2"
	# ~~~
	fun flatten: Map[String, String] do
		var map = new HashMap[String, String]
		for key, value in self do
			if value == null then continue
			map[key] = value
		end
		for section in sections do
			for key, value in section do
				if value == null then continue
				map["{section.name}.{key}"] = value
			end
		end
		return map
	end

	# Write `self` to a `stream`
	#
	# Key with `null` values are ignored.
	# The empty string can be used to represent an empty value.
	#
	# ~~~
	# var ini = new IniFile
	# ini["key"] = "value1"
	# ini["key2"] = null
	# ini["key3"] = ""
	# ini["section1.key"] = "value2"
	# ini["section1.key2"] = null
	# ini["section2.key"] = "value3"
	#
	# var stream = new StringWriter
	# ini.write_to(stream)
	#
	# assert stream.to_s == """
	# key=value1
	# key3=
	# [section1]
	# key=value2
	# [section2]
	# key=value3
	# """
	# ~~~
	redef fun write_to(stream) do
		for key, value in self do
			if value == null then continue
			stream.write "{key}={value}\n"
		end
		for section in sections do
			stream.write "[{section.name}]\n"
			for key, value in section do
				if value == null then continue
				stream.write "{key}={value}\n"
			end
		end
	end

	# Read INI content from `string`
	#
	# ~~~
	# var ini = new IniFile
	# ini.load_string("""
	# section1.key1=value1
	# section1.key2=value2
	# [section2]
	# key=value3
	# """)
	# assert ini["section1.key1"] == "value1"
	# assert ini["section1.key2"] == "value2"
	# assert ini["section2.key"] == "value3"
	# ~~~
	#
	# Returns `true` if the parsing finished correctly.
	#
	# See also `stop_on_first_error` and `errors`.
	fun load_string(string: String): Bool do
		var stream = new StringReader(string)
		var last_section = null
		var was_error = false
		var i = 0
		while not stream.eof do
			i += 1
			var line = stream.read_line.trim
			if line.is_empty then
				continue
			else if line.has_prefix(";") then
				continue
			else if line.has_prefix("#") then
				continue
			else if line.has_prefix("[") then
				var section = new IniSection(line.substring(1, line.length - 2).trim)
				sections.add section
				last_section = section
				continue
			else
				var parts = line.split_once_on("=")
				if parts.length != 2 then
					# FIXME silent skip?
					# we definitely need exceptions...
					was_error = true
					errors.add new IniError("Unexpected string `{line}` at line {i}.")
					if stop_on_first_error then return was_error
					continue
				end
				var key = parts[0].trim
				var value = parts[1].trim

				if last_section != null then
					last_section[key] = value
				else
					self[key] = value
				end
			end
		end
		stream.close
		return was_error
	end

	# Load a `file` content as INI
	#
	# New properties will be appended to the `self`, existing properties will be
	# overwrote by the values contained in `file`.
	#
	# ~~~
	# var ini = new IniFile
	# ini["key1"] = "value1"
	# ini["key2"] = "value2"
	#
	# """
	# key2=changed
	# key3=added
	# """.write_to_file("load_config.ini")
	#
	# ini.load_file("load_config.ini")
	# assert ini["key1"] == "value1"
	# assert ini["key2"] == "changed"
	# assert ini["key3"] == "added"
	#
	# "load_config.ini".to_path.delete
	# ~~~
	#
	# The process fails silently if the file does not exist.
	#
	# ~~~
	# ini = new IniFile
	# ini.load_file("ini_not_found.ini")
	# assert ini.is_empty
	# ~~~
	#
	# Returns `true` if the parsing finished correctly.
	#
	# See also `stop_on_first_error` and `errors`.
	fun load_file(file: String): Bool do return load_string(file.to_path.read_all)

	# Stop parsing on the first error
	#
	# By default, `load_string` will skip unparsable properties so the string can
	# be loaded.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key1=value1
	# key2
	# key3=value3
	# """)
    #
	# assert ini.length == 2
	# assert ini["key1"] == "value1"
	# assert ini["key2"] == null
	# assert ini["key3"] == "value3"
	# ~~~
	#
	# Set `stop_on_first_error` to `true` to force the parsing to stop.
	#
	# ~~~
	# ini = new IniFile
	# ini.stop_on_first_error = true
	# ini.load_string("""
	# key1=value1
	# key2
	# key3=value3
	# """)
    #
	# assert ini.length == 1
	# assert ini["key1"] == "value1"
	# assert ini["key2"] == null
	# assert ini["key3"] == null
	# ~~~
	#
	# See also `errors`.
	var stop_on_first_error = false is optional, writable

	# Errors found during parsing
	#
	# Wathever the value of `stop_on_first_error`, errors from parsing a string
	# or a file are logged into `errors`.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key1=value1
	# key2
	# key3=value3
	# """)
    #
	# assert ini.errors.length == 1
	# assert ini.errors.first.message == "Unexpected string `key2` at line 2."
	# ~~~
	#
	# `errors` is not cleared between two parsing:
	#
	# ~~~
	# ini.load_string("""
	# key4
	# key5=value5
	# """)
    #
	# assert ini.errors.length == 2
	# assert ini.errors.last.message == "Unexpected string `key4` at line 1."
	# ~~~
	#
	# See also `stop_on_first_error`.
	var errors = new Array[IniError]
end

# A section in a IniFile
#
# Section properties values are strings associated keys.
# Sections cannot be nested.
#
# ~~~
# var section = new IniSection("section")
# section["key1"] = "value1"
# section["key2"] = "value2"
#
# assert section.length == 2
# assert section["key1"] == "value1"
# assert section["not.found"] == null
# assert section.join(", ", ": ") == "key1: value1, key2: value2"
#
# var i = 0
# for key, value in section do
#	assert key.has_prefix("key")
#	assert value.has_prefix("value")
#	i += 1
# end
# assert i == 2
# ~~~
class IniSection
	super HashMap[String, nullable String]

	# Section name
	var name: String

	# Get the value associated with `key`
	#
	# Returns `null` if the `key` is not found.
	#
	# ~~~
	# var section = new IniSection("section")
	# section["key"] = "value1"
	# section["sub.key"] = "value2"
	#
	# assert section["key"] == "value1"
	# assert section["sub.key"] == "value2"
	# assert section["not.found"] == null
	# ~~~
	redef fun [](key) do
		if not has_key(key) then return null
		return super
	end
end

# Error for `IniFile` parsing
class IniError
	super Error
end
lib/ini/ini.nit:15,1--558,3