Is this good or not? Input arguments for OsFile_Open
must be in UTF8 format.
#include <stdio.h>
#ifdef WIN32
#include <Windows.h>
#endif
// Types declaration, originally they are located in header file
//
typedef int Bool_T;
typedef signed long long Int64_T;
#define true 1
#define false 0
#define IN
#define OUT
Bool_T OsFile_Open( IN const char * name, IN const char * mode, OUT FILE ** file )
{
FILE * handle;
#ifdef WIN32
wchar_t unicodeMode[MAX_PATH];
wchar_t unicodeName[MAX_PATH];
int unicodeNameLen;
int unicodeModeLen;
unicodeNameLen = MultiByteToWideChar( CP_UTF8, 0, name, -1, unicodeName, 0 );
if( unicodeNameLen != 0 )
{
if( MultiByteToWideChar( CP_UTF8, 0, name, -1, unicodeName, unicodeNameLen ) != 0 )
{
unicodeModeLen = MultiByteToWideChar( CP_UTF8, 0, mode, -1, unicodeMode, 0 );
if( unicodeModeLen != 0 )
{
if( MultiByteToWideChar( CP_UTF8, 0, mode, -1, unicodeMode, unicodeModeLen ) != 0 )
{
handle = _wfopen( unicodeName, unicodeMode );
if( handle != NULL )
{
*file = handle;
return true;
}else{
return false;
}
}else{
return false;
}
}else{ // if( unicodeModeLen != 0 )
return false;
}
}else{ // if( MultiByteToWideChar( CP_UTF8, 0, name, -1, unicodeName, unicodeNameLen ) != 0 )
return false;
}
}else{ // if( unicodeNameLen != 0 )
return false;
}
#else
handle = fopen64( name, mode );
if( handle != NULL )
{
*file = handle;
return true;
}else{
return false;
}
#endif
}
Bool_T OsFile_Close( IN FILE * file )
{
return ( fclose( file ) == 0 ) ? true : false;
}
Bool_T OsFile_Seek( IN FILE * file, IN Int64_T offset, IN int origin )
{
int result;
#ifdef WIN32
result = _fseeki64( file, offset, origin );
#else
result = fseeko64( file, offset, origin );
#endif
return ( result == 0 ) ? true : false;
}
Bool_T OsFile_GetPos( IN FILE * file, OUT Int64_T * curPos )
{
#ifdef WIN32
*curPos = _ftelli64( file );
#else
*curPos = ftello64( file );
#endif
return ( *curPos != -1 ) ? true : false;
}
size_t OsFile_Read( IN FILE * file, IN int numOfBytes, OUT unsigned char * buffer )
{
return fread( buffer, sizeof( unsigned char ), numOfBytes, file );
}
int OsFile_Eof( IN FILE * file )
{
return feof( file );
}