OpenBarnyard
 
Loading...
Searching...
No Matches
TTSFO Class Reference

#include <TTRB.h>

Inheritance diagram for TTSFO:
TTSF

Classes

struct  HunkMark
 

Public Types

enum  ERROR_ : ERROR { ERROR_OK , ERROR_UNKNOWN , ERROR_FILE }
 
typedef TUINT8 ERROR
 

Public Member Functions

 TTSFO ()
 
 ~TTSFO ()
 
TUINT32 WriteAlignmentPad ()
 Aligns current position to 4.
 
TTSFO::ERROR Create (const TCHAR *filepath, const TCHAR *magic="TRBF", Endianess endianess=Endianess_Little)
 Creates file for writing.
 
void Close ()
 Closes the file.
 
TSIZE BeginForm (const TCHAR *name)
 Begin a new form and saves it's info.
 
TSIZE EndForm ()
 Ends the current form if it exists.
 
TBOOL OpenHunk (HunkMark *hunkMark, const TCHAR *hunkName)
 Opens new hunk.
 
TBOOL CloseHunk (HunkMark *hunkMark)
 Closes the hunk.
 
TUINT32 WriteHunk (TUINT32 hunkName, void *buffer=nullptr, TSIZE bufferSize=0)
 Writes a hunk of data.
 
template<class T>
TUINT32 Write (const T &value)
 
TUINT32 WriteRaw (const void *buffer, TSIZE size)
 
TUINT32 WriteCompressed (const void *buffer, TSIZE size)
 
void WriteBool (TBOOL value)
 
void WriteInt8 (int8_t value)
 
void WriteUInt8 (TUINT8 value)
 
void WriteUInt16 (TUINT16 value)
 
void WriteInt32 (TINT32 value)
 
void WriteUInt32 (TUINT32 value)
 
void WriteFloat (float value)
 
void WriteVector3 (TVector3 &value)
 
void WriteVector4 (TVector4 &value)
 
void WriteQuaternion (TQuaternion &value)
 
- Public Member Functions inherited from TTSF
 TTSF ()
 
 ~TTSF ()=default
 
Endianess GetEndianess () const
 

Additional Inherited Members

- Static Public Attributes inherited from TTSF
static constexpr TUINT32 IDMAGICB = TFourCC( "TSFB" )
 
static constexpr TUINT32 IDMAGICL = TFourCC( "TSFL" )
 
- Protected Attributes inherited from TTSF
Endianess m_Endianess
 
TFilem_pFile
 

Detailed Description

Definition at line 117 of file TTRB.h.

Member Typedef Documentation

◆ ERROR

Definition at line 120 of file TTRB.h.

Member Enumeration Documentation

◆ ERROR_

Enumerator
ERROR_OK 
ERROR_UNKNOWN 
ERROR_FILE 

Definition at line 121 of file TTRB.h.

122 {
123 ERROR_OK,
126 };
@ ERROR_OK
Definition TTRB.h:123
@ ERROR_UNKNOWN
Definition TTRB.h:124
@ ERROR_FILE
Definition TTRB.h:125

Constructor & Destructor Documentation

◆ TTSFO()

TTSFO::TTSFO ( )
inline

Definition at line 135 of file TTRB.h.

136 : m_PositionCount( 0 ), m_Positions() {}

◆ ~TTSFO()

TTSFO::~TTSFO ( )
inline

Definition at line 137 of file TTRB.h.

137{ Close(); }
void Close()
Closes the file.
Definition TTSF.cpp:228

Member Function Documentation

◆ BeginForm()

TSIZE TTSFO::BeginForm ( const TCHAR * name)

Begin a new form and saves it's info.

Parameters
name- 4 bytes long value which will be written after the hunk
Returns
number of written bytes

Definition at line 238 of file TTSF.cpp.

239{
240 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
241
242 TTSF::Hunk hunk{ TFourCC( "FORM" ), 0 };
243 auto nameValue = TFourCC( name );
244
245 if ( m_Endianess == Endianess_Big )
246 {
247 hunk.Name = PARSEDWORD_BIG( hunk.Name );
248 nameValue = PARSEDWORD_BIG( nameValue );
249 }
250
251 m_Positions[ m_PositionCount++ ] = m_pFile->Tell();
252
253 auto written1 = Write( hunk );
254 auto written2 = Write( nameValue );
255
256 return written1 + written2;
257}
#define TASSERT(X,...)
Definition Defines.h:138
@ Endianess_Big
Definition Endianness.h:9
TFORCEINLINE constexpr TUINT32 TFourCC(const TCHAR str[4])
Definition Helpers.h:15
#define TNULL
Definition Typedefs.h:23
TFile * m_pFile
Definition TTRB.h:70
Endianess m_Endianess
Definition TTRB.h:69
TUINT32 Write(const T &value)
Definition TTRB.h:216

