! Copyright (C) 2016 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
-USING: alien.data command-line formatting io io.encodings
+USING: alien.data command-line io io.encodings
io.encodings.binary io.files kernel math math.bitwise
-math.vectors math.vectors.simd namespaces sequences
+math.parser math.vectors math.vectors.simd namespaces sequences
specialized-arrays ;
SPECIALIZED-ARRAY: uchar-16
input-stream get binary re-decode
[ count-lines ] with-input-stream* ;
+: print-wc ( n name/f -- )
+ [ number>string 8 CHAR: \s pad-head write ]
+ [ bl [ write ] when* ] bi* nl ;
+
PRIVATE>
: wc ( path -- n )
: run-wc ( -- )
command-line get [
- wc-stdin "%8d\n" printf
+ wc-stdin f print-wc
] [
- [ [ wc ] keep dupd "%8d %s\n" printf ] map
- dup length 1 > [ sum "%8d total\n" printf ] [ drop ] if
+ [
+ dup file-exists? [
+ [ wc ] keep dupd print-wc
+ ] [
+ write ": not found" print flush f
+ ] if
+ ] map sift dup length 1 > [ sum "total" print-wc ] [ drop ] if
] if-empty ;
MAIN: run-wc