Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ Add `[clojure-ini "0.0.2"]` as a dependency to your `project.clj`.
server=192.0.2.62
port=143
file = payroll.dat

[access-allowed]
users[]=mike
users[]=nike
users[]=joe
users[]=tom


REPL session:

> (use 'clojure-ini.core)
> (read-ini "conf.ini" :keywordize? true)
{:database {:file "payroll.dat"
{:access-allowed {:users ("tom" "joe" "nika" "mike")}
:database {:file "payroll.dat"
:port "143"
:server "192.0.2.62"}
:organization "Acme Widgets Inc."
Expand Down
29 changes: 26 additions & 3 deletions src/clojure_ini/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
(:require [clojure.string :as s]
[clojure.java.io :as io]))


(defn- is-list? [kword]
(let [rkw (reverse (name kword))]
(and (= (first rkw) \]) (= (second rkw) \[))))


(defn- strip-braces [kw]
(let [kword (name kw)]
(let [n (.indexOf kword "[")]
(if (not (neg? n))
(if (keyword? kw)
(keyword (subs kword 0 n))
(subs kword 0 n))))))


(defn- parse-line [s kw trim]
(if (= (first s) \[)
(-> s (subs 1 (.indexOf s "]")) trim kw)
Expand All @@ -11,6 +26,7 @@
[(-> s (subs 0 n) trim kw)
(-> s (subs (inc n)) trim)]))))


(defn- strip-comment [s chr allow-anywhere?]
(let [n (.indexOf s (int chr))]
(if (and (not (neg? n))
Expand All @@ -19,6 +35,7 @@
(subs s 0 n)
s)))


(defn- mapify [coll]
(loop [xs coll m {} key nil]
(if-let [x (first xs)]
Expand All @@ -27,14 +44,19 @@
(recur (rest xs)
(assoc m (first x) (second x))
key)
(recur (rest xs)
(assoc-in m [key (first x)] (second x))
key))
(if (is-list? (first x))
(recur (rest xs)
(update-in m [key (strip-braces (first x))] conj (second x))
key)
(recur (rest xs)
(assoc-in m [key (first x)] (second x))
key)))
(recur (rest xs)
(assoc m x {})
x))
m)))


(defn read-ini
"Read an .ini-file into a Clojure map.

Expand Down Expand Up @@ -63,3 +85,4 @@
(remove (fn [s] (every? #(Character/isWhitespace %) s)))
(map #(parse-line % kw trim))
mapify))))