From b3b07a0566e394dcf1f52b3f216342ff1ab7342d Mon Sep 17 00:00:00 2001 From: retropikzel Date: Thu, 12 Feb 2026 06:29:53 +0200 Subject: [PATCH] Add mouth library --- retropikzel/mouth.scm | 26 ++++++++++++++++++++++++++ retropikzel/mouth.sld | 8 ++++++++ 2 files changed, 34 insertions(+) create mode 100644 retropikzel/mouth.scm create mode 100644 retropikzel/mouth.sld diff --git a/retropikzel/mouth.scm b/retropikzel/mouth.scm new file mode 100644 index 0000000..1545ed8 --- /dev/null +++ b/retropikzel/mouth.scm @@ -0,0 +1,26 @@ +(define bite-size 4000) + +(define (slurp file-path) + (when (not (string? file-path)) (error "slurp: file-path must be string" file-path)) + (when (not (file-exists? file-path)) (error "slurp: file-path does not exist" file-path)) + (letrec ((looper (lambda (str result) + (if (eof-object? str) + result + (looper (read-string bite-size) (string-append result str)))))) + (with-input-from-file file-path (lambda () (looper (read-string bite-size) ""))))) + +(define (spit file-path text . append?) + (when (not (string? file-path)) (error "spit: file-path must be string" file-path)) + (when (not (string? text)) (error "spit: text must be string" text)) + (when (and (not (null? append?)) + (not (boolean? (car append?)))) + (error "spit: append? must be boolean" (car append?))) + (let ((content (if (and (not (null? append?)) (equal? (car append?) #t)) + (slurp file-path) + ""))) + (when (file-exists? file-path) (delete-file file-path)) + (with-output-to-file + file-path + (lambda () + (display content) + (display text))))) diff --git a/retropikzel/mouth.sld b/retropikzel/mouth.sld new file mode 100644 index 0000000..9b9b254 --- /dev/null +++ b/retropikzel/mouth.sld @@ -0,0 +1,8 @@ +(define-library + (retropikzel mouth) + (import (scheme base) + (scheme write) + (scheme file)) + (export slurp + spit) + (include "mouth.scm"))