From bebd9e6adc2efdd6da81d5177698f069b9f4d96b Mon Sep 17 00:00:00 2001 From: darkf Date: Sun, 27 Oct 2013 02:02:26 -0700 Subject: [PATCH] add factoid serialization --- irc.lamb | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/irc.lamb b/irc.lamb index 8ce30b8..618b822 100644 --- a/irc.lamb +++ b/irc.lamb @@ -58,9 +58,6 @@ ircnick'("!"::xs, acc) -> acc. ircnick'(x::xs, acc) -> ircnick'(xs, acc + x). ircnick(str) -> ircnick'(str, ""). --- state stuff -initialState = ([]). - -- state getters getFactoids(state) -> do (factoids) = state; @@ -72,6 +69,26 @@ setFactoids(state, factoids) -> do (factoids) end. +-- factoid serialization +-- basic "key value" (space-separated) format +saveFactoids(factoids) -> do + file = fopen("factoids.txt", "w"); + map(\(k,v) -> fputstr(file, k + " " + v + "\n"), factoids); + fclose(file) +end. + +loadFactoids() -> do + file = fopen("factoids.txt", "r"); + fact = loop(\pairs -> + if feof(file) != true then do + line = fgetline(file); + pair = takeUntilSpace(line); + pair :: pairs + end else false, []); + fclose(file); + fact +end. + -- event handling say(chan, msg) -> fputstr(sock, "PRIVMSG " + chan + " :" + msg + "\r\n"). @@ -187,6 +204,9 @@ mainloop(state) -> else false. +putstrln("initializing"). +initialState = (loadFactoids()). + putstrln("beginning mainloop"). loop(mainloop, initialState). fclose(sock).