Skip to content

Commit

Permalink
Added TCString::operator+=
Browse files Browse the repository at this point in the history
Added TCString::Concat
Renamed TSystem to TSystemTools.cpp
  • Loading branch information
AdventureT committed Dec 3, 2023
1 parent 9c2a581 commit 5de401b
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Tools/UnitTests/Source/TKernel/TCString_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ TEST_CASE("Constructor", "[TCString]")
{
TCString string("ABCD");
REQUIRE(string == "ABCD");
}

TEST_CASE("Concat Strings", "[TCString]")
{
TCString string("ABCD");
TCString string2("EFGH");
string += string2;
REQUIRE(string == "ABCDEFGH");
}
9 changes: 9 additions & 0 deletions Toshi/Include/TKernel/TCString.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TOSHI_EXPORT TCString
FreeBuffer();
}

TCString& Concat(TCString const& a_rString, TINT a_iLength = -1);
TINT Compare(TPCCHAR a_pcString, int a_iLength = -1) const;

void Copy(const TCString& a_rOther, TINT a_iLength = -1);
Expand All @@ -47,6 +48,8 @@ class TOSHI_EXPORT TCString
TCString& __cdecl Format(TPCCHAR a_pcFormat, ...);
TINT Find(char a_cFind, TINT a_iIndex = 0) const;

void Truncate(TINT a_iLength);

TBOOL IsIndexValid(TINT a_iIndex = 0) const
{
return a_iIndex <= m_iStrLen && a_iIndex >= 0;
Expand Down Expand Up @@ -99,6 +102,12 @@ class TOSHI_EXPORT TCString
return *this;
}

TCString& operator+=(TCString const& a_rString)
{
Concat(a_rString);
return *this;
}

TCCHAR& operator[](TINT a_iIndex) const
{
TASSERT((a_iIndex>=0) && (a_iIndex<=(TINT)m_iStrLen));
Expand Down
4 changes: 4 additions & 0 deletions Toshi/Include/TKernel/TFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ class TOSHI_EXPORT TFileManager
void MountFileSystem(TFileSystem* a_pFileSystem);
void UnmountFileSystem(TFileSystem* a_pFileSystem);

TCString MakeAbsolutePath(TCString const& a_rPath);

static TFileManager* __stdcall GetFileManager() { return s_pFileManager; }
void SetSystemPath(TCString const& a_rSysPath)
{
m_pcSystemPath = a_rSysPath;
InvalidateSystemPath();
}
TCString const& GetWorkingDirectory() const { return m_pcWorkingDirectory; }

private:
void ValidateSystemPath();
Expand Down Expand Up @@ -175,6 +178,7 @@ class TOSHI_EXPORT TFile
static TFile* Create(const TCString& a_sName, TUINT a_uiMode);
static void Destroy(TFile* a_pFile);
static void PrintFileAccess(TBOOL a_bFileAccess) {}
static TCString __stdcall ConcatPath(TCString const& a_rPath1, TCString const& a_rPath2);

void Destroy();
TFileSystem* GetFileSystem() const { return m_pFileSystem; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TOSHI_EXPORT TSystem

static TINT __stdcall StringLength(TPCCHAR a_String);
static TINT __stdcall StringCompareNoCase(TPCCHAR a_String1, TPCCHAR a_String2, TINT a_uiSize);
static TCHAR const* __stdcall StringCopy(TPCHAR a_DestinationString, TCHAR const* a_SourceString, TINT a_iCount);
static TPVOID __stdcall MemCopy(TPVOID a_dest, TPCVOID a_src, TUINT a_iSize);

static TPBYTE GetScratchMem() { return ms_aScratchMem; }
Expand Down
2 changes: 1 addition & 1 deletion Toshi/Include/TKernel/Win/TNativeFileWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TOSHI_EXPORT TNativeFileSystem : public TFileSystem
virtual TFile* CreateFile(Toshi::TCString const& a_rFilename, TUINT a_uiMode) override;
virtual void DestroyFile(TFile* a_pFile) override;
virtual TBOOL RemoveFile(TCString const& a_rFilename) override;
virtual TCString MakeInternalPath(const TCString& a_rsPath) override;
virtual TCString MakeInternalPath(TCString const& a_rsPath) override;

private:
HANDLE m_hFileSystem; // 0x1C
Expand Down
42 changes: 40 additions & 2 deletions Toshi/Source/TKernel/TCString.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "TCString.h"
#include "TSystem.h"
#include "TSystemTools.h"
#include <string.h>
#include <TKernel/TMemory.h>

TOSHI_NAMESPACE_USING

Expand Down Expand Up @@ -36,10 +37,30 @@ TBOOL TCString::AllocBuffer(TINT a_iLength, TBOOL a_bClear)
}
m_iStrLen = a_iLength;
}
if (a_bClear) m_pBuffer[0] = 0;
if (a_bClear) m_pBuffer[0] = '\0';
return hasChanged;
}

