request for get-bytes (to join get-int, get-string, etc)

Started by TedWalther, May 07, 2009, 07:59:29 PM

Previous topic - Next topic

TedWalther

I am writing a Postgres module modeled on the MySQL module.



Postgres sometimes sends binary data as a result of queries.



newlisp strings can contain binary data, right?



Although I could probably work out something with (append), would it make sense to have a (get-bytes addr nbytes) function in the base distribution?



My brain is too fried right now to implement it.



Also, it might be more efficient to use the underlying memcpy() function of C in a builtin (get-bytes ...) rather than me implementing it.  Although as my mind works, I do see I could do it in a line or two of code.  Once I take a break.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

Lutz

#1
This is how you can do it:


(set 'buffer (dup "00" nbytes))

(cpymem addr buffer nbytes)


the variable buffer has now the original binary content in the original byte order and with the correct size information.



And here is a slightly more complicated and slower method:


(set 'buffer (first (unpack (format "s%d" nbytes) addr)))

TedWalther

#2
Quote from: "Lutz"This is how you can do it:


(set 'buffer (dup "00" nbytes))

(cpymem addr buffer nbytes)


the variable buffer has now the original binary content in the original byte order and with the correct size information.



And here is a slightly more complicated and slower method:


(set 'buffer (first (unpack (format "s%d" nbytes) addr)))




Thanks Lutz.  Somehow I missed the cpymem function.



Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.