public: add Q_memfgets, replacement for COM_MemFgets rewritten using crtlib functions. Add a test

This commit is contained in:
Alibek Omarov
2026-03-01 03:04:33 +05:00
parent 53c5a33ffd
commit c19a824a42
8 changed files with 79 additions and 58 deletions

View File

@@ -776,7 +776,7 @@ void CL_InitClientMove( void )
clgame.pmove->COM_FileSize = COM_FileSize;
clgame.pmove->COM_LoadFile = COM_LoadFile;
clgame.pmove->COM_FreeFile = COM_FreeFile;
clgame.pmove->memfgets = COM_MemFgets;
clgame.pmove->memfgets = Q_memfgets;
clgame.pmove->PM_PlaySound = pfnPlaySound;
clgame.pmove->PM_TraceTexture = PM_CL_TraceTexture;
clgame.pmove->PM_PlaybackEventFull = pfnPlaybackEventFull;

View File

@@ -227,7 +227,7 @@ client_textmessage_t *CL_TextMessageParse( poolhandle_t mempool, byte *pMemFile,
lastLinePos = 0;
messageCount = 0;
while( COM_MemFgets( pMemFile, fileSize, &filePos, buf, 512 ) != NULL )
while( Q_memfgets( pMemFile, fileSize, &filePos, buf, 512 ) != NULL )
{
COM_TrimSpace( trim, buf, sizeof( trim ));

View File

@@ -597,7 +597,6 @@ void COM_HexConvert( const char *pszInput, int nInputLength, byte *pOutput )
byte *p = pOutput;
int i;
for( i = 0; i < nInputLength; i += 2 )
{
pIn = &pszInput[i];
@@ -606,59 +605,6 @@ void COM_HexConvert( const char *pszInput, int nInputLength, byte *pOutput )
}
}
/*
=============
COM_MemFgets
=============
*/
char *GAME_EXPORT COM_MemFgets( byte *pMemFile, int fileSize, int *filePos, char *pBuffer, int bufferSize )
{
int i, last, stop;
if( !pMemFile || !pBuffer || !filePos )
return NULL;
if( *filePos >= fileSize )
return NULL;
i = *filePos;
last = fileSize;
// fgets always NULL terminates, so only read bufferSize-1 characters
if( last - *filePos > ( bufferSize - 1 ))
last = *filePos + ( bufferSize - 1);
stop = 0;
// stop at the next newline (inclusive) or end of buffer
while( i < last && !stop )
{
if( pMemFile[i] == '\n' )
stop = 1;
i++;
}
// if we actually advanced the pointer, copy it over
if( i != *filePos )
{
// we read in size bytes
int size = i - *filePos;
// copy it out
memcpy( pBuffer, pMemFile + *filePos, size );
// If the buffer isn't full, terminate (this is always true)
if( size < bufferSize ) pBuffer[size] = 0;
// update file pointer
*filePos = i;
return pBuffer;
}
return NULL;
}
/*
=============
COM_LoadFileForMe

View File

@@ -628,7 +628,6 @@ qboolean SV_Active( void );
==============================================================
*/
char *COM_MemFgets( byte *pMemFile, int fileSize, int *filePos, char *pBuffer, int bufferSize );
void COM_HexConvert( const char *pszInput, int nInputLength, byte *pOutput );
byte COM_Nibble( char c );
int COM_SaveFile( const char *filename, const void *data, int len );

View File

@@ -484,7 +484,7 @@ void SV_InitClientMove( void )
svgame.pmove->COM_FileSize = COM_FileSize;
svgame.pmove->COM_LoadFile = COM_LoadFile;
svgame.pmove->COM_FreeFile = COM_FreeFile;
svgame.pmove->memfgets = COM_MemFgets;
svgame.pmove->memfgets = Q_memfgets;
svgame.pmove->PM_PlaySound = pfnPlaySound;
svgame.pmove->PM_TraceTexture = pfnTraceTexture;
svgame.pmove->PM_PlaybackEventFull = pfnPlaybackEventFull;