|
|
@@ -3,7 +3,9 @@ module config.base; |
|
|
|
import config.lock : ConfigLock; |
|
|
|
import std.file : read, write; |
|
|
|
import std.stdio : File; |
|
|
|
import asdf; |
|
|
|
import jsonizer.tojson; |
|
|
|
import jsonizer.fromjson; |
|
|
|
import jsonizer.common; |
|
|
|
|
|
|
|
const string CONFIG_DIR = "/etc/horrific"; |
|
|
|
|
|
|
@@ -16,11 +18,9 @@ class Config(T) { |
|
|
|
|
|
|
|
this(string jsonFile) { |
|
|
|
this.jsonFile = jsonFile; |
|
|
|
Asdf asdf = File(jsonFile) |
|
|
|
.byChunk(4096) |
|
|
|
.parseJson(32); |
|
|
|
|
|
|
|
contents = deserialize!(T[])(asdf); |
|
|
|
|
|
|
|
JsonizeOptions opts; |
|
|
|
contents = fromJSONString!(T[])(jsonFile, opts); |
|
|
|
} |
|
|
|
|
|
|
|
public void append(T item) { |
|
|
@@ -30,15 +30,21 @@ class Config(T) { |
|
|
|
|
|
|
|
public void remove(T item) { |
|
|
|
this.lock.acquire(); |
|
|
|
this.contents.remove(item); |
|
|
|
foreach (i, entry; this.contents) { |
|
|
|
if (entry == item) { // hacky solution to remove a single item (std.algorithm is... difficult) |
|
|
|
this.contents = this.contents[0..i] ~ this.contents[i+1..$]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void write() { |
|
|
|
public void save() { |
|
|
|
if (!this.lock.hasLock) { |
|
|
|
throw new Exception("Tried to write without a file lock..."); |
|
|
|
} |
|
|
|
|
|
|
|
write(this.jsonFile, this.contents.serializeToJson()); |
|
|
|
write(this.jsonFile, this.contents.toJSONString()); |
|
|
|
} |
|
|
|
|
|
|
|
} |