schemelib/bindings/utils.scm

23 lines
539 B
Scheme
Raw Normal View History

2020-07-09 14:20:59 +00:00
; Convert a char* to a Scheme string
; Extracted from this paper:
; http://scheme2011.ucombinator.org/papers/Keep2011.pdf
(define (char*->string fptr)
(let f ([i 0])
(let ([c (ftype-ref char () fptr i)])
(if (char=? c #\nul)
(make-string i)
(let ([str (f (fx+ i 1))])
(string-set! str i c)str)))))
(define (alloc size fx)
(let ([v (foreign-alloc size)])
(fx v)
(foreign-free v)))
(define (check-err ret msg)
(cond
((< ret 0)
(perror msg)
(raise msg))
(#t ret)))