From: g0dil Date: Wed, 24 Jun 2009 15:39:04 +0000 (+0000) Subject: Packets: Implement (dynamically sized) StringParser X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=d0378a5654f8def418ab99828dc61764460f1ea6;p=senf.git Packets: Implement (dynamically sized) StringParser git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1236 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Packets/PacketParser.cc b/Packets/PacketParser.cc new file mode 100644 index 0000000..c97421f --- /dev/null +++ b/Packets/PacketParser.cc @@ -0,0 +1,58 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief PacketParser non-inline non-template implementation */ + +#include "Packets.hh" +#include "PacketParser.ih" + +// Custom includes + +//#include "PacketParser.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +prefix_ void senf::PacketParserBase::resize(size_type oldSize, size_type newSize) +{ + safe_data_iterator safe_i (*this); + if (oldSize > newSize) + data().erase(i()+newSize, i()+oldSize); + else if (oldSize < newSize) + data().insert(i()+oldSize, newSize-oldSize, 0u); + i_ = safe_i; +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "PacketParser.mpp" + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Packets/PacketParser.hh b/Packets/PacketParser.hh index 8c218e0..b5ef0ea 100644 --- a/Packets/PacketParser.hh +++ b/Packets/PacketParser.hh @@ -354,6 +354,16 @@ namespace senf { parsers when access to previous or following packets is needed e.g. for calculating checksums etc. */ + void resize(size_type oldSize, size_type newSize); ///< Resize data container + /**< This command will erase or insert bytes from/into the + data container at the end of the parser (at i() + \a + newSize). If \a oldSize is > \a newSize, bytes will be + removed, otherwise bytes will be inserted. + + \warning This may invalidate iterators and other + parsers. The current parser itself is automatically + updated */ + private: data_iterator end() const; diff --git a/Packets/Packets.hh b/Packets/Packets.hh index ea0e318..087d012 100644 --- a/Packets/Packets.hh +++ b/Packets/Packets.hh @@ -60,6 +60,7 @@ #include "SafeIterator.hh" #include "ArrayParser.hh" #include "IntParser.hh" +#include "StringParser.hh" #include "AuxParser.hh" #include "ListParser.hh" #include "ListBParser.hh" diff --git a/Packets/StringParser.cci b/Packets/StringParser.cci new file mode 100644 index 0000000..7d1119b --- /dev/null +++ b/Packets/StringParser.cci @@ -0,0 +1,81 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StringParser inline non-template implementation */ + +//#include "StringParser.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ senf::StringParser::StringParser(data_iterator i, state_type s) + : PacketParserBase(i, s, init_bytes) + {} + +prefix_ senf::StringParser::size_type senf::StringParser::bytes() + const +{ + return parse(0)+2; +} + +prefix_ senf::StringParser::value_type senf::StringParser::value() + const +{ + validate(bytes()); + return std::string(i()+2, i()+bytes()); +} + +prefix_ void senf::StringParser::value(value_type v) +{ + validate(bytes()); + resize(bytes(), v.size()+2); + parse(0) = v.size(); + std::copy(v.begin(), v.end(), i()+2); +} + +prefix_ senf::StringParser::operator value_type() + const +{ + return value(); +} + +prefix_ senf::StringParser const & senf::StringParser::operator=(value_type other) +{ + value(other); return *this; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Packets/StringParser.hh b/Packets/StringParser.hh new file mode 100644 index 0000000..5bae1a4 --- /dev/null +++ b/Packets/StringParser.hh @@ -0,0 +1,78 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StringParser public header */ + +#ifndef HH_SENF_Packets_StringParser_ +#define HH_SENF_Packets_StringParser_ 1 + +#ifndef HH_SENF_Packets_Packets_ +#error "Don't include 'StringParser.hh' directly, include 'Packets.hh'" +#endif + +// Custom includes + +//#include "StringParser.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + + struct StringParser + : public PacketParserBase + { + + StringParser(data_iterator i, state_type s); + + /////////////////////////////////////////////////////////////////////////// + + typedef std::string value_type; + static const size_type init_bytes = 2; + size_type bytes() const; + + value_type value() const; + void value(value_type v); + operator value_type() const; + StringParser const & operator=(value_type other); + }; + +} + +///////////////////////////////hh.e//////////////////////////////////////// +#endif +#if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_StringParser_i_) +#define HH_SENF_Packets_StringParser_i_ +#include "StringParser.cci" +//#include "StringParser.ct" +//#include "StringParser.cti" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Packets/StringParser.test.cc b/Packets/StringParser.test.cc new file mode 100644 index 0000000..6845299 --- /dev/null +++ b/Packets/StringParser.test.cc @@ -0,0 +1,70 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StringParser.test unit tests */ + +//#include "StringParser.test.hh" +//#include "StringParser.test.ih" + +// Custom includes +#include "Packets.hh" + +#include "../Utils/auto_unit_test.hh" +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +namespace { + struct VoidPacket : public senf::PacketTypeBase + {}; +} + +BOOST_AUTO_UNIT_TEST(stringParser) +{ + senf::PacketInterpreterBase::byte data[] = { 0x00, 0x04, 'T', 'E', 'S', 'T' }; + senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter::create(data)); + + BOOST_CHECK_EQUAL( p->data().size(), 6u ); + BOOST_CHECK_EQUAL( senf::StringParser(p->data().begin(), &p->data()).value(), "TEST" ); + + senf::StringParser(p->data().begin(), &p->data()).value("Another Test"); + BOOST_CHECK_EQUAL( p->data().size(), 14u ); + BOOST_CHECK_EQUAL( p->data()[0], 0u ); + BOOST_CHECK_EQUAL( p->data()[1], 12u ); + BOOST_CHECK_EQUAL( senf::StringParser(p->data().begin(), &p->data()).value(), "Another Test" ); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: