need help converting java to c++

S

steven acer

hello,
i have a java app that constructs an xml from a specific file format
and vice versa.
i've been asked to convert it to c++, but im not an expert in c++,
actually im mere beginner you can say.
i got a couple of questions though:
1- is there any equivalent to java's Stream
classes(ByteArrayInputStream,InputStreamRedare,DataInputStream ..)
2- how to open a stream on a byte ( char) array in c++ in order to
read it as text. i have a byte array i need to strip off some bytes
from and then pass it to a text stream.
3- i know strings are ended in '\0' the null character in c++.in the
fil i want to parse, xml-to-be-elements are separated by '\0', how can
i split the read text into strings using '\0' as my delimiter without
confusing c++.
4- the most important : how to parse and construct xml in c++, is
there any api for that and where to find it?
5- last nut not least , since im targetting Windows , would it be
better to use unmanaged Visual C++ . does it have the types i am
looking for,and does it make it easy to accomplish the task ?
oh and i use g++ for windows.
Thanks
regards
 
P

peter koch

hello,
i have a java app that constructs an xml from a specific file format
and vice versa.
i've been asked to convert it to c++, but im not an expert in c++,
actually im mere beginner you can say.
I am not sure that is a fair "first" job.
i got a couple of questions though:
1- is there any equivalent to java's Stream
classes(ByteArrayInputStream,InputStreamRedare,DataInputStream ..)

Yes. For formatted IO you probably need an std::eek:fstream. For
unformatted/binary, you could use a streambuffer or the C file
iosystem. You better study that.
2- how to open a stream on a byte ( char) array in c++ in order to
read it as text. i have a byte array i need to strip off some bytes
from and then pass it to a text stream.
You should read the documentation and be more specific in your
question here. It's not difficult, but we can't answer without more
knowledge of your problem.
3- i know strings are ended in '\0' the null character in c++.
That is false, actually. C strings are null-terminated, C++ strings
are not.
in the
fil i want to parse, xml-to-be-elements are separated by '\0', how can
i split the read text into strings using '\0' as my delimiter without
confusing c++.
This again depends on your requirements.
4- the most important : how to parse and construct xml in c++, is
there any api for that and where to find it?
There are several nice xml-parsers for C++. A short google should give
you a candidate. Choosing one probably is more of a political decision
to be made by your company.
5- last nut not least , since im targetting Windows , would it be
better to use unmanaged Visual C++ . does it have the types i am
looking for,and does it make it easy to accomplish the task ?
oh and i use g++ for windows.
There is only one C++ - namely the language we discuss in this
newsgroup. Microsoft has created a new language which they for some
reason call "managed C++", but it is not C++. "Managed C++" might
likely be a fine language for programming, but it can't be used for
porting to C++.

/Peter
 
L

Lionel B

I am not sure that is a fair "first" job.

Yes. For formatted IO you probably need an std::eek:fstream.

(did you mean std::ifstream ?)
For unformatted/binary, you could use a streambuffer or the C file
iosystem. You better study that.

Easier to use one of the std::ifstream::get() functions, or better
std::ifstream::read(); but you may not need to use unformatted input - see
below.
You should read the documentation and be more specific in your
question here. It's not difficult, but we can't answer without more
knowledge of your problem.

That is false, actually. C strings are null-terminated, C++ strings
are not.

To be more precise: the C++ string class std::string is probably not
implemented using null-terminated character arrays (the standard does not
require this).

Perhaps you could use std::getline() specifying '\0' as delimiter to read
your xml elements straight into std::string's. You won't confuse C++ that
way, you won't have to mess around with memory management and your
elements end up in nice C++-style strings.

/.../
 
P

peter koch

On 13 Feb., 12:17, "steven acer" wrote: [snip]
Yes. For formatted IO you probably need an std::eek:fstream.

(did you mean std::ifstream ?) Yup.
For unformatted/binary, you could use a streambuffer or the C file
iosystem. You better study that.

Easier to use one of the std::ifstream::get() functions, or better
std::ifstream::read(); but you may not need to use unformatted input - see
below.

What is "easier" depends. My experience with binary IO in C++ has
either been a layered above C file io for simple IO jobs as the OP has
or going "to the metal" for IO with high performance requirements (or
large files: so far as I know MSVC still only supports IO for files up
to 2GB).
[snip]
To be more precise: the C++ string class std::string is probably not
implemented using null-terminated character arrays (the standard does not
require this).
The standard allows embedded nulls and also has other requirements
that forbids std::string to be implemented as a null-terminated
string.

[snip]

/Peter
 
S

steven acer

Thanks for the replies guys.
first i dont wanna study and delve into c++ because im a java
developer and im doing this conversion for *just 1 time*.besides it's
a really small app.so any help i can get is really appreciated.
ok let's see
To be more precise: the C++ string class std::string is probably not
implemented using null-terminated character arrays (the standard does not
require this).
Perhaps you could use std::getline() specifying '\0' as delimiter to read
your xml elements straight into std::string's. You won't confuse C++ that
way, you won't have to mess around with memory management and your
elements end up in nice C++-style strings.

