From b54f9c2290da455fff2f02fb98c1ff480c543a1e Mon Sep 17 00:00:00 2001 From: Jordan Cristiano Date: Sun, 3 May 2015 20:27:12 -0400 Subject: [PATCH] Replaced usage of string with vector --- demboyz/demboyz.cpp | 30 ++++++++++++++---------------- demboyz/demofile.cpp | 2 +- demboyz/demofile.h | 12 ++++++------ 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/demboyz/demboyz.cpp b/demboyz/demboyz.cpp index e229efa..f30525e 100644 --- a/demboyz/demboyz.cpp +++ b/demboyz/demboyz.cpp @@ -3,11 +3,12 @@ #include "demofilebitbuf.h" #include "netmessages.h" #include +#include -void ParsePacket(const char* packetBuf, const int numBytes) +void ParsePacket(const std::vector& packet) { - assert(numBytes <= NET_MAX_PAYLOAD); - CBitRead bitBuf(packetBuf, numBytes); + assert(packet.size() <= NET_MAX_PAYLOAD); + CBitRead bitBuf(packet.data(), packet.size()); while (bitBuf.GetNumBitsLeft() >= NETMSG_TYPE_BITS) { uint32 typeId = bitBuf.ReadUBitLong(NETMSG_TYPE_BITS); @@ -16,9 +17,9 @@ void ParsePacket(const char* packetBuf, const int numBytes) } } -void ParseSignonData(const std::string& signonData) +void ParseSignonData(const std::vector& signonData) { - CBitRead bitbuf(signonData.data(), signonData.length()); + CBitRead bitbuf(signonData.data(), signonData.size()); const char cmd = bitbuf.ReadChar(); assert(cmd == dem_signon); const int32 tick = bitbuf.ReadLong(); @@ -28,10 +29,10 @@ void ParseSignonData(const std::string& signonData) assert(seq1 == seq2); const int32 numBytes = bitbuf.ReadLong(); - std::string packet; + std::vector packet; packet.resize(numBytes); bitbuf.ReadBytes(&packet[0], numBytes); - ParsePacket(packet.data(), numBytes); + ParsePacket(packet); } int main(const int argc, const char* argv[]) @@ -56,7 +57,7 @@ int main(const int argc, const char* argv[]) int32 sequenceInfo1; int32 sequenceInfo2; democmdinfo_t cmdInfo; - char* packetBuf = (char*)malloc(NET_MAX_PAYLOAD); + std::vector packet; demoFile.ReadCmdHeader(cmd, tick); assert(cmd == dem_synctick && tick == 0); @@ -70,13 +71,11 @@ int main(const int argc, const char* argv[]) switch (cmd) { case dem_packet: - { - demoFile.ReadCmdInfo(cmdInfo); - demoFile.ReadSequenceInfo(sequenceInfo1, sequenceInfo2); - assert(sequenceInfo1 == sequenceInfo2); - const int32 length = demoFile.ReadRawData(packetBuf, NET_MAX_PAYLOAD); - ParsePacket(packetBuf, length); - } + demoFile.ReadCmdInfo(cmdInfo); + demoFile.ReadSequenceInfo(sequenceInfo1, sequenceInfo2); + assert(sequenceInfo1 == sequenceInfo2); + demoFile.ReadRawData(packet); + ParsePacket(packet); break; case dem_stop: assert(i == numFrames && tick == numTicks); @@ -91,7 +90,6 @@ int main(const int argc, const char* argv[]) } } - free(packetBuf); demoFile.Close(); return 0; diff --git a/demboyz/demofile.cpp b/demboyz/demofile.cpp index 87c40a1..3866ee0 100644 --- a/demboyz/demofile.cpp +++ b/demboyz/demofile.cpp @@ -124,7 +124,7 @@ int32 CDemoFile::ReadRawData( char *buffer, int32 length ) return size; } -bool CDemoFile::ReadRawData(std::string& buf) +bool CDemoFile::ReadRawData(std::vector& buf) { if (!m_fileBuffer.size()) { diff --git a/demboyz/demofile.h b/demboyz/demofile.h index aa4bc81..47adf85 100644 --- a/demboyz/demofile.h +++ b/demboyz/demofile.h @@ -28,7 +28,7 @@ #pragma once #endif -#include +#include #define __STDC_FORMAT_MACROS #include @@ -229,7 +229,7 @@ public: int32 ReadRawData( char *buffer, int32 length ); - bool ReadRawData(std::string& buf); + bool ReadRawData(std::vector& buf); void ReadSequenceInfo( int32 &nSeqNrIn, int32 &nSeqNrOutAck ); @@ -241,13 +241,13 @@ public: const demoheader_t *GetDemoHeader() const; - const std::string& GetSignOnData() const; + const std::vector& GetSignOnData() const; private: demoheader_t m_DemoHeader; //general demo info - std::string m_signOnData; - std::string m_fileBuffer; + std::vector m_signOnData; + std::vector m_fileBuffer; size_t m_fileBufferPos; }; @@ -257,7 +257,7 @@ inline const demoheader_t *CDemoFile::GetDemoHeader() const return &m_DemoHeader; } -inline const std::string& CDemoFile::GetSignOnData() const +inline const std::vector& CDemoFile::GetSignOnData() const { return m_signOnData; }