Skip to content

Commit

Permalink
Implemented TFile
Browse files Browse the repository at this point in the history
Fixed TCString::Copy Bug
  • Loading branch information
AdventureT committed Dec 2, 2023
1 parent 12ab454 commit aa77a58
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*.lib
*.lib.*
*.exe.*
bin
bin-int
act.exe

# User-specific files
*.rsuser
Expand Down
5 changes: 1 addition & 4 deletions Tools/UnitTests/Source/TApplication/TApplication_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ TOSHI_NAMESPACE_USING

TEST_CASE("Create", "[TApplication]")
{
TApplication app;
TPCHAR argv[2] = {(TPCHAR)"Path", (TPCHAR)"Hello there"};
TBOOL bRes = app.Create("Test", 1, argv);
REQUIRE(bRes);

}
12 changes: 9 additions & 3 deletions Tools/UnitTests/Source/TKernel/TFile_Tests.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "TKernel/TFile.h"
#include <catch2/catch_test_macros.hpp>
#include <windows.h>
#include <fstream>
#include <direct.h>
#include "TKernel/TDebug.h"
#include "TKernel/Win/TNativeFileWin.h"

TOSHI_NAMESPACE_USING

TEST_CASE("Create File", "[TFile]")
{
TFileManager* pFileManager = new TFileManager();
TFile::Create("Test.txt", TMODE_CREATE);
TFile* f = TFile::Create("Test.txt", TMODE_CREATE);
f->Destroy();
std::ifstream f2("Test.txt");
REQUIRE(f2.good());
}
5 changes: 3 additions & 2 deletions Tools/UnitTests/Source/TKernel/TKernelInterface_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ TOSHI_NAMESPACE_USING

TEST_CASE("DumpInfo", "[TKernelInterface]")
{
TKernelInterface ki = TKernelInterface(0, (TPCHAR*)"", TTRUE);
ki.DumpInfo();

//TKernelInterface ki = TKernelInterface(0, (TPCHAR*)"", TTRUE);
//ki.DumpInfo();
}
19 changes: 17 additions & 2 deletions Tools/UnitTests/Source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
#include <catch2/catch_session.hpp>
#include "TApplication/TApplication.h"

int main(int argc, char* argv[]) {
TOSHI_NAMESPACE_USING

int result = Catch::Session().run(argc, argv);
class AApplication : public Toshi::TApplication
{
};

static AApplication g_oTheApp;

int main(int argc, char* argv[])
{
int result;
if (g_oTheApp.Create("UnitTests", argc, argv)) {
//g_oTheApp.Execute();
result = Catch::Session().run(argc, argv);
}

g_oTheApp.Destroy();

return result;
}
3 changes: 2 additions & 1 deletion Toshi/Include/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ typedef unsigned int TUINT32;
typedef unsigned __int64 TUINT64;
typedef short TSHORT;
typedef unsigned short TUSHORT;
typedef unsigned short TWCHAR;
typedef wchar_t TWCHAR;
typedef unsigned short* TPWCHAR;
typedef const char* TPCCHAR;
typedef char* TPCHAR;
typedef char TCHAR;
Expand Down
36 changes: 28 additions & 8 deletions Toshi/Include/TKernel/TFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ class TOSHI_EXPORT TFileManager
class TSysPathIter
{
public:
TSysPathIter(const TCString &a_rsSysPath) : m_sSysPath(a_rsSysPath), m_iPosition(-1) { };
TSysPathIter(const TSysPathIter &a_rOther) : m_sSysPath(a_rOther.m_sSysPath), m_iPosition(a_rOther.m_iPosition) { };
TSysPathIter(const TCString &a_rsSysPath)
{
m_sSysPath = a_rsSysPath;
m_iPosition = -1;
}

TSysPathIter(const TSysPathIter &a_rOther)
{
m_sSysPath = a_rOther.m_sSysPath;
m_iPosition = a_rOther.m_iPosition;
}

TBOOL First(TCString &a_rSysPath)
{
Expand Down Expand Up @@ -90,19 +99,24 @@ class TOSHI_EXPORT TFileManager
}

private:
const TCString& m_sSysPath; // 0x0
TINT m_iPosition; // 0x4
TCString m_sSysPath; // 0x0
TINT m_iPosition; // 0x4
};

TFileManager();
~TFileManager();

TFile* CreateFile(const TCString& a_sName, TUINT a_uiMode);
TFile* CreateFile(TCString const &a_sName, TUINT a_uiMode);
TFileSystem* FindFileSystem(const TCString& a_rFileSysName);
void MountFileSystem(TFileSystem* a_pFileSystem);
void UnmountFileSystem(TFileSystem* a_pFileSystem);

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

private:
void ValidateSystemPath();
Expand All @@ -125,6 +139,7 @@ enum TMODE : TUINT
TMODE_WRITEONLY = BITFIELD(1),
TMODE_READWRITE = BITFIELD(2),
TMODE_CREATE = BITFIELD(3),
TMODE_NOBUFFER = BITFIELD(4)
};