◆ Close()

void TTSFO::Close ( )

Closes the file.

Definition at line 228 of file TTSF.cpp.

229{
230 if ( m_pFile != TNULL )
231 {
232 EndForm();
233 m_pFile->Destroy();
234 m_pFile = TNULL;
235 }
236}
TSIZE EndForm()
Ends the current form if it exists.
Definition TTSF.cpp:259

◆ CloseHunk()

TBOOL TTSFO::CloseHunk ( HunkMark * hunkMark)

Closes the hunk.

Parameters
hunkMark- pointer to TTSFO::HunkMark which has to be closed
Returns
always TTRUE

Definition at line 295 of file TTSF.cpp.

296{
297 TASSERT( hunkMark != TNULL, "HunkMark is TNULL" );
298 auto oldPos = m_pFile->Tell();
299
300 m_pFile->Seek( hunkMark->Pos, TSEEK_SET );
301 WriteHunk( hunkMark->Name, TNULL, ( oldPos - hunkMark->Pos ) - sizeof( TTSF::Hunk ) );
302 m_pFile->Seek( oldPos, TSEEK_SET );
304
305 return TTRUE;
306}
@ TSEEK_SET
Definition TFile.h:30
#define TTRUE
Definition Typedefs.h:25
TUINT32 WriteAlignmentPad()
Aligns current position to 4.
Definition TTRB.h:144
TUINT32 WriteHunk(TUINT32 hunkName, void *buffer=nullptr, TSIZE bufferSize=0)
Writes a hunk of data.
Definition TTSF.cpp:308

◆ Create()

TTSFO::ERROR TTSFO::Create ( const TCHAR * filepath,
const TCHAR * magic = "TRBF",
Endianess endianess = Endianess_Little )

Creates file for writing.

Parameters
filepath- path to the file
magic- magic value which is placed after the first hunk
Returns
TTRB::ERROR_OK if successful

Definition at line 198 of file TTSF.cpp.

199{
201
202 if ( m_pFile != TNULL )
203 {
204 TTSF::Hunk hunk{ TFourCCLE( "TSFL" ), 0 };
205 TUINT32 magicValue = TFourCC( magic );
206
207 if ( endianess == Endianess_Big )
208 {
209 hunk.Name = TFourCCLE( "TSFB" );
210 hunk.Size = PARSEDWORD_BIG( hunk.Size );
211 }
212
213 Write( hunk );
214 Write( magicValue );
215
216 m_Endianess = endianess;
217 m_Positions[ m_PositionCount ] = 0;
218 m_PositionCount += 1;
219
220 return TTRB::ERROR_OK;
221 }
222 else
223 {
224 return TTSFO::ERROR_FILE;
225 }
226}
@ TFILEMODE_WRITE
Definition TFile.h:39
@ TFILEMODE_CREATENEW
Definition TFile.h:41
TFORCEINLINE TUINT32 TFourCCLE(const TCHAR str[4])
Definition Helpers.h:21
uint32_t TUINT32
Definition Typedefs.h:13
static TFile * Create(const TString8 &a_rcFilename, TFILEMODE a_eMode=TFILEMODE_READ)
Definition TFile.cpp:29
@ ERROR_OK
Definition TTRB.h:258

◆ EndForm()

TSIZE TTSFO::EndForm ( )

Ends the current form if it exists.

Returns
size of the form

Definition at line 259 of file TTSF.cpp.

260{
261 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
262
263 if ( m_PositionCount > 0 )
264 {
265 auto formPosition = m_Positions[ --m_PositionCount ];
266
267 auto oldPos = m_pFile->Tell();
268 auto formSize = ( oldPos - formPosition ) - 8;
269
270 if ( m_Endianess == Endianess_Big )
271 {
272 formSize = PARSEDWORD_BIG( formSize );
273 }
274
275 m_pFile->Seek( formPosition + 4, TSEEK_SET );
276 Write( formSize );
277 m_pFile->Seek( oldPos, TSEEK_SET );
278
279 return formSize;
280 }
281
282 return 0;
283}

◆ OpenHunk()

TBOOL TTSFO::OpenHunk ( HunkMark * hunkMark,
const TCHAR * hunkName )

Opens new hunk.

Parameters
hunkMark- pointer to TTSFO::HunkMark which has to be opened
hunkName- 4 bytes long name of the hunk
Returns
always TTRUE

Definition at line 285 of file TTSF.cpp.

286{
287 TASSERT( hunkMark != TNULL, "HunkMark is TNULL" );
288 hunkMark->Name = TFourCC( hunkName );
289 hunkMark->Pos = m_pFile->Tell();
290
291 WriteHunk( hunkMark->Name, TNULL, 0 );
292 return TTRUE;
293}

