Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Re-included write-e-map.

  • Loading branch information...
commit bdd79e17862490c025d5505a4356f7d1694361f8 1 parent 7dfdf60
MayDaniel authored
Showing with 24 additions and 5 deletions.
  1. +2 −2 foo.bar
  2. +22 −3 src/clj_ini/core.clj
View
4 foo.bar
@@ -1,6 +1,6 @@
# Comment, line 1
# Comment, line 2
-:c = {:d 4, :e 5}
-:b = [2 3]
:a = 1
+:b = [2 3]
+:c = {:d 4, :e 5}
View
25 src/clj_ini/core.clj
@@ -8,14 +8,33 @@
[name]
(when-not (.exists (File. name)) (.createNewFile (File. name))) name)
-(defn read-map
+(defn clean-file
+ [name]
+ (spit (create-file name) ""))
+
+(defn read-map
"Constructs a Clojure hash-map from write-map dump. Creates the file
if it does not exist."
[file]
(let [contents (read-lines (create-file file))
- file-map (remove #(not (includes? % \=)) contents)]
+ file-map (remove #(not (includes? % \=)) contents)
+ meta-comments (take-while #(= (first %) \#) contents)]
(if (empty? file-map) {}
(loop [lines file-map acc {}]
- (if (empty? lines) acc
+ (if (empty? lines) (with-meta acc {:comments meta-comments})
(let [x (re-split #" = " (first lines) 2)]
(recur (rest lines) (assoc acc (read-string (first x)) (read-string (second x))))))))))
+
+(defn write-map
+ "Writes a (merge (read-map file) map) to a file, and takes optional comments metadata
+to include in the dump."
+ [file map]
+ (let [contents (read-map (create-file file))]
+ (clean-file file)
+ (if-let [meta-comments (:comments (meta map))]
+ (do
+ (doseq [line meta-comments]
+ (append-spit file (str "# " line \newline)))
+ (append-spit file \newline)))
+ (doseq [kv (merge contents map)]
+ (append-spit file (str (key kv) " = " (val kv) \newline)))))
Please sign in to comment.
Something went wrong with that request. Please try again.