From b2c9b188b378c96e6d7b8e22987e2fa9cc4637ff Mon Sep 17 00:00:00 2001 From: Silent Date: Thu, 23 Mar 2017 11:30:06 +0100 Subject: [PATCH] DelimStringReader --- SilentPatch/DelimStringReader.h | 51 +++++++++++++++++++++ SilentPatchSA/SilentPatchSA.vcxproj | 1 + SilentPatchSA/SilentPatchSA.vcxproj.filters | 3 ++ SilentPatchSA/VehicleSA.cpp | 21 ++------- 4 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 SilentPatch/DelimStringReader.h diff --git a/SilentPatch/DelimStringReader.h b/SilentPatch/DelimStringReader.h new file mode 100644 index 0000000..6f59fe0 --- /dev/null +++ b/SilentPatch/DelimStringReader.h @@ -0,0 +1,51 @@ +#pragma once + +template +class BasicDelimStringReader +{ +public: + BasicDelimStringReader( size_t size ) + : m_buffer( new T[size] ), m_size( size ) + { + m_cursor = m_buffer; + } + + ~BasicDelimStringReader() + { + delete[] m_buffer; + } + + inline T* GetBuffer() const + { + return m_buffer; + } + + inline size_t GetSize() const + { + return m_size; + } + + const T* GetString( size_t* size = nullptr ) + { + if ( *m_cursor == '\0' ) + { + if ( size != nullptr ) *size = 0; + return nullptr; + } + const T* curString = m_cursor; + size_t len = 0; + + while ( *m_cursor++ != '\0' ) len++; + + if ( size != nullptr ) *size = len; + return curString; + } + +private: + T* m_buffer; + const T* m_cursor; + size_t m_size; +}; + +typedef BasicDelimStringReader DelimStringReader; +typedef BasicDelimStringReader WideDelimStringReader; \ No newline at end of file diff --git a/SilentPatchSA/SilentPatchSA.vcxproj b/SilentPatchSA/SilentPatchSA.vcxproj index 191de96..b896c10 100644 --- a/SilentPatchSA/SilentPatchSA.vcxproj +++ b/SilentPatchSA/SilentPatchSA.vcxproj @@ -154,6 +154,7 @@ copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\scripts\newsteam_r2_lowvio + diff --git a/SilentPatchSA/SilentPatchSA.vcxproj.filters b/SilentPatchSA/SilentPatchSA.vcxproj.filters index fb33e00..a69594e 100644 --- a/SilentPatchSA/SilentPatchSA.vcxproj.filters +++ b/SilentPatchSA/SilentPatchSA.vcxproj.filters @@ -137,6 +137,9 @@ Header Files + + Header Files + diff --git a/SilentPatchSA/VehicleSA.cpp b/SilentPatchSA/VehicleSA.cpp index 010dd25..9b82a62 100644 --- a/SilentPatchSA/VehicleSA.cpp +++ b/SilentPatchSA/VehicleSA.cpp @@ -4,6 +4,7 @@ #include #include "VehicleSA.h" #include "TimerSA.h" +#include "DelimStringReader.h" std::vector vecRotorExceptions; @@ -56,27 +57,15 @@ static RpMaterial* SetCompAlphaCB(RpMaterial* pMaterial, void* data) void ReadRotorFixExceptions(const wchar_t* pPath) { const size_t SCRATCH_PAD_SIZE = 32767; - wchar_t* buf = new wchar_t[ SCRATCH_PAD_SIZE ]; + WideDelimStringReader reader( SCRATCH_PAD_SIZE ); - GetPrivateProfileSectionW( L"RotorFixExceptions", buf, SCRATCH_PAD_SIZE, pPath ); - for( wchar_t* raw = buf; *raw != '\0'; ++raw ) + GetPrivateProfileSectionW( L"RotorFixExceptions", reader.GetBuffer(), reader.GetSize(), pPath ); + while ( const wchar_t* str = reader.GetString() ) { - // Construct a std::wstring with the line - wchar_t fileID[128]; - wchar_t* ptr = fileID; - do - { - *ptr++ = *raw++; - } - while ( *raw != '\0' && ptr < fileID + (_countof(fileID)-1) ); - *ptr = '\0'; - - unsigned int toList = _wtoi( fileID ); + unsigned int toList = _wtoi( str ); if ( toList != 0 ) vecRotorExceptions.push_back( toList ); } - - delete[] buf; } void CVehicle::SetComponentAtomicAlpha(RpAtomic* pAtomic, int nAlpha)