diff options
Diffstat (limited to 'scm/ppag.scm')
-rwxr-xr-x | scm/ppag.scm | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/scm/ppag.scm b/scm/ppag.scm new file mode 100755 index 0000000..e53ca1a --- /dev/null +++ b/scm/ppag.scm @@ -0,0 +1,50 @@ +#!/usr/bin/env sh +exec guile -e '(@ (ppag) main)' -s "${0}" "${@}" +!# +(define-module (ppag)) + +(export main) + +(use-modules (ice-9 match)) +(use-modules (ice-9 pretty-print)) +(use-modules (ice-9 rdelim)) + +(define (read-file filename) + "Read the entire contents of the provided file + +Reads the file line by line, accumulating the result into a list. Stops when +reaching the EOF marker. `cons' will construct the list in reverse, reverse at +the end. + +We assume the file provided exists and is readable." + (define (iter fh acc) + (let ((line (read-line fh))) + (if (eof-object? line) + acc + (iter fh (cons line acc))))) + (let ((fh (open-input-file filename))) + (reverse (iter fh '())))) + +(define (json-encode-line line) + "JSON encode the provided line." + (string-join (append '("\"") (map json-encode-char (string->list line)) '("\"")) "")) + +(define (json-encode-char char) + "JSON encode character + +We need to take special care around quotes and backslack characters." + (cond ((eq? char #\") "\"") + ((eq? char #\\) "\\") + (else + (list->string (list char))))) + +(define (json-encode-file filename) + "JSON encode the file, line by line" + (map json-encode-line (read-file filename))) + +(define (main args) + (match args + ((_ input-file) + (display (string-join (json-encode-file input-file) ",\n")) + (newline)) + (_ (error "Unrecognized usage.")))) |