OpenBarnyard
 
Loading...
Searching...
No Matches
TFile.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TFile.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12// $Barnyard: FUNCTION 006bddc0
14{
15 m_pFileSystem = pFS;
16}
17
18TFile::TFile( const TFile& other )
19{
20 m_pFileSystem = other.GetFileSystem();
21}
22
23// $Barnyard: FUNCTION 006bddf0
25{
26}
27
28// $Barnyard: FUNCTION 006be700
29TFile* TFile::Create( const TString8& filename, TFILEMODE mode )
30{
31 TASSERT( filename.IsIndexValid( 0 ), "filename is empty" );
32
33 TFile* pFile = TFileManager::GetSingletonSafe()->CreateFile( filename, mode );
34
35 if ( pFile != TNULL )
36 {
37 TTRACE( "TFile::Create - Created file %s with mode %hu\n", filename.GetString(), mode );
38 }
39 else
40 {
41 TTRACE( "TFile::Create - Failed to create file %s with mode %hu\n", filename.GetString(), mode );
42 }
43
44 return pFile;
45}
46
47// $Barnyard: FUNCTION 006bdff0
48TString8 TFile::ConcatPath( const TString8& a_rcPath1, const TString8& a_rcPath2 )
49{
50 TString8 strResult;
51
52 TINT iPath1StartPos = 0;
53 TINT iPath2StartPos = 0;
54
55 // Copy filesystem/volume name from a_rcPath1 or a_rcPath2
56 // prioritizing the one in a_rcPath2
57 TINT iColonPos = a_rcPath2.FindReverse( ':' );
58
59 if ( iColonPos == -1 )
60 {
61 iColonPos = a_rcPath1.FindReverse( ':' );
62
63 if ( iColonPos > -1 )
64 {
65 // Copy volume/filesystem name from the first path
66 iPath1StartPos = iColonPos + 1;
67 strResult.Concat( a_rcPath1, iPath1StartPos );
68 }
69 }
70 else
71 {
72 // Copy volume/filesystem name from the second path
73 iPath2StartPos = iColonPos + 1;
74 strResult.Concat( a_rcPath2, iPath2StartPos );
75
76 // Ignore everything before the ':' character in the first path
77 iPath1StartPos = a_rcPath1.FindReverse( ':' ) + 1;
78 }
79
80 TINT iPath1Length = a_rcPath1.Length();
81 TINT iPath2Length = a_rcPath2.Length();
82
83 if ( iPath2StartPos < iPath2Length &&
84 ( a_rcPath2[ iPath2StartPos ] == '/' || a_rcPath2[ iPath2StartPos ] == '\\' ) )
85 {
86 // The second path starts from a filesystem/volume root so ignore the first path
87 strResult += a_rcPath2.GetString( iPath2StartPos );
88 }
89 else
90 {
91 // Concat the first path with the second one trimming parts like 'C:' or 'native:' and etc.
92 strResult += a_rcPath1.GetString( iPath1StartPos );
93 if ( iPath1StartPos < iPath1Length && iPath2StartPos < iPath2Length &&
94 a_rcPath1[ iPath1Length - 1 ] != '/' && a_rcPath1[ iPath1Length - 1 ] != '\\' )
95 {
96 strResult += "/";
97 }
98
99 strResult += a_rcPath2.GetString( iPath2StartPos );
100 }
101
102 return SimplifyPath( strResult );
103}
104
105// $Barnyard: FUNCTION 006bde20
107{
108 TString8 strPath = a_rcPath;
109 FixPathSlashesBack( strPath );
110
111 TINT iPos = 0;
112 TString8 strResult;
113
114 // Keep filesystem/volume/protocol name in the simplified path and leave it as it is
115 TINT iColonPos = strPath.FindReverse( ':' );
116 if ( iColonPos > -1 )
117 {
118 iPos = iColonPos + 1;
119 strResult.Concat( strPath, iPos );
120 }
121
122 if ( strPath[ iPos ] == '/' )
123 {
124 iPos += 1;
125 strResult += "/";
126 }
127
128 TINT iInitialResultLength = strResult.Length();
129 TINT iResultLength = iInitialResultLength;
130
131 strPath += "/";
132 TINT iSlashPos = strPath.Find( '/', iPos );
133
134 do
135 {
136 // Is the path over?
137 if ( iSlashPos < 0 )
138 {
139 if ( iInitialResultLength < strResult.Length() )
140 {
141 strResult.Truncate( strResult.Length() - 1 );
142 }
143
144 return strResult;
145 }
146
147 if ( !strncmp( strPath.GetString( iPos ), "../", 3 ) )
148 {
149 if ( iResultLength == strResult.Length() )
150 {
151 // Can't go back to a previous dir since it's the only one
152 strResult += "../";
153 iResultLength += 3;
154 }
155 else
156 {
157 // Truncate the path removing the current dir
158 TINT iTruncateTo;
159
160 if ( strResult.Length() > 1 && ( iTruncateTo = strResult.FindReverse( '/', strResult.Length() - 2 ), iResultLength <= iTruncateTo ) )
161 {
162 strResult.Truncate( iTruncateTo + 1 );
163 }
164 else
165 {
166 strResult.Truncate( iResultLength );
167 }
168 }
169 }
170 // Add anything except './'
171 else if ( strncmp( strPath.GetString( iPos ), "./", 2 ) != 0 )
172 {
173 strResult.Concat( strPath.GetString( iPos ), ( iSlashPos - iPos ) + 1 );
174 }
175
176 iPos = iSlashPos + 1;
177 iSlashPos = strPath.Find( '/', iPos );
178
179 } while ( TTRUE );
180}
181
182// $Barnyard: FUNCTION 006be310
183// $Barnyard: FUNCTION 006bdcf0
185{
186 TVALIDPTR( m_pFileSystem );
187 m_pFileSystem->DestroyFile( this );
188}
189
Core file system interface for the Toshi engine.
TUINT16 TFILEMODE
Definition TFile.h:35
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TTRACE(...)
Definition Defines.h:155
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TVALIDPTR(PTR)
Definition Defines.h:139
void FixPathSlashesBack(TString8 &a_rPath)
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TTRUE
Definition Typedefs.h:25
void Destroy()
Definition TFile.cpp:184
static TString8 ConcatPath(const TString8 &a_rcPath1, const TString8 &a_rcPath2)
Definition TFile.cpp:48
static TString8 SimplifyPath(const TString8 &a_rcPath)
Definition TFile.cpp:106
static TFile * Create(const TString8 &a_rcFilename, TFILEMODE a_eMode=TFILEMODE_READ)
Definition TFile.cpp:29
virtual ~TFile()
Definition TFile.cpp:24
TFile(TFileSystem *a_pFS)
Definition TFile.cpp:13
TFileSystem * GetFileSystem() const
Definition TFile.h:216
TFile * CreateFile(const TString8 &a_strName, TFILEMODE flags)
static TFORCEINLINE TFileManager * GetSingletonSafe()
Definition TSingleton.h:37
TString8 & Concat(const TString8 &str, TINT size=-1)
Definition TString8.h:66
TINT Find(TCHAR character, TINT pos=0) const
Definition TString8.cpp:107
TINT FindReverse(TCHAR a_findChar, TINT pos=-1) const
Definition TString8.cpp:234
TBOOL IsIndexValid(TINT a_iIndex) const
Definition TString8.h:103
void Truncate(TINT length)
Definition TString8.cpp:257
TINT Length() const
Definition TString8.h:93
const TCHAR * GetString(TINT a_iIndex=0) const
Definition TString8.cpp:286