2012-11-26 18:58:24 +00:00
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Doom 3 BFG Edition GPL Source Code
2012-11-28 15:47:07 +00:00
Copyright ( C ) 1993 - 2012 id Software LLC , a ZeniMax Media company .
2012-12-06 23:09:53 +00:00
Copyright ( C ) 2012 Robert Beckebans
2012-11-26 18:58:24 +00:00
2012-11-28 15:47:07 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ( " Doom 3 BFG Edition Source Code " ) .
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code . If not , see < http : //www.gnu.org/licenses/>.
In addition , the Doom 3 BFG Edition Source Code is also subject to certain additional terms . You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code . If not , please request a copy in writing from id Software at the address below .
If you have questions concerning this license or the applicable additional terms , you may contact in writing id Software LLC , c / o ZeniMax Media Inc . , Suite 120 , Rockville , Maryland 20850 USA .
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-12-22 15:18:19 +00:00
# include "precompiled.h"
2012-11-26 18:58:24 +00:00
# pragma hdrstop
# include "Unzip.h"
/*
= = = = = = = = = = = = = = = = =
FS_WriteFloatString
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int FS_WriteFloatString ( char * buf , const char * fmt , va_list argPtr )
{
2012-12-11 22:04:53 +00:00
// DG: replaced long with int for 64bit compatibility in the whole function
int i ;
unsigned int u ;
2012-11-26 18:58:24 +00:00
double f ;
2012-11-28 15:47:07 +00:00
char * str ;
2012-11-26 18:58:24 +00:00
int index ;
idStr tmp , format ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
index = 0 ;
2012-11-28 15:47:07 +00:00
while ( * fmt )
{
switch ( * fmt )
{
2012-11-26 18:58:24 +00:00
case ' % ' :
format = " " ;
format + = * fmt + + ;
2012-11-28 15:47:07 +00:00
while ( ( * fmt > = ' 0 ' & & * fmt < = ' 9 ' ) | |
* fmt = = ' . ' | | * fmt = = ' - ' | | * fmt = = ' + ' | | * fmt = = ' # ' )
{
2012-11-26 18:58:24 +00:00
format + = * fmt + + ;
}
format + = * fmt ;
2012-11-28 15:47:07 +00:00
switch ( * fmt )
{
2012-11-26 18:58:24 +00:00
case ' f ' :
case ' e ' :
case ' E ' :
case ' g ' :
case ' G ' :
f = va_arg ( argPtr , double ) ;
2012-11-28 15:47:07 +00:00
if ( format . Length ( ) < = 2 )
{
2012-11-26 18:58:24 +00:00
// high precision floating point number without trailing zeros
sprintf ( tmp , " %1.10f " , f ) ;
tmp . StripTrailing ( ' 0 ' ) ;
tmp . StripTrailing ( ' . ' ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , " %s " , tmp . c_str ( ) ) ;
2012-11-26 18:58:24 +00:00
}
2012-11-28 15:47:07 +00:00
else
{
index + = sprintf ( buf + index , format . c_str ( ) , f ) ;
2012-11-26 18:58:24 +00:00
}
break ;
case ' d ' :
case ' i ' :
2012-12-11 22:04:53 +00:00
i = va_arg ( argPtr , int ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) , i ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' u ' :
2012-12-11 22:04:53 +00:00
u = va_arg ( argPtr , unsigned int ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) , u ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' o ' :
2012-12-11 22:04:53 +00:00
u = va_arg ( argPtr , unsigned int ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) , u ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' x ' :
2012-12-11 22:04:53 +00:00
u = va_arg ( argPtr , unsigned int ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) , u ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' X ' :
2012-12-11 22:04:53 +00:00
u = va_arg ( argPtr , unsigned int ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) , u ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' c ' :
2012-12-11 22:04:53 +00:00
i = va_arg ( argPtr , int ) ;
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) , ( char ) i ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' s ' :
2012-11-28 15:47:07 +00:00
str = va_arg ( argPtr , char * ) ;
index + = sprintf ( buf + index , format . c_str ( ) , str ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' % ' :
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , format . c_str ( ) ) ; //-V618
2012-11-26 18:58:24 +00:00
break ;
default :
common - > Error ( " FS_WriteFloatString: invalid format %s " , format . c_str ( ) ) ;
break ;
}
fmt + + ;
break ;
case ' \\ ' :
fmt + + ;
2012-11-28 15:47:07 +00:00
switch ( * fmt )
{
2012-11-26 18:58:24 +00:00
case ' t ' :
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , " \t " ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' v ' :
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , " \v " ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' n ' :
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , " \n " ) ;
2012-11-26 18:58:24 +00:00
break ;
case ' \\ ' :
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , " \\ " ) ;
2012-11-26 18:58:24 +00:00
break ;
default :
common - > Error ( " FS_WriteFloatString: unknown escape character \' %c \' " , * fmt ) ;
break ;
}
fmt + + ;
break ;
default :
2012-11-28 15:47:07 +00:00
index + = sprintf ( buf + index , " %c " , * fmt ) ;
2012-11-26 18:58:24 +00:00
fmt + + ;
break ;
}
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
return index ;
2012-12-11 22:04:53 +00:00
// DG end
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile : : GetName
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
const char * idFile : : GetName ( ) const
{
2012-11-26 18:58:24 +00:00
return " " ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : GetFullPath
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
const char * idFile : : GetFullPath ( ) const
{
2012-11-26 18:58:24 +00:00
return " " ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Read
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : Read ( void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile::Read: cannot read from idFile " ) ;
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Write
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : Write ( const void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile::Write: cannot write to idFile " ) ;
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Length
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : Length ( ) const
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Timestamp
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
ID_TIME_T idFile : : Timestamp ( ) const
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : Tell ( ) const
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ForceFlush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile : : ForceFlush ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Flush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile : : Flush ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Seek
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : Seek ( long offset , fsOrigin_t origin )
{
2012-11-26 18:58:24 +00:00
return - 1 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Rewind
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile : : Rewind ( )
{
2012-11-26 18:58:24 +00:00
Seek ( 0 , FS_SEEK_SET ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : Printf
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : Printf ( const char * fmt , . . . )
{
2012-11-26 18:58:24 +00:00
char buf [ MAX_PRINT_MSG ] ;
int length ;
va_list argptr ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
va_start ( argptr , fmt ) ;
2012-11-28 15:47:07 +00:00
length = idStr : : vsnPrintf ( buf , MAX_PRINT_MSG - 1 , fmt , argptr ) ;
2012-11-26 18:58:24 +00:00
va_end ( argptr ) ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
// so notepad formats the lines correctly
2012-11-28 15:47:07 +00:00
idStr work ( buf ) ;
work . Replace ( " \n " , " \r \n " ) ;
return Write ( work . c_str ( ) , work . Length ( ) ) ;
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile : : VPrintf
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : VPrintf ( const char * fmt , va_list args )
{
2012-11-26 18:58:24 +00:00
char buf [ MAX_PRINT_MSG ] ;
int length ;
2012-11-28 15:47:07 +00:00
length = idStr : : vsnPrintf ( buf , MAX_PRINT_MSG - 1 , fmt , args ) ;
2012-11-26 18:58:24 +00:00
return Write ( buf , length ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteFloatString
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteFloatString ( const char * fmt , . . . )
{
2012-11-26 18:58:24 +00:00
char buf [ MAX_PRINT_MSG ] ;
int len ;
va_list argPtr ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
va_start ( argPtr , fmt ) ;
len = FS_WriteFloatString ( buf , fmt , argPtr ) ;
va_end ( argPtr ) ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
return Write ( buf , len ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadInt
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadInt ( int & value )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & value , sizeof ( value ) ) ;
2012-11-28 15:47:07 +00:00
value = LittleLong ( value ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadUnsignedInt
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadUnsignedInt ( unsigned int & value )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & value , sizeof ( value ) ) ;
2012-11-28 15:47:07 +00:00
value = LittleLong ( value ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadShort
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadShort ( short & value )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & value , sizeof ( value ) ) ;
2012-11-28 15:47:07 +00:00
value = LittleShort ( value ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadUnsignedShort
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadUnsignedShort ( unsigned short & value )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & value , sizeof ( value ) ) ;
2012-11-28 15:47:07 +00:00
value = LittleShort ( value ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadChar
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadChar ( char & value )
{
2012-11-26 18:58:24 +00:00
return Read ( & value , sizeof ( value ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadUnsignedChar
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadUnsignedChar ( unsigned char & value )
{
2012-11-26 18:58:24 +00:00
return Read ( & value , sizeof ( value ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadFloat
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadFloat ( float & value )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & value , sizeof ( value ) ) ;
2012-11-28 15:47:07 +00:00
value = LittleFloat ( value ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadBool
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadBool ( bool & value )
{
2012-11-26 18:58:24 +00:00
unsigned char c ;
int result = ReadUnsignedChar ( c ) ;
value = c ? true : false ;
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadString
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadString ( idStr & string )
{
2012-11-26 18:58:24 +00:00
int len ;
int result = 0 ;
ReadInt ( len ) ;
2012-11-28 15:47:07 +00:00
if ( len > = 0 )
{
2012-11-26 18:58:24 +00:00
string . Fill ( ' ' , len ) ;
result = Read ( & string [ 0 ] , len ) ;
}
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadVec2
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadVec2 ( idVec2 & vec )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & vec , sizeof ( vec ) ) ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & vec , sizeof ( float ) , sizeof ( vec ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadVec3
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadVec3 ( idVec3 & vec )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & vec , sizeof ( vec ) ) ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & vec , sizeof ( float ) , sizeof ( vec ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadVec4
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadVec4 ( idVec4 & vec )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & vec , sizeof ( vec ) ) ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & vec , sizeof ( float ) , sizeof ( vec ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadVec6
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadVec6 ( idVec6 & vec )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & vec , sizeof ( vec ) ) ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & vec , sizeof ( float ) , sizeof ( vec ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : ReadMat3
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : ReadMat3 ( idMat3 & mat )
{
2012-11-26 18:58:24 +00:00
int result = Read ( & mat , sizeof ( mat ) ) ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & mat , sizeof ( float ) , sizeof ( mat ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return result ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteInt
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteInt ( const int value )
{
int v = LittleLong ( value ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteUnsignedInt
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteUnsignedInt ( const unsigned int value )
{
unsigned int v = LittleLong ( value ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteShort
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteShort ( const short value )
{
short v = LittleShort ( value ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteUnsignedShort
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteUnsignedShort ( const unsigned short value )
{
unsigned short v = LittleShort ( value ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteChar
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteChar ( const char value )
{
2012-11-26 18:58:24 +00:00
return Write ( & value , sizeof ( value ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteUnsignedChar
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteUnsignedChar ( const unsigned char value )
{
2012-11-26 18:58:24 +00:00
return Write ( & value , sizeof ( value ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteFloat
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteFloat ( const float value )
{
float v = LittleFloat ( value ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteBool
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteBool ( const bool value )
{
2012-11-26 18:58:24 +00:00
unsigned char c = value ;
return WriteUnsignedChar ( c ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteString
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteString ( const char * value )
{
2012-11-26 18:58:24 +00:00
int len = strlen ( value ) ;
WriteInt ( len ) ;
2012-11-28 15:47:07 +00:00
return Write ( value , len ) ;
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteVec2
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteVec2 ( const idVec2 & vec )
{
2012-11-26 18:58:24 +00:00
idVec2 v = vec ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & v , sizeof ( float ) , sizeof ( v ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteVec3
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteVec3 ( const idVec3 & vec )
{
2012-11-26 18:58:24 +00:00
idVec3 v = vec ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & v , sizeof ( float ) , sizeof ( v ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteVec4
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteVec4 ( const idVec4 & vec )
{
2012-11-26 18:58:24 +00:00
idVec4 v = vec ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & v , sizeof ( float ) , sizeof ( v ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteVec6
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteVec6 ( const idVec6 & vec )
{
2012-11-26 18:58:24 +00:00
idVec6 v = vec ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & v , sizeof ( float ) , sizeof ( v ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile : : WriteMat3
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile : : WriteMat3 ( const idMat3 & mat )
{
2012-11-26 18:58:24 +00:00
idMat3 v = mat ;
2012-11-28 15:47:07 +00:00
LittleRevBytes ( & v , sizeof ( float ) , sizeof ( v ) / sizeof ( float ) ) ;
2012-11-26 18:58:24 +00:00
return Write ( & v , sizeof ( v ) ) ;
}
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile_Memory
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : idFile_Memory
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Memory : : idFile_Memory ( )
{
2012-11-26 18:58:24 +00:00
name = " *unknown* " ;
maxSize = 0 ;
fileSize = 0 ;
allocated = 0 ;
granularity = 16384 ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_WRITE ) ;
filePtr = NULL ;
curPtr = NULL ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : idFile_Memory
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Memory : : idFile_Memory ( const char * name )
{
2012-11-26 18:58:24 +00:00
this - > name = name ;
maxSize = 0 ;
fileSize = 0 ;
allocated = 0 ;
granularity = 16384 ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_WRITE ) ;
filePtr = NULL ;
curPtr = NULL ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : idFile_Memory
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Memory : : idFile_Memory ( const char * name , char * data , int length )
{
2012-11-26 18:58:24 +00:00
this - > name = name ;
maxSize = length ;
fileSize = 0 ;
allocated = length ;
granularity = 16384 ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_WRITE ) ;
filePtr = data ;
curPtr = data ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : idFile_Memory
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Memory : : idFile_Memory ( const char * name , const char * data , int length )
{
2012-11-26 18:58:24 +00:00
this - > name = name ;
maxSize = 0 ;
fileSize = length ;
allocated = 0 ;
granularity = 16384 ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_READ ) ;
2012-11-28 15:47:07 +00:00
filePtr = const_cast < char * > ( data ) ;
curPtr = const_cast < char * > ( data ) ;
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : TakeDataOwnership
this also makes the file read only
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : TakeDataOwnership ( )
{
if ( filePtr ! = NULL & & fileSize > 0 )
{
2012-11-26 18:58:24 +00:00
maxSize = 0 ;
mode = ( 1 < < FS_READ ) ;
allocated = fileSize ;
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : ~ idFile_Memory
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Memory : : ~ idFile_Memory ( )
{
if ( filePtr & & allocated > 0 & & maxSize = = 0 )
{
2012-11-26 18:58:24 +00:00
Mem_Free ( filePtr ) ;
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Read
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Memory : : Read ( void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
2012-11-28 15:47:07 +00:00
if ( ! ( mode & ( 1 < < FS_READ ) ) )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Memory::Read: %s not opened in read mode " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
if ( curPtr + len > filePtr + fileSize )
{
2012-11-26 18:58:24 +00:00
len = filePtr + fileSize - curPtr ;
}
memcpy ( buffer , curPtr , len ) ;
curPtr + = len ;
return len ;
}
idCVar memcpyImpl ( " memcpyImpl " , " 0 " , 0 , " Which implementation of memcpy to use for idFile_Memory::Write() [ 0 / 1 - standard ( 1 eliminates branch misprediction ) , 2 - auto - vectorized ] " ) ;
2012-11-28 15:47:07 +00:00
void * memcpy2 ( void * __restrict b , const void * __restrict a , size_t n )
{
char * s1 = ( char * ) b ;
const char * s2 = ( const char * ) a ;
for ( ; 0 < n ; - - n )
{
2012-11-26 18:58:24 +00:00
* s1 + + = * s2 + + ;
}
return b ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Write
= = = = = = = = = = = = = = = = =
*/
idHashTableT < int , int > histogram ;
2012-11-28 15:47:07 +00:00
CONSOLE_COMMAND ( outputHistogram , " " , 0 )
{
for ( int i = 0 ; i < histogram . Num ( ) ; i + + )
{
2012-11-26 18:58:24 +00:00
int key ;
histogram . GetIndexKey ( i , key ) ;
2012-11-28 15:47:07 +00:00
int * value = histogram . GetIndex ( i ) ;
2012-11-26 18:58:24 +00:00
idLib : : Printf ( " %d \t %d \n " , key , * value ) ;
}
}
2012-11-28 15:47:07 +00:00
CONSOLE_COMMAND ( clearHistogram , " " , 0 )
{
2012-11-26 18:58:24 +00:00
histogram . Clear ( ) ;
}
2012-11-28 15:47:07 +00:00
int idFile_Memory : : Write ( const void * buffer , int len )
{
if ( len = = 0 )
{
2012-11-26 18:58:24 +00:00
// ~4% falls into this case for some reason...
return 0 ;
}
2012-11-28 15:47:07 +00:00
if ( ! ( mode & ( 1 < < FS_WRITE ) ) )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Memory::Write: %s not opened in write mode " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
int alloc = curPtr + len + 1 - filePtr - allocated ; // need room for len+1
2012-11-28 15:47:07 +00:00
if ( alloc > 0 )
{
if ( maxSize ! = 0 )
{
2012-11-26 18:58:24 +00:00
common - > Error ( " idFile_Memory::Write: exceeded maximum size %d " , maxSize ) ;
return 0 ;
}
int extra = granularity * ( 1 + alloc / granularity ) ;
2012-11-28 15:47:07 +00:00
char * newPtr = ( char * ) Mem_Alloc ( allocated + extra , TAG_IDFILE ) ;
if ( allocated )
{
2012-11-26 18:58:24 +00:00
memcpy ( newPtr , filePtr , allocated ) ;
}
allocated + = extra ;
2012-11-28 15:47:07 +00:00
curPtr = newPtr + ( curPtr - filePtr ) ;
if ( filePtr )
{
2012-11-26 18:58:24 +00:00
Mem_Free ( filePtr ) ;
}
filePtr = newPtr ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
//memcpy( curPtr, buffer, len );
memcpy2 ( curPtr , buffer , len ) ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
#if 0
2012-11-28 15:47:07 +00:00
if ( memcpyImpl . GetInteger ( ) = = 0 )
{
2012-11-26 18:58:24 +00:00
memcpy ( curPtr , buffer , len ) ;
2012-11-28 15:47:07 +00:00
}
else if ( memcpyImpl . GetInteger ( ) = = 1 )
{
2012-11-26 18:58:24 +00:00
memcpy ( curPtr , buffer , len ) ;
2012-11-28 15:47:07 +00:00
}
else if ( memcpyImpl . GetInteger ( ) = = 2 )
{
2012-11-26 18:58:24 +00:00
memcpy2 ( curPtr , buffer , len ) ;
}
# endif
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
#if 0
2012-11-28 15:47:07 +00:00
int * value ;
if ( histogram . Get ( len , & value ) & & value ! = NULL )
{
( * value ) + + ;
}
else
{
2012-11-26 18:58:24 +00:00
histogram . Set ( len , 1 ) ;
}
# endif
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
curPtr + = len ;
fileSize + = len ;
filePtr [ fileSize ] = 0 ; // len + 1
return len ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Length
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Memory : : Length ( ) const
{
2012-11-26 18:58:24 +00:00
return fileSize ;
}
/*
= = = = = = = = = = = = = = = = = = = = = = = =
idFile_Memory : : SetLength
= = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : SetLength ( size_t len )
{
2012-11-26 18:58:24 +00:00
PreAllocate ( len ) ;
fileSize = len ;
}
/*
= = = = = = = = = = = = = = = = = = = = = = = =
idFile_Memory : : PreAllocate
= = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : PreAllocate ( size_t len )
{
if ( len > allocated )
{
if ( maxSize ! = 0 )
{
2012-11-26 18:58:24 +00:00
idLib : : Error ( " idFile_Memory::SetLength: exceeded maximum size %d " , maxSize ) ;
}
2012-11-28 15:47:07 +00:00
char * newPtr = ( char * ) Mem_Alloc ( len , TAG_IDFILE ) ;
if ( allocated > 0 )
{
2012-11-26 18:58:24 +00:00
memcpy ( newPtr , filePtr , allocated ) ;
}
allocated = len ;
2012-11-28 15:47:07 +00:00
curPtr = newPtr + ( curPtr - filePtr ) ;
if ( filePtr ! = NULL )
{
2012-11-26 18:58:24 +00:00
Mem_Free ( filePtr ) ;
}
filePtr = newPtr ;
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Timestamp
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
ID_TIME_T idFile_Memory : : Timestamp ( ) const
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Memory : : Tell ( ) const
{
2012-11-26 18:58:24 +00:00
return ( curPtr - filePtr ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : ForceFlush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : ForceFlush ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Flush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : Flush ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Seek
returns zero on success and - 1 on failure
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Memory : : Seek ( long offset , fsOrigin_t origin )
{
2012-11-26 18:58:24 +00:00
2012-11-28 15:47:07 +00:00
switch ( origin )
{
case FS_SEEK_CUR :
{
2012-11-26 18:58:24 +00:00
curPtr + = offset ;
break ;
}
2012-11-28 15:47:07 +00:00
case FS_SEEK_END :
{
2012-11-26 18:58:24 +00:00
curPtr = filePtr + fileSize - offset ;
break ;
}
2012-11-28 15:47:07 +00:00
case FS_SEEK_SET :
{
2012-11-26 18:58:24 +00:00
curPtr = filePtr + offset ;
break ;
}
2012-11-28 15:47:07 +00:00
default :
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Memory::Seek: bad origin for %s \n " , name . c_str ( ) ) ;
return - 1 ;
}
}
2012-11-28 15:47:07 +00:00
if ( curPtr < filePtr )
{
2012-11-26 18:58:24 +00:00
curPtr = filePtr ;
return - 1 ;
}
2012-11-28 15:47:07 +00:00
if ( curPtr > filePtr + fileSize )
{
2012-11-26 18:58:24 +00:00
curPtr = filePtr + fileSize ;
return - 1 ;
}
return 0 ;
}
/*
= = = = = = = = = = = = = = = = = = = = = = = =
2012-11-28 15:47:07 +00:00
idFile_Memory : : SetMaxLength
2012-11-26 18:58:24 +00:00
= = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : SetMaxLength ( size_t len )
{
2012-11-26 18:58:24 +00:00
size_t oldLength = fileSize ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
SetLength ( len ) ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
maxSize = len ;
fileSize = oldLength ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : MakeReadOnly
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : MakeReadOnly ( )
{
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_READ ) ;
Rewind ( ) ;
}
/*
= = = = = = = = = = = = = = = = = = = = = = = =
idFile_Memory : : MakeWritable
= = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : MakeWritable ( )
{
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_WRITE ) ;
Rewind ( ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : Clear
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : Clear ( bool freeMemory )
{
2012-11-26 18:58:24 +00:00
fileSize = 0 ;
granularity = 16384 ;
2012-11-28 15:47:07 +00:00
if ( freeMemory )
{
2012-11-26 18:58:24 +00:00
allocated = 0 ;
Mem_Free ( filePtr ) ;
filePtr = NULL ;
curPtr = NULL ;
2012-11-28 15:47:07 +00:00
}
else
{
2012-11-26 18:58:24 +00:00
curPtr = filePtr ;
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_Memory : : SetData
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : SetData ( const char * data , int length )
{
2012-11-26 18:58:24 +00:00
maxSize = 0 ;
fileSize = length ;
allocated = 0 ;
granularity = 16384 ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
mode = ( 1 < < FS_READ ) ;
2012-11-28 15:47:07 +00:00
filePtr = const_cast < char * > ( data ) ;
curPtr = const_cast < char * > ( data ) ;
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = = = = = = = = =
idFile_Memory : : TruncateData
= = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Memory : : TruncateData ( size_t len )
{
if ( len > allocated )
{
2012-11-26 18:58:24 +00:00
idLib : : Error ( " idFile_Memory::TruncateData: len (%d) exceeded allocated size (%d) " , len , allocated ) ;
2012-11-28 15:47:07 +00:00
}
else
{
2012-11-26 18:58:24 +00:00
fileSize = len ;
}
}
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile_BitMsg
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : idFile_BitMsg
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_BitMsg : : idFile_BitMsg ( idBitMsg & msg )
{
2012-11-26 18:58:24 +00:00
name = " *unknown* " ;
mode = ( 1 < < FS_WRITE ) ;
this - > msg = & msg ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : idFile_BitMsg
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_BitMsg : : idFile_BitMsg ( const idBitMsg & msg )
{
2012-11-26 18:58:24 +00:00
name = " *unknown* " ;
mode = ( 1 < < FS_READ ) ;
2012-11-28 15:47:07 +00:00
this - > msg = const_cast < idBitMsg * > ( & msg ) ;
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : ~ idFile_BitMsg
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_BitMsg : : ~ idFile_BitMsg ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Read
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_BitMsg : : Read ( void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
2012-11-28 15:47:07 +00:00
if ( ! ( mode & ( 1 < < FS_READ ) ) )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_BitMsg::Read: %s not opened in read mode " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
return msg - > ReadData ( buffer , len ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Write
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_BitMsg : : Write ( const void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
2012-11-28 15:47:07 +00:00
if ( ! ( mode & ( 1 < < FS_WRITE ) ) )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Memory::Write: %s not opened in write mode " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
msg - > WriteData ( buffer , len ) ;
return len ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Length
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_BitMsg : : Length ( ) const
{
2012-11-26 18:58:24 +00:00
return msg - > GetSize ( ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Timestamp
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
ID_TIME_T idFile_BitMsg : : Timestamp ( ) const
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_BitMsg : : Tell ( ) const
{
if ( mode = = FS_READ )
{
2012-11-26 18:58:24 +00:00
return msg - > GetReadCount ( ) ;
2012-11-28 15:47:07 +00:00
}
else
{
2012-11-26 18:58:24 +00:00
return msg - > GetSize ( ) ;
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : ForceFlush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_BitMsg : : ForceFlush ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Flush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_BitMsg : : Flush ( )
{
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_BitMsg : : Seek
returns zero on success and - 1 on failure
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_BitMsg : : Seek ( long offset , fsOrigin_t origin )
{
2012-11-26 18:58:24 +00:00
return - 1 ;
}
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile_Permanent
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : idFile_Permanent
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Permanent : : idFile_Permanent ( )
{
2012-11-26 18:58:24 +00:00
name = " invalid " ;
o = NULL ;
mode = 0 ;
fileSize = 0 ;
handleSync = false ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : ~ idFile_Permanent
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Permanent : : ~ idFile_Permanent ( )
{
if ( o )
{
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
CloseHandle ( o ) ;
2012-12-06 23:09:53 +00:00
# else
fclose ( o ) ;
# endif
// RB end
2012-11-26 18:58:24 +00:00
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : Read
Properly handles partial reads
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Permanent : : Read ( void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
int block , remaining ;
int read ;
2012-11-28 15:47:07 +00:00
byte * buf ;
2012-11-26 18:58:24 +00:00
int tries ;
2012-11-28 15:47:07 +00:00
if ( ! ( mode & ( 1 < < FS_READ ) ) )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Permanent::Read: %s not opened in read mode " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
if ( ! o )
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
2012-11-28 15:47:07 +00:00
buf = ( byte * ) buffer ;
2012-11-26 18:58:24 +00:00
remaining = len ;
tries = 0 ;
2012-11-28 15:47:07 +00:00
while ( remaining )
{
2012-11-26 18:58:24 +00:00
block = remaining ;
2012-12-08 17:20:13 +00:00
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
DWORD bytesRead ;
2012-11-28 15:47:07 +00:00
if ( ! ReadFile ( o , buf , block , & bytesRead , NULL ) )
{
2012-11-26 18:58:24 +00:00
idLib : : Warning ( " idFile_Permanent::Read failed with %d from %s " , GetLastError ( ) , name . c_str ( ) ) ;
}
read = bytesRead ;
2012-12-06 23:09:53 +00:00
# else
read = fread ( buf , 1 , block , o ) ;
# endif
// RB end
2012-12-08 17:20:13 +00:00
2012-11-28 15:47:07 +00:00
if ( read = = 0 )
{
2012-11-26 18:58:24 +00:00
// we might have been trying to read from a CD, which
// sometimes returns a 0 read on windows
2012-11-28 15:47:07 +00:00
if ( ! tries )
{
2012-11-26 18:58:24 +00:00
tries = 1 ;
}
2012-11-28 15:47:07 +00:00
else
{
return len - remaining ;
2012-11-26 18:58:24 +00:00
}
}
2012-11-28 15:47:07 +00:00
if ( read = = - 1 )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Permanent::Read: -1 bytes read from %s " , name . c_str ( ) ) ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
remaining - = read ;
buf + = read ;
}
return len ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : Write
Properly handles partial writes
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Permanent : : Write ( const void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
int block , remaining ;
int written ;
2012-11-28 15:47:07 +00:00
byte * buf ;
2012-11-26 18:58:24 +00:00
int tries ;
2012-11-28 15:47:07 +00:00
if ( ! ( mode & ( 1 < < FS_WRITE ) ) )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_Permanent::Write: %s not opened in write mode " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
if ( ! o )
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
2012-11-28 15:47:07 +00:00
buf = ( byte * ) buffer ;
2012-11-26 18:58:24 +00:00
remaining = len ;
tries = 0 ;
2012-11-28 15:47:07 +00:00
while ( remaining )
{
2012-11-26 18:58:24 +00:00
block = remaining ;
2012-12-08 17:20:13 +00:00
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
DWORD bytesWritten ;
WriteFile ( o , buf , block , & bytesWritten , NULL ) ;
written = bytesWritten ;
2012-12-06 23:09:53 +00:00
# else
written = fwrite ( buf , 1 , block , o ) ;
# endif
// RB end
2012-12-08 17:20:13 +00:00
2012-11-28 15:47:07 +00:00
if ( written = = 0 )
{
if ( ! tries )
{
2012-11-26 18:58:24 +00:00
tries = 1 ;
}
2012-11-28 15:47:07 +00:00
else
{
2012-11-26 18:58:24 +00:00
common - > Printf ( " idFile_Permanent::Write: 0 bytes written to %s \n " , name . c_str ( ) ) ;
return 0 ;
}
}
2012-11-28 15:47:07 +00:00
if ( written = = - 1 )
{
2012-11-26 18:58:24 +00:00
common - > Printf ( " idFile_Permanent::Write: -1 bytes written to %s \n " , name . c_str ( ) ) ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
remaining - = written ;
buf + = written ;
fileSize + = written ;
}
2012-11-28 15:47:07 +00:00
if ( handleSync )
{
2012-11-26 18:58:24 +00:00
Flush ( ) ;
}
return len ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : ForceFlush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Permanent : : ForceFlush ( )
{
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
FlushFileBuffers ( o ) ;
2012-12-06 23:09:53 +00:00
# else
setvbuf ( o , NULL , _IONBF , 0 ) ;
# endif
// RB end
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : Flush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Permanent : : Flush ( )
{
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
FlushFileBuffers ( o ) ;
2012-12-06 23:09:53 +00:00
# else
fflush ( o ) ;
# endif
// RB end
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Permanent : : Tell ( ) const
{
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
return SetFilePointer ( o , 0 , NULL , FILE_CURRENT ) ;
2012-12-06 23:09:53 +00:00
# else
return ftell ( o ) ;
# endif
// RB end
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = =
idFile_Permanent : : Length
= = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Permanent : : Length ( ) const
{
2012-11-26 18:58:24 +00:00
return fileSize ;
}
/*
= = = = = = = = = = = = = = = =
idFile_Permanent : : Timestamp
= = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
ID_TIME_T idFile_Permanent : : Timestamp ( ) const
{
2012-11-26 18:58:24 +00:00
ID_TIME_T ts = Sys_FileTimeStamp ( o ) ;
return ts ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Permanent : : Seek
returns zero on success and - 1 on failure
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Permanent : : Seek ( long offset , fsOrigin_t origin )
{
2012-12-06 23:09:53 +00:00
// RB begin
# if defined(_WIN32)
2012-11-26 18:58:24 +00:00
int retVal = INVALID_SET_FILE_POINTER ;
2012-11-28 15:47:07 +00:00
switch ( origin )
{
case FS_SEEK_CUR :
retVal = SetFilePointer ( o , offset , NULL , FILE_CURRENT ) ;
break ;
case FS_SEEK_END :
retVal = SetFilePointer ( o , offset , NULL , FILE_END ) ;
break ;
case FS_SEEK_SET :
retVal = SetFilePointer ( o , offset , NULL , FILE_BEGIN ) ;
break ;
2012-11-26 18:58:24 +00:00
}
return ( retVal = = INVALID_SET_FILE_POINTER ) ? - 1 : 0 ;
2012-12-06 23:09:53 +00:00
# else
int _origin ;
2012-12-08 17:20:13 +00:00
2012-12-06 23:09:53 +00:00
switch ( origin )
{
case FS_SEEK_CUR :
{
_origin = SEEK_CUR ;
break ;
}
case FS_SEEK_END :
{
_origin = SEEK_END ;
break ;
}
case FS_SEEK_SET :
{
_origin = SEEK_SET ;
break ;
}
default :
{
_origin = SEEK_CUR ;
common - > FatalError ( " idFile_Permanent::Seek: bad origin for %s \n " , name . c_str ( ) ) ;
break ;
}
}
2012-12-08 17:20:13 +00:00
2012-12-06 23:09:53 +00:00
return fseek ( o , offset , _origin ) ;
# endif
// RB end
2012-11-26 18:58:24 +00:00
}
# if 1
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile_Cached
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile_Cached : : idFile_Cached
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Cached : : idFile_Cached ( ) : idFile_Permanent ( )
{
2012-11-26 18:58:24 +00:00
internalFilePos = 0 ;
bufferedStartOffset = 0 ;
bufferedEndOffset = 0 ;
buffered = NULL ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Cached : : ~ idFile_Cached
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_Cached : : ~ idFile_Cached ( )
{
2012-11-26 18:58:24 +00:00
Mem_Free ( buffered ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_ReadBuffered : : BufferData
Buffer a section of the file
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_Cached : : CacheData ( uint64 offset , uint64 length )
{
2012-11-26 18:58:24 +00:00
Mem_Free ( buffered ) ;
bufferedStartOffset = offset ;
bufferedEndOffset = offset + length ;
buffered = ( byte * ) Mem_Alloc ( length , TAG_RESOURCE ) ;
2012-11-28 15:47:07 +00:00
if ( buffered = = NULL )
{
2012-11-26 18:58:24 +00:00
return ;
}
int internalFilePos = idFile_Permanent : : Tell ( ) ;
idFile_Permanent : : Seek ( offset , FS_SEEK_SET ) ;
idFile_Permanent : : Read ( buffered , length ) ;
idFile_Permanent : : Seek ( internalFilePos , FS_SEEK_SET ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_ReadBuffered : : Read
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Cached : : Read ( void * buffer , int len )
{
if ( internalFilePos > = bufferedStartOffset & & internalFilePos + len < bufferedEndOffset )
{
2012-11-26 18:58:24 +00:00
// this is in the buffer
2012-11-28 15:47:07 +00:00
memcpy ( buffer , ( void * ) & buffered [ internalFilePos - bufferedStartOffset ] , len ) ;
2012-11-26 18:58:24 +00:00
internalFilePos + = len ;
return len ;
}
int read = idFile_Permanent : : Read ( buffer , len ) ;
2012-11-28 15:47:07 +00:00
if ( read ! = - 1 )
{
2012-11-26 18:58:24 +00:00
internalFilePos + = ( int64 ) read ;
}
return read ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Cached : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Cached : : Tell ( ) const
{
2012-11-26 18:58:24 +00:00
return internalFilePos ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_Cached : : Seek
returns zero on success and - 1 on failure
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_Cached : : Seek ( long offset , fsOrigin_t origin )
{
if ( origin = = FS_SEEK_SET & & offset > = bufferedStartOffset & & offset < bufferedEndOffset )
{
2012-11-26 18:58:24 +00:00
// don't do anything to the actual file ptr, just update or internal position
internalFilePos = offset ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
int retVal = idFile_Permanent : : Seek ( offset , origin ) ;
internalFilePos = idFile_Permanent : : Tell ( ) ;
return retVal ;
}
# endif
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile_InZip
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : idFile_InZip
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_InZip : : idFile_InZip ( )
{
2012-11-26 18:58:24 +00:00
name = " invalid " ;
zipFilePos = 0 ;
fileSize = 0 ;
memset ( & z , 0 , sizeof ( z ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : ~ idFile_InZip
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_InZip : : ~ idFile_InZip ( )
{
2012-11-26 18:58:24 +00:00
unzCloseCurrentFile ( z ) ;
unzClose ( z ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : Read
Properly handles partial reads
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InZip : : Read ( void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
int l = unzReadCurrentFile ( z , buffer , len ) ;
return l ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : Write
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InZip : : Write ( const void * buffer , int len )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_InZip::Write: cannot write to the zipped file %s " , name . c_str ( ) ) ;
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : ForceFlush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_InZip : : ForceFlush ( )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_InZip::ForceFlush: cannot flush the zipped file %s " , name . c_str ( ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : Flush
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
void idFile_InZip : : Flush ( )
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_InZip::Flush: cannot flush the zipped file %s " , name . c_str ( ) ) ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InZip : : Tell ( ) const
{
2013-01-02 01:04:57 +00:00
// DG: make sure the value fits into an int
// it's a long after all, and there'S also unztell64 that returns ZPOS64_T
// OTOH idFile in general seems to assume file-length <= INT_MAX so it may be ok..
z_off_t ret = unztell ( z ) ;
assert ( ret < = INT_MAX ) ;
return ret ;
// DG end
2012-11-26 18:58:24 +00:00
}
/*
= = = = = = = = = = = = = = = =
idFile_InZip : : Length
= = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InZip : : Length ( ) const
{
2012-11-26 18:58:24 +00:00
return fileSize ;
}
/*
= = = = = = = = = = = = = = = =
idFile_InZip : : Timestamp
= = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
ID_TIME_T idFile_InZip : : Timestamp ( ) const
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InZip : : Seek
returns zero on success and - 1 on failure
= = = = = = = = = = = = = = = = =
*/
# define ZIP_SEEK_BUF_SIZE (1<<15)
2012-11-28 15:47:07 +00:00
int idFile_InZip : : Seek ( long offset , fsOrigin_t origin )
{
2012-11-26 18:58:24 +00:00
int res , i ;
2012-11-28 15:47:07 +00:00
char * buf ;
switch ( origin )
{
case FS_SEEK_END :
{
2012-11-26 18:58:24 +00:00
offset = fileSize - offset ;
}
2013-01-02 01:04:57 +00:00
// FALLTHROUGH
2012-11-28 15:47:07 +00:00
case FS_SEEK_SET :
{
2012-11-26 18:58:24 +00:00
// set the file position in the zip file (also sets the current file info)
2013-01-02 01:04:57 +00:00
// DG use standard unzip.h function instead of custom one (not needed anymore with minizip 1.1)
unzSetOffset64 ( z , zipFilePos ) ;
2012-11-26 18:58:24 +00:00
unzOpenCurrentFile ( z ) ;
2012-11-28 15:47:07 +00:00
if ( offset < = 0 )
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
}
2013-01-02 01:04:57 +00:00
// FALLTHROUGH
2012-11-28 15:47:07 +00:00
case FS_SEEK_CUR :
{
buf = ( char * ) _alloca16 ( ZIP_SEEK_BUF_SIZE ) ;
for ( i = 0 ; i < ( offset - ZIP_SEEK_BUF_SIZE ) ; i + = ZIP_SEEK_BUF_SIZE )
{
2012-11-26 18:58:24 +00:00
res = unzReadCurrentFile ( z , buf , ZIP_SEEK_BUF_SIZE ) ;
2012-11-28 15:47:07 +00:00
if ( res < ZIP_SEEK_BUF_SIZE )
{
2012-11-26 18:58:24 +00:00
return - 1 ;
}
}
res = i + unzReadCurrentFile ( z , buf , offset - i ) ;
return ( res = = offset ) ? 0 : - 1 ;
}
2012-11-28 15:47:07 +00:00
default :
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_InZip::Seek: bad origin for %s \n " , name . c_str ( ) ) ;
break ;
}
}
return - 1 ;
}
# if 1
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFile_InnerResource
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = =
idFile_InnerResource : : idFile_InnerResource
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_InnerResource : : idFile_InnerResource ( const char * _name , idFile * rezFile , int _offset , int _len )
{
2012-11-26 18:58:24 +00:00
name = _name ;
offset = _offset ;
length = _len ;
resourceFile = rezFile ;
internalFilePos = 0 ;
resourceBuffer = NULL ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InnerResource : : ~ idFile_InnerResource
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFile_InnerResource : : ~ idFile_InnerResource ( )
{
if ( resourceBuffer ! = NULL )
{
2012-11-26 18:58:24 +00:00
fileSystem - > FreeResourceBuffer ( ) ;
}
}
/*
= = = = = = = = = = = = = = = = =
idFile_InnerResource : : Read
Properly handles partial reads
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InnerResource : : Read ( void * buffer , int len )
{
if ( resourceFile = = NULL )
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
2012-11-28 15:47:07 +00:00
if ( internalFilePos + len > length )
{
2012-11-26 18:58:24 +00:00
len = length - internalFilePos ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
int read = 0 ; //fileSystem->ReadFromBGL( resourceFile, (byte*)buffer, offset + internalFilePos, len );
2012-11-28 15:47:07 +00:00
if ( read ! = len )
{
if ( resourceBuffer ! = NULL )
{
2012-11-26 18:58:24 +00:00
memcpy ( buffer , & resourceBuffer [ internalFilePos ] , len ) ;
read = len ;
2012-11-28 15:47:07 +00:00
}
else
{
2012-11-26 18:58:24 +00:00
read = fileSystem - > ReadFromBGL ( resourceFile , buffer , offset + internalFilePos , len ) ;
}
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
internalFilePos + = read ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
return read ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InnerResource : : Tell
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InnerResource : : Tell ( ) const
{
2012-11-26 18:58:24 +00:00
return internalFilePos ;
}
/*
= = = = = = = = = = = = = = = = =
idFile_InnerResource : : Seek
returns zero on success and - 1 on failure
= = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
int idFile_InnerResource : : Seek ( long offset , fsOrigin_t origin )
{
switch ( origin )
{
case FS_SEEK_END :
{
2012-11-26 18:58:24 +00:00
internalFilePos = length - offset - 1 ;
return 0 ;
}
2012-11-28 15:47:07 +00:00
case FS_SEEK_SET :
{
2012-11-26 18:58:24 +00:00
internalFilePos = offset ;
2012-11-28 15:47:07 +00:00
if ( internalFilePos > = 0 & & internalFilePos < length )
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
return - 1 ;
}
2012-11-28 15:47:07 +00:00
case FS_SEEK_CUR :
{
2012-11-26 18:58:24 +00:00
internalFilePos + = offset ;
2012-11-28 15:47:07 +00:00
if ( internalFilePos > = 0 & & internalFilePos < length )
{
2012-11-26 18:58:24 +00:00
return 0 ;
}
return - 1 ;
}
2012-11-28 15:47:07 +00:00
default :
{
2012-11-26 18:58:24 +00:00
common - > FatalError ( " idFile_InnerResource::Seek: bad origin for %s \n " , name . c_str ( ) ) ;
break ;
}
}
return - 1 ;
}
# endif
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
idFileLocal
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = = = = = = = = = = = = =
idFileLocal : : ~ idFileLocal
Destructor that will destroy ( close ) the managed file when this wrapper class goes out of scope .
= = = = = = = = = = = = = = = = = = = = = = = =
*/
2012-11-28 15:47:07 +00:00
idFileLocal : : ~ idFileLocal ( )
{
if ( file ! = NULL )
{
2012-11-26 18:58:24 +00:00
delete file ;
file = NULL ;
}
}
2012-11-28 15:47:07 +00:00
static const char * testEndianNessFilename = " temp.bin " ;
struct testEndianNess_t
{
testEndianNess_t ( )
{
2012-11-26 18:58:24 +00:00
a = 0x12345678 ;
b = 0x12345678 ;
c = 3.0f ;
d = - 4.0f ;
e = " test " ;
f = idVec3 ( 1.0f , 2.0f , - 3.0f ) ;
g = false ;
h = true ;
2012-11-28 15:47:07 +00:00
for ( int index = 0 ; index < sizeof ( i ) ; index + + )
{
2012-11-26 18:58:24 +00:00
i [ index ] = 0x37 ;
}
}
2012-11-28 15:47:07 +00:00
bool operator = = ( testEndianNess_t & test ) const
{
2012-11-26 18:58:24 +00:00
return a = = test . a & &
2012-11-28 15:47:07 +00:00
b = = test . b & &
c = = test . c & &
d = = test . d & &
e = = test . e & &
f = = test . f & &
g = = test . g & &
h = = test . h & &
( memcmp ( i , test . i , sizeof ( i ) ) = = 0 ) ;
2012-11-26 18:58:24 +00:00
}
int a ;
unsigned int b ;
float c ;
float d ;
idStr e ;
idVec3 f ;
bool g ;
bool h ;
byte i [ 10 ] ;
} ;
2012-11-28 15:47:07 +00:00
CONSOLE_COMMAND ( testEndianNessWrite , " Tests the read/write compatibility between platforms " , 0 )
{
2012-11-26 18:58:24 +00:00
idFileLocal file ( fileSystem - > OpenFileWrite ( testEndianNessFilename ) ) ;
2012-11-28 15:47:07 +00:00
if ( file = = NULL )
{
2012-11-26 18:58:24 +00:00
idLib : : Printf ( " Couldn't open the %s testfile. \n " , testEndianNessFilename ) ;
return ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
testEndianNess_t testData ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
file - > WriteBig ( testData . a ) ;
file - > WriteBig ( testData . b ) ;
file - > WriteFloat ( testData . c ) ;
file - > WriteFloat ( testData . d ) ;
file - > WriteString ( testData . e ) ;
file - > WriteVec3 ( testData . f ) ;
file - > WriteBig ( testData . g ) ;
file - > WriteBig ( testData . h ) ;
2012-11-28 15:47:07 +00:00
file - > Write ( testData . i , sizeof ( testData . i ) / sizeof ( testData . i [ 0 ] ) ) ;
2012-11-26 18:58:24 +00:00
}
2012-11-28 15:47:07 +00:00
CONSOLE_COMMAND ( testEndianNessRead , " Tests the read/write compatibility between platforms " , 0 )
{
2012-11-26 18:58:24 +00:00
idFileLocal file ( fileSystem - > OpenFileRead ( testEndianNessFilename ) ) ;
2012-11-28 15:47:07 +00:00
if ( file = = NULL )
{
2012-11-26 18:58:24 +00:00
idLib : : Printf ( " Couldn't find the %s testfile. \n " , testEndianNessFilename ) ;
return ;
}
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
testEndianNess_t srcData ;
testEndianNess_t testData ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
memset ( & testData , 0 , sizeof ( testData ) ) ;
2012-11-28 15:47:07 +00:00
2012-11-26 18:58:24 +00:00
file - > ReadBig ( testData . a ) ;
file - > ReadBig ( testData . b ) ;
file - > ReadFloat ( testData . c ) ;
file - > ReadFloat ( testData . d ) ;
file - > ReadString ( testData . e ) ;
file - > ReadVec3 ( testData . f ) ;
file - > ReadBig ( testData . g ) ;
file - > ReadBig ( testData . h ) ;
2012-11-28 15:47:07 +00:00
file - > Read ( testData . i , sizeof ( testData . i ) / sizeof ( testData . i [ 0 ] ) ) ;
2012-11-26 18:58:24 +00:00
assert ( srcData = = testData ) ;
}
2012-11-28 15:47:07 +00:00
CONSOLE_COMMAND ( testEndianNessReset , " Tests the read/write compatibility between platforms " , 0 )
{
2012-11-26 18:58:24 +00:00
fileSystem - > RemoveFile ( testEndianNessFilename ) ;
}