T
Tony Arcieri
[Note: parts of this message were removed to make it a legal post.]
I'm extracting data from a function in C which accepts a pointer to the
string to dump the data into.
I know the length beforehand, so what I need to do is create a new String of
a given length and pass the pointer to the String's buffer to function which
writes into it.
Currently I'm doing this:
str = rb_str_buf_new(length);
buffer_read(buf, RSTRING_PTR(str), length);
RSTRING(str)->as.heap.len = length; /* <-- something tells me this is bad
*/
RSTRING_PTR(str)[length] = '\0'; /* sentinel */
(this is intended for Ruby 1.9)
buffer_read is the function which writes data into the string's buffer.
The RSTRING(str)->as.heap.len = length bit was stolen from the STR_SET_LEN
macro in string.c, but this doesn't seem available in ruby.h
What's the proper way to do this sort of thing?
I'm extracting data from a function in C which accepts a pointer to the
string to dump the data into.
I know the length beforehand, so what I need to do is create a new String of
a given length and pass the pointer to the String's buffer to function which
writes into it.
Currently I'm doing this:
str = rb_str_buf_new(length);
buffer_read(buf, RSTRING_PTR(str), length);
RSTRING(str)->as.heap.len = length; /* <-- something tells me this is bad
*/
RSTRING_PTR(str)[length] = '\0'; /* sentinel */
(this is intended for Ruby 1.9)
buffer_read is the function which writes data into the string's buffer.
The RSTRING(str)->as.heap.len = length bit was stolen from the STR_SET_LEN
macro in string.c, but this doesn't seem available in ruby.h
What's the proper way to do this sort of thing?