-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.scm
89 lines (72 loc) · 2.82 KB
/
util.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;;; Fvoxedit utilities
;; Copyright (C) 2005 Brailcom, o.p.s.
;; Author: Milan Zamazal <pdm@brailcom.org>
;; COPYRIGHT NOTICE
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
;; for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
(define-module (fvoxedit util)
:use-module (srfi srfi-1)
:use-module (srfi srfi-6)
:use-module (srfi srfi-13)
:use-module (ice-9 optargs)
:use-module (ice-9 rdelim)
:use-module (ice-9 regex)
:use-syntax (ice-9 syncase)
:export (push!))
(define-public (read-from-string str)
(let ((f (open-input-string str)))
(read f)))
(define-public (read-from-file file-name)
(let ((f (open-file file-name "r")))
(read f)))
(define-public (for-file-lines file function)
(if (access? file R_OK)
(let ((f (open-file file "r")))
(letrec ((next-line (lambda ()
(let ((line (read-line f)))
(if (eof-object? line)
#f
(or (function line)
(next-line)))))))
(next-line)))))
(define*-public (directory dir #:key pattern)
(let ((port (opendir dir))
(regexp (and pattern (make-regexp pattern))))
(letrec ((entries
(lambda (entries%)
(let ((entry (readdir port)))
(if (eof-object? entry)
entries%
(entries (if (or (not pattern)
(regexp-exec regexp entry))
(cons (string-append dir "/" entry) entries%)
entries%)))))))
(entries '()))))
(define-public (safe-take lst n)
(if (<= (length lst) n)
lst
(take lst n)))
;; `filter' is a built-in function in snd
;; taken from Guile
(define-public (filter* pred list)
(letrec ((filiter (lambda (pred rest result)
(if (null? rest)
(reverse! result)
(filiter pred (cdr rest)
(cond ((pred (car rest))
(cons (car rest) result))
(else
result)))))))
(filiter pred list '())))
(define-syntax push! (syntax-rules ()
((_ obj list-var)
(set! list-var (cons obj list-var)))))