Update of /cvsroot/wpdev/wolfpack
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22975
Modified Files:
basics.cpp basics.h guilds.cpp guilds.h persistentobject.h
world.cpp
Log Message:
Binary saving and loading of guilds.
Index: persistentobject.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/persistentobject.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** persistentobject.h 15 Aug 2004 02:17:39 -0000 1.13
--- persistentobject.h 15 Aug 2004 20:54:16 -0000 1.14
***************
*** 145,154 ****
Register a SQL query to retrieve objects of a given type.
*/
! void registerSqlQuery( const QString& type, const QString& query ) {
sql_queries.insert(type, query);
sql_keys.push_back( type );
}
/*
Retrieve a SQL query to retrieve objects of a given type.
*/
--- 145,167 ----
Register a SQL query to retrieve objects of a given type.
*/
! void registerSqlQuery( const QString& type, const QString& query, const QString &sqlcount ) {
sql_queries.insert(type, query);
+ sql_count_queries.insert(type, sqlcount);
sql_keys.push_back( type );
}
/*
+ Retrieve a SQL query to retrieve the number of objects for a given type.
+ */
+ QString findSqlCountQuery(const QString& type) const {
+ QMap<QString, QString>::const_iterator iter = sql_count_queries.find( type );
+
+ if (iter == sql_count_queries.end())
+ return QString::null;
+ else
+ return iter.data();
+ }
+
+ /*
Retrieve a SQL query to retrieve objects of a given type.
*/
***************
*** 171,174 ****
--- 184,188 ----
private:
QMap<QString, QString> sql_queries;
+ QMap<QString, QString> sql_count_queries;
QStringList sql_keys;
};
***************
*** 201,206 ****
}
// Register the SQL query for this type in the factory
! PersistentFactory::instance()->registerSqlQuery(className, sqlString);
// Register the type for the 8-bit binary mapping
--- 215,226 ----
}
+ QString sqlCountString = QString("SELECT COUNT(*) FROM %1").arg(tables.join(","));
+ if (conditions.count() > 0) {
+ sqlCountString.append(" WHERE ");
+ sqlCountString.append(conditions.join(" AND "));
+ }
+
// Register the SQL query for this type in the factory
! PersistentFactory::instance()->registerSqlQuery(className, sqlString, sqlCountString);
// Register the type for the 8-bit binary mapping
Index: basics.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/basics.h,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** basics.h 15 Aug 2004 02:17:39 -0000 1.26
--- basics.h 15 Aug 2004 20:54:16 -0000 1.27
***************
*** 34,38 ****
--- 34,40 ----
#include <functional>
+ #include <algorithm>
#include <qmap.h>
+ #include <qfile.h>
// Forward definitions
***************
*** 62,66 ****
};
! class QCString;
class cBufferedWriter
--- 64,96 ----
};
! // Swap the value in place
! inline void swapBytes( unsigned int& data )
! {
! data = ( ( data & 0xFF ) << 24 ) | ( ( data & 0xFF00 ) << 8 ) | ( ( data & 0xFF0000 ) >> 8 ) | ( ( data & 0xFF000000 ) >> 24 );
! }
!
! inline void swapBytes( double& value )
! {
! unsigned char * ptr = ( unsigned char * ) &value;
! std::swap( ptr[0], ptr[7] );
! std::swap( ptr[1], ptr[6] );
! std::swap( ptr[2], ptr[5] );
! std::swap( ptr[3], ptr[4] );
! }
!
! inline void swapBytes( int& data )
! {
! data = ( ( data & 0xFF ) << 24 ) | ( ( data & 0xFF00 ) << 8 ) | ( ( data & 0xFF0000 ) >> 8 ) | ( ( data & 0xFF000000 ) >> 24 );
! }
!
! inline void swapBytes( unsigned short& data )
! {
! data = ( ( data & 0xFF00 ) >> 8 ) | ( ( data & 0xFF ) << 8 );
! }
!
! inline void swapBytes( short& data )
! {
! data = ( ( data & 0xFF00 ) >> 8 ) | ( ( data & 0xFF ) << 8 );
! }
class cBufferedWriter
***************
*** 77,87 ****
void flush();
! void writeInt( unsigned int data, bool unbuffered = false );
! void writeShort( unsigned short data, bool unbuffered = false );
! void writeByte( unsigned char data, bool unbuffered = false );
! void writeUtf8( const QString& data, bool unbuffered = false );
! void writeAscii( const QCString& data, bool unbuffered = false );
! void writeRaw( const void* data, unsigned int size, bool unbuffered = false );
! void writeDouble( double data, bool unbuffered = false );
unsigned int position();
--- 107,118 ----
void flush();
! inline void writeInt( unsigned int data, bool unbuffered = false );
! inline void writeShort( unsigned short data, bool unbuffered = false );
! inline void writeByte( unsigned char data, bool unbuffered = false );
! inline void writeBool( bool data, bool unbuffered = false );
! inline void writeUtf8( const QString& data, bool unbuffered = false );
! inline void writeAscii( const QCString& data, bool unbuffered = false );
! inline void writeRaw( const void* data, unsigned int size, bool unbuffered = false );
! inline void writeDouble( double data, bool unbuffered = false );
unsigned int position();
***************
*** 92,95 ****
--- 123,142 ----
};
+ class cBufferedWriterPrivate
+ {
+ public:
+ QFile file;
+ unsigned int version;
+ QCString magic;
+ bool needswap;
+ QByteArray buffer;
+ unsigned int bufferpos;
+ QMap<QCString, unsigned int> dictionary;
+ QMap<unsigned char, unsigned int> skipmap;
+ QMap<unsigned char, QString> typemap;
+ unsigned int lastStringId;
+ unsigned int objectCount;
+ };
+
class cBufferedReader
{
***************
*** 108,111 ****
--- 155,159 ----
unsigned short readShort();
unsigned char readByte();
+ bool readBool();
double readDouble();
QString readUtf8();
***************
*** 120,122 ****
--- 168,283 ----
};
+ inline void cBufferedWriter::writeInt( unsigned int data, bool unbuffered )
+ {
+ // Inplace Swapping (data is a copy anyway)
+ if ( d->needswap ) {
+ swapBytes( data );
+ }
+
+ writeRaw( &data, sizeof( data ), unbuffered );
+ }
+
+ inline void cBufferedWriter::writeShort( unsigned short data, bool unbuffered )
+ {
+ // Inplace Swapping (data is a copy anyway)
+ if ( d->needswap )
+ {
+ swapBytes( data );
+ }
+
+ writeRaw( &data, sizeof( data ), unbuffered );
+ }
+
+ inline void cBufferedWriter::writeBool( bool data, bool unbuffered ) {
+ writeByte( data ? 1 : 0, unbuffered );
+ }
+
+ inline void cBufferedWriter::writeByte( unsigned char data, bool unbuffered )
+ {
+ if ( unbuffered )
+ {
+ flush();
+ d->file.writeBlock( ( const char * ) &data, sizeof( data ) );
+ }
+ else
+ {
+ if ( d->bufferpos + sizeof( data ) >= 4096 )
+ {
+ flush(); // Flush buffer to file
+ }
+
+ *( unsigned char * ) ( d->buffer.data() + d->bufferpos ) = data;
+ d->bufferpos += sizeof( data );
+ }
+ }
+
+ inline void cBufferedWriter::writeUtf8( const QString& data, bool unbuffered )
+ {
+ QCString utf8 = data.utf8();
+ writeAscii( utf8, unbuffered );
+ }
+
+ inline void cBufferedWriter::writeAscii( const QCString& data, bool unbuffered )
+ {
+ QMap<QCString, unsigned int>::iterator it = d->dictionary.find( data );
+
+ if ( it != d->dictionary.end() )
+ {
+ writeInt( it.data(), unbuffered );
+ }
+ else
+ {
+ d->dictionary.insert( data, ++d->lastStringId );
+ writeInt( d->lastStringId, unbuffered );
+ }
+ }
+
+ inline void cBufferedWriter::writeRaw( const void* data, unsigned int size, bool unbuffered )
+ {
+ if ( unbuffered )
+ {
+ flush();
+ d->file.writeBlock( ( const char * ) data, size );
+ }
+ else
+ {
+ // Flush out entire blocks if neccesary until we dont
+ // overflow the buffer anymore, then just append
+ unsigned int pos = 0;
+
+ while ( d->bufferpos + size >= 4096 )
+ {
+ unsigned int bspace = 4096 - d->bufferpos;
+
+ // Try putting in some bytes of the remaining data
+ if ( bspace != 0 )
+ {
+ memcpy( d->buffer.data() + d->bufferpos, ( unsigned char * ) data + pos, bspace );
+ d->bufferpos = 4096;
+ pos += bspace;
+ size -= bspace;
+ }
+
+ flush();
+ }
+
+ // There are still some remaining bytes of our data
+ if ( size != 0 )
+ {
+ memcpy( d->buffer.data() + d->bufferpos, ( unsigned char * ) data + pos, size );
+ d->bufferpos += size;
+ }
+ }
+ }
+
+ inline void cBufferedWriter::writeDouble( double value, bool unbuffered )
+ {
+ if ( d->needswap )
+ {
+ swapBytes( value );
+ }
+
+ writeRaw( &value, sizeof( value ), unbuffered );
+ }
+
#endif
Index: guilds.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/guilds.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** guilds.h 15 Aug 2004 02:17:39 -0000 1.9
--- guilds.h 15 Aug 2004 20:54:16 -0000 1.10
***************
*** 40,44 ****
\brief This class represents a guild and all associated information.
*/
! class cGuild : public cPythonScriptable, public PersistentObject
{
public:
--- 40,44 ----
\brief This class represents a guild and all associated information.
*/
! class cGuild : public cPythonScriptable
{
public:
***************
*** 131,146 ****
QMap<P_PLAYER, MemberInfo*> memberinfo_;
- static unsigned char classid;
public:
- static void registerInFactory();
-
- static void setClassid(unsigned char id) {
- cGuild::classid = id;
- }
-
- unsigned char getClassid() const {
- return cGuild::classid;
- }
-
const char* objectID() const {
return "cGuild";
--- 131,135 ----
***************
*** 171,179 ****
void load( const cDBResult& result );
- /*
- Build a sql string for loading all objects of this type from the database.
- */
- static void buildSqlString( const char *objectid, QStringList& fields, QStringList& tables, QStringList& conditions );
-
/*!
\returns The guildstone for this guild. May be NULL.
--- 160,163 ----
***************
*** 377,385 ****
// Wrappers
- void load( cBufferedReader& reader );
- void save( cBufferedWriter& reader );
void load( cBufferedReader& reader, unsigned int version );
void save( cBufferedWriter& reader, unsigned int version );
- void postload( unsigned int version );
// Methods inherited from cPythonScriptable
--- 361,366 ----
Index: guilds.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/guilds.cpp,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** guilds.cpp 15 Aug 2004 02:17:39 -0000 1.19
--- guilds.cpp 15 Aug 2004 20:54:16 -0000 1.20
***************
*** 26,29 ****
--- 26,30 ----
*/
+ #include "basics.h"
#include "guilds.h"
***************
*** 33,38 ****
#include "items.h"
- unsigned char cGuild::classid;
-
cGuilds::~cGuilds()
{
--- 34,37 ----
***************
*** 58,67 ****
}
! void cGuilds::save()
! {
// Clear the tables first: guilds are not saved incremental.
! PersistentBroker::instance()->executeQuery( "DELETE FROM guilds;" );
! PersistentBroker::instance()->executeQuery( "DELETE FROM guilds_members;" );
! PersistentBroker::instance()->executeQuery( "DELETE FROM guilds_canidates;" );
for (iterator it = begin(); it != end(); ++it) {
--- 57,65 ----
}
! void cGuilds::save() {
// Clear the tables first: guilds are not saved incremental.
! PersistentBroker::instance()->executeQuery( "DELETE FROM guilds;" );
! PersistentBroker::instance()->executeQuery( "DELETE FROM guilds_members;" );
! PersistentBroker::instance()->executeQuery( "DELETE FROM guilds_canidates;" );
for (iterator it = begin(); it != end(); ++it) {
***************
*** 70,75 ****
}
! void cGuilds::load()
! {
// Get all guilds from the database
cDBResult result = PersistentBroker::instance()->query( "SELECT serial,name,abbreviation,charta,website,alignment,leader,founded,guildstone FROM guilds" );
--- 68,72 ----
}
! void cGuilds::load() {
// Get all guilds from the database
cDBResult result = PersistentBroker::instance()->query( "SELECT serial,name,abbreviation,charta,website,alignment,leader,founded,guildstone FROM guilds" );
***************
*** 890,916 ****
}
! void cGuild::registerInFactory() {
! classid = BinaryTypemap::instance()->registerType("cGuild");
! }
! void cGuild::load( cBufferedReader& reader ) {
! }
! void cGuild::save( cBufferedWriter& reader ) {
! }
! void cGuild::load( cBufferedReader& reader, unsigned int version ) {
! }
! void cGuild::save( cBufferedWriter& reader, unsigned int version ) {
! }
! void cGuild::postload( unsigned int version ) {
! }
! void cGuild::buildSqlString( const char *objectid, QStringList& fields, QStringList& tables, QStringList& conditions ) {
! fields.append("serial,name,abbreviation,charta,website,alignment,leader,founded,guildstone");
! tables.append("guilds");
}
! static FactoryRegistration< cGuild > registration("cGuild");
--- 887,961 ----
}
! void cGuild::load( cBufferedReader& reader, unsigned int version ) {
! serial_ = reader.readInt();
! name_ = reader.readUtf8();
! abbreviation_ = reader.readUtf8();
! charta_ = reader.readUtf8();
! website_ = reader.readUtf8();
! alignment_ = (eAlignment)reader.readByte();
! leader_ = dynamic_cast<P_PLAYER>(World::instance()->findChar(reader.readInt()));
! founded_.setTime_t(reader.readInt());
! guildstone_ = World::instance()->findItem(reader.readInt());
! // Save Members/Canidates
! P_PLAYER player;
! int count, i;
! count = reader.readInt();
! for (i = 0; i < count; ++i) {
! P_PLAYER player = dynamic_cast<P_PLAYER>( World::instance()->findChar( reader.readInt() ) );
! MemberInfo* info = new MemberInfo;
! info->setShowSign( reader.readBool() );
! info->setGuildTitle( reader.readUtf8() );
! info->setJoined( reader.readInt() );
! if (player) {
! player->setGuild(this);
! members_.append(player);
! memberinfo_.insert( player, info );
! } else {
! delete info;
! }
! }
! count = reader.readInt();
! for (i = 0; i < count; ++i) {
! P_PLAYER player = dynamic_cast<P_PLAYER>( World::instance()->findChar( reader.readInt() ) );
! if (player) {
! addCanidate(player);
! }
! }
}
! void cGuild::save( cBufferedWriter& writer, unsigned int version ) {
! writer.writeByte(0xFD);
!
! writer.writeInt(serial_);
! writer.writeUtf8(name_);
! writer.writeUtf8(abbreviation_);
! writer.writeUtf8(charta_);
! writer.writeUtf8(website_);
! writer.writeByte(alignment_);
! writer.writeInt(leader_ ? leader_->serial() : INVALID_SERIAL);
! writer.writeInt(founded_.toTime_t());
! writer.writeInt(guildstone_ ? guildstone_->serial() : INVALID_SERIAL);
!
! // Save Members/Canidates
! P_PLAYER player;
!
! writer.writeInt(members_.count());
! for ( player = members_.first(); player; player = members_.next() ) {
! MemberInfo* info = getMemberInfo(player);
! writer.writeInt(player->serial());
! writer.writeBool(info->showSign());
! writer.writeUtf8(info->guildTitle());
! writer.writeInt(info->joined());
! }
!
! writer.writeInt(canidates_.count());
! for (player = canidates_.first(); player; player = canidates_.next()) {
! writer.writeInt(player->serial());
! }
! }
Index: world.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/world.cpp,v
retrieving revision 1.111
retrieving revision 1.112
diff -C2 -d -r1.111 -r1.112
*** world.cpp 15 Aug 2004 02:17:39 -0000 1.111
--- world.cpp 15 Aug 2004 20:54:16 -0000 1.112
***************
*** 427,431 ****
unsigned char type;
const QMap<unsigned char, QCString> &typemap = reader.typemap();
- const QMap<unsigned char, QCString> &server_typemap = BinaryTypemap::instance()->getTypemap();
unsigned int loaded = 0;
unsigned int count = reader.objectCount();
--- 427,430 ----
***************
*** 440,462 ****
if ( typemap.contains( type ) )
{
! if ( !server_typemap.contains( type ) )
! {
! // Get the size for this block from the worldfile
! // and skip the entire block
! Console::instance()->log( LOG_WARNING, QString( "Skipping unknown object type %1." ).arg( typemap[type] ) );
! }
! else
! {
! PersistentObject *object = PersistentFactory::instance()->createObject( typemap[type] );
! if (object) {
! try
! {
! object->load( reader );
! }
! catch ( wpException e )
! {
! }
}
}
--- 439,452 ----
if ( typemap.contains( type ) )
{
! PersistentObject *object = PersistentFactory::instance()->createObject( typemap[type] );
! if (object) {
! try {
! object->load( reader );
! } catch (wpException e) {
! Console::instance()->log( LOG_WARNING, e.error() + "\n" );
}
+ } else {
+ // Skip an unknown object type.
}
***************
*** 476,479 ****
--- 466,481 ----
// Special Type for Tags
}
+ else if ( type == 0xFD )
+ {
+ cGuild *guild = 0;
+
+ try {
+ guild = new cGuild();
+ guild->load(reader, reader.version());
+ Guilds::instance()->registerGuild(guild);
+ } catch (wpException e) {
+ delete guild;
+ }
+ }
else if ( type == 0xFE )
{
***************
*** 532,536 ****
QString type = types[j];
! cDBResult res = PersistentBroker::instance()->query( QString( "SELECT COUNT(*) FROM uobjectmap WHERE type = '%1'" ).arg( type ) );
// Find out how many objects of this type are available
--- 534,539 ----
QString type = types[j];
! QString countQuery = PersistentFactory::instance()->findSqlCountQuery(type);
! cDBResult res = PersistentBroker::instance()->query( countQuery );
// Find out how many objects of this type are available
***************
*** 559,564 ****
// Fetch row-by-row
PersistentBroker::instance()->driver()->setActiveConnection( CONN_SECOND );
! while ( res.fetchrow() )
! {
unsigned short offset = 0;
char** row = res.data();
--- 562,566 ----
// Fetch row-by-row
PersistentBroker::instance()->driver()->setActiveConnection( CONN_SECOND );
! while ( res.fetchrow() ) {
unsigned short offset = 0;
char** row = res.data();
***************
*** 740,744 ****
result.free();
- // Load Guilds
Guilds::instance()->load();
--- 742,745 ----
***************
*** 792,839 ****
unsigned int startTime = getNormalizedTime();
! if ( Config::instance()->databaseDriver() == "binary" )
! {
// Save in binary format
- cItemIterator itemIterator;
- P_ITEM item;
cBufferedWriter writer( "WOLFPACK", DATABASE_VERSION );
writer.open( "world.bin" );
- const QMap<unsigned char, QCString> &typemap = BinaryTypemap::instance()->getTypemap();
! for ( item = itemIterator.first(); item; item = itemIterator.next() )
! {
! if ( !item->container() && !item->multi() )
! {
! item->save( writer );
}
}
! cCharIterator charIterator;
! P_CHAR character;
! for ( character = charIterator.first(); character; character = charIterator.next() )
! {
! if ( !character->multi() )
! {
! character->save( writer );
}
}
writer.writeByte( 0xFF ); // Terminator Type
writer.close();
! }
! else
! {
! if ( !PersistentBroker::instance()->openDriver( Config::instance()->databaseDriver() ) )
! {
Console::instance()->log( LOG_ERROR, QString( "Unknown Worldsave Database Driver '%1', check your wolfpack.xml" ).arg( Config::instance()->databaseDriver() ) );
return;
}
! try
! {
PersistentBroker::instance()->connect( Config::instance()->databaseHost(), Config::instance()->databaseName(), Config::instance()->databaseUsername(), Config::instance()->databasePassword() );
! }
! catch ( QString& e )
! {
Console::instance()->log( LOG_ERROR, QString( "Couldn't open the database: %1\n" ).arg( e ) );
return;
--- 793,834 ----
unsigned int startTime = getNormalizedTime();
! if (Config::instance()->databaseDriver() == "binary") {
// Save in binary format
cBufferedWriter writer( "WOLFPACK", DATABASE_VERSION );
writer.open( "world.bin" );
! cCharIterator charIterator;
! P_CHAR character;
! for (character = charIterator.first(); character; character = charIterator.next()) {
! if (!character->multi()) {
! character->save(writer);
}
}
! cItemIterator itemIterator;
! P_ITEM item;
! for (item = itemIterator.first(); item; item = itemIterator.next()) {
! if (!item->container() && !item->multi()) {
! item->save( writer );
}
}
+ // Write Guilds
+ cGuilds::iterator git;
+ for (git = Guilds::instance()->begin(); git != Guilds::instance()->end(); ++git) {
+ (*git)->save(writer, writer.version());
+ }
+
writer.writeByte( 0xFF ); // Terminator Type
writer.close();
! } else {
! if (!PersistentBroker::instance()->openDriver( Config::instance()->databaseDriver())) {
Console::instance()->log( LOG_ERROR, QString( "Unknown Worldsave Database Driver '%1', check your wolfpack.xml" ).arg( Config::instance()->databaseDriver() ) );
return;
}
! try {
PersistentBroker::instance()->connect( Config::instance()->databaseHost(), Config::instance()->databaseName(), Config::instance()->databaseUsername(), Config::instance()->databasePassword() );
! } catch (QString& e) {
Console::instance()->log( LOG_ERROR, QString( "Couldn't open the database: %1\n" ).arg( e ) );
return;
Index: basics.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/basics.cpp,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -d -r1.33 -r1.34
*** basics.cpp 15 Aug 2004 02:17:39 -0000 1.33
--- basics.cpp 15 Aug 2004 20:54:16 -0000 1.34
***************
*** 186,237 ****
}
- // Swap the value in place
- inline void swapBytes( unsigned int& data )
- {
- data = ( ( data & 0xFF ) << 24 ) | ( ( data & 0xFF00 ) << 8 ) | ( ( data & 0xFF0000 ) >> 8 ) | ( ( data & 0xFF000000 ) >> 24 );
- }
-
- inline void swapBytes( double& value )
- {
- unsigned char * ptr = ( unsigned char * ) &value;
- std::swap( ptr[0], ptr[7] );
- std::swap( ptr[1], ptr[6] );
- std::swap( ptr[2], ptr[5] );
- std::swap( ptr[3], ptr[4] );
- }
-
- inline void swapBytes( int& data )
- {
- data = ( ( data & 0xFF ) << 24 ) | ( ( data & 0xFF00 ) << 8 ) | ( ( data & 0xFF0000 ) >> 8 ) | ( ( data & 0xFF000000 ) >> 24 );
- }
-
- inline void swapBytes( unsigned short& data )
- {
- data = ( ( data & 0xFF00 ) >> 8 ) | ( ( data & 0xFF ) << 8 );
- }
-
- inline void swapBytes( short& data )
- {
- data = ( ( data & 0xFF00 ) >> 8 ) | ( ( data & 0xFF ) << 8 );
- }
-
- class cBufferedWriterPrivate
- {
- public:
- QFile file;
- unsigned int version;
- QCString magic;
- bool needswap;
- QByteArray buffer;
- unsigned int bufferpos;
- QMap<QCString, unsigned int> dictionary;
- QMap<unsigned char, unsigned int> skipmap;
- QMap<unsigned char, QString> typemap;
- unsigned int lastStringId;
- unsigned int objectCount;
- };
-
- #define BUFFERSIZE 4096
-
cBufferedWriter::cBufferedWriter( const QCString& magic, unsigned int version )
{
--- 186,189 ----
***************
*** 239,243 ****
d->version = version;
d->magic = magic;
! d->buffer.resize( BUFFERSIZE );
d->bufferpos = 0;
d->lastStringId = 0;
--- 191,195 ----
d->version = version;
d->magic = magic;
! d->buffer.resize( 4096 );
d->bufferpos = 0;
d->lastStringId = 0;
***************
*** 356,400 ****
}
- void cBufferedWriter::writeInt( unsigned int data, bool unbuffered )
- {
- // Inplace Swapping (data is a copy anyway)
- if ( d->needswap )
- {
- swapBytes( data );
- }
-
- writeRaw( &data, sizeof( data ), unbuffered );
- }
-
- void cBufferedWriter::writeShort( unsigned short data, bool unbuffered )
- {
- // Inplace Swapping (data is a copy anyway)
- if ( d->needswap )
- {
- swapBytes( data );
- }
-
- writeRaw( &data, sizeof( data ), unbuffered );
- }
-
- void cBufferedWriter::writeByte( unsigned char data, bool unbuffered )
- {
- if ( unbuffered )
- {
- flush();
- d->file.writeBlock( ( const char * ) &data, sizeof( data ) );
- }
- else
- {
- if ( d->bufferpos + sizeof( data ) >= BUFFERSIZE )
- {
- flush(); // Flush buffer to file
- }
-
- *( unsigned char * ) ( d->buffer.data() + d->bufferpos ) = data;
- d->bufferpos += sizeof( data );
- }
- }
-
void cBufferedWriter::flush()
{
--- 308,311 ----
***************
*** 403,465 ****
}
- void cBufferedWriter::writeUtf8( const QString& data, bool unbuffered )
- {
- QCString utf8 = data.utf8();
- writeAscii( utf8, unbuffered );
- }
-
- void cBufferedWriter::writeAscii( const QCString& data, bool unbuffered )
- {
- QMap<QCString, unsigned int>::iterator it = d->dictionary.find( data );
-
- if ( it != d->dictionary.end() )
- {
- writeInt( it.data(), unbuffered );
- }
- else
- {
- d->dictionary.insert( data, ++d->lastStringId );
- writeInt( d->lastStringId, unbuffered );
- }
- }
-
- void cBufferedWriter::writeRaw( const void* data, unsigned int size, bool unbuffered )
- {
- if ( unbuffered )
- {
- flush();
- d->file.writeBlock( ( const char * ) data, size );
- }
- else
- {
- // Flush out entire blocks if neccesary until we dont
- // overflow the buffer anymore, then just append
- unsigned int pos = 0;
-
- while ( d->bufferpos + size >= BUFFERSIZE )
- {
- unsigned int bspace = BUFFERSIZE - d->bufferpos;
-
- // Try putting in some bytes of the remaining data
- if ( bspace != 0 )
- {
- memcpy( d->buffer.data() + d->bufferpos, ( unsigned char * ) data + pos, bspace );
- d->bufferpos = BUFFERSIZE;
- pos += bspace;
- size -= bspace;
- }
-
- flush();
- }
-
- // There are still some remaining bytes of our data
- if ( size != 0 )
- {
- memcpy( d->buffer.data() + d->bufferpos, ( unsigned char * ) data + pos, size );
- d->bufferpos += size;
- }
- }
- }
-
unsigned int cBufferedWriter::position()
{
--- 314,317 ----
***************
*** 472,485 ****
}
- void cBufferedWriter::writeDouble( double value, bool unbuffered )
- {
- if ( d->needswap )
- {
- swapBytes( value );
- }
-
- writeRaw( &value, sizeof( value ), unbuffered );
- }
-
void cBufferedWriter::setSkipSize( unsigned char type, unsigned int skipsize )
{
--- 324,327 ----
***************
*** 506,510 ****
{
d = new cBufferedReaderPrivate;
! d->buffer.resize( BUFFERSIZE );
d->bufferpos = 0;
d->buffersize = 0; // Current amount of data in buffer
--- 348,352 ----
{
d = new cBufferedReaderPrivate;
! d->buffer.resize( 4096 );
d->bufferpos = 0;
d->buffersize = 0; // Current amount of data in buffer
***************
*** 669,672 ****
--- 511,518 ----
}
+ bool cBufferedReader::readBool() {
+ return readByte() != 0;
+ }
+
unsigned char cBufferedReader::readByte()
{
***************
*** 747,754 ****
if ( available == 0 )
{
! unsigned int read = d->file.readBlock( d->buffer.data(), BUFFERSIZE );
// We will never be able to statisfy the request
! if ( read != BUFFERSIZE && read < size )
{
throw wpException( QString( "Unexpected end of file while reading." ) );
--- 593,600 ----
if ( available == 0 )
{
! unsigned int read = d->file.readBlock( d->buffer.data(), 4096 );
// We will never be able to statisfy the request
! if ( read != 4096 && read < size )
{
throw wpException( QString( "Unexpected end of file while reading." ) );
|