Re: Need an STL (std:string) API call example
- From: "Jeff F" <not@xxxxxxxxxxxx>
- Date: Thu, 31 Mar 2005 08:03:35 -0500
"Jeff F" <not@xxxxxxxxxxxx> wrote in message
news:%23qfhQGfNFHA.1500@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Olaf van der Spek" <OlafvdSpek@xxxxxxxxx> wrote in message
> news:eh2qMCeNFHA.3760@xxxxxxxxxxxxxxxxxxxxxxx
>> Joseph M. Newcomer wrote:
>>> I'm writing a little utility and thought I would include in it the code
>>> necessary to
>>> allocate a buffer and fill it up with an API call. The API call is one
>>> of those that takes
>>> a NULL, 0 pair and returns the length. For example, in MFC, I would do
>>>
>>> CString s;
>>> int length = SomeAPICall(..., NULL, 0);
>>> LPTSTR data = s.GetBuffer(length);
>>> SomeAPICall(...,data, length);
>>> s.ReleaseBuffer();
>>>
>>> what I want to write is
>>>
>>> std::string s;
>>> int length = SomeAPICall(..., NULL, 0);
>>> ...and then I'm stuck
>>
>> You can't do that with string. You could do it with vector though.
>
> Specifically(untested):
>
> std::vector<char> data( SomeAPICall( ..., NULL, 0 ) );
>
> SomeAPICall( ..., &data[0], data.size() );
>
> std::string stringdata( data.begin(), data.end() );
>
>
> Which, using boost could be encapsulated into a function:
>
> std::string StringFrom( const boost::function2<std::string,char*,size_t>&
> aFnc )
> {
> std::vector<char> data( aFnc( NULL, 0 ) );
>
> aFnc( &data[0], data.size() );
>
> return std::string stringdata( data.begin(), data.end() );
oops - cut and paste error:
return std::string( data.begin(), data.end() );
> }
>
> which could be called:
>
> std::string mystring = StringFrom( boost::bind( SomeApiCall, ..., _1,
> _2 ) );
>
> Jeff Flinn
>
.
- References:
- Need an STL (std:string) API call example
- From: Joseph M . Newcomer
- Re: Need an STL (std:string) API call example
- From: Olaf van der Spek
- Re: Need an STL (std:string) API call example
- From: Jeff F
- Need an STL (std:string) API call example
- Prev by Date: Re: Need an STL (std:string) API call example
- Next by Date: Re: OnContextMenu forwarding, pWnd changes
- Previous by thread: Re: Need an STL (std:string) API call example
- Next by thread: Re: Need an STL (std:string) API call example
- Index(es):
Relevant Pages
|