summaryrefslogtreecommitdiff
path: root/ldp/content.scm
diff options
context:
space:
mode:
Diffstat (limited to 'ldp/content.scm')
-rw-r--r--ldp/content.scm55
1 files changed, 55 insertions, 0 deletions
diff --git a/ldp/content.scm b/ldp/content.scm
new file mode 100644
index 0000000..57d4549
--- /dev/null
+++ b/ldp/content.scm
@@ -0,0 +1,55 @@
+(define-module (ldp content)
+ #:use-module (oop goops)
+ #:use-module (ice-9 binary-ports)
+ #:use-module (rnrs bytevectors)
+ #:use-module (rnrs))
+
+(define-class <content> ()
+ (port #:init-keyword #:port #:getter content-port)
+ (additional #:init-keyword #:additional #:getter content-additional))
+
+(define (the-boolean x)
+ (unless (boolean? x)
+ (scm-error 'wrong-type-arg
+ "the-boolean"
+ "Expected a boolean."
+ '()
+ (list x)))
+ x)
+
+(define (the-binary-port x)
+ (unless (binary-port? x)
+ (scm-error 'wrong-type-arg
+ "the-binary-port"
+ "Expected a binary port."
+ '()
+ (list x)))
+ x)
+
+(define (the-bytevector x)
+ (unless (bytevector? x)
+ (scm-error 'wrong-type-arg
+ "the-bytevector"
+ "Expected a bytevector."
+ '()
+ (list x)))
+ x)
+
+(define-public (make-content port additional)
+ (when (string? additional)
+ (set! additional (string->utf8 additional)))
+ (make <content>
+ #:port (the-binary-port port)
+ #:additional additional))
+
+(define-public (load-content content binary?)
+ (let ((left (get-bytevector-all (content-port content)))
+ (right (content-additional content)))
+ (let ((nl (bytevector-length left))
+ (nr (bytevector-length right)))
+ (let ((total (make-bytevector (+ nl nr))))
+ (bytevector-copy! left 0 total 0 nl)
+ (bytevector-copy! right 0 total nl nr)
+ (if binary?
+ total
+ (utf8->string total))))))