473,785 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_buffe r(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 1983
Olaf wrote:
I wrap a legacy C library, e.g. the signature is

void set_error_buffe r(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_buff er', but
it's a BAD IDEA(tm). What I'd probably do:

class ErrorBufferWrap per {
char* buffer;
ErrorBufferWrap per(const ErrorBufferWrap per&);
ErrorBufferWrap per& operator=(const ErrorBufferWrap per&);
public:
ErrorBufferWrap per() : buffer(new char[512]) {
set_error_buffe r(buffer);
}
~ErrorBufferWra pper() { delete[] buffer; }
std::string asString() const { return std::string(buf fer); }
} const globalErrorBuff er; // notice the 'const'

...
// somewhere in your code
if (thereWasAnErro r)
std::cerr << globalErrorBuff er.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_buffe r(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_buffe r() 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_buffe r( std::string const & str ) {
assert ( str.size() < 512 );
set_error_buffe r( 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_buffe r ( std::string & str ) {
char my_buffer [512];
set_error_buffe r( &my_buffer );
str.assign( &my_buffer );
}

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

Kai-Uwe Bux
Jul 22 '07 #3
What does set_error_buffe r() 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_buffe r( std::string const & str ) {
assert ( str.size() < 512 );
set_error_buffe r( 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_buffe r ( std::string & str ) {
char my_buffer [512];
set_error_buffe r( &my_buffer );
str.assign( &my_buffer );
}

This assumes(!) that set_error_buffe r will leave a 0-terminated string
whereever it writes.
Yep,

Thanks,
Olaf
Jul 22 '07 #4
"Olaf" <ol**@mdcc.dewr ote in message news:f7******** **@viper.mdlink .de...
Hi,

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

void set_error_buffe r(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_buffe r() 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_buffe r( std::string const & str ) {
assert ( str.size() < 512 );
set_error_buffe r( 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
3576
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
3411
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 locale correctly, say to it_IT.utf8 (on UNIX). However, when reading in text from a file using:
9
3302
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
1940
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 when compiled with 6.0 it contains string from Vector (that's how I want it to work). std::vector<std::string> Vector; ... void MyClass::DoThis(std::vector<std::string> Vector) { const char *arrString;
15
6905
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 implementations but I don't care. I would be happy to find ANY comparison at this point if it was valid and semi scientifically done.
33
3679
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 has prepared for those who love him" 1 Cor 2:9
2
10937
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 think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
3
9530
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++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
5
4576
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.