89 lines
3.1 KiB
Scheme
89 lines
3.1 KiB
Scheme
;;;; -*- mode: Scheme; indent-tabs-mode: nil; fill-column: 80; -*-
|
||
;;;;
|
||
;;;; Copyright © 2015 Rémi Delrue <asgeir@free.fr>
|
||
;;;;
|
||
;;;; 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 3 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, see <http://www.gnu.org/licenses/>.
|
||
|
||
(define-module (gnu gnunet binding-utils)
|
||
#:use-module (ice-9 match)
|
||
#:use-module ((srfi srfi-1) #:select (find))
|
||
#:use-module ((rnrs base) #:select (assert))
|
||
#:use-module (rnrs bytevectors)
|
||
#:use-module (system foreign)
|
||
#:export (getf
|
||
interleave
|
||
|
||
rassq
|
||
rassq-ref
|
||
rassv
|
||
rassv-ref
|
||
rassoc
|
||
rassoc-ref
|
||
|
||
string->pointer*
|
||
make-c-struct*))
|
||
|
||
(define (getf plist value)
|
||
(let ((entry (member value plist)))
|
||
(if entry (cadr entry) #f)))
|
||
|
||
;; reverse association lists
|
||
(define-syntax-rule (define-assoc name name-ref test)
|
||
(begin
|
||
(define (name alist value)
|
||
"Return the first entry in ALIST with the given VALUE."
|
||
(find (match-lambda ((_ . val) (test value val))) alist))
|
||
|
||
(define (name-ref alist value)
|
||
"Return the key from the first entry in ALIST with the given VALUE."
|
||
(car (or (name alist value) '(#f . #f))))))
|
||
|
||
(define-assoc rassq rassq-ref eq?)
|
||
(define-assoc rassv rassv-ref eqv?)
|
||
(define-assoc rassoc rassoc-ref equal?)
|
||
|
||
(define (interleave obj lst)
|
||
"Interleaves OBJ in LST.
|
||
|
||
(interleave '& '(kirk spock ahura sulu scotty))
|
||
⇒
|
||
(kirk & spock & ahura & sulu & scotty)"
|
||
(define (%interleave lst)
|
||
(if (null? lst)
|
||
lst
|
||
(cons obj (cons (car lst) (%interleave (cdr lst))))))
|
||
(if (or (null? lst) (null? (cdr lst)))
|
||
lst
|
||
(cons (car lst) (%interleave (cdr lst)))))
|
||
|
||
;;; FFI utilities
|
||
|
||
(define (string->pointer* string)
|
||
"Return a foreign pointer to a nul-terminated copy of STRING, or %null-pointer
|
||
if STRING is empty (\"\")."
|
||
(if (string=? "" string) %null-pointer (string->pointer string)))
|
||
|
||
(define (make-c-struct* types)
|
||
"Create a foreign pointer to a zeroed C struct from TYPES."
|
||
(assert (not (null? types)))
|
||
(letrec ((spec->zeros (lambda (spec)
|
||
(match spec
|
||
('* %null-pointer)
|
||
((? number?) 0)
|
||
((? list? lst) (map spec->zeros lst))
|
||
(_ (scm-error 'wrong-type-arg "make-c-struct*"
|
||
"Wrong argument in position 1: (… ~a …)"
|
||
(list spec) #f))))))
|
||
(make-c-struct types (map spec->zeros types))))
|