Show widgetlib.cpp syntax highlighted
/***************************************************************************
widgetlib.cpp - Widget library file loader
-------------------
begin : Tue Jul 1 2003
copyright : (C) 2003 by Reality Rift Studios
email : mattias@realityrift.com
***************************************************************************
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is the NeoEngine, NeoWTK, widgetlib.cpp
The Initial Developer of the Original Code is Mattias Jansson.
Portions created by Mattias Jansson are Copyright (C) 2002
Reality Rift Studios. All Rights Reserved.
***************************************************************************/
#include "widgetlib.h"
#include "chunktype.h"
#ifdef HAVE_NEOCHUNKIO
#include <neoengine/file.h>
#include <neoengine/logstream.h>
#include <neochunkio/chunkio.h>
#include <neochunkio/chunk.h>
#include <neochunkio/stdstring.h>
using namespace std;
using namespace NeoEngine;
using namespace NeoChunkIO;
namespace NeoWTK
{
Object *WidgetLibrary::s_pkRefRoot = 0;
WidgetLibrary::WidgetLibrary( FileManager *pkFileManager ) :
LoadableEntity( pkFileManager ),
Object( 0, 0 )
{
SetName( "widgetlib" );
}
WidgetLibrary::~WidgetLibrary()
{
}
bool WidgetLibrary::LoadNode( unsigned int uiFlags )
{
if( !m_pkFile || !m_pkFile->IsValid() )
{
neolog << LogLevel( ERROR ) << "[WidgetLibrary::LoadNode] ERROR: Null ptr or invalid file" << endl;
return false;
}
char szStr[5]; szStr[4] = 0;
string strMode;
if( !m_pkFile->DetermineByteOrder( 0x4449574e ) )
{
neolog << LogLevel( ERROR ) << "[WidgetLibrary::LoadNode] ERROR: Unable to determine file byte ordering, possible corrupt file" << endl;
return false;
}
m_pkFile->Read( szStr, 4 );
if( string( szStr ) != "NWID" )
{
neolog << LogLevel( ERROR ) << "[WidgetLibrary::LoadNode] ERROR: Invalid ID in [" << m_pkFile->GetName() << "]" << endl;
return false;
}
//Read mode string
m_pkFile->Read( szStr, 4 );
ChunkIO *pkIO = 0;
if( string( szStr ) == "!bin" )
{
pkIO = new ChunkIO( ChunkIO::BINARY );
m_pkFile->SetBinary( true );
}
else if( string( szStr ) == "!txt" )
{
pkIO = new ChunkIO( ChunkIO::ASCII );
m_pkFile->SetBinary( false );
}
else
{
neolog << LogLevel( ERROR ) << "[WidgetLibrary::LoadNode] ERROR: unknown chunk file mode [" << strMode << "]" << endl;
return false;
}
*m_pkFile >> pkIO->m_iMajorVersion >> pkIO->m_iMinorVersion;
if( !( pkIO->m_iMajorVersion == ChunkIO::MAJORVERSIONREQUIRED ) || ( pkIO->m_iMinorVersion < ChunkIO::MINORVERSIONREQUIRED ) )
{
neolog << LogLevel( ERROR ) << "[WidgetLibrary::LoadNode] ERROR: Invalid chunk format version, got "
<< pkIO->m_iMajorVersion << "." << pkIO->m_iMinorVersion << " in [" << m_pkFile->GetName() << "]" << endl;
return false;
}
Chunk *pkChunk = 0;
vector< Chunk* > vpkWidgetChunks;
while( ( pkChunk = pkIO->ReadChunk( m_pkFile ) ) )
{
if( ( pkChunk->FindChunk( "", NeoWTK::ChunkType::OBJECT, 0, true ) ) ||
( pkChunk->FindChunk( "", NeoWTK::ChunkType::LIBREF, 0, true ) ) )
vpkWidgetChunks.push_back( pkChunk );
else
{
neolog << LogLevel( WARNING ) << "[WidgetLibrary::LoadNode] unsupported toplevel chunk type ["
<< pkChunk->GetType() << "] [" << pkChunk->GetTypeAsString() << "]" << endl;
delete pkChunk;
}
}
s_pkRefRoot = this;
unsigned int uiParseFlags = 0;
vector< Chunk* >::iterator ppkChunk = vpkWidgetChunks.begin();
vector< Chunk* >::iterator ppkEnd = vpkWidgetChunks.end();
//Now parse chunks
for( ; ppkChunk != ppkEnd; ++ppkChunk )
{
if( pkIO->ParseChunk( *ppkChunk, uiParseFlags, m_pkFileManager ) < 0 )
neolog << LogLevel( WARNING ) << "[WidgetLibrary::LoadNode] error parsing widget chunk" << endl;
else
{
ObjectChunk *pkObjChunk = dynamic_cast< ObjectChunk* >( *ppkChunk );
if( pkObjChunk && pkObjChunk->m_pkObject )
{
AttachObject( pkObjChunk->m_pkObject );
pkObjChunk->m_pkObject = 0;
}
}
delete( *ppkChunk );
}
delete pkIO;
return true;
}
int LibRefChunk::ParseData( unsigned int uiFlags, FileManager *pkFileManager )
{
Chunk *pkChunk = 0;
if( ( pkChunk = FindChunk( "file", NeoChunkIO::ChunkType::STRING, -1 ) ) )
{
string strFile = dynamic_cast< StringChunk* >( pkChunk )->m_strData + ".nwid";
Object *pkRefRoot = WidgetLibrary::s_pkRefRoot;
WidgetLibrary *pkLib = new WidgetLibrary( pkFileManager );
if( pkLib->Load( strFile, uiFlags ) )
{
//Get all reference objects
const vector< Object* > &rvpkSrcObjects = WidgetLibrary::s_pkRefRoot->GetChildObjects();
vector< Object* > vpkObjects( rvpkSrcObjects.size(), 0 );
vector< Object* >::const_iterator ppkSrcObject = rvpkSrcObjects.begin();
vector< Object* >::const_iterator ppkSrcObjectEnd = rvpkSrcObjects.end();
vector< Object* >::iterator ppkDstObject = vpkObjects.begin();
for( ; ppkSrcObject != ppkSrcObjectEnd; ++ppkSrcObject, ++ppkDstObject )
*ppkDstObject = *ppkSrcObject;
vector< Object* >::iterator ppkObject = vpkObjects.begin();
vector< Object* >::iterator ppkObjectEnd = vpkObjects.end();
for( ; ppkObject != ppkObjectEnd; ++ppkObject )
pkRefRoot->AttachObject( *ppkObject );
}
delete pkLib;
WidgetLibrary::s_pkRefRoot = pkRefRoot;
}
return 1;
}
}; /*! namespace NeoWTK */
#endif
See more files for this project here