class TOSHI_EXPORT TFile
Expand All @@ -134,13 +149,18 @@ class TOSHI_EXPORT TFile
{
m_pFileSystem = a_pFileSystem;
}
virtual ~TFile() = default;
public:

enum TSEEK
{

TSEEK_SET,
TSEEK_CUR,
TSEEK_END
};


virtual TINT Read(TPVOID a_pBuffer, TINT m_iSize) = 0;
virtual TINT Write(void const *a_pBuffer, TINT m_iSize) = 0;
virtual TBOOL Seek(TINT a_iOffset, TSEEK a_eSeek) = 0;
virtual TINT Tell() = 0;
virtual TINT GetSize() = 0;
Expand All @@ -159,7 +179,7 @@ class TOSHI_EXPORT TFile
void Destroy();
TFileSystem* GetFileSystem() const { return m_pFileSystem; }

private:
protected:
// 0x0 vftable
TFileSystem* m_pFileSystem; // 0x4
};
Expand Down
19 changes: 2 additions & 17 deletions Toshi/Include/TKernel/TMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,5 @@ inline static TMemory g_oMemManager;

TOSHI_NAMESPACE_END

TPVOID TOSHI_EXPORT __stdcall tmalloc(TINT a_iSize, TPCHAR a_pBuffer, TINT a_iUnk)
{
#ifdef TOSHI_NOTFINAL
return malloc(a_iSize);
#else
return Toshi::TMemory::GetMemMangager().Alloc(a_iSize, 16, Toshi::TMemory::GetGlobalBlock(), a_pBuffer, a_iUnk);
#endif
}

void TOSHI_EXPORT __stdcall tfree(TPVOID a_pMem)
{
#ifdef TOSHI_NOTFINAL
free(a_pMem);
#else
Toshi::TMemory::GetMemMangager().Free(a_pMem);
#endif
}
TPVOID TOSHI_EXPORT __stdcall tmalloc(TINT a_iSize, TPCHAR a_pBuffer, TINT a_iUnk);
void TOSHI_EXPORT __stdcall tfree(TPVOID a_pMem);
69 changes: 69 additions & 0 deletions Toshi/Include/TKernel/Win/TNativeFileWin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once
#include "../TFile.h"

#define BUFFER_SIZE 0x800

TOSHI_NAMESPACE_BEGIN

class TOSHI_EXPORT TNativeFileSystem : public TFileSystem
{
public:
TNativeFileSystem(TPCCHAR a_pcName);

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;

private:
HANDLE m_hFileSystem; // 0x1C
};

class TOSHI_EXPORT TNativeFileManager : public TFileManager
{

};

class TOSHI_EXPORT TNativeFile : public TFile
{
friend class TNativeFileSystem;
public:

virtual TINT Read(TPVOID a_pBuffer, TINT a_iSize) override;
virtual TINT Write(void const* a_pBuffer, TINT a_iSize) override;
virtual TBOOL Seek(TINT a_iOffset, TSEEK a_eSeek) override;
virtual TINT Tell() override { return m_iPosition; }
virtual TINT GetSize() override;
virtual TUINT64 GetDate() override;
virtual TINT GetCChar() override;
virtual TINT GetWChar() override;
virtual TINT PutCChar(TCHAR a_cChar) override;
virtual TINT PutWChar(TWCHAR a_wcChar) override;
virtual TINT VCPrintf(TCHAR const* a_pFormat, va_list a_args) override;
virtual TINT VWPrintf(TWCHAR const* a_pFormat, va_list a_args) override;

protected:
TNativeFile(TNativeFileSystem *a_pFileManager);
virtual ~TNativeFile() = default;

TBOOL Open(const TCString &a_rFileName, TUINT a_uiMode);
void Close();

private:
TINT ReadUnbuffered(TPVOID a_pBuffer, TINT a_iSize);
TBOOL LoadBuffer(TINT a_iBufferPos);
TINT FlushWriteBuffer();

private:
HANDLE m_hFile; // 0x8
TINT m_iPosition; // 0xC
TINT m_iBufferPosition; // 0x10
TINT m_iPrevBufferPos; // 0x14
TINT m_iLastBufferSize; // 0x18
TPVOID m_pBuffer; // 0x1C
TPVOID m_pWriteBuffer; // 0x20
TINT m_iWriteBufferUsed; // 0x24
TBOOL m_bWriteBuffered; // 0x28
};

