Config Parser and Builder
Parse a simple configuration file format and output all key-value pairs in the order they were defined.
The configuration file consists of:
- Comment lines: lines beginning with
#(possibly after leading whitespace) - Blank lines: lines with only whitespace
- Section headers: lines matching
[section_name]wheresection_nameis a sequence of letters, digits, underscores, and dots - Key-value pairs: lines matching
key = valuewherekeyis alphanumeric/underscore andvalueis the rest of the line after=, with leading and trailing whitespace trimmed
For each key-value pair, output section_path.key = value where section_path is the dot-separated path of the current section (empty if not in any section), and both the key and value have whitespace preserved as-is after initial trimming.
Input
The first line contains n (1 ≤ n ≤ 1000), the number of lines in the configuration file. The next n lines form the configuration file.
Each line is at most 200 characters and contains only printable ASCII characters (space through tilde).
Output
For each key-value pair encountered in order, print section_path.key = value on its own line. If not currently in a section, omit the leading dot (i.e., just key = value). The section path should reflect the most recent [section_header] line encountered before the key-value pair.
Notes
- Section names can contain dots to represent nested sections (e.g.,
[db.primary]). - Whitespace around the
=in a key-value pair is stripped from both the key and value. - Values retain any internal spaces or trailing spaces after the initial trim.
- Comment lines and blank lines do not produce output.
- If a key appears multiple times in the same section, each occurrence is output separately.
- Section headers and keys are case-sensitive.