TCString& TCString::Concat(TCString const& a_rString, TINT a_iLength)
{
TINT len = a_rString.Length();
if (len < a_iLength || a_iLength == -1) {
a_iLength = len;
}
TPCHAR pBuffer = m_pBuffer;
len = Length();
TBOOL ret = AllocBuffer(len + a_iLength, TFALSE);
if (ret) {
TSystem::StringCopy(m_pBuffer, pBuffer, -1);
}
TSystem::StringCopy(m_pBuffer + len, a_rString.m_pBuffer, a_iLength);
m_pBuffer[len + a_iLength] = '\0';
if (ret && len != 0) {
tfree(pBuffer);
}
return *this;
}

TINT TCString::Compare(TPCCHAR a_pcString, int a_iLength) const
{
TASSERT(a_pcString!=TNULL);
Expand Down Expand Up @@ -91,3 +112,20 @@ TINT TCString::Find(char a_cFind, TINT a_iIndex) const

return foundAt - GetString();
}

void TCString::Truncate(TINT a_iLength)
{
TINT len = Length();
if (len < a_iLength) {
a_iLength = len;
}
TPCHAR pBuffer = m_pBuffer;
TBOOL ret = AllocBuffer(a_iLength, TFALSE);
if (ret) {
TSystem::StringCopy(m_pBuffer, pBuffer, a_iLength);
}
m_pBuffer[a_iLength] = '\0';
if (ret && len != 0) {
tfree(pBuffer);
}
}
2 changes: 1 addition & 1 deletion Toshi/Source/TKernel/TDebug.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "TDebug.h"
#include <stdio.h>
#include <windows.h>
#include "TSystem.h"
#include "TSystemTools.h"

#ifdef TOSHI_NOTFINAL

Expand Down
12 changes: 12 additions & 0 deletions Toshi/Source/TKernel/TFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ void TFile::Destroy(TFile* a_pFile)
}
}

TCString __stdcall TFile::ConcatPath(TCString const& a_rPath1, TCString const& a_rPath2)
{
TCString str = TCString();

return TCString();
}

void TFile::Destroy()
{
Destroy(this);
Expand Down Expand Up @@ -89,6 +96,11 @@ void TFileManager::UnmountFileSystem(TFileSystem* a_pFileSystem)
InvalidateSystemPath();
}

TCString TFileManager::MakeAbsolutePath(TCString const& a_rPath)
{
return TFile::ConcatPath(a_rPath, GetWorkingDirectory());
}

TFileSystem* __stdcall TFileManager::FindFileSystem(TDList<TFileSystem>& a_rFileSystems, const TCString& a_rFileSysName)
{
for (auto pNode = a_rFileSystems.Begin(); pNode != a_rFileSystems.End(); pNode++) {
Expand Down
2 changes: 1 addition & 1 deletion Toshi/Source/TKernel/TObject.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "TObject.h"
#include "TSystem.h"
#include "TSystemTools.h"

TOSHI_NAMESPACE_USING

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "TSystem.h"
#include "TSystemTools.h"
#include "TDebug.h"
#include <string.h>

Expand All @@ -18,6 +18,19 @@ TINT __stdcall TSystem::StringCompareNoCase(TPCCHAR a_String1, TPCCHAR a_String2
return a_uiSize != -1 ? _strnicmp(a_String1, a_String2, a_uiSize) : _stricmp(a_String1, a_String2);
}

TCHAR const* __stdcall TSystem::StringCopy(TPCHAR a_DestinationString, TCHAR const* a_SourceString, TINT a_iCount)
{
TASSERT((a_DestinationString!=TNULL) && (a_SourceString!=TNULL));
if (a_iCount != -1) {
return strncpy(a_DestinationString, a_SourceString, a_iCount);
}
TPCHAR d = a_DestinationString;
TCHAR const* s = a_SourceString;
while (*s != '\0')
*d++ = *s++;
return a_DestinationString;
}

TPVOID __stdcall TSystem::MemCopy(TPVOID a_dest, TPCVOID a_src, TUINT a_iSize)
{
// Note: Idk if they used gcclib's memcpy() or their own
Expand Down
15 changes: 14 additions & 1 deletion Toshi/Source/TKernel/Win/TNativeFileWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ TBOOL TNativeFileSystem::RemoveFile(TCString const& a_rFilename)
return DeleteFile(a_rFilename.GetString());
}

TCString TNativeFileSystem::MakeInternalPath(const TCString& a_rsPath)
TCString TNativeFileSystem::MakeInternalPath(TCString const& a_rsPath)
{
TCString prefix = GetPrefix();
if (prefix.Length() > 0) {
if (prefix[prefix.Length() - 1] == '/' || prefix[prefix.Length() - 1] == '\\') {
prefix.Truncate(prefix.Length() - 1);
}
}
TFileManager *pFileManager = TFileManager::GetFileManager();
TVALIDADDRESS(pFileManager);
{
TCString empty;
pFileManager->MakeAbsolutePath(empty);
prefix += empty;
}
return TCString();
}

Expand Down

0 comments on commit 5de401b

Please sign in to comment.