Replaced usage of string with vector<unsigned char>

This commit is contained in:
Jordan Cristiano 2015-05-03 20:27:12 -04:00
parent b3bab786c8
commit b54f9c2290
3 changed files with 21 additions and 23 deletions

View File

@ -3,11 +3,12 @@
#include "demofilebitbuf.h"
#include "netmessages.h"
#include <assert.h>
#include <vector>
void ParsePacket(const char* packetBuf, const int numBytes)
void ParsePacket(const std::vector<unsigned char>& 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<unsigned char>& 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<unsigned char> 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<unsigned char> 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;

View File

@ -124,7 +124,7 @@ int32 CDemoFile::ReadRawData( char *buffer, int32 length )
return size;
}
bool CDemoFile::ReadRawData(std::string& buf)
bool CDemoFile::ReadRawData(std::vector<unsigned char>& buf)
{
if (!m_fileBuffer.size())
{

View File

@ -28,7 +28,7 @@
#pragma once
#endif
#include <string>
#include <vector>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
@ -229,7 +229,7 @@ public:
int32 ReadRawData( char *buffer, int32 length );
bool ReadRawData(std::string& buf);
bool ReadRawData(std::vector<unsigned char>& 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<unsigned char>& GetSignOnData() const;
private:
demoheader_t m_DemoHeader; //general demo info
std::string m_signOnData;
std::string m_fileBuffer;
std::vector<unsigned char> m_signOnData;
std::vector<unsigned char> 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<unsigned char>& CDemoFile::GetSignOnData() const
{
return m_signOnData;
}