Skip to content

Do not clobber previous section during parsing #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/configparser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Section* = ref object
properties: Table[string, string]


proc setProperty*(this: Section, name: string, value: string) =
proc setProperty*(this: Section, name: string, value: sink string) =
this.properties[name] = value

proc newSection*() : Section =
Expand Down Expand Up @@ -43,8 +43,7 @@ proc hasSection*(this: Ini, name: string): bool =
proc deleteSection*(this: Ini, name:string) =
this.sections.del(name)

proc sectionsCount*(this: Ini) : int =
echo $this.sections
proc sectionsCount*(this: Ini) : int =
return len(this.sections)

proc hasProperty*(this: Ini, sectionName: string, key: string): bool=
Expand Down Expand Up @@ -90,8 +89,8 @@ proc parseIni*(s: string): Ini =
let lines = s.splitLines

var currentSectionName: string = ""
var currentSection = newSection()
var currentSection: Section

for rawLine in lines:
let line = rawLine.strip()
if line.strip() == "" or line.startsWith(";") or line.startsWith("#"):
Expand All @@ -104,25 +103,18 @@ proc parseIni*(s: string): Ini =

if state == readSection:
currentSectionName = line[1..<line.len-1]
currentSection = newSection()
ini.setSection(currentSectionName, currentSection)
state = readKV
continue

if state == readKV:
let parts = line.split({'='})
var parts = line.split({'='}, 1)
if len(parts) == 2:
let key = parts[0].strip()
let val = parts[1].strip()
ini.setProperty(currentSectionName, key, val)
elif len(parts) > 2:
let key = parts[0].strip()
let val = line.replace(key & " =", "").strip()
var
key = strip(move parts[0])
val = strip(move parts[1])
ini.setProperty(currentSectionName, key, val)
else:
raise newException(ValueError, fmt("Expected line {line} to have key = value"))
return ini