Is there a quick way to assign a set amount of memory to a string?

Previous topic - Next topic

Wampus

Is there a quick way to allocate a set amount of memory to a GLB string? I'm copying from memory directly to a string like this:-

Code (glbasic) Select
FUNCTION peekString$:mem%, BYREF str$, size%
INLINE
memcpy((char *) str_Str.c_str(), (char *) mem, size);
ENDINLINE
ENDFUNCTION


The above works great (afaik). However, the string must be of adequate size. Getting it to be the right size when there is a large amount of data is fairly crude right now. I use a loop with inc str$, "     (lots of space)   ".

More efficient way? This is hardly critical to say the least. I'm just curious.

Wampus

And btw I've tried strncpy(str_Str, (char *) mem, size); after extern "C" void *strncpy(char *destination, const char *source, size_t num ); but of course it wouldn't be that simple :)

Kitty Hello

Oh, that's bad!!!!

if you want to allocate a string inline, you muse use:

Code (glbasic) Select

FUNCTION poke%: mem%, BYREF str$, size%
INLINE
char* pChars = str_Str.getbuffer( __max_required_length__ );


// if your string ends with '\0':
//   str_Str.releasebuffer();
// otherwise:
str_Str.setlength( __new_length_of_your_string__ );
ENDINLINE
ENDFUNCTION