473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Jul 22 '07 #1
5 1962
Olaf wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 22 '07 #2
Olaf wrote:
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
Jul 22 '07 #3
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
Jul 22 '07 #4
"Olaf" <ol**@mdcc.dewrote in message news:f7**********@viper.mdlink.de...
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()
Jul 22 '07 #5
Olaf wrote:
>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.
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.

Jul 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
1
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the...
9
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
15
by: roberts.noah | last post by:
Are there any decent benchmarks of the difference between using c strings and std::string out there? Google isn't being friendly about it. Obviously this would be dependant on different...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
5
by: Ramesh | last post by:
Hi. Assuming I have a code snippet like below: #include <iostream> using namespace std; char Mac = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }; std::string csMac;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.