i think you got me wrong . i have a .trs(whatever) file which i want
to read and generate an xml based upon its contents.
in this file i have data separated by null characters, in java i would
have done read the contents of the trs file into a string
and then myString.split("\0") would return me an array of tokens.is
there anything similar in c++ to split a string using a
delimiter( since strings do not end with "\0" no problem).

regarding question 2:
=====================an example of java code would clear things up : BufferedReader in=new
BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
data is an array of bytes (char). at the end of this statement i would
be reading text characters.that is what i meant by opening a text
stream on a byte array.can this be done in c++?
1 more thing is there a way to copy an array to another in c++ other
then looping through the array ?

thanks alot
 
L

Lionel B

On Tue, 13 Feb 2007 07:12:03 -0800, steven acer wrote:

[snip]
i think you got me wrong .

Not necessarily. Read on.
i have a .trs(whatever) file which i want
to read and generate an xml based upon its contents.
in this file i have data separated by null characters, in java i would
have done read the contents of the trs file into a string
and then myString.split("\0") would return me an array of tokens.is
there anything similar in c++ to split a string using a
delimiter( since strings do not end with "\0" no problem).

Code snippet off the top of my head (not tested!):

#include <ifstream>
#include <string>
#include <vector>

ifstream ifs(your_trs_file);
if (!ifs) ... // handle file open failure

std::string str;
std::vector<std::string> token;
while (!getline(ifs,str,'\0')) token.push_back(str);

now token is the i-th token from your file.

[snip]
 
S

steven acer

#include said:
#include <string>
#include <vector>

ifstream ifs(your_trs_file);
if (!ifs) ... // handle file open failure

std::string str;
std::vector<std::string> token;
while (!getline(ifs,str,'\0')) token.push_back(str);

now token is the i-th token from your file.


all right i was not that far , but what about consecutive nulls like
'\0\0' ?

i've googled for a c++ xml parser and found apache's xercesc-c++
(kinda like SAX), but it just parses xml as i've seen,do you know of
any that constructs or transforms xml ?

one more silly question - *sorry found nothing about it on the web* -
how to make c++ ( or the preprocessor) find the following include
#include <xercesc/dom/DOM.hpp> - or any other
does it have to point to some file on my filesystem , how does c++
look for it?
and regarding byte arrays, any suggestions ?


many thanks
 
J

John Harrison

steven said:
i've googled for a c++ xml parser and found apache's xercesc-c++
(kinda like SAX), but it just parses xml as i've seen,do you know of
any that constructs or transforms xml ?

I though xerces did that too.
one more silly question - *sorry found nothing about it on the web* -
how to make c++ ( or the preprocessor) find the following include
#include <xercesc/dom/DOM.hpp> - or any other
does it have to point to some file on my filesystem , how does c++
look for it?

C++ does not specify how the file is found. That depends on your
compiler. Consult your compiler docs.
and regarding byte arrays, any suggestions ?

In C++ you would use an std::istringstream which is a stream that reads
from a string, you would need to convert you byte array into a string
first. Simple enough unless you have character encoding issues (you seem
to use byte and char interchangeably so I'm not sure exactly what you need).

many thanks

john
 
L

Lionel B

#include <ifstream>
#include <string>
#include <vector>

ifstream ifs(your_trs_file);
if (!ifs) ... // handle file open failure

std::string str;
std::vector<std::string> token;
while (!getline(ifs,str,'\0')) token.push_back(str);

now token is the i-th token from your file.


all right i was not that far , but what about consecutive nulls like
'\0\0' ?


Well, how do you want to treat them? If there's nothing between delimiters
('\0' in this case) you will end up with a token comprising an empty
string; i.e. a string containing no characters, or of length zero, if you
like.

[...]
and regarding byte arrays, any suggestions ?

I'ts not clear to me what you want "byte arrays" *for* so it's difficult
to suggest anything... C++ certainly has arrays and I suspect that "byte"
probably translates to the C++ "char" type. But if all you want is
something that behaves like a string of characters then std::string is the
way to go. If you want to do formatted I/O with strings then you might
look at std::stringstream.
 
S

steven acer

I though xerces did that too.
can you please show me a small example of writing xml to a file.
C++ does not specify how the file is found. That depends on your
compiler. Consult your compiler docs.

i did . i'm usig g++ for windows.it says to use the -
I<include_files_diretory> option while compiling.
but it did not work !
it just could not find the files
i have in my header file
#include <xerces/dom/DOM.hpp>
on the command line typing : -Ic:\includes as an option for g++ never
works though the hierarchy xerces\dom\DOM.hpp is in c:\includes.
im starting to think i need an IDE for this small task.
do you know of any free IDE that provies intelliscence help, i'm using
the CDT plugin for eclipse but it's ot much of an environment.
 
L

Lionel B

On Wed, 14 Feb 2007 04:17:15 -0800, steven acer wrote:

[...]
i did . i'm usig g++ for windows.it says to use the -
I<include_files_diretory> option while compiling.
but it did not work !
it just could not find the files
i have in my header file
#include <xerces/dom/DOM.hpp>
on the command line typing : -Ic:\includes as an option for g++ never
works though the hierarchy xerces\dom\DOM.hpp is in c:\includes.

Maybe try specifying -Ic:/includes (with forward slash); I think g++ may
be happier with Unix-style paths.
im starting to think i need an IDE for this small task.

I suspect that might end up making your small task much larger...
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top