TOSHI_NAMESPACE_END
9 changes: 4 additions & 5 deletions Toshi/Source/TKernel/TCString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ TINT TCString::Compare(TPCCHAR a_pcString, int a_iLength) const

void TCString::Copy(const TCString& a_rOther, TINT a_iLength)
{
if (*this != a_rOther)
{
if (m_iStrLen < a_iLength || a_iLength == -1) a_iLength = m_iStrLen;
if (*this != a_rOther) {
if (a_rOther.m_iStrLen < a_iLength || a_iLength == -1) a_iLength = a_rOther.m_iStrLen;
AllocBuffer(a_iLength);
TSystem::MemCopy(m_pBuffer, a_rOther.m_pBuffer, a_iLength);
m_pBuffer[a_iLength] = '\0';
}
}

void TCString::Copy(TPCCHAR a_pcString, TINT a_iLength)
{
if (m_pBuffer != a_pcString)
{
if (m_pBuffer != a_pcString) {
TINT iLength = a_pcString ? TSystem::StringLength(a_pcString) : 0;
if (iLength < a_iLength || a_iLength == -1) a_iLength = iLength;
AllocBuffer(a_iLength, TTRUE);
Expand Down
6 changes: 3 additions & 3 deletions Toshi/Source/TKernel/TFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TFileManager::~TFileManager()
s_pFileManager = TNULL;
}

TFile* TFileManager::CreateFile(const TCString& a_sName, TUINT a_uiMode)
TFile* TFileManager::CreateFile(TCString const& a_sName, TUINT a_uiMode)
{
TASSERT(a_sName.Length() > 0);
ValidateSystemPath();
Expand Down Expand Up @@ -77,14 +77,14 @@ TFileSystem* TFileManager::FindFileSystem(const TCString& a_rFileSysName)

void TFileManager::MountFileSystem(TFileSystem* a_pFileSystem)
{
TASSERT(FindFileSystem(a_pFileSystem->GetName()) == NULL);
TASSERT(FindFileSystem(a_pFileSystem->GetName()) == TNULL);
m_aInvalidated.InsertTail(a_pFileSystem);
InvalidateSystemPath();
}

void TFileManager::UnmountFileSystem(TFileSystem* a_pFileSystem)
{
TASSERT(FindFileSystem(a_pFileSystem->GetName()) == NULL);
TASSERT(FindFileSystem(a_pFileSystem->GetName()) == TNULL);
a_pFileSystem->Remove();
InvalidateSystemPath();
}
Expand Down
15 changes: 15 additions & 0 deletions Toshi/Source/TKernel/TKernelInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "TKernelInterface.h"
#include <windows.h>
#include <direct.h> // _getcwd
#include <TKernel/TFile.h>
#include TOSHI_MULTIPLATFORM(TNativeFile)

TOSHI_NAMESPACE_USING

Expand All @@ -8,6 +11,18 @@ IMPLEMENT_DYNAMIC(TKernelInterface, TObject);
TKernelInterface::TKernelInterface(TINT argc, TPCHAR* const argv, TBOOL a_bVerbose)
{
TWARNING("TKernelInterface::TKernelInterface() not implemented");
TCHAR pPath[260];
TPCHAR pBuffer = _getcwd(pPath, sizeof(pPath));
TVALIDADDRESS(pBuffer);
TFileManager *pFileManager = new TFileManager();
TVALIDADDRESS(pFileManager);
TNativeFileSystem *pLocalSystem = new TNativeFileSystem("local");
TVALIDADDRESS(pLocalSystem);
pLocalSystem->SetPrefix(pBuffer);
TNativeFileSystem *pAbsSystem = new TNativeFileSystem("abs");
TVALIDADDRESS(pAbsSystem);
pAbsSystem->SetPrefix("");
pFileManager->SetSystemPath("local");
}

TBOOL TKernelInterface::Update()
Expand Down
18 changes: 18 additions & 0 deletions Toshi/Source/TKernel/TMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ TBOOL TMemory::Free(TPVOID a_pMem)
}
return TBOOL();
}

TPVOID TOSHI_EXPORT __stdcall tmalloc(TINT a_iSize, TPCHAR a_pBuffer, TINT a_iUnk)
{
#ifdef TOSHI_NOTFINAL
return malloc(a_iSize);
#else
return Toshi::TMemory::GetMemMangager().Alloc(a_iSize, 16, Toshi::TMemory::GetGlobalBlock(), a_pBuffer, a_iUnk);
#endif
}

void TOSHI_EXPORT __stdcall tfree(TPVOID a_pMem)
{
#ifdef TOSHI_NOTFINAL
free(a_pMem);
#else
Toshi::TMemory::GetMemMangager().Free(a_pMem);
#endif
}
Loading

0 comments on commit aa77a58

Please sign in to comment.