unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* #define SOMETHING some_value
@ 2017-06-10  8:31 Catonano
  2017-06-10  9:51 ` Amirouche Boubekki
  2017-06-10 15:55 ` Matt Wette
  0 siblings, 2 replies; 10+ messages in thread
From: Catonano @ 2017-06-10  8:31 UTC (permalink / raw)
  To: guile-user

The Freexl documentation states that I should call freexl_get_info like this

freexl_get_info(&handler, FREEXL_BIFF_SHEET_COUNT, &outcome);

Now, FREEXL_BIFF_SHEET_COUNT is defined in the header file like this

/** Information query for BIFF sheet count */
#define FREEXL_BIFF_SHEET_COUNT        32010

But at the REPL, when I call my procedure like this

scheme@(freexl common)> (freexl-open "resources/Lavoro_P.xls")
$1 = #<pointer 0x1227f50>
scheme@(freexl common)> (freexl-get-info $1 32010)

I get

ERROR: Throw to key `get-info-error' with args `(error-code -3)'.

That is, an exception is being raised, as you can see in this excerpt of my
code

(define freexl-get-info
  (let* ((ptr     (freexl-func "freexl_get_info"))
         (proc    (pointer->procedure int ptr (list '* unsigned-int '*))))
    (lambda (handle-ptr what)
      (let* ((outcome-ptr (bytevector->pointer (make-bytevector (sizeof
unsigned-int))))
         (result (proc handle-ptr what outcome-ptr)))
    (if (not (= result 0))
        (throw 'get-info-error 'error-code result) <-- THIS IS THE
EXCEPTION BEING RAISED
        outcome-ptr)
    ))))

How do I deal with these #define'd things ?

I tried with both unsigned-int and uint16

Thanks in advance


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-10  8:31 #define SOMETHING some_value Catonano
@ 2017-06-10  9:51 ` Amirouche Boubekki
  2017-06-11 19:42   ` Mark H Weaver
  2017-06-10 15:55 ` Matt Wette
  1 sibling, 1 reply; 10+ messages in thread
From: Amirouche Boubekki @ 2017-06-10  9:51 UTC (permalink / raw)
  To: Catonano, guile-user

What I do is that I hardcode the define in scheme using simple define form

for instance, the following:

#define SOMETHING some_value

Becomes:

(define SOMETHING some_value)

I do that even for enums. But guile-squee has another point of view on this.



On Sat, Jun 10, 2017 at 10:31 AM Catonano <catonano@gmail.com> wrote:

> The Freexl documentation states that I should call freexl_get_info like
> this
>
> freexl_get_info(&handler, FREEXL_BIFF_SHEET_COUNT, &outcome);
>
> Now, FREEXL_BIFF_SHEET_COUNT is defined in the header file like this
>
> /** Information query for BIFF sheet count */
> #define FREEXL_BIFF_SHEET_COUNT        32010
>
> But at the REPL, when I call my procedure like this
>
> scheme@(freexl common)> (freexl-open "resources/Lavoro_P.xls")
> $1 = #<pointer 0x1227f50>
> scheme@(freexl common)> (freexl-get-info $1 32010)
>
> I get
>
> ERROR: Throw to key `get-info-error' with args `(error-code -3)'.
>
> That is, an exception is being raised, as you can see in this excerpt of my
> code
>
> (define freexl-get-info
>   (let* ((ptr     (freexl-func "freexl_get_info"))
>          (proc    (pointer->procedure int ptr (list '* unsigned-int '*))))
>     (lambda (handle-ptr what)
>       (let* ((outcome-ptr (bytevector->pointer (make-bytevector (sizeof
> unsigned-int))))
>          (result (proc handle-ptr what outcome-ptr)))
>     (if (not (= result 0))
>         (throw 'get-info-error 'error-code result) <-- THIS IS THE
> EXCEPTION BEING RAISED
>         outcome-ptr)
>     ))))
>
> How do I deal with these #define'd things ?
>
> I tried with both unsigned-int and uint16
>
> Thanks in advance
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-10  8:31 #define SOMETHING some_value Catonano
  2017-06-10  9:51 ` Amirouche Boubekki
@ 2017-06-10 15:55 ` Matt Wette
  2017-06-11 17:08   ` Catonano
  1 sibling, 1 reply; 10+ messages in thread
From: Matt Wette @ 2017-06-10 15:55 UTC (permalink / raw)
  To: Catonano; +Cc: guile-user


> On Jun 10, 2017, at 1:31 AM, Catonano <catonano@gmail.com> wrote:
> 
> The Freexl documentation states that I should call freexl_get_info like this
> 
> freexl_get_info(&handler, FREEXL_BIFF_SHEET_COUNT, &outcome);
> 
> Now, FREEXL_BIFF_SHEET_COUNT is defined in the header file like this
> 
> /** Information query for BIFF sheet count */
> #define FREEXL_BIFF_SHEET_COUNT        32010
> 
> But at the REPL, when I call my procedure like this
> 
> scheme@(freexl common)> (freexl-open "resources/Lavoro_P.xls")
> $1 = #<pointer 0x1227f50>
> scheme@(freexl common)> (freexl-get-info $1 32010)
> 
> I get
> 
> ERROR: Throw to key `get-info-error' with args `(error-code -3)'.
> 
> That is, an exception is being raised, as you can see in this excerpt of my
> code
> 
> (define freexl-get-info
>  (let* ((ptr     (freexl-func "freexl_get_info"))
>         (proc    (pointer->procedure int ptr (list '* unsigned-int '*))))
>    (lambda (handle-ptr what)
>      (let* ((outcome-ptr (bytevector->pointer (make-bytevector (sizeof
> unsigned-int))))
>         (result (proc handle-ptr what outcome-ptr)))
>    (if (not (= result 0))
>        (throw 'get-info-error 'error-code result) <-- THIS IS THE
> EXCEPTION BEING RAISED
>        outcome-ptr)
>    ))))
> 
> How do I deal with these #define'd things ?
> 
> I tried with both unsigned-int and uint16
> 
> Thanks in advance