◆ Write()

template<class T>
TUINT32 TTSFO::Write ( const T & value)
inline

Definition at line 216 of file TTRB.h.

217 {
218 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
219 return m_pFile->Write( &value, sizeof( T ) );
220 }

◆ WriteAlignmentPad()

TUINT32 TTSFO::WriteAlignmentPad ( )
inline

Aligns current position to 4.

Returns
number of written bytes

Definition at line 144 of file TTRB.h.

145 {
146 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
147 static TCHAR s_AlignBuffer[ 4 ] = { 0, 0, 0, 0 };
148 TUINT8 alignValue = 4 - ( m_pFile->Tell() & 3 );
149
150 if ( alignValue != 4 )
151 {
152 return m_pFile->Write( s_AlignBuffer, alignValue );
153 }
154
155 return 0;
156 }
char TCHAR
Definition Typedefs.h:20
uint8_t TUINT8
Definition Typedefs.h:17

◆ WriteBool()

void TTSFO::WriteBool ( TBOOL value)
inline

Definition at line 236 of file TTRB.h.

236{ Write( value ); }

◆ WriteCompressed()

TUINT32 TTSFO::WriteCompressed ( const void * buffer,
TSIZE size )
inline

Definition at line 228 of file TTRB.h.

229 {
230 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
231 TUINT32 writtenSize = TCompress::Compress( m_pFile, (TBYTE*)buffer, size, 0, m_Endianess == Endianess_Big );
233 return writtenSize;
234 }
uint8_t TBYTE
Definition Typedefs.h:19
static size_t Compress(TFile *file, TBYTE *data, TUINT32 size, TUINT32 unused, TBOOL isBigEndian)

◆ WriteFloat()

void TTSFO::WriteFloat ( float value)
inline

Definition at line 242 of file TTRB.h.

242{ Write( value ); }

◆ WriteHunk()

TUINT32 TTSFO::WriteHunk ( TUINT32 hunkName,
void * buffer = nullptr,
TSIZE bufferSize = 0 )

Writes a hunk of data.

Parameters
hunkName- magic value that is used to identificate hunk (use TMAKEFOUR to get it)
buffer- pointer to the buffer of data (can be TNULL)
bufferSize- size of the buffer (can be 0)
Returns
number of written bytes

Definition at line 308 of file TTSF.cpp.

309{
310 TTSF::Hunk hunk{ hunkName, bufferSize };
311
312 // Convert endianess
313 if ( m_Endianess == Endianess_Big )
314 {
315 hunk.Name = PARSEDWORD_BIG( hunk.Name );
316 hunk.Size = PARSEDWORD_BIG( hunk.Size );
317 }
318
319 // Write hunk
320 TSIZE writtenSize = Write( hunk );
321
322 // Write buffer
323 if ( buffer != TNULL && bufferSize > 0 )
324 {
325 writtenSize += m_pFile->Write( buffer, bufferSize );
326 }
327
328 // Return num of written bytes
329 return writtenSize + TTSFO::WriteAlignmentPad();
330}
size_t TSIZE
Definition Typedefs.h:9

◆ WriteInt32()

void TTSFO::WriteInt32 ( TINT32 value)
inline

Definition at line 240 of file TTRB.h.

240{ Write( value ); }

◆ WriteInt8()

void TTSFO::WriteInt8 ( int8_t value)
inline

Definition at line 237 of file TTRB.h.

237{ Write( value ); }

◆ WriteQuaternion()

void TTSFO::WriteQuaternion ( TQuaternion & value)
inline

Definition at line 245 of file TTRB.h.

245{ Write( value ); }

◆ WriteRaw()

TUINT32 TTSFO::WriteRaw ( const void * buffer,
TSIZE size )
inline

Definition at line 222 of file TTRB.h.

223 {
224 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
225 return m_pFile->Write( buffer, size );
226 }

◆ WriteUInt16()

void TTSFO::WriteUInt16 ( TUINT16 value)
inline

Definition at line 239 of file TTRB.h.

239{ Write( value ); }

◆ WriteUInt32()

void TTSFO::WriteUInt32 ( TUINT32 value)
inline

Definition at line 241 of file TTRB.h.

241{ Write( value ); }

◆ WriteUInt8()

void TTSFO::WriteUInt8 ( TUINT8 value)
inline

Definition at line 238 of file TTRB.h.

238{ Write( value ); }

◆ WriteVector3()

void TTSFO::WriteVector3 ( TVector3 & value)
inline

Definition at line 243 of file TTRB.h.

243{ Write( value ); }

◆ WriteVector4()

void TTSFO::WriteVector4 ( TVector4 & value)
inline

Definition at line 244 of file TTRB.h.

244{ Write( value ); }

The documentation for this class was generated from the following files: