diff options
Diffstat (limited to 'Hosts.hs')
-rw-r--r-- | Hosts.hs | 38 |
1 files changed, 26 insertions, 12 deletions
@@ -9,6 +9,9 @@ module Hosts | |||
9 | , empty | 9 | , empty |
10 | , hasName | 10 | , hasName |
11 | , hasAddr | 11 | , hasAddr |
12 | , encode | ||
13 | , decode | ||
14 | , diff | ||
12 | ) where | 15 | ) where |
13 | 16 | ||
14 | import Data.Maybe | 17 | import Data.Maybe |
@@ -22,6 +25,7 @@ import qualified Data.ByteString.Lazy.Char8 as L | |||
22 | import System.IO.Unsafe (unsafePerformIO) | 25 | import System.IO.Unsafe (unsafePerformIO) |
23 | import Control.Exception as Exception (IOException(..),catch) | 26 | import Control.Exception as Exception (IOException(..),catch) |
24 | import Control.Applicative ( (<$>), (<*>) ) | 27 | import Control.Applicative ( (<$>), (<*>) ) |
28 | import Control.Monad (mplus) | ||
25 | import Network.Socket | 29 | import Network.Socket |
26 | 30 | ||
27 | handleIO_ h a = Exception.catch a (\(_ :: IOException) -> h) | 31 | handleIO_ h a = Exception.catch a (\(_ :: IOException) -> h) |
@@ -51,7 +55,9 @@ data Hosts = Hosts | |||
51 | } | 55 | } |
52 | 56 | ||
53 | instance Show Hosts where | 57 | instance Show Hosts where |
54 | show hosts = L.unpack . L.unlines . map snd . Map.assocs $ numline hosts | 58 | show = L.unpack . encode |
59 | |||
60 | encode = L.unlines . map snd . Map.assocs . numline | ||
55 | 61 | ||
56 | parseLine s = (addr,names) | 62 | parseLine s = (addr,names) |
57 | where | 63 | where |
@@ -71,6 +77,9 @@ empty = Hosts { lineCount = 0 | |||
71 | 77 | ||
72 | parseHosts fname = do | 78 | parseHosts fname = do |
73 | input <- L.readFile fname | 79 | input <- L.readFile fname |
80 | decode input | ||
81 | |||
82 | decode input = do | ||
74 | let ls = L.lines input | 83 | let ls = L.lines input |
75 | ans = map (\l->(parseLine l,l)) ls | 84 | ans = map (\l->(parseLine l,l)) ls |
76 | hosts = foldl' upd empty ans | 85 | hosts = foldl' upd empty ans |
@@ -150,14 +159,19 @@ assignNewName addr name hosts = | |||
150 | if hasName name hosts then hosts | 159 | if hasName name hosts then hosts |
151 | else assignName0 True addr name hosts | 160 | else assignName0 True addr name hosts |
152 | 161 | ||
153 | {- | 162 | -- Returns a list of bytestrings intended to show the |
154 | main = do | 163 | -- differences between the two host databases. It is |
155 | args <- getArgs | 164 | -- assumed that no lines are deleted, only altered or |
156 | let fname = args !! 0 | 165 | -- appended. |
157 | p <- parseHosts fname | 166 | diff :: Hosts -> Hosts -> [L.ByteString] |
158 | let addr = (fromJust $ inet_pton "fdee:0abe:1f80:31c7:d1af:bce0:0f6c:91d2") | 167 | diff as bs = cs |
159 | p' = assignName addr "bigshift" p | 168 | where |
160 | p'' = assignNewName addr "poopy" p' | 169 | [as',bs'] = map (L.lines . Hosts.encode) [as,bs] |
161 | putStr $ show p'' | 170 | ext xs = map Just xs ++ repeat Nothing |
162 | return () | 171 | ds = takeWhile (isJust . uncurry mplus) $ zip (ext as') (ext bs') |
163 | -} | 172 | es = filter (uncurry (/=)) ds |
173 | cs = do | ||
174 | (a,b) <- es | ||
175 | [a,b] <- return $ map maybeToList [a,b] | ||
176 | fmap ("- " <>) a ++ fmap ("+ " <>) b | ||
177 | |||