Yea.  I may have steered you wrong before. (I didn’t look back.)  The argument signature might need to be (list ‘* unsigned-short ‘*) rather than your current one with unsigned-int.





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-10 15:55 ` Matt Wette
@ 2017-06-11 17:08   ` Catonano
  2017-06-11 20:06     ` Mark H Weaver
  0 siblings, 1 reply; 10+ messages in thread
From: Catonano @ 2017-06-11 17:08 UTC (permalink / raw)
  To: Matt Wette; +Cc: guile-user

2017-06-10 17:55 GMT+02:00 Matt Wette <matt.wette@gmail.com>:

>
>
>
Yea.  I may have steered you wrong before. (I didn’t look back.)  The
> argument signature might need to be (list ‘* unsigned-short ‘*) rather than
> your current one with unsigned-int.
>

I tried with no result :-/

Also, unsigned-short isn't mentioned in the guile manual
(API reference -> Foreign pointers -> Foreign types)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-10  9:51 ` Amirouche Boubekki
@ 2017-06-11 19:42   ` Mark H Weaver
  2017-06-11 20:07     ` Matt Wette
  2017-06-15 19:21     ` Matt Wette
  0 siblings, 2 replies; 10+ messages in thread
From: Mark H Weaver @ 2017-06-11 19:42 UTC (permalink / raw)
  To: Amirouche Boubekki; +Cc: guile-user

Amirouche Boubekki <amirouche.boubekki@gmail.com> writes:

> What I do is that I hardcode the define in scheme using simple define form
>
> for instance, the following:
>
> #define SOMETHING some_value
>
> Becomes:
>
> (define SOMETHING some_value)

Right.  Unfortunately, preprocessor macros are replaced with their
right-hand-sides in the first phase of C compilation (preprocessing),
and then forgotten.  These macros are not stored in the shared objects,
so it's simply not possible for us to retrieve them.

For this reason, I'm sorry to say that the association between SOMETHING
and some_value must be redundantly represented in your Guile bindings,
as Amirouche describes above.

It would be nice to have a tool to extract this information from .h
files automatically, but since there's no guarantee that the .h files
are present on the user's machine at run time, nor is there a robust way
to find those .h files, this would be a tool for your convenience as a
developer, and would still require you to redundantly store the output
of this tool in your Scheme sources.

I'm sorry that I don't have a better answer for you.

      Mark



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-11 17:08   ` Catonano
@ 2017-06-11 20:06     ` Mark H Weaver
  2017-06-11 20:32       ` Catonano
  0 siblings, 1 reply; 10+ messages in thread
From: Mark H Weaver @ 2017-06-11 20:06 UTC (permalink / raw)
  To: Catonano; +Cc: guile-user, Matt Wette

Catonano <catonano@gmail.com> writes:

