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