Skip to content

Commit 1330cb4

Browse files
committed
Improved code design of CFileReader
1 parent 1d9eb2b commit 1330cb4

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <StdInc.h>
2+
3+
CFileReader::CFileReader ( void )
4+
{
5+
pFilePath = nullptr;
6+
pFilePointer = nullptr;
7+
pFileDataBuffer = nullptr;
8+
u32FileLength = 0;
9+
u32BytesReadFromBuffer = 0;
10+
}
11+
12+
CFileReader::~CFileReader ( void )
13+
{
14+
UnloadFile ();
15+
}
16+
17+
BOOL CFileReader::CreateLoader ( const char * pFileToRead )
18+
{
19+
SetLoaderFilePath ( pFileToRead );
20+
if ( !OpenFile ( ) || !GetFileLength ( ) )
21+
return FALSE;
22+
23+
AllocateBufferMemory ( );
24+
pFileDataBuffer [ u32FileLength ] = '\0';
25+
return TRUE;
26+
}
27+
28+
BOOL CFileReader::LoadFile ( void )
29+
{
30+
if ( !LoadToMemory ( ) )
31+
return FALSE;
32+
33+
std::cout << "Length: " << u32FileLength << std::endl;
34+
return TRUE;
35+
}
36+
37+
void CFileReader::UnloadFile ( void )
38+
{
39+
if ( pFileDataBuffer != nullptr )
40+
{
41+
delete[] pFileDataBuffer;
42+
pFileDataBuffer = nullptr;
43+
}
44+
if ( pFilePointer != nullptr )
45+
{
46+
fclose ( pFilePointer );
47+
}
48+
}
49+
50+
void CFileReader::ReadBytes ( void * pDestination, const UINT BytesToRead )
51+
{
52+
const UINT ReadOffset = u32BytesReadFromBuffer;
53+
u32BytesReadFromBuffer += BytesToRead;
54+
memcpy ( pDestination, pFileDataBuffer + ReadOffset, BytesToRead );
55+
}
56+
57+
std::string CFileReader::ReadString ( UINT u32SizeInBytes )
58+
{
59+
std::string String;
60+
String.resize ( u32SizeInBytes );
61+
62+
for ( UINT i = 0; i < u32SizeInBytes; i++ )
63+
{
64+
const UINT ReadOffset = u32BytesReadFromBuffer;
65+
String [ i ] = pFileDataBuffer [ ReadOffset ];
66+
u32BytesReadFromBuffer++;
67+
}
68+
return String;
69+
}
70+
71+
void CFileReader::ReadCString ( char * pDestination, const UINT u32BytesToRead )
72+
{
73+
const UINT u32ReadOffset = u32BytesReadFromBuffer;
74+
u32BytesReadFromBuffer += u32BytesToRead;
75+
memcpy ( pDestination, pFileDataBuffer + u32ReadOffset, u32BytesToRead );
76+
*( pDestination + (u32BytesToRead- 1) ) = '\0';
77+
}
78+
79+
void CFileReader::SkipBytes ( UINT u32BytesToSkip )
80+
{
81+
u32BytesReadFromBuffer += u32BytesToSkip;
82+
}
83+
84+
inline void CFileReader::SetLoaderFilePath ( const char * szFileToRead )
85+
{
86+
pFilePath = szFileToRead;
87+
}
88+
89+
BOOL CFileReader::OpenFile ( void )
90+
{
91+
errno_t FileOpen = fopen_s ( &pFilePointer, pFilePath, "rb" );
92+
if ( FileOpen != 0 )
93+
{
94+
std::cout << "Failed to open file";
95+
return FALSE;
96+
}
97+
return TRUE;
98+
}
99+
100+
BOOL CFileReader::GetFileLength ( void )
101+
{
102+
if ( !pFilePointer )
103+
return FALSE;
104+
105+
if ( fseek ( pFilePointer, 0, SEEK_END ) != 0 )
106+
{
107+
std::cout << "Failed to set cursor to end of file." << std::endl;
108+
return FALSE;
109+
}
110+
111+
u32FileLength = ftell ( pFilePointer );
112+
if ( u32FileLength == -1L )
113+
{
114+
std::cout << "Failed to get length of file (ftell)" << std::endl;
115+
return FALSE;
116+
}
117+
118+
rewind ( pFilePointer );
119+
return TRUE;
120+
}
121+
122+
void CFileReader::AllocateBufferMemory ( void )
123+
{
124+
if ( !pFilePointer )
125+
return;
126+
127+
pFileDataBuffer = new char [ u32FileLength + 1 ];
128+
}
129+
130+
BOOL CFileReader::LoadToMemory ( void )
131+
{
132+
if ( !pFilePointer )
133+
return FALSE;
134+
135+
UINT u32ReadSize = fread ( pFileDataBuffer, sizeof ( char ), u32FileLength, pFilePointer );
136+
if ( u32ReadSize != u32FileLength )
137+
{
138+
std::cout << "loadToMemory Failed. ReadSize != u32FileLength";
139+
return FALSE;
140+
}
141+
return TRUE;
142+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#ifndef CFILEREADER_H
3+
#define CFILEREADER_H
4+
5+
#include <Windows.h>
6+
#include <iostream>
7+
#include <fstream>
8+
9+
class CFileReader
10+
{
11+
12+
public:
13+
CFileReader ( void );
14+
~CFileReader ( void );
15+
16+
BOOL CreateLoader ( const char * szFileToRead );
17+
BOOL LoadFile ( void );
18+
void UnloadFile ( void );
19+
20+
template < class T >
21+
void ReadBuffer ( T * pDestination )
22+
{
23+
const UINT u32ReadOffset = u32BytesReadFromBuffer;
24+
u32BytesReadFromBuffer += sizeof ( T );
25+
*pDestination = *reinterpret_cast < T * > ( pFileDataBuffer + u32ReadOffset );
26+
}
27+
28+
void ReadBytes ( void * pDestination, const UINT u32BytesToRead );
29+
std::string ReadString ( UINT u32SizeInBytes );
30+
void ReadCString ( char * pDestination, const UINT u32BytesToRead );
31+
void SkipBytes ( UINT u32BytesToSkip );
32+
33+
private:
34+
inline void SetLoaderFilePath ( const char * szFileToRead );
35+
BOOL OpenFile ( void );
36+
BOOL GetFileLength ( void );
37+
void AllocateBufferMemory ( void );
38+
BOOL LoadToMemory ( void );
39+
40+
const char * pFilePath;
41+
FILE * pFilePointer;
42+
char * pFileDataBuffer;
43+
UINT u32FileLength;
44+
UINT u32BytesReadFromBuffer;
45+
};
46+
47+
#endif

0 commit comments

Comments
 (0)