]> gitweb.factorcode.org Git - factor.git/blob - basis/etc-hosts/etc-hosts.factor
factor: qualifying blank?, random fixes, renamed test file
[factor.git] / basis / etc-hosts / etc-hosts.factor
1 USING: arrays assocs environment hashtables io.encodings.utf8
2 io.files io.pathnames kernel memoize sequences splitting system
3 unicode ;
4
5 IN: etc-hosts
6
7 HOOK: hosts-path os ( -- path )
8
9 M: windows hosts-path
10     "SystemRoot" os-env "System32/drivers/etc/hosts" append-path ;
11
12 M: unix hosts-path "/etc/hosts" ;
13
14 : parse-hosts ( path -- hosts )
15     utf8 file-lines
16     [ [ unicode:blank? ] trim ] map harvest
17     [ "#" head? ] reject
18     [
19         [ unicode:blank? ] split1-when
20         [ unicode:blank? ] split-when harvest
21     ] H{ } map>assoc ;
22
23 MEMO: system-hosts ( -- hosts ) hosts-path parse-hosts ;
24
25 : host>ips ( host -- ips )
26     system-hosts [ member? nip ] with assoc-filter keys ;
27
28 : ip>hosts ( ip -- hosts )
29     system-hosts at ;