|
| 1 | +;;; SRFI 233 |
| 2 | +;;; INI files |
| 3 | +;;; |
| 4 | +;;; An INI file is a configuration file that consists of key-value pairs |
| 5 | +;;; for properties, and sections that group the properties. The name of |
| 6 | +;;; these configuration files comes from the filename extension INI, |
| 7 | +;;; short for initialization. The format has become an informal standard |
| 8 | +;;; in many contexts of configuration. This SRFI provides access to the |
| 9 | +;;; contents of an INI file. |
| 10 | +;;; |
| 11 | +;;; Author of spec: John Cowan |
| 12 | +;;; |
| 13 | +;;; Copyright © 2022 Arvydas Silanskas. All rights reserved. |
| 14 | +;;; |
| 15 | +;;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | +;;; of this software and associated documentation files (the "Software"), to deal |
| 17 | +;;; in the Software without restriction, including without limitation the rights |
| 18 | +;;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 19 | +;;; copies of the Software, and to permit persons to whom the Software is |
| 20 | +;;; furnished to do so, subject to the following conditions: |
| 21 | +;;; |
| 22 | +;;; The above copyright notice and this permission notice shall be included in all |
| 23 | +;;; copies or substantial portions of the Software. |
| 24 | +;;; |
| 25 | +;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | +;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 27 | +;;; FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 28 | +;;; COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 29 | +;;; IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | +;;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 31 | +;;; |
| 32 | +;;; Adaptation to LispKit |
| 33 | +;;; Copyright © 2023 Matthias Zenger. All rights reserved. |
| 34 | + |
| 35 | +(define-library (srfi 233) |
| 36 | + |
| 37 | + (export make-ini-file-generator |
| 38 | + make-ini-file-accumulator) |
| 39 | + |
| 40 | + (import (lispkit base)) |
| 41 | + |
| 42 | + (begin |
| 43 | + |
| 44 | + (define make-ini-file-accumulator |
| 45 | + (case-lambda |
| 46 | + ((port) |
| 47 | + (make-accumulator port #\= #\; |
| 48 | + )) |
| 49 | + ((port key-value-sep) |
| 50 | + (make-accumulator port key-value-sep #\; |
| 51 | + )) |
| 52 | + ((port key-value-sep comment-delim) |
| 53 | + (make-accumulator port key-value-sep comment-delim)))) |
| 54 | + |
| 55 | + (define (make-accumulator port key-value-sep comment-delim) |
| 56 | + (define current-section '||) |
| 57 | + |
| 58 | + (define (write-comment str) |
| 59 | + (display comment-delim port) |
| 60 | + (display " " port) |
| 61 | + (display str port) |
| 62 | + (newline port)) |
| 63 | + |
| 64 | + (define (write-data section key value) |
| 65 | + (unless (equal? section current-section) |
| 66 | + (set! current-section section) |
| 67 | + (display "[" port) |
| 68 | + (display section port) |
| 69 | + (display "]" port) |
| 70 | + (newline port)) |
| 71 | + (display key port) |
| 72 | + (display key-value-sep port) |
| 73 | + (display value port) |
| 74 | + (newline port)) |
| 75 | + |
| 76 | + (define (data-triple? arg) |
| 77 | + (and (list? arg) |
| 78 | + (= 3 (length arg)) |
| 79 | + (symbol? (list-ref arg 0)) |
| 80 | + (symbol? (list-ref arg 1)) |
| 81 | + (string? (list-ref arg 2)))) |
| 82 | + |
| 83 | + (lambda (arg) |
| 84 | + (cond |
| 85 | + ((eof-object? arg) (eof-object)) |
| 86 | + ((string? arg) (write-comment arg)) |
| 87 | + ((data-triple? arg) (apply write-data arg)) |
| 88 | + (else (error "Unexpected input"))))) |
| 89 | + |
| 90 | + (define make-ini-file-generator |
| 91 | + (case-lambda |
| 92 | + ((port) |
| 93 | + (make-generator port #\= #\; |
| 94 | + )) |
| 95 | + ((port key-value-sep) |
| 96 | + (make-generator port key-value-sep #\; |
| 97 | + )) |
| 98 | + ((port key-value-sep comment-delim) |
| 99 | + (make-generator port key-value-sep comment-delim)))) |
| 100 | + |
| 101 | + (define (make-generator port key-value-sep comment-delim) |
| 102 | + |
| 103 | + ;; remove space from both ends of string |
| 104 | + (define (trim str) |
| 105 | + (define (space? char) (char=? #\space char)) |
| 106 | + (let loop1 ((end (string-length str))) |
| 107 | + (if (and (> end 0) (space? (string-ref str (- end 1)))) |
| 108 | + (loop1 (- end 1)) |
| 109 | + (let loop2 ((start 0)) |
| 110 | + (if (and (< start end) (space? (string-ref str start))) |
| 111 | + (loop2 (+ start 1)) |
| 112 | + (substring str start end)))))) |
| 113 | + |
| 114 | + ;; return #t if the line is a comment, |
| 115 | + ;; #f otherwise |
| 116 | + (define (comment line) |
| 117 | + (define len (string-length line)) |
| 118 | + (let loop ((i 0)) |
| 119 | + (cond |
| 120 | + ((>= i len) #f) |
| 121 | + ((equal? (string-ref line i) #\space) (loop (+ 1 i))) |
| 122 | + ((equal? (string-ref line i) comment-delim) #t) |
| 123 | + (else #f)))) |
| 124 | + |
| 125 | + ;; return section name as a symbol if the line is a section declaration, |
| 126 | + ;; #f otherwise |
| 127 | + (define (section line) |
| 128 | + (define len (string-length line)) |
| 129 | + (cond |
| 130 | + ((= len 0) #f) |
| 131 | + ((not (char=? (string-ref line 0) #\[)) #f) |
| 132 | + (else (let loop ((i 0)) |
| 133 | + (cond |
| 134 | + ((>= i len) #f) |
| 135 | + ((and (= i (- len 1)) |
| 136 | + (char=? (string-ref line i) #\])) |
| 137 | + (string->symbol (substring line 1 (- len 1)))) |
| 138 | + (else (loop (+ i 1)))))))) |
| 139 | + |
| 140 | + ;; return pair of key and value |
| 141 | + ;; |
| 142 | + ;; if line has a separator char, |
| 143 | + ;; key is a (whitespace trimmed) symbol up to first separator char, |
| 144 | + ;; value is a (whitespace trimmed) string |
| 145 | + ;; |
| 146 | + ;; if line doesn't have a separator char |
| 147 | + ;; key is a #f, value is the entire line |
| 148 | + ;; |
| 149 | + ;; if separator char is encounted multiple times, first one is used |
| 150 | + ;; to distinguish key from value |
| 151 | + (define (key-value line) |
| 152 | + (define len (string-length line)) |
| 153 | + (let loop ((i 0)) |
| 154 | + (cond |
| 155 | + ;; line parsed without encountering a separator |
| 156 | + ((>= i len) |
| 157 | + (cons #f |
| 158 | + line)) |
| 159 | + ;; encountered a (first leftmost) separator |
| 160 | + ((char=? key-value-sep (string-ref line i)) |
| 161 | + (cons (string->symbol (trim (substring line 0 i))) |
| 162 | + (if (>= (+ i 1) len) |
| 163 | + "" |
| 164 | + (trim (substring line (+ i 1) len))))) |
| 165 | + (else |
| 166 | + (loop (+ i 1)))))) |
| 167 | + |
| 168 | + (define current-section '||) |
| 169 | + (define eof #f) |
| 170 | + |
| 171 | + (lambda () |
| 172 | + (or (and eof (eof-object)) |
| 173 | + (let loop () |
| 174 | + (define line (read-line port)) |
| 175 | + (if (eof-object? line) |
| 176 | + (begin |
| 177 | + (set! eof #t) |
| 178 | + (eof-object)) |
| 179 | + (let ((trimmed-line (trim line))) |
| 180 | + (cond |
| 181 | + ((= 0 (string-length trimmed-line)) |
| 182 | + (loop)) |
| 183 | + ((comment trimmed-line) |
| 184 | + (loop)) |
| 185 | + ((section trimmed-line) => |
| 186 | + (lambda (section) |
| 187 | + (set! current-section section) |
| 188 | + (loop))) |
| 189 | + ((key-value trimmed-line) => |
| 190 | + (lambda (key-value-pair) |
| 191 | + (list current-section |
| 192 | + (car key-value-pair) |
| 193 | + (cdr key-value-pair))))))))))) |
| 194 | + ) |
| 195 | +) |
0 commit comments