prefered way for support legacy C API on char* and std::string

O

Olaf

Hi,

I wrap a legacy C library, e.g. the signature is

void set_error_buffer(char* buf);

where the buf length should be of length of 512 (it's defined).

Now I want to wrap it with std::string. What is the prefered way?

Thanks,
Olaf
 
V

Victor Bazarov

Olaf said:
I wrap a legacy C library, e.g. the signature is

void set_error_buffer(char* buf);

where the buf length should be of length of 512 (it's defined).

Now I want to wrap it with std::string. What is the prefered way?

What do you expect to accomplish with 'std::string'? Apparently,
the API you're using is going to store the pointer to that buffer
and fill it in at some point in the future, without even asking
you about it. You simply cannot rely on them to be doing the same
thing to a 'std::string' object.

Of course, you could try defining a global 'std::string' object,
making sure it has at least 512 bytes and then passing the pointer
returned from its 'data()' member to that 'set_error_buffer', but
it's a BAD IDEA(tm). What I'd probably do:

class ErrorBufferWrapper {
char* buffer;
ErrorBufferWrapper(const ErrorBufferWrapper&);
ErrorBufferWrapper& operator=(const ErrorBufferWrapper&);
public:
ErrorBufferWrapper() : buffer(new char[512]) {
set_error_buffer(buffer);
}
~ErrorBufferWrapper() { delete[] buffer; }
std::string asString() const { return std::string(buffer); }
} const globalErrorBuffer; // notice the 'const'

...
// somewhere in your code
if (thereWasAnError)
std::cerr << globalErrorBuffer.asString() << std::endl;
...

V
 
K

Kai-Uwe Bux

Olaf said:
Hi,

I wrap a legacy C library, e.g. the signature is

void set_error_buffer(char* buf);

where the buf length should be of length of 512 (it's defined).

Now I want to wrap it with std::string. What is the prefered way?

What does set_error_buffer() do to the buffer? Does it just read it and use
the info found to set some internal error_state (in this case, the
signature could/should use char const *)? Or does it write some internal
error information into buf?

In the first case, you can do something like:

void set_error_buffer( std::string const & str ) {
assert ( str.size() < 512 );
set_error_buffer( str.c_str() );
}

This makes str.size() < 512 part of the contract and it would be the clients
responsibility to make sure the condition is not violated. If that is not
what you want, you could throw an exception or truncate the string
internally.


In the other case, you could do

void set_error_buffer ( std::string & str ) {
char my_buffer [512];
set_error_buffer( &my_buffer );
str.assign( &my_buffer );
}

This assumes(!) that set_error_buffer will leave a 0-terminated string
whereever it writes.


Best

Kai-Uwe Bux
 
O

Olaf

What does set_error_buffer() do to the buffer? Does it just read it and use
the info found to set some internal error_state (in this case, the
signature could/should use char const *)? Or does it write some internal
error information into buf?

It write internals into.
In the first case, you can do something like:

void set_error_buffer( std::string const & str ) {
assert ( str.size() < 512 );
set_error_buffer( str.c_str() );
}

This was my first attempt; the function wants to write into the buffer
and c_str() returns a const reference.
void set_error_buffer ( std::string & str ) {
char my_buffer [512];
set_error_buffer( &my_buffer );
str.assign( &my_buffer );
}

This assumes(!) that set_error_buffer will leave a 0-terminated string
whereever it writes.

Yep,

Thanks,
Olaf
 
J

Jim Langston

Olaf said:
Hi,

I wrap a legacy C library, e.g. the signature is

void set_error_buffer(char* buf);

where the buf length should be of length of 512 (it's defined).

Now I want to wrap it with std::string. What is the prefered way?

Unfortunately, you don't have access to std::string's internal data.
However, you do have access to a std::vector's internal data.

I've toyed with using a std::vector instead of char array, but usually in my
classes I wind up using a char array anyway for this type of thing.
However, if the c-style function is reading from the data isntead of writing
to it, then I'll use std::string with .c_str()
 
R

Rolf Magnus

Olaf said:
It write internals into.


This was my first attempt; the function wants to write into the buffer
and c_str() returns a const reference.

It returns a pointer to const char, and it does that for a reason. The
returned buffer must not be written to. There is no safe way to make a C
function directly write into an std::string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top