String Access



 DEVELOP > c-Plus-Plus > String Access

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Hakan"
Date: 23 Nov 2003 12:20:21 AM
Object: String Access
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?
If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...
TIA.
.

User: "=?iso-8859-1?Q?Juli=E1n?= Albo"

Title: Re: String Access 23 Nov 2003 04:41:05 AM
Hakan escribi=F3:

If none exist I have to work around it by getting the length of the buf=

fer

area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the=
character array...

None exist, the standard does not guarantee that the string are stored
in a continuous block of memory.
You can use a vector of char instead of manually allocated memory.
Current implementations use a block of memory, and a revision of the
standard will make mandatory this.
Regards.
.

User: "Gianni Mariani"

Title: Re: String Access 23 Nov 2003 12:54:37 AM
Hakan wrote:

I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

You give us little to go on.
Here is one idea.
#include <string>
#include <cassert>
void BustedApi( void *, int * length );
int const MAXIMUM_SIZE = 1024;
void MyWrapper( std::string & fill_me_in )
{
fill_me_in.resize( MAXIMUM_SIZE );
int length;
BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );
assert( length <= MAXIMUM_SIZE );
fill_me_in.resize( length );
}
.
User: "Hakan"

Title: Re: String Access 23 Nov 2003 10:07:13 PM
Thanks, worked great!
On 23 Nov 2003 06:54:37 GMT, Gianni Mariani wrote:

Hakan wrote:

I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...



You give us little to go on.

Here is one idea.


#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}

.
User: "Gianni Mariani"

Title: Re: String Access 24 Nov 2003 01:31:22 AM
Hakan wrote:

Thanks, worked great!

On 23 Nov 2003 06:54:37 GMT, Gianni Mariani wrote:


Hakan wrote:

I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...



You give us little to go on.

Here is one idea.


#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}

I believe that you need to read Julian Albo's post. I thought that the
upcoming "contiguous" requirement applied to strings as well as vectors.
In other words, this may not be portable across all compliant string
implementations - I have yet to meet one that this does not work for but
you'll find out soon enough.
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER