Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #include <memory.h>
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. // Copied from sound.cpp in the DLL
  12. char *memfgets( unsigned char *pMemFile, int fileSize, int *pFilePos, char *pBuffer, int bufferSize )
  13. {
  14. int filePos = *pFilePos;
  15. int i, last, stop;
  16. // Bullet-proofing
  17. if ( !pMemFile || !pBuffer )
  18. return NULL;
  19. if ( filePos >= fileSize )
  20. return NULL;
  21. i = filePos;
  22. last = fileSize;
  23. // fgets always NULL terminates, so only read bufferSize-1 characters
  24. if ( last - filePos > (bufferSize-1) )
  25. last = filePos + (bufferSize-1);
  26. stop = 0;
  27. // Stop at the next newline (inclusive) or end of buffer
  28. while ( i < last && !stop )
  29. {
  30. if ( pMemFile[i] == '\n' )
  31. stop = 1;
  32. i++;
  33. }
  34. // If we actually advanced the pointer, copy it over
  35. if ( i != filePos )
  36. {
  37. // We read in size bytes
  38. int size = i - filePos;
  39. // copy it out
  40. memcpy( pBuffer, pMemFile + filePos, sizeof(unsigned char)*size );
  41. // If the buffer isn't full, terminate (this is always true)
  42. if ( size < bufferSize )
  43. pBuffer[size] = 0;
  44. // Update file pointer
  45. *pFilePos = i;
  46. return pBuffer;
  47. }
  48. // No data read, bail
  49. return NULL;
  50. }