diff --git a/extra/checksums/process/process-docs.factor b/extra/checksums/process/process-docs.factor index b0c4fb22ab..847a637712 100644 --- a/extra/checksums/process/process-docs.factor +++ b/extra/checksums/process/process-docs.factor @@ -1,13 +1,13 @@ ! Copyright (C) 2016 Alexander Ilin. ! See http://factorcode.org/license.txt for BSD license. -USING: checksums checksums.common destructors help.markup help.syntax -kernel strings ; +USING: ascii checksums checksums.common destructors help.markup +help.syntax kernel strings ; IN: checksums.process ABOUT: "checksums.process" ARTICLE: "checksums.process" "Checksumming with External Utilities" -"With the " { $vocab-link "checksums.process" } " vocabulary any console utility can be used to checksum data, provided it supports a certain interface: it should accept input data on STDIN and output result to STDOUT. The output should consist of the hexadecimal checksum string, terminated with \" *-\". For instance, all the checksums from the GNU CoreUtils package support this mode of operation as the default." +"With the " { $vocab-link "checksums.process" } " vocabulary any console utility can be used to checksum data, provided it supports a certain interface: it should accept input data on STDIN and output result to STDOUT. The output should consist of the hexadecimal checksum string, terminated with a " { $link blank? } " character. For instance, all the checksums from the GNU CoreUtils package support this mode of operation as the default." $nl "The " { $link checksum-process } " tuple holds a launch descriptor (see " { $link "io.launcher.descriptors" } ") of the utility, e.g. \"sha1sum\". When the " { $link initialize-checksum-state } " method is called on it, a new instance of the checksum utility is started in the background. In Factor it is represented by the " { $link process-state } " tuple. You can then use " { $link add-checksum-bytes } " to stream data to it. When done, call " { $link get-checksum } " to read the resulting checksum and dispose of the tuple in one step. If you want to cancel the work without calling " { $link get-checksum } ", you must " { $link dispose } " of the tuple so that the underlying process is terminated." $nl diff --git a/extra/checksums/process/process-tests.factor b/extra/checksums/process/process-tests.factor new file mode 100644 index 0000000000..7cf12cdb4e --- /dev/null +++ b/extra/checksums/process/process-tests.factor @@ -0,0 +1,9 @@ +! Copyright (C) 2016 Alexander Ilin. +! See http://factorcode.org/license.txt for BSD license. +USING: tools.test checksums.process ; +IN: checksums.process.tests + +{ "" } [ "" trim-hash ] unit-test +{ "" } [ " aoeu" trim-hash ] unit-test +{ "aoeu" } [ "aoeu" trim-hash ] unit-test +{ "aoeu" } [ "aoeu i" trim-hash ] unit-test diff --git a/extra/checksums/process/process.factor b/extra/checksums/process/process.factor index 53cae08841..381c901674 100644 --- a/extra/checksums/process/process.factor +++ b/extra/checksums/process/process.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2016 Alexander Ilin. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors checksums checksums.common combinators destructors -io io.encodings.binary io.launcher kernel math.parser sequences -strings ; +USING: accessors ascii checksums checksums.common combinators +destructors io io.encodings.binary io.launcher kernel math.parser +sequences strings ; IN: checksums.process TUPLE: checksum-process launch-desc ; @@ -20,14 +20,15 @@ M: process-state dispose* ( process-state -- ) M: process-state add-checksum-bytes ( process-state bytes -- process-state' ) over proc>> stream-write ; -: trim-hash ( str -- str' ) dup " *-" swap start head ; +: trim-hash ( str -- str' ) + dup empty? [ dup [ blank? ] find drop [ head ] when* ] unless ; M: process-state get-checksum ( checksum-state -- value ) dup result>> [ dup proc>> [ [ [ out>> dispose ] keep - stream-contents >string trim-hash hex-string>bytes + stream-contents trim-hash hex-string>bytes ] with-disposal ] [ B{ } clone ] if* [ >>result ] keep