ProtoStream updated

git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1296 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
hellwig
2012-06-04 15:20:32 +00:00
parent 70d6dae1e2
commit 15d659500b
2 changed files with 19 additions and 28 deletions

View File

@ -2,9 +2,8 @@
using namespace std; using namespace std;
ProtoOStream::ProtoOStream(ostream* outfile) ProtoOStream::ProtoOStream(ostream* outfile) : m_outfile(outfile)
{ {
m_outfile = outfile;
m_log.setDescription("ProtoStream"); m_log.setDescription("ProtoStream");
m_log.showTime(false); m_log.showTime(false);
} }
@ -14,7 +13,7 @@ bool ProtoOStream::writeMessage(google::protobuf::Message* m)
m_size = htonl(m->ByteSize()); m_size = htonl(m->ByteSize());
m_outfile->write(reinterpret_cast<char*>(&m_size), sizeof(m_size)); m_outfile->write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
if(m_outfile->bad()) { if (m_outfile->bad()) {
m_log << "Could not write to file!" << endl; m_log << "Could not write to file!" << endl;
return false; return false;
} }
@ -25,35 +24,33 @@ bool ProtoOStream::writeMessage(google::protobuf::Message* m)
} }
//PROTOISTREAM
ProtoIStream::ProtoIStream(istream* infile) : m_infile(infile)
{
m_log.setDescription("ProtoStream");
m_log.showTime(false);
}
void ProtoIStream:: reset() void ProtoIStream:: reset()
{ {
m_infile->clear();
m_infile->seekg(0,ios::beg); m_infile->seekg(0,ios::beg);
} }
//PROTOISTREAM
ProtoIStream::ProtoIStream(istream* infile)
{
m_infile = infile;
m_log.setDescription("ProtoStream");
m_log.showTime(false);
m_infile->seekg(0, ios::end);
m_sizeOfInfile = m_infile->tellg();
m_infile->seekg(0, ios::beg);
}
bool ProtoIStream::getNext(google::protobuf::Message* m) bool ProtoIStream::getNext(google::protobuf::Message* m)
{ {
if(m_infile->tellg() >= m_sizeOfInfile) {
return false;
}
m_infile->read(reinterpret_cast<char*>(&m_size), sizeof(m_size)); m_infile->read(reinterpret_cast<char*>(&m_size), sizeof(m_size));
if (!m_infile->good())
return false;
m_size = ntohl(m_size); m_size = ntohl(m_size);
//FIXME: This could be inefficient because for each data buf is
//allocated each time new
char *buf = new char[m_size]; char *buf = new char[m_size];
m_infile->read(buf, m_size); m_infile->read(buf, m_size);
if (!m_infile->good())
return false;
std::string st(buf, m_size); std::string st(buf, m_size);
m->ParseFromString(st); m->ParseFromString(st);

View File

@ -43,8 +43,6 @@ class ProtoOStream
virtual ~ProtoOStream() {}; virtual ~ProtoOStream() {};
/** /**
* Writes a message to a file. * Writes a message to a file.
* This works only if the constructor was called wit a pointer
* of an ostream.
* @param m The protobuf-message to be written. * @param m The protobuf-message to be written.
* @return Returns true if data was written. * @return Returns true if data was written.
*/ */
@ -74,14 +72,10 @@ class ProtoIStream
/** /**
* Resets the position of the get pointer. After that getNext * Resets the position of the get pointer. After that getNext
* delivers the first message again. * delivers the first message again.
* This works only if the constructor was called wit a pointer
* of an istream.
*/ */
void reset(); void reset();
/** /**
* Delivers the protobuf-messages sequentially from file. * Delivers the protobuf-messages sequentially from file.
* This works only if the constructor was called wit a pointer
* of an istream.
* @param m The protobuf-message in which, the data should be * @param m The protobuf-message in which, the data should be
* written. * written.
* @return Returns true if data was written to protobuf-message. * @return Returns true if data was written to protobuf-message.