> 2017-06-10 17:55 GMT+02:00 Matt Wette <matt.wette@gmail.com>:
>
> Yea.  I may have steered you wrong before. (I didn’t look back.)  The
>> argument signature might need to be (list ‘* unsigned-short ‘*) rather than
>> your current one with unsigned-int.

I agree with Matt that you should use 'unsigned-short' here, since
that's the type specified in the C function prototype.  Although it is
true that most platform ABIs will treat them equivalently for purposes
of argument passing, the C standards provide no guarantee of that as far
as I can tell.

> I tried with no result :-/

What does "no result" mean here?  For now, I will assume it means that
you get the same error as before:

  ERROR: Throw to key `get-info-error' with args `(error-code -3)'.

The -3 corresponds to:

  #define FREEXL_INVALID_HANDLE -3   /* Invalid xls_handle argument. */

So I would consider it likely that the problem is with the first
argument (the handle), not the second one (the unsigned short).

> Also, unsigned-short isn't mentioned in the guile manual
> (API reference -> Foreign pointers -> Foreign types)

Indeed, this is an omission in the manual.  Thanks for letting us know.

      Mark



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-11 19:42   ` Mark H Weaver
@ 2017-06-11 20:07     ` Matt Wette
  2017-06-15 19:21     ` Matt Wette
  1 sibling, 0 replies; 10+ messages in thread
From: Matt Wette @ 2017-06-11 20:07 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-user


> On Jun 11, 2017, at 12:42 PM, Mark H Weaver <mhw@netris.org> wrote:
> 
> Amirouche Boubekki <amirouche.boubekki@gmail.com> writes:
> 
>> What I do is that I hardcode the define in scheme using simple define form
>> 
>> for instance, the following:
>> 
>> #define SOMETHING some_value
>> 
>> Becomes:
>> 
>> (define SOMETHING some_value)
> 
> Right.  Unfortunately, preprocessor macros are replaced with their
> right-hand-sides in the first phase of C compilation (preprocessing),
> and then forgotten.  These macros are not stored in the shared objects,
> so it's simply not possible for us to retrieve them.
> 
> For this reason, I'm sorry to say that the association between SOMETHING
> and some_value must be redundantly represented in your Guile bindings,
> as Amirouche describes above.
> 
> It would be nice to have a tool to extract this information from .h
> files automatically, but since there's no guarantee that the .h files
> are present on the user's machine at run time, nor is there a robust way
> to find those .h files, this would be a tool for your convenience as a
> developer, and would still require you to redundantly store the output
> of this tool in your Scheme sources.
> 
> I'm sorry that I don't have a better answer for you.
> 
>      Mark
> 

I am working on a tool “ffi helper”  to automatically generate the ffi code.  The headers have to be present.  And ffi-helper will use pig-config to locate the header and libraries.  With respect to the #defines, I am working to autogenerate that, but I have to be able to filter out the wanted stuff and not the cruft, so it will be looking at simple defines (i.e., w/o args) and if those evaluate to a C constant (e.g., integer or string) then keep, otherwise toss.

Currently ffi-help will wrap enums.  From Cairo
;; typedef enum _cairo_status cairo_status_t;
(define wrap-cairo_status_t
  (let ((vnl '((0 . CAIRO_STATUS_SUCCESS)
               (1 . CAIRO_STATUS_NO_MEMORY)
               (2 . CAIRO_STATUS_INVALID_RESTORE)
               ...
               (38 . CAIRO_STATUS_JBIG2_GLOBAL_MISSING)
               (39 . CAIRO_STATUS_LAST_STATUS))))
    (lambda (name) (assq-ref vnl name))))
(define unwrap-cairo_status_t
  (let ((nvl '((CAIRO_STATUS_SUCCESS . 0)
               (CAIRO_STATUS_NO_MEMORY . 1)
               (CAIRO_STATUS_INVALID_RESTORE . 2)
               ...
               (CAIRO_STATUS_JBIG2_GLOBAL_MISSING . 38)
               (CAIRO_STATUS_LAST_STATUS . 39))))
    (lambda (name) (assq-ref nvl name))))
(export wrap-cairo_status_t unwrap-cairo_status_t)


It uses a helper file that looks like:

;; cairo.ffi                            -*- Scheme -*-
(use-modules (ffi-help))

(define-ffi-helper (cairo cairo)
  #:pkg-config "cairo"
  #:include "cairo-svg.h"
  #:library "libcairo"
  #:filter (lambda (path) (string=? "cairo" (substring path 0 5)))
  )




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-11 20:06     ` Mark H Weaver
@ 2017-06-11 20:32       ` Catonano
  0 siblings, 0 replies; 10+ messages in thread
From: Catonano @ 2017-06-11 20:32 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-user, Matt Wette

2017-06-11 22:06 GMT+02:00 Mark H Weaver <mhw@netris.org>:

> Catonano <catonano@gmail.com> writes:
>
> > 2017-06-10 17:55 GMT+02:00 Matt Wette <matt.wette@gmail.com>:
> >
> > Yea.  I may have steered you wrong before. (I didn’t look back.)  The
> >> argument signature might need to be (list ‘* unsigned-short ‘*) rather
> than
> >> your current one with unsigned-int.
>
> I agree with Matt that you should use 'unsigned-short' here, since
> that's the type specified in the C function prototype.  Although it is
> true that most platform ABIs will treat them equivalently for purposes
> of argument passing, the C standards provide no guarantee of that as far
> as I can tell.
>

yes, I corrected that to an unsigned-short


>
> > I tried with no result :-/
>
> What does "no result" mean here?  For now, I will assume it means that
> you get the same error as before:
>

yes, I meant I was gettiing te same error as before


>
>   ERROR: Throw to key `get-info-error' with args `(error-code -3)'.
>
> The -3 corresponds to:
>
>   #define FREEXL_INVALID_HANDLE -3   /* Invalid xls_handle argument. */
>

