[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: unsigned-int

From: Mark H Weaver
Subject: Re: unsigned-int
Date: Fri, 23 Jun 2017 08:37:07 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux)

Catonano <address@hidden> writes:

> 2017-06-22 21:13 GMT+02:00 Catonano <address@hidden>:
>> I apologize if my questions are naive.
>> I wrote my last scrap of C code in about 2004 and it never was my thing
>> Also the manual is a great reference but not a great tutorial and I'm not
> a great reader, probably.
> Something that is concerning me is that in the example  a "rows" variable
> and a "columns" variable get declared as
> unsigned int rows;
> unsigned short columns;
> and then they get passed as arguments to freexl_worksheet_dimensions as
> &rows, &columns
> like this
> ret = freexl_worksheet_dimensions (handle, &rows, &columns);
> In scheme I am NOT declaring anything because I don't now how to
> I am just passing void pointers to freexl_worksheet_dimensions

No, you need to allocate the memory where 'rows' and 'columns' will be
stored, and pass pointers to that memory.  freexl_worksheet_dimensions
will then store the values in those locations.

I looked at your code, and I see you've updated the code since you
posted these messages, but it's still not right:

> (define freexl-worksheet-dimensions
>   (let* ((ptr     (freexl-func "freexl_worksheet_dimensions"))
>          (proc    (pointer->procedure int ptr (list '* '* '*))))
>     (lambda (handle-ptr)
>       (let* ((rows-ptr (bytevector->pointer (make-bytevector (sizeof '*))))
>              (columns-ptr (bytevector->pointer (make-bytevector (sizeof '*))))

In the two lines above, instead of (sizeof '*), it should be
(sizeof unsigned-int) for the rows bytevector, and
(sizeof unsigned-short) for the columns bytevector.

>              (result (proc (unwrap-handler handle-ptr) rows-ptr columns-ptr)))
>         (if (not (= result 0))
>             (throw 'get-worksheet-dimensions-error 'error-code result)
>             ;;(list (dereference-pointer rows-ptr) (dereference-pointer 
> columns-ptr))
>             (list
>              (pointer->bytevector
>               rows-ptr (sizeof unsigned-int)) 
>              (pointer->bytevector
>               columns-ptr (sizeof unsigned-short))  
>               ))

This returns a list of two bytevectors.  Wouldn't it be better to return
the actual values?  Also, instead of using 'pointer->bytevector' here,
it would be better to have saved the bytevectors that you created in the
first place.  Maybe something like this (untested):

(define freexl-worksheet-dimensions
  (let* ((ptr     (freexl-func "freexl_worksheet_dimensions"))
         (proc    (pointer->procedure int ptr (list '* '* '*))))
    (lambda (handle-ptr)
      (let* ((rows-bv    (make-bytevector (sizeof unsigned-int)))
             (columns-bv (make-bytevector (sizeof unsigned-short)))
             (result     (proc (unwrap-handler handle-ptr)
                               (bytevector->pointer rows-bv)
                               (bytevector->pointer columns-bv))))
        (unless (zero? result)
          (throw 'get-worksheet-dimensions-error 'error-code result))
        (list (bytevector-uint-ref rows-bv 0 (native-endianness)
                                   (sizeof unsigned-int))
              (bytevector-uint-ref columns-bv 0 (native-endianness)
                                   (sizeof unsigned-short)))))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]