From bd841c01de8a38f8498999df0eb516161e118894 Mon Sep 17 00:00:00 2001 From: Andrea Bulgarelli <bulgarelli@iasfbo.inaf.it> Date: Sun, 2 Mar 2014 14:03:34 +0100 Subject: [PATCH] added (1) decoding only section (bool onlySections), (2) PartOfPacket::decode() only on demand (3) ByteStream(ByteStreamPtr b0, dword start, dword end, bool memory_sharing) --- include/ByteStream.h | 4 ++++ include/Packet.h | 8 ++++---- include/PartOfPacket.h | 6 ++++++ include/SDFRBBlock.h | 2 +- include/SDFRBlock.h | 13 +++++++++++-- include/SourceDataField.h | 3 +++ src/ByteStream.cpp | 16 ++++++++++++++++ src/Packet.cpp | 18 +++++++++--------- src/PartOfPacket.cpp | 25 ++++++++++++++++++++----- src/SDFRBBlock.cpp | 4 +++- src/SDFRBlock.cpp | 20 ++++++++++++++++++-- 11 files changed, 95 insertions(+), 24 deletions(-) diff --git a/include/ByteStream.h b/include/ByteStream.h index 9920f83..02330e1 100644 --- a/include/ByteStream.h +++ b/include/ByteStream.h @@ -49,6 +49,10 @@ public: /// It's possibile to pass 0 as pointer. /// The mamory of byte* is allocated. ByteStream(ByteStreamPtr b0, ByteStreamPtr b1, ByteStreamPtr b2); + + /// Creates a new ByteStream from start to end + /// If end=-1 use the end of the b0 + ByteStream(ByteStreamPtr b0, dword start, dword end=-1, bool memory_sharing=true); ~ByteStream(); diff --git a/include/Packet.h b/include/Packet.h index 0e31e55..10808fb 100644 --- a/include/Packet.h +++ b/include/Packet.h @@ -70,14 +70,14 @@ public: /// \param packetHeader This is the header of the packet /// \param packetDataField This is the data field of the packet /// \post If return is true all the fields are set with the correct value. - virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField); + virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField, bool onlySections = false); /// Sets all the fields of the packet with correct value contained into the input ByteStream. /// \pre The structure of the stream must be loaded. /// \param prefix This is the prefix of the packet /// \param packet This is the packet /// \post If return is true all the fields are set with the correct value. - virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet); + virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet, bool onlySections = false); /// Verifies if within the ByteStream passed with arguments it's present a correct packet. /// \pre The structure of the stream must be loaded. @@ -92,7 +92,7 @@ public: /// Sets all the fields of the packet with correct value contained into the input ByteStream. /// \param stream A pointer to the stream of byte, with prefix and packet - bool setPacketValue(byte* stream); + bool setPacketValue(byte* stream, bool onlySections = false); /// Verifies if within the ByteStream passed with arguments it's present a correct packet. /// \pre The structure of the stream must be loaded. @@ -248,7 +248,7 @@ protected: bool setPacketValueDataFieldHeader(ByteStreamPtr packetDataField); - bool setPacketValueSourceDataField(ByteStreamPtr packetDataField); + bool setPacketValueSourceDataField(ByteStreamPtr packetDataField, bool onlySections = false); bool setPacketValueHeader(ByteStreamPtr header); diff --git a/include/PartOfPacket.h b/include/PartOfPacket.h index 4504664..fcd7616 100644 --- a/include/PartOfPacket.h +++ b/include/PartOfPacket.h @@ -74,6 +74,7 @@ public: /// \param index Represent the index in the list. virtual inline Field* getFields(word index) { + decode(); if(index < numberOfFields) return fields[index]; else @@ -84,6 +85,7 @@ public: /// \param index Represent the index in the list. virtual inline word getFieldValue(word index) { + decode(); if(index < numberOfFields) return fields[index]->value; else @@ -305,6 +307,10 @@ public: protected: + bool decode(); + + bool decoded; + /// Represent current stream reads from input. ByteStreamPtr stream; diff --git a/include/SDFRBBlock.h b/include/SDFRBBlock.h index 1de619e..181c2fc 100644 --- a/include/SDFRBBlock.h +++ b/include/SDFRBBlock.h @@ -150,7 +150,7 @@ public: virtual ByteStreamPtr generateStream(bool bigendian); - virtual bool setByteStream(ByteStreamPtr s); + virtual bool setByteStream(ByteStreamPtr s, bool onlySections = false); virtual ByteStreamPtr getByteStream() { diff --git a/include/SDFRBlock.h b/include/SDFRBlock.h index 994fff3..347b8fd 100644 --- a/include/SDFRBlock.h +++ b/include/SDFRBlock.h @@ -33,6 +33,8 @@ public: SDFRBlock(PartOfPacket* pop = 0); virtual ~SDFRBlock(); + + virtual bool loadFields(InputText& fp) throw(PacketException*); @@ -41,7 +43,12 @@ public: /// \param nblock the number of the block /// \param rBlockIndex the number of the rblock virtual SDFRBBlock* getBlock(word nblock, word rBlockIndex); - + + ///Get the fixed part of the source data field + virtual ByteStreamPtr getFixedPart() { return block[0].fixed.getByteStream(); }; + + ///Get the variable part of the source data field + //virtual ByteStreamPtr getVariablePart(); /// Returns a pointer of a field in the fixed part of this source data field. /// \param index Represent the index in the list. @@ -65,8 +72,10 @@ public: virtual dword getMaxDimension(); virtual dword getDimension(); + + virtual dword getDimensionFixedPart(); - virtual bool setByteStream(ByteStreamPtr s); + virtual bool setByteStream(ByteStreamPtr s, bool onlySections = false); virtual bool setOutputStream(ByteStreamPtr os, dword first); diff --git a/include/SourceDataField.h b/include/SourceDataField.h index 6897d37..9323502 100644 --- a/include/SourceDataField.h +++ b/include/SourceDataField.h @@ -32,6 +32,9 @@ public: SourceDataField(const char* sdfName = 0); virtual ~SourceDataField(); + + /// Sets the stream of byte. This method assigns the value of stream for each field of part of packet + virtual bool setByteStream(ByteStreamPtr s, bool onlySections = false) { PartOfPacket::setByteStream(s); }; /// Gets the total max dimension in bytes of source data field virtual dword getMaxDimension() = 0; diff --git a/src/ByteStream.cpp b/src/ByteStream.cpp index 672a208..f91cb33 100644 --- a/src/ByteStream.cpp +++ b/src/ByteStream.cpp @@ -75,6 +75,22 @@ PacketLib::ByteStream::ByteStream(byte* stream, dword dim, bool bigendian, bool mem_allocation_constructor = false; } +PacketLib::ByteStream::ByteStream(ByteStreamPtr b0, dword start, dword end, bool memory_sharing) { + mem_allocation_constructor = true; + + if(end == -1) + end = b0->getDimension(); + + byteInTheStream = end-start; + this->stream = b0->stream+start; + this->bigendian = b0->isBigendian(); + if(!memory_sharing) + swapWordIfStreamIsLittleEndian(); + + setMemoryAllocated(!memory_sharing); + mem_allocation_constructor = false; +} + PacketLib::ByteStream::ByteStream(ByteStreamPtr b0, ByteStreamPtr b1, ByteStreamPtr b2) { diff --git a/src/Packet.cpp b/src/Packet.cpp index 53c9307..bff385a 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -293,7 +293,7 @@ void Packet::printIdentifiers() } -bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField) +bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField, bool onlySections) { //cout << "@ " << packetDataField->getDimension() << endl; memByteStream(prefix, packetHeader, packetDataField); @@ -324,7 +324,7 @@ bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, By return false; } /// 5) - if(!setPacketValueSourceDataField(packetDataField)) + if(!setPacketValueSourceDataField(packetDataField, onlySections)) { PRINTERROR("Error in set packet value source data field"); return false; @@ -341,13 +341,13 @@ bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, By -bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet) +bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet, bool onlySections) { dword dimHeader = header->getDimension(); memByteStream(prefix, packet); tempHeader->setStream(packet, 0, dimHeader-1); tempDataField->setStream(packet, dimHeader, packet->getDimension()); - return setPacketValue(prefix, tempHeader, tempDataField); + return setPacketValue(prefix, tempHeader, tempDataField, onlySections); } @@ -596,7 +596,7 @@ bool Packet::setPacketValueDataFieldHeader(ByteStreamPtr packetDataField) } -bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField) +bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField, bool onlySections) { bool b; dword packetLength; @@ -624,7 +624,7 @@ bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField) b = tempPacketDataField->setStream(packetDataField, packetLength, packetLength+packetLength2-1); if(b) { - bool ret = dataField->sourceDataField->setByteStream(tempPacketDataField); + bool ret = dataField->sourceDataField->setByteStream(tempPacketDataField, onlySections); //word nrd = dataField->sourceDataField->getNumberOfRealDataBlock(); return ret; } @@ -806,10 +806,10 @@ bool Packet::verifyPacketValue(byte* stream) { return verifyPacketValue(prefix, packet); } -bool Packet::setPacketValue(byte* stream) { +bool Packet::setPacketValue(byte* stream, bool onlySections) { dword dimPre = 0; if(thereisprefix) - dimPre += dimPrefix; + dimPre = dimPrefix; ByteStreamPtr prefix = ByteStreamPtr(new ByteStream(stream, dimPre, bigendian)); dword dim = 0; @@ -820,6 +820,6 @@ bool Packet::setPacketValue(byte* stream) { dim += header->getPacketLength() + 1; ByteStreamPtr packet = ByteStreamPtr(new ByteStream(stream+dimPre, dim, bigendian)); - return setPacketValue(prefix, packet); + return setPacketValue(prefix, packet, onlySections); } diff --git a/src/PartOfPacket.cpp b/src/PartOfPacket.cpp index a2476ed..e97d4a6 100644 --- a/src/PartOfPacket.cpp +++ b/src/PartOfPacket.cpp @@ -29,6 +29,7 @@ PartOfPacket::PartOfPacket(const char* popName) numberOfFields = 0; fields = 0; outputstream = 0; + decoded = false; this->popName = (char*) popName; } @@ -203,8 +204,8 @@ MemoryBuffer* PartOfPacket::loadFieldsInBuffer(InputText & fp) bool PartOfPacket::setByteStream(ByteStreamPtr s) { - Field* ftemp; - + + decoded = false; /// If NULL is passed it exits if(s == NULL) return true; @@ -217,10 +218,21 @@ bool PartOfPacket::setByteStream(ByteStreamPtr s) /// The stream is assigned this->stream = s; + + //decode(); + return true; +} + +bool PartOfPacket::decode() { + if(decoded) + return true; + Field* ftemp; //this->stream->setStream(s, 0, s->getDimension() - 1); /// The pointer is converted from byte to void. The reading from file allows the correct data interpretation /// for big or little endian machines - byte* stream = (byte*) s->stream; + byte* stream = (byte*) this->stream->stream; + if(stream == NULL) + return false; /// It indicates the position inside the word: byte posbit = 0; /// It indicates the word to be analyzed inside the stream @@ -242,7 +254,7 @@ bool PartOfPacket::setByteStream(ByteStreamPtr s) byte bl = *(stream + posword + 1); //word wordtemp = *(stream + posword); word wordtemp; - if (s->isBigendian()) + if (this->stream->isBigendian()) wordtemp = bh * 256 + bl; else wordtemp = bl * 256 + bh; @@ -258,7 +270,7 @@ bool PartOfPacket::setByteStream(ByteStreamPtr s) posword += 2; bh = *(stream + posword); bl = *(stream + posword + 1); - if (s->isBigendian()) + if (this->stream->isBigendian()) wordtemp = bh * 256 + bl; else wordtemp = bl * 256 + bh; @@ -286,6 +298,7 @@ bool PartOfPacket::setByteStream(ByteStreamPtr s) posbit =0; } } + decoded = true; return true; } @@ -293,6 +306,7 @@ bool PartOfPacket::setByteStream(ByteStreamPtr s) char** PartOfPacket::printValue(const char* addString) { + decode(); //bool first = true; string s1, s2, s3; char *s = new char[1]; //importante, altrimenti non funziona @@ -345,6 +359,7 @@ char** PartOfPacket::printValue(const char* addString) void PartOfPacket::printValueStdout() { + decode(); //bool first = true; string s1, s2, s3; char *s = new char[1]; //importante, altrimenti non funziona diff --git a/src/SDFRBBlock.cpp b/src/SDFRBBlock.cpp index 64734c4..6d565e6 100644 --- a/src/SDFRBBlock.cpp +++ b/src/SDFRBBlock.cpp @@ -512,7 +512,7 @@ ByteStreamPtr SDFRBBlock::generateStream(bool bigendian) return outputstream; } -bool SDFRBBlock::setByteStream(ByteStreamPtr s) +bool SDFRBBlock::setByteStream(ByteStreamPtr s, bool onlySections) { //cout << "bool SDFRBBlock::setByteStream(ByteStreamPtr s)" << " " << s << endl; //AB dword bytestart=0; @@ -530,6 +530,8 @@ bool SDFRBBlock::setByteStream(ByteStreamPtr s) return false; bytestart = bytestop + 1; } + if(onlySections) + return true; if(type->variablePresent) { word bi = 0; diff --git a/src/SDFRBlock.cpp b/src/SDFRBlock.cpp index f92b40d..4294588 100644 --- a/src/SDFRBlock.cpp +++ b/src/SDFRBlock.cpp @@ -72,6 +72,17 @@ SDFRBBlock* SDFRBlock::getBlock(word nblock,word rBlockIndex) return block[0].getBlock(nblock, rBlockIndex); } +/* +ByteStreamPtr SDFRBlock::getVariablePart() { + ByteStreamPtr fixed = getFixedPart(); + int fixedpartdim = fixed->getDimension(); + int sdfdim = getDimension(); + ByteStreamPtr sdfbs = getByteStream(); + ByteStreamPtr camera = ByteStreamPtr(new ByteStream(sdfbs->stream+fixedpartdim, sdfdim-fixedpartdim, sdfbs->isBigendian())); + return camera; +} +*/ + dword SDFRBlock::getMaxDimension() { return block[0].getMaxDimension(); @@ -82,6 +93,11 @@ dword SDFRBlock::getDimension() return block[0].getDimension(); } +dword SDFRBlock::getDimensionFixedPart() +{ + return block[0].fixed.getDimension(); +} + void SDFRBlock::setNumberOfRealDataBlock(word number, word rblockIndex) throw (PacketException*) { /// The block[0] is the only block present @@ -107,10 +123,10 @@ ByteStreamPtr SDFRBlock::generateStream(bool bigendian) return block[0].generateStream(bigendian); } -bool SDFRBlock::setByteStream(ByteStreamPtr s) +bool SDFRBlock::setByteStream(ByteStreamPtr s, bool onlySections) { stream = s; - return block[0].setByteStream(s); + return block[0].setByteStream(s, onlySections); } Field* SDFRBlock::getFields(word index) -- GitLab