How to treat a buffer

B

Barzo

Hi,

I would ask a suggestion about how to treat a buffer of char*.
I'm little bit confusing about the use of streams or strings.

If I have a char* buffer like this

const char* pkt = {0x07, 0x00, 0x22, 0x04, 0x00, 0x68, 0xAC,
0xE0 .... }

...and I have to parse it...which is the best method?
Using string I have method like substr() etc... but my buffer may
contains 0x00, so?

Tnx in advance.
Daniele.
 
B

Bob Hairgrove

Barzo said:
Hi,

I would ask a suggestion about how to treat a buffer of char*.
I'm little bit confusing about the use of streams or strings.

If I have a char* buffer like this

const char* pkt = {0x07, 0x00, 0x22, 0x04, 0x00, 0x68, 0xAC,
0xE0 .... }

..and I have to parse it...which is the best method?
Using string I have method like substr() etc... but my buffer may
contains 0x00, so?

Tnx in advance.
Daniele.

What exactly do you mean by "parse"?

If it is binary data and not text, you should probably use
std::vector<char>.
 
K

Kai-Uwe Bux

Barzo said:
Hi,

I would ask a suggestion about how to treat a buffer of char*.
I'm little bit confusing about the use of streams or strings.

If I have a char* buffer like this

const char* pkt = {0x07, 0x00, 0x22, 0x04, 0x00, 0x68, 0xAC,
0xE0 .... }

..and I have to parse it...which is the best method?

"Best" probably depends on your application.
Using string I have method like substr() etc... but my buffer may
contains 0x00, so?

(a) I think, std::string does not care about zero chars.

(b) The algorithms part of the library has the find() and the search()
family of functions. Thus, most of what std::string offers you can be done
on arbitrary sequences.


Best

Kai-Uwe Bux
 
B

Barzo

What exactly do you mean by "parse"?

For example, in my case, the buffer is a binary XML, and gave it in
input I have to produce an XML string in output..
If it is binary data and not text, you should probably use
std::vector<char>.

I didn't have tought. I'll look for it.

Tnx!
Daniele.
 
B

Bob Hairgrove

Barzo said:
For example, in my case, the buffer is a binary XML, and gave it in
input I have to produce an XML string in output..

I thought XML files were TEXT files... What do you mean by "binary XML"?
 
B

Barzo

(b) The algorithms part of the library has the find() and the search()
family of functions. Thus, most of what std::string offers you can be done
on arbitrary sequences.

Mmmm..yes!
There are a lot of methods, I know, maybe the problem is choose the
right one in every case! :)
I'm a newbie to STL and I don't know every thing yet..
Using vector<char> and algorithms maybe a good solution!

Thanks Kai.
Daniele.
 
J

Juha Nieminen

Barzo said:
For example, in my case, the buffer is a binary XML, and gave it in
input I have to produce an XML string in output..

XML containing 0x00? That would be broken XML.
 
K

Kai-Uwe Bux

Barzo said:
Mmmm..yes!
There are a lot of methods, I know, maybe the problem is choose the
right one in every case! :)
I'm a newbie to STL and I don't know every thing yet..

No sweat. I suggest you get a good book. If you have more specific problems,
you can get more specific advise here.

Using vector<char> and algorithms maybe a good solution!

Yes, that seems good. Just for completeness, I should mention that you could
also work with the buffer directly and use char* or char const * as the
iterator type. The STL algorithms would not care.

However, if you have a choice as to _how_ the buffer arises, you should go
Thanks Kai.

Uhm, it's "Kai-Uwe". I do not have a middle name.


Best

Kai-Uwe Bux
 
B

Barzo

No sweat. I suggest you get a good book. If you have more specific problems,
you can get more specific advise here.

Yes...in effect I have studied c++ at school but for 10 years I had to
use VB6.
Now, finally, I can take my books from which "The C++ Programming
Language (3rd edition)" Stroustrup.
Yes, that seems good. Just for completeness, I should mention that you could
also work with the buffer directly and use char* or char const * as the
iterator type. The STL algorithms would not care.

Mmm..sorry, but...in which sense?

Uhm, it's "Kai-Uwe". I do not have a middle name.

Ops...very sorry!
Thanks again, Kai-Uwe!

Daniele.
 
B

Barzo

I thought XML files were TEXT files... What do you mean by "binary XML"?

Binary XML (http://en.wikipedia.org/wiki/Binary_XML) is a way to
encode XML documents in binary forms where there are a limited (and
predefined) number of tags each encoded in a specific byte.
This is used to reduce a document size to trasmit it in a low-band
channel.
I work on a Motorola proprietary standard.

Regards,
Daniele.
 
B

Bob Hairgrove

Barzo said:
Binary XML (http://en.wikipedia.org/wiki/Binary_XML) is a way to
encode XML documents in binary forms where there are a limited (and
predefined) number of tags each encoded in a specific byte.
This is used to reduce a document size to trasmit it in a low-band
channel.
I work on a Motorola proprietary standard.

Regards,
Daniele.

Very interesting.

If you have to call functions in an external library to do the parsing,
passing a char* as argument to your buffer, you should know that the
C++ standard requires std::vector to allocate its elements in one
contiguous area of memory. That means that if you pass the address of
the first member to a function expecting a char*, the other elements can
be accessed just like a regular array.
 
B

Barzo

If you have to call functions in an external library to do the parsing,

No..I have to build the library that made the parsing and
convertion! :)
I built a library that receive a stream via RS232 from a device
converting it into XML and viceversa.
C++ standard requires std::vector to allocate its elements in one
contiguous area of memory.

Sorry, but...what do u mean with "requires"?

Cheers,
Daniele.
 
K

Kai-Uwe Bux

Barzo wrote:

[snip]
Mmm..sorry, but...in which sense?
[snip]

E.g., as follows:

#include <cstddef>

template < typename T, typename std::size_t N >
T * begin ( T (&arg) [N] ) {
return ( arg );
}

template < typename T, typename std::size_t N >
T * end ( T (&arg) [N] ) {
return ( arg + N );
}

// The following is useful for 0-terminated strings.
// It returns the pointer to the terminating 0.
template < typename T, typename std::size_t N >
T * last ( T (&arg) [N] ) {
return ( arg + N - 1 );
}

#include <algorithm>
#include <iostream>
#include <ostream>

int main ( void ) {
char const heystack [] = "hello world!";
char const needle [] = "world";
char const * where =
std::search( begin( heystack ), last( heystack ),
begin( needle ), last( needle ) );
if ( where != last( heystack ) ) {
std::cout << "needle found.\n";
} else {
std::cout << "needle not found.\n";
}
}

The key point is that pointers satisfy the requirements for random access
iterators. Since all algorithms are templated on the iterator types, one
can use raw pointers as iterators. It is rarely a good idea to not use
standard containers, but sometimes an API forces raw arrays upon you anyway
and then using pointers as iterators comes in handy.


Best

Kai-Uwe Bux
 
B

Bob Hairgrove

Barzo said:
Sorry, but...what do u mean with "requires"?

I should have said:

"The C++ standard requires conforming implementations [i.e. compiler
vendors who supply one of several different flavors of the STL] of
std::vector..." etc.

So if you use a C++ compiler which claims to be standards-conforming,
you can expect the std::vector to behave according to the ISO/ANSI C++
standards document which requires this.
 
J

James Kanze

XML containing 0x00? That would be broken XML.

If I understand correctly, the situation is that XML isn't at
all appropriate for what he's doing (e.g. because he needs a
compact representation), but for marketing reasons, they have to
use XML. So they define a compact binary format, and call it
binary XML. The only "XML", however, is in the name.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top