1 #include "CustomFile.h"
3 extern void* MemFOpen( uint8_t* buffer, size_t dataSize, const char * const mode );
4 extern void MemFClose( const void *fp );
5 extern int MemFRead( void* buf, int eleSize, int count, const void *fp );
6 extern void MemFWrite( void *buf, int size, const void *fp );
7 extern int MemFSeek( const void *fp, int offset, int origin );
8 extern int MemFTell( const void *fp );
9 extern bool MemFEof( const void *fp );
11 extern void* OriginalFOpen( const char *name, const char *mode );
12 extern int OriginalFClose( const void *fp );
13 extern int OriginalFRead( void* buf, int eleSize, int count, const void *fp );
14 extern void OriginalFWrite( void *buf, int size, const void *fp );
15 extern void OriginalFWrite( void *buf, int eleSize, int count, const void *fp );
16 extern int OriginalFSeek( const void *fp, int offset, int origin );
17 extern int OriginalFTell( const void *fp );
18 extern bool OriginalFEof( const void *fp );
22 std::string GetRealName(const char* name)
24 if (nullptr != name && '*' == name[0])
28 const char *p = name + 1;
30 while (0 != *p && '*' != *p)
32 envName.push_back(*p);
38 char *envValue = std::getenv(envName.c_str());
49 return std::string(name);
56 FILE* FOpen( const char *name, const char *mode )
58 if( NULL != name && '*' == name[0] )
60 std::string realName = GetRealName( name );
61 FILE* ret = (FILE*)OriginalFOpen( realName.c_str(), mode );
70 return (FILE*)OriginalFOpen( name, mode );
74 int FClose( const void* fp )
76 if( -1 == *( (char*)fp + 4 ) )
83 return OriginalFClose( fp );
87 FILE* FMemopen( void* buffer, size_t dataSize, const char * mode )
89 return (FILE*)MemFOpen( ( uint8_t*)buffer, dataSize, mode );
92 int FRead( void* buf, int eleSize, int count, const void *fp )
94 if( -1 == *( (char*)fp + 4 ) )
96 return MemFRead( buf, eleSize, count, fp );
100 return OriginalFRead( buf, eleSize, count, fp );
104 void FWrite( void *buf, int size, const void *fp )
106 if( -1 == *( (char*)fp + 4 ) )
108 MemFWrite( buf, size, fp );
112 OriginalFWrite( buf, size, fp );
116 unsigned int FWrite( const char *buf, unsigned int eleSize, unsigned int count, void *fp )
118 if( -1 == *( (char*)fp + 4 ) )
120 MemFWrite( (void*)buf, eleSize * count, fp );
124 OriginalFWrite((void*)buf, eleSize, count, fp );
127 return eleSize * count;
130 int FSeek( const void *fp, int offset, int origin )
132 if( -1 == *( (char*)fp + 4 ) )
134 return MemFSeek( fp, offset, origin );
138 return OriginalFSeek( fp, offset, origin );
142 int FTell( const void *fp )
144 if( -1 == *( (char*)fp + 4 ) )
146 return MemFTell( fp );
150 return OriginalFTell( fp );
154 bool FEof( const void *fp )
156 if( -1 == *( (char*)fp + 4 ) )
158 return MemFEof( fp );
162 return OriginalFEof( fp );
169 size_t __cdecl fread_for_c( void* _Buffer, size_t _ElementSize, size_t _ElementCount, void* _Stream )
171 return CustomFile::FRead( _Buffer, _ElementSize, _ElementCount, _Stream );
174 void __cdecl fwrite_for_c( void *buf, int size, const void *fp )
176 CustomFile::FWrite( buf, size, fp );