summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--share/txr/stdlib/getput.tl33
-rw-r--r--txr.124
2 files changed, 42 insertions, 15 deletions
diff --git a/share/txr/stdlib/getput.tl b/share/txr/stdlib/getput.tl
index 61523d8e..a50ee109 100644
--- a/share/txr/stdlib/getput.tl
+++ b/share/txr/stdlib/getput.tl
@@ -24,16 +24,25 @@
;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-(defun sys:get-buf-common (s)
- (let ((b (make-buf 4096))
+(defun sys:get-buf-common (s bytes seek)
+ (let ((b (make-buf 0 0 (min bytes 4096)))
(o 0))
- (while t
- (let ((p (fill-buf b o s)))
- (when (< p (len b))
- (buf-set-length b (if (plusp p) p o))
+ (when (plusp seek)
+ (unless (ignerr (seek-stream s seek :from-current))
+ (let ((b (make-buf (min seek 4096)))
+ (c 0))
+ (while (< c seek)
+ (let ((p (fill-buf b 0 s)))
+ (if (zerop p)
+ (return))
+ (inc c p))))))
+ (while (or (null bytes) (< (len b) bytes))
+ (let ((p (fill-buf-adjust b o s)))
+ (when (= p o)
(return))
- (buf-set-length b (+ p (len b)))
- (set o p)))
+ (set o p)
+ (when (eql p (buf-alloc-size b))
+ (buf-set-length b (min (+ p p) bytes)))))
b))
(defun file-get (name)
@@ -71,9 +80,9 @@
(with-stream (s (open-file name "a"))
(put-lines lines s)))
-(defun file-get-buf (name)
+(defun file-get-buf (name : bytes (seek 0))
(with-stream (s (open-file name "rb"))
- (sys:get-buf-common s)))
+ (sys:get-buf-common s bytes seek)))
(defun file-put-buf (name buf)
(with-stream (s (open-file name "wb"))
@@ -106,9 +115,9 @@
(with-stream (s (open-command cmd "w"))
(put-lines lines s)))
-(defun command-get-buf (cmd)
+(defun command-get-buf (cmd : bytes (skip 0))
(with-stream (s (open-command cmd "rb"))
- (sys:get-buf-common s)))
+ (sys:get-buf-common s bytes skip)))
(defun command-put-buf (cmd buf)
(with-stream (s (open-command cmd "wb"))
diff --git a/txr.1 b/txr.1
index c94e2fb0..25ce4255 100644
--- a/txr.1
+++ b/txr.1
@@ -24947,15 +24947,15 @@ adjusts the length of the buffer to match the position that is returned.
.coNP Functions @ file-get-buf and @ command-get-buf
.synb
-.mets (file-get-buf << name )
-.mets (command-get-buf << cmd )
+.mets (file-get-buf < name >> [ max-bytes <> [ skip-bytes ]])
+.mets (command-get-buf < cmd >> [ max-bytes <> [ skip-bytes ]])
.syne
.desc
The
.code file-get-buf
function opens a binary stream over the file indicated by the string argument
.meta name
-for reading. The entire file is read and its contents are returned as a
+for reading. By default, the entire file is read and its contents are returned as a
buffer object. The buffer's length corresponds to the number of bytes
read from the file.
@@ -24969,6 +24969,24 @@ as if by the
function. It read bytes from the pipe until the indication that no more
input is available. The bytes are returned aggregated into a buffer object.
+If the
+.meta max-bytes
+parameter is given an argument, it must be a non-negative integer.
+That value specifies a limit on the number of bytes to read. A buffer
+no longer than
+.meta max-bytes
+shall be returned.
+
+If the
+.meta skip-bytes
+parameter is given an argument, it must be a non-negative integer.
+That value specifies how many initial bytes of the input should be
+discarded before accumulation of the buffer begins.
+If possible, the semantics of this parameter is achieved by performing a
+.code seek-stream
+operation, falling back on reading and discarding bytes if the
+stream doesn't support seeking.
+
.coNP Functions @, file-put-buf @ file-append-buf and @ command-put-buf
.synb
.mets (file-put-buf < name << buf )