Added struct StrRange for convenience.

This commit is contained in:
Adam Sawicki 2018-08-07 13:50:50 +02:00
parent 6d2e2e0cac
commit 0b9bcf0340

View File

@ -1762,6 +1762,14 @@ static const int RESULT_ERROR_COMMAND_LINE = -1;
static const int RESULT_ERROR_SOURCE_FILE = -2; static const int RESULT_ERROR_SOURCE_FILE = -2;
static const int RESULT_ERROR_FORMAT = -3; static const int RESULT_ERROR_FORMAT = -3;
struct StrRange
{
const char* beg;
const char* end;
size_t length() const { return end - beg; }
};
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// LineSplit class // LineSplit class
@ -1776,7 +1784,7 @@ public:
{ {
} }
bool GetNextLine(const char *&outBeg, const char*&outEnd); bool GetNextLine(StrRange& out);
size_t GetNextLineIndex() const { return m_NextLineIndex; } size_t GetNextLineIndex() const { return m_NextLineIndex; }
private: private:
@ -1786,15 +1794,15 @@ private:
size_t m_NextLineIndex; size_t m_NextLineIndex;
}; };
bool LineSplit::GetNextLine(const char *&outBeg, const char*&outEnd) bool LineSplit::GetNextLine(StrRange& out)
{ {
if(m_NextLineBeg < m_NumBytes) if(m_NextLineBeg < m_NumBytes)
{ {
outBeg = m_Data + m_NextLineBeg; out.beg = m_Data + m_NextLineBeg;
size_t currLineEnd = m_NextLineBeg; size_t currLineEnd = m_NextLineBeg;
while(currLineEnd < m_NumBytes && m_Data[currLineEnd] != '\n') while(currLineEnd < m_NumBytes && m_Data[currLineEnd] != '\n')
++currLineEnd; ++currLineEnd;
outEnd = m_Data + currLineEnd; out.end = m_Data + currLineEnd;
m_NextLineBeg = currLineEnd + 1; // Past '\n' m_NextLineBeg = currLineEnd + 1; // Past '\n'
++m_NextLineIndex; ++m_NextLineIndex;
return true; return true;
@ -1812,7 +1820,7 @@ class CsvSplit
public: public:
static const size_t RANGE_COUNT_MAX = 32; static const size_t RANGE_COUNT_MAX = 32;
void Set(const char* beg, const char* end, size_t maxCount = RANGE_COUNT_MAX); void Set(const StrRange& line, size_t maxCount = RANGE_COUNT_MAX);
size_t GetCount() const { return m_Count; } size_t GetCount() const { return m_Count; }
void GetRange(size_t index, const char*& outBeg, const char*& outEnd) const void GetRange(size_t index, const char*& outBeg, const char*& outEnd) const
@ -1827,11 +1835,11 @@ private:
size_t m_Ranges[RANGE_COUNT_MAX * 2]; // Pairs of begin-end. size_t m_Ranges[RANGE_COUNT_MAX * 2]; // Pairs of begin-end.
}; };
void CsvSplit::Set(const char* beg, const char* end, size_t maxCount) void CsvSplit::Set(const StrRange& line, size_t maxCount)
{ {
assert(maxCount <= RANGE_COUNT_MAX); assert(maxCount <= RANGE_COUNT_MAX);
m_Str = beg; m_Str = line.beg;
const size_t strLen = end - beg; const size_t strLen = line.length();
size_t rangeIndex = 0; size_t rangeIndex = 0;
size_t charIndex = 0; size_t charIndex = 0;
while(charIndex < strLen && rangeIndex < maxCount) while(charIndex < strLen && rangeIndex < maxCount)
@ -1856,7 +1864,7 @@ public:
void Init(); void Init();
~Player(); ~Player();
void ExecuteLine(size_t lineNumber, const char* lineBeg, const char* lineEnd); void ExecuteLine(size_t lineNumber, const StrRange& line);
private: private:
static const size_t MAX_WARNINGS_TO_SHOW = 16; static const size_t MAX_WARNINGS_TO_SHOW = 16;
@ -1881,10 +1889,10 @@ Player::~Player()
printf("WARNING: %zu more warnings not shown.\n", m_WarningCount - MAX_WARNINGS_TO_SHOW); printf("WARNING: %zu more warnings not shown.\n", m_WarningCount - MAX_WARNINGS_TO_SHOW);
} }
void Player::ExecuteLine(size_t lineNumber, const char* lineBeg, const char* lineEnd) void Player::ExecuteLine(size_t lineNumber, const StrRange& line)
{ {
CsvSplit csvSplit; CsvSplit csvSplit;
csvSplit.Set(lineBeg, lineEnd); csvSplit.Set(line);
if(csvSplit.GetCount() >= 4) if(csvSplit.GetCount() >= 4)
{ {
@ -1906,11 +1914,11 @@ static void PrintCommandLineSyntax()
" VmaReplay <SrcFile.csv>\n"); " VmaReplay <SrcFile.csv>\n");
} }
static inline bool StrRangeEq(const char* lhsBeg, const char* lhsEnd, const char* rhsSz) static inline bool StrRangeEq(const StrRange& lhs, const char* rhsSz)
{ {
const size_t rhsLen = strlen(rhsSz); const size_t rhsLen = strlen(rhsSz);
return rhsLen == lhsEnd - lhsBeg && return rhsLen == lhs.length() &&
memcmp(lhsBeg, rhsSz, rhsLen) == 0; memcmp(lhs.beg, rhsSz, rhsLen) == 0;
} }
static int ProcessFile(const char* data, size_t numBytes) static int ProcessFile(const char* data, size_t numBytes)
@ -1919,18 +1927,17 @@ static int ProcessFile(const char* data, size_t numBytes)
printf("File size: %zu B\n", numBytes); printf("File size: %zu B\n", numBytes);
LineSplit lineSplit(data, numBytes); LineSplit lineSplit(data, numBytes);
const char* lineBeg; StrRange line;
const char* lineEnd;
if(!lineSplit.GetNextLine(lineBeg, lineEnd) || if(!lineSplit.GetNextLine(line) ||
!StrRangeEq(lineBeg, lineEnd, "Vulkan Memory Allocator,Calls recording")) !StrRangeEq(line, "Vulkan Memory Allocator,Calls recording"))
{ {
printf("ERROR: Incorrect file format.\n"); printf("ERROR: Incorrect file format.\n");
return RESULT_ERROR_FORMAT; return RESULT_ERROR_FORMAT;
} }
if(!lineSplit.GetNextLine(lineBeg, lineEnd) || if(!lineSplit.GetNextLine(line) ||
!StrRangeEq(lineBeg, lineEnd, "1,0")) !StrRangeEq(line, "1,0"))
{ {
printf("ERROR: Incorrect file format version.\n"); printf("ERROR: Incorrect file format version.\n");
return RESULT_ERROR_FORMAT; return RESULT_ERROR_FORMAT;
@ -1939,9 +1946,9 @@ static int ProcessFile(const char* data, size_t numBytes)
Player player; Player player;
player.Init(); player.Init();
while(lineSplit.GetNextLine(lineBeg, lineEnd)) while(lineSplit.GetNextLine(line))
{ {
player.ExecuteLine(lineSplit.GetNextLineIndex(), lineBeg, lineEnd); player.ExecuteLine(lineSplit.GetNextLineIndex(), line);
} }
// End stats. // End stats.