diff --git a/core/util/ProtoStream.cc b/core/util/ProtoStream.cc index 78978cc9..3e322aa7 100644 --- a/core/util/ProtoStream.cc +++ b/core/util/ProtoStream.cc @@ -2,9 +2,8 @@ using namespace std; -ProtoOStream::ProtoOStream(ostream* outfile) +ProtoOStream::ProtoOStream(ostream* outfile) : m_outfile(outfile) { - m_outfile = outfile; m_log.setDescription("ProtoStream"); m_log.showTime(false); } @@ -14,7 +13,7 @@ bool ProtoOStream::writeMessage(google::protobuf::Message* m) m_size = htonl(m->ByteSize()); m_outfile->write(reinterpret_cast(&m_size), sizeof(m_size)); - if(m_outfile->bad()) { + if (m_outfile->bad()) { m_log << "Could not write to file!" << endl; 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() { + m_infile->clear(); 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) -{ - if(m_infile->tellg() >= m_sizeOfInfile) { - return false; - } - +{ m_infile->read(reinterpret_cast(&m_size), sizeof(m_size)); + if (!m_infile->good()) + return false; 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]; m_infile->read(buf, m_size); + if (!m_infile->good()) + return false; std::string st(buf, m_size); m->ParseFromString(st); diff --git a/core/util/ProtoStream.hpp b/core/util/ProtoStream.hpp index 6a06d57f..e3665cc6 100644 --- a/core/util/ProtoStream.hpp +++ b/core/util/ProtoStream.hpp @@ -43,8 +43,6 @@ class ProtoOStream virtual ~ProtoOStream() {}; /** * 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. * @return Returns true if data was written. */ @@ -74,14 +72,10 @@ class ProtoIStream /** * Resets the position of the get pointer. After that getNext * delivers the first message again. - * This works only if the constructor was called wit a pointer - * of an istream. */ void reset(); /** * 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 * written. * @return Returns true if data was written to protobuf-message.