From befa2112f8c9f9ffc588412ccecf7827000038a5 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 1 Sep 2008 12:39:19 +0000 Subject: [PATCH] From John Vidar Larring, initial cut of .ive support for ShapeAttributeList user data --- src/osgPlugins/ive/ShapeAttributeList.cpp | 120 ++++++++++++++++++++++ src/osgPlugins/ive/ShapeAttributeList.h | 18 ++++ 2 files changed, 138 insertions(+) create mode 100644 src/osgPlugins/ive/ShapeAttributeList.cpp create mode 100644 src/osgPlugins/ive/ShapeAttributeList.h diff --git a/src/osgPlugins/ive/ShapeAttributeList.cpp b/src/osgPlugins/ive/ShapeAttributeList.cpp new file mode 100644 index 000000000..f04e902f8 --- /dev/null +++ b/src/osgPlugins/ive/ShapeAttributeList.cpp @@ -0,0 +1,120 @@ +/********************************************************************** + * + * FILE: ShapeAttributeList.cpp + * + * DESCRIPTION: Read/Write osgSim::ShapeAttributeList in binary + * format to disk. + * + * CREATED BY: John Vidar Larring + * + * HISTORY: Created 25.8.2008 + * + **********************************************************************/ + +#include "Exception.h" +#include "ShapeAttributeList.h" + +using namespace ive; + +void ShapeAttributeList::write(DataOutputStream* out) +{ + + // Write ShapeAttributeList's identification. + out->writeInt(IVESHAPEATTRIBUTELIST); + + // Write ShapeAttributeList's properties. + + // Write size of list + out->writeUInt(size()); + + // Write elements of the list + osgSim::ShapeAttributeList::const_iterator it = begin(); + for (const_iterator it = begin(); it != end(); it++) + { + write(out, *it); + } +} + +void ShapeAttributeList::read(DataInputStream* in) +{ + // Peek on ShapeAttributeList's identification. + int id = in->peekInt(); + if(id == IVESHAPEATTRIBUTELIST){ + // Read ShapeAttributeList's identification. + id = in->readInt(); + + // Read ShapeAttributeList's properties + + // Read size of the list + uint count = in->readUInt(); + + resize(count); + + // Read elements of the list + for (uint i=0; i < count; i++) + { + read(in, (*this)[i]); + } + } + else{ + throw Exception("ShapeAttributeList::read(): Expected ShapeAttributeList identification."); + } +} + +void ShapeAttributeList::write(DataOutputStream* out, const osgSim::ShapeAttribute& sa) +{ + // Write name + out->writeString(sa.getName()); + + // Write datatype + osgSim::ShapeAttribute::Type type = sa.getType(); + out->writeInt((int)type); + + // Write data + switch (type) + { + case osgSim::ShapeAttribute::INTEGER: + out->writeInt(sa.getInt()); + break; + case osgSim::ShapeAttribute::DOUBLE: + out->writeDouble(sa.getDouble()); + break; + case osgSim::ShapeAttribute::STRING: + out->writeBool(sa.getString() != 0); + if (sa.getString()) out->writeString(std::string(sa.getString())); + break; + default: + // Ignore Unknown data type + break; + } +} + +void ShapeAttributeList::read(DataInputStream* in, osgSim::ShapeAttribute& sa) +{ + // Read name + sa.setName(in->readString()); + + // Read type + int type = in->readInt(); + + // Read data + switch (type) + { + case osgSim::ShapeAttribute::INTEGER: + sa.setValue(in->readInt()); + break; + case osgSim::ShapeAttribute::DOUBLE: + sa.setValue(in->readDouble()); + break; + case osgSim::ShapeAttribute::STRING: + if (in->readBool()) + sa.setValue(in->readString().c_str()); + else + sa.setValue((char*)0); + break; + default: + // Ignore Unknown data type + break; + } +} + diff --git a/src/osgPlugins/ive/ShapeAttributeList.h b/src/osgPlugins/ive/ShapeAttributeList.h new file mode 100644 index 000000000..b391a26bd --- /dev/null +++ b/src/osgPlugins/ive/ShapeAttributeList.h @@ -0,0 +1,18 @@ +#ifndef IVE_SHAPEATTRIBUTELIST +#define IVE_SHAPEATTRIBUTELIST 1 + +#include +#include "ReadWrite.h" + +namespace ive{ +class ShapeAttributeList : public osgSim::ShapeAttributeList, public ReadWrite { +public: + void write(DataOutputStream* out); + void read(DataInputStream* in); + + void write(DataOutputStream* out, const osgSim::ShapeAttribute& sa); + void read(DataInputStream* in, osgSim::ShapeAttribute& sa); +}; +} + +#endif