[gambit-list] Converting (unsigned char *) into u8vector?

Taylor Venable taylor at metasyntax.net
Mon May 18 22:30:24 EDT 2009


I have some binary data which is stored in an (unsigned char *) and I
want to convert it into a u8vector.  So I tried writing a conversion
function like this:

___SCMOBJ BYTEARRAY_to_SCMOBJ (unsigned char *src, ___SCMOBJ *dst, int arg_num)
{
    unsigned char *srcp = src;
    unsigned int size;
    int i = 0;

    bcopy(src, &size, sizeof(unsigned int));
    src += sizeof(unsigned int);

    ___SCMOBJ ___err = ___FIX(___NO_ERR);
    ___BEGIN_ALLOC_U8VECTOR(size);

    for (i = 0; i < size; i += 1) {
        ___SCMOBJ scm_int;

        ___err = ___EXT(___U8_to_SCMOBJ) (src[i], &scm_int, arg_num);

        if (___err != ___FIX(___NO_ERR))
            return ___FIX(___UNKNOWN_ERR);

        ___ADD_U8VECTOR_ELEM(i, scm_int);
        ___EXT(___release_scmobj) (scm_int);
    }

    ___END_ALLOC_U8VECTOR(size);
    dst = ___GET_U8VECTOR(size);
    return ___FIX(___NO_ERR);
}

(This function will be used by macros to support c-define-type.  I'm
storing the size of the array in the first sizeof(int) bytes, like a
"Pascal string" does.)

By looking through gambit.h I found some macros which seem to apply to
making and manipulating u8vectors, such as ___ADD_U8VECTOR_ELEM.
However, they use a variable ___hp which isn't defined anywhere.  Is
this the right API to use for this sort of thing?  Or if not hopefully
somebody can share an example of this kind of conversion that I can
use as a guide.

Thanks,

-- 
Taylor Christopher Venable
http://real.metasyntax.net:2357/



More information about the Gambit-list mailing list