Right, I found that out a few minutes ago


>
> So I would consider it likely that the problem is with the first
> argument (the handle), not the second one (the unsigned short).
>

Spot on !

I was assuming that the error returned was FREEXL_INVALID_INFO_ARG

I was wrong, of course.

I realized this just a ffew minutes ago !

Sorry for the noiise :-/

Thanks Mark !


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-11 19:42   ` Mark H Weaver
  2017-06-11 20:07     ` Matt Wette
@ 2017-06-15 19:21     ` Matt Wette
  2017-06-16 16:37       ` Catonano
  1 sibling, 1 reply; 10+ messages in thread
From: Matt Wette @ 2017-06-15 19:21 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-user


> On Jun 11, 2017, at 12:42 PM, Mark H Weaver <mhw@netris.org> wrote:
> 
> Amirouche Boubekki <amirouche.boubekki@gmail.com> writes:
> 
>> What I do is that I hardcode the define in scheme using simple define form
>> 
>> for instance, the following:
>> 
>> #define SOMETHING some_value
>> 
>> Becomes:
>> 
>> (define SOMETHING some_value)
> 
> Right.  Unfortunately, preprocessor macros are replaced with their
> right-hand-sides in the first phase of C compilation (preprocessing),
> and then forgotten.  These macros are not stored in the shared objects,
> so it's simply not possible for us to retrieve them.
> 
> For this reason, I'm sorry to say that the association between SOMETHING
> and some_value must be redundantly represented in your Guile bindings,
> as Amirouche describes above.
> 
> It would be nice to have a tool to extract this information from .h
> files automatically, but since there's no guarantee that the .h files
> are present on the user's machine at run time, nor is there a robust way
> to find those .h files, this would be a tool for your convenience as a
> developer, and would still require you to redundantly store the output
> of this tool in your Scheme sources.
> 
> I'm sorry that I don't have a better answer for you.
> 
>      Mark
> 

I now have my ffi-helper automatically extracting #defines which are constants.
I process on cairo-svg.h + cairo.h and get the following.  I don’t think defining
and exporting all these as (define CAIRO_VERSION_MAJOR 1) etc is a good idea.

Comments on this method of providing access?

Matt

(define xxx-def-val
  (let ((deftab
	'((CAIRO_VERSION_MAJOR . 1)
	  (CAIRO_VERSION_MINOR . 14)
	  (CAIRO_VERSION_MICRO . 8)
	  (CAIRO_HAS_FC_FONT . 1)
	  (CAIRO_HAS_FT_FONT . 1)
	  (CAIRO_HAS_GOBJECT_FUNCTIONS . 1)
	  (CAIRO_HAS_IMAGE_SURFACE . 1)
	  (CAIRO_HAS_MIME_SURFACE . 1)
	  (CAIRO_HAS_OBSERVER_SURFACE . 1)
	  (CAIRO_HAS_PDF_SURFACE . 1)
	  (CAIRO_HAS_PNG_FUNCTIONS . 1)
	  (CAIRO_HAS_PS_SURFACE . 1)
	  (CAIRO_HAS_QUARTZ_FONT . 1)
	  (CAIRO_HAS_QUARTZ_IMAGE_SURFACE . 1)
	  (CAIRO_HAS_QUARTZ_SURFACE . 1)
	  (CAIRO_HAS_RECORDING_SURFACE . 1)
	  (CAIRO_HAS_SCRIPT_SURFACE . 1)
	  (CAIRO_HAS_SVG_SURFACE . 1)
	  (CAIRO_HAS_TEE_SURFACE . 1)
	  (CAIRO_HAS_USER_FONT . 1)
	  (CAIRO_HAS_XCB_SURFACE . 1)
	  (CAIRO_HAS_XLIB_SURFACE . 1)
	  (CAIRO_HAS_XLIB_XCB_FUNCTIONS . 1)
	  (CAIRO_HAS_XLIB_XRENDER_SURFACE . 1)
	  (CAIRO_HAS_XML_SURFACE . 1)
	  )))
    (lambda (k) (assq-ref deftab k))))





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: #define SOMETHING some_value
  2017-06-15 19:21     ` Matt Wette
@ 2017-06-16 16:37       ` Catonano
  0 siblings, 0 replies; 10+ messages in thread
From: Catonano @ 2017-06-16 16:37 UTC (permalink / raw)
  To: Matt Wette; +Cc: guile-user

2017-06-15 21:21 GMT+02:00 Matt Wette <matt.wette@gmail.com>:

> I now have my ffi-helper automatically extracting #defines which are
> constants.
> I process on cairo-svg.h + cairo.h and get the following.  I don’t think
> defining
> and exporting all these as (define CAIRO_VERSION_MAJOR 1) etc is a good
> idea.
>
> Comments on this method of providing access?
>

Why didn't you use enumerations like cwebber did in his guile-squee ?

Just curious

Anyway, your ffi-helper is very interesting

I wonder if it could be used on Gnunet to re-do the guile bindings from
scratch




> Matt
>
> (define xxx-def-val
>   (let ((deftab
>         '((CAIRO_VERSION_MAJOR . 1)
>           (CAIRO_VERSION_MINOR . 14)
>           (CAIRO_VERSION_MICRO . 8)
>           (CAIRO_HAS_FC_FONT . 1)
>           (CAIRO_HAS_FT_FONT . 1)
>           (CAIRO_HAS_GOBJECT_FUNCTIONS . 1)
>           (CAIRO_HAS_IMAGE_SURFACE . 1)
>           (CAIRO_HAS_MIME_SURFACE . 1)
>           (CAIRO_HAS_OBSERVER_SURFACE . 1)
>           (CAIRO_HAS_PDF_SURFACE . 1)
>           (CAIRO_HAS_PNG_FUNCTIONS . 1)
>           (CAIRO_HAS_PS_SURFACE . 1)
>           (CAIRO_HAS_QUARTZ_FONT . 1)
>           (CAIRO_HAS_QUARTZ_IMAGE_SURFACE . 1)
>           (CAIRO_HAS_QUARTZ_SURFACE . 1)
>           (CAIRO_HAS_RECORDING_SURFACE . 1)
>           (CAIRO_HAS_SCRIPT_SURFACE . 1)
>           (CAIRO_HAS_SVG_SURFACE . 1)
>           (CAIRO_HAS_TEE_SURFACE . 1)
>           (CAIRO_HAS_USER_FONT . 1)
>           (CAIRO_HAS_XCB_SURFACE . 1)
>           (CAIRO_HAS_XLIB_SURFACE . 1)
>           (CAIRO_HAS_XLIB_XCB_FUNCTIONS . 1)
>           (CAIRO_HAS_XLIB_XRENDER_SURFACE . 1)
>           (CAIRO_HAS_XML_SURFACE . 1)
>           )))
>     (lambda (k) (assq-ref deftab k))))
>
>
>
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-06-16 16:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-10  8:31 #define SOMETHING some_value Catonano
2017-06-10  9:51 ` Amirouche Boubekki
2017-06-11 19:42   ` Mark H Weaver
2017-06-11 20:07     ` Matt Wette
2017-06-15 19:21     ` Matt Wette
2017-06-16 16:37       ` Catonano
2017-06-10 15:55 ` Matt Wette
2017-06-11 17:08   ` Catonano
2017-06-11 20:06     ` Mark H Weaver
2017-06-11 20:32       ` Catonano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).