[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
C error-returning functions and Guile exceptions
From: |
Ludovic Courtès |
Subject: |
C error-returning functions and Guile exceptions |
Date: |
Fri, 11 Mar 2005 15:49:52 +0100 |
User-agent: |
Gnus/5.1007 (Gnus v5.10.7) Emacs/21.3 (gnu/linux) |
Hi,
I'd like to accommodate Scheme's functional style by wrapping
error-returning C functions into exception-raising Scheme functions.
Here's an example. While on the C side I have this:
/* Read at most AMOUNT bytes from FILE into BUFFER. Return in *READ
the amount of data actually read. On error, return non-zero. */
errcode_t read_stuff (file_t *file, char *buffer, size_t amount,
size_t *read);
I'd like it to translate into Scheme into something along the lines of:
(read-stuff file amount)
where (i) in the nominal case, a "buffer object" (or a string) is
returned (this corresponds to the BUFFER and READ arguments of the C
function) and (ii) in the error case, a Guile exception is raised that
corresponds to the error code returned by `read_stuff ()'.
So there are two things to do here:
1. Write a C function that wraps `read_stuff ()', checks the
error code returned and calls `scm_throw ()' if necessary;
2. Find a way to make the BUFFER/READ pair translate into a return
value of the Scheme function.
(1) is my main problem. For (2), I could obviously create a `buffer'
SMOB and use it as the return value type, but maybe there's a more
elegant way to do this?
Thanks for writing G-Wrap, it really rocks compared to SWIG. ;-)
Ludovic.
- C error-returning functions and Guile exceptions,
Ludovic Courtès <=