summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-07-31 15:30:53 -0400
committerjoe <joe@jerkface.net>2014-07-31 15:30:53 -0400
commit4891dd8cd8d2227158dc81a437865283305f82ac (patch)
tree113a911cb13c505dc687b1a425fa6410e6722be9
parentfa436b19b25a6a908a78bc6874f1adf3a70ea8a9 (diff)
implemented 'delete' for hosts utility
-rw-r--r--Hosts.hs19
-rw-r--r--hosts.hs2
2 files changed, 14 insertions, 7 deletions
diff --git a/Hosts.hs b/Hosts.hs
index 8d02cc3..5f09de1 100644
--- a/Hosts.hs
+++ b/Hosts.hs
@@ -9,6 +9,7 @@ module Hosts
9 , assignName 9 , assignName
10 , assignName' 10 , assignName'
11 , assignNewName 11 , assignNewName
12 , removeName
12 , inet_pton 13 , inet_pton
13 , inet_ntop 14 , inet_ntop
14 , empty 15 , empty
@@ -184,7 +185,18 @@ cannonizeName name line = scrubName f line
184 f ws = [name," "] ++ pre ++ drop 2 rst 185 f ws = [name," "] ++ pre ++ drop 2 rst
185 where 186 where
186 (pre,rst) = break (==name) ws 187 (pre,rst) = break (==name) ws
188
189removeName name hosts = hosts'
190 where
191 hosts' = scrubTrailingEmpties (maybe hosts (removeName0 name hosts) ns)
192 ns = Map.lookup name (namenum hosts)
193
187 194
195removeName0 name hosts nums = hosts
196 { namenum = Map.delete name (namenum hosts)
197 , numline = foldl' scrub (numline hosts) nums
198 }
199 where scrub m num = Map.adjust (scrubName $ filter (/=name)) num m
188 200
189assignName' :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts 201assignName' :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts
190assignName' iscannon addr name hosts = hosts' 202assignName' iscannon addr name hosts = hosts'
@@ -198,7 +210,7 @@ assignName' iscannon addr name hosts = hosts'
198 else hosts { numline = foldl' canonize (numline hosts) $ fromJust ns} 210 else hosts { numline = foldl' canonize (numline hosts) $ fromJust ns}
199 else 211 else
200 let hosts0 = -- remove name if it's present 212 let hosts0 = -- remove name if it's present
201 scrubTrailingEmpties $ maybe hosts (removeName hosts) ns 213 scrubTrailingEmpties $ maybe hosts (removeName0 name hosts) ns
202 ns' = fmap (filter $ 214 ns' = fmap (filter $
203 isLonerName 215 isLonerName
204 . fromJust 216 . fromJust
@@ -212,11 +224,6 @@ assignName' iscannon addr name hosts = hosts'
212 (appendName iscannon name hosts0) 224 (appendName iscannon name hosts0)
213 a 225 a
214 in hosts1 226 in hosts1
215 removeName hosts nums = hosts
216 { namenum = Map.delete name (namenum hosts)
217 , numline = foldl' scrub (numline hosts) nums
218 }
219 where scrub m num = Map.adjust (scrubName $ filter (/=name)) num m
220 newLine hosts = hosts 227 newLine hosts = hosts
221 { lineCount = cnt 228 { lineCount = cnt
222 , numline = Map.insert cnt line $ numline hosts 229 , numline = Map.insert cnt line $ numline hosts
diff --git a/hosts.hs b/hosts.hs
index 6c93b10..b73ecb3 100644
--- a/hosts.hs
+++ b/hosts.hs
@@ -65,7 +65,7 @@ grokPairing :: Bool -> Hosts -> (String,Either String SockAddr) -> Hosts
65grokPairing bCanon hosts (name,eaddr) = 65grokPairing bCanon hosts (name,eaddr) =
66 case eaddr of 66 case eaddr of
67 Right addr -> assignName' bCanon addr (L.pack name) hosts 67 Right addr -> assignName' bCanon addr (L.pack name) hosts
68 Left "delete" -> error "deletion not implemented." 68 Left "delete" -> removeName (L.pack name) hosts
69 69
70main = do 70main = do
71 args <- getArgs 71 args <- getArgs