aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2023-04-10 22:44:16 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2023-04-10 23:45:29 +0200
commit5e714a3068a3cfe9acdc28aa6bfe0c79589edf4d (patch)
tree9bdebf6f6c9af734e76af07a3c858b87e08603a5
parentAdd eval- and exec-state. (diff)
downloadcalp-5e714a3068a3cfe9acdc28aa6bfe0c79589edf4d.tar.gz
calp-5e714a3068a3cfe9acdc28aa6bfe0c79589edf4d.tar.xz
Add ->port.
-rw-r--r--doc/ref/guile/util.texi5
-rw-r--r--module/hnh/util/io.scm10
2 files changed, 14 insertions, 1 deletions
diff --git a/doc/ref/guile/util.texi b/doc/ref/guile/util.texi
index f60e2059..72aa30b1 100644
--- a/doc/ref/guile/util.texi
+++ b/doc/ref/guile/util.texi
@@ -372,6 +372,11 @@ the file, and @code{#f} otherwise.
@defun call-with-tmpfile proc [#:tmpl ``/tmp/file-XXXXXXX'']
@end defun
+@defun ->port port-or-strings
+If @var{port-or-string} is a port, return it directly. If it's a
+string, instead return an input string containing the strings content.
+@end defun
+
@c Is this even a procedure?
@defun read-file path
Open file at path, and return its content as a string.
diff --git a/module/hnh/util/io.scm b/module/hnh/util/io.scm
index d73a1de8..09900f8d 100644
--- a/module/hnh/util/io.scm
+++ b/module/hnh/util/io.scm
@@ -5,7 +5,8 @@
open-output-port
read-lines
with-atomic-output-to-file
- call-with-tmpfile))
+ call-with-tmpfile
+ ->port))
(define (open-input-port str)
(if (string=? "-" str)
@@ -72,3 +73,10 @@
(begin1
(proc port filename)
(close-port port))))))
+
+(define (->port port-or-string)
+ (cond ((port? port-or-string) port-or-string)
+ ((string? port-or-string) (open-input-string port-or-string))
+ (else (scm-error 'misc-error "->port"
+ "Not a port or string"
+ (list port-or-string) #f))))