Counter Strike : Global Offensive Source Code
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.

96 lines
3.2 KiB

  1. //===================== Copyright (c) Valve Corporation. All Rights Reserved. ======================
  2. //
  3. // C runtime and standard library wrappers / equivalents / upgrades.
  4. // Allows centralization of CRT and CRT-like code and consistent behavior across platforms.
  5. //
  6. //==================================================================================================
  7. #include "tier1/strtools.h"
  8. #include "tier1/utlbuffer.h"
  9. #include "tier1/utlstring.h"
  10. #include "tier1/utlvector.h"
  11. #include "tier1/fmtstr.h"
  12. #include "tier1/characterset.h"
  13. // Prints out a memory dump where stuff that's ascii is human readable, etc.
  14. void V_LogMultiline(bool input, char const *label, const char *data, size_t len, CUtlString &output);
  15. // If the string above is really long, you can't pass it into varargs print routines, so this splits everything out into smaller chunks
  16. void V_LogMultilineToArray( bool input, char const *label, const char *data, size_t len, CUtlVector< CUtlString, CUtlMemory<CUtlString, int> > &output )
  17. {
  18. static const char HEX[] = "0123456789abcdef";
  19. const char * direction = (input ? " << " : " >> ");
  20. const size_t LINE_SIZE = 24;
  21. char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
  22. while (len > 0)
  23. {
  24. V_memset(asc_line, ' ', sizeof(asc_line));
  25. V_memset(hex_line, ' ', sizeof(hex_line));
  26. size_t line_len = MIN(len, LINE_SIZE);
  27. for (size_t i=0; i<line_len; ++i) {
  28. unsigned char ch = static_cast<unsigned char>(data[i]);
  29. asc_line[i] = ( V_isprint(ch) && !V_iscntrl(ch) ) ? data[i] : '.';
  30. hex_line[i*2 + i/4] = HEX[ch >> 4];
  31. hex_line[i*2 + i/4 + 1] = HEX[ch & 0xf];
  32. }
  33. asc_line[sizeof(asc_line)-1] = 0;
  34. hex_line[sizeof(hex_line)-1] = 0;
  35. CUtlString s;
  36. s.Format( "%s %s %s %s\n", label, direction, asc_line, hex_line );
  37. output.AddToTail( s );
  38. data += line_len;
  39. len -= line_len;
  40. }
  41. }
  42. char* AllocString(const char *pStr, int nMaxChars);
  43. void V_SplitString2InPlace( char *pString, const char **pSeparators, int nSeparators, CUtlVector<const char *> &outStrings )
  44. {
  45. // We must pass in an empty outStrings buffer or call outStrings.PurgeAndDeleteElements between
  46. // calls.
  47. Assert( outStrings.Count() == 0 );
  48. // This will make outStrings empty but it will not free any memory that the elements were pointing to.
  49. outStrings.Purge();
  50. char *pCurPos = pString;
  51. while ( 1 )
  52. {
  53. int iFirstSeparator = -1;
  54. char *pFirstSeparator = 0;
  55. for ( int i=0; i < nSeparators; i++ )
  56. {
  57. char *pTest = V_stristr_fast( pCurPos, pSeparators[i] );
  58. if ( pTest && (!pFirstSeparator || pTest < pFirstSeparator) )
  59. {
  60. iFirstSeparator = i;
  61. pFirstSeparator = pTest;
  62. }
  63. }
  64. if ( pFirstSeparator )
  65. {
  66. // Split on this separator and continue on.
  67. int separatorLen = V_strlen( pSeparators[iFirstSeparator] );
  68. V_memset( pFirstSeparator, 0, separatorLen );
  69. outStrings.AddToTail( pCurPos );
  70. pCurPos = pFirstSeparator + separatorLen;
  71. }
  72. else
  73. {
  74. // Copy the rest of the string
  75. if ( *pCurPos != '\0' )
  76. {
  77. outStrings.AddToTail( pCurPos );
  78. }
  79. return;
  80. }
  81. }
  82. }
  83. void V_SplitStringInPlace( IN_Z char *pString, IN_Z const char *pSeparator, CUtlVector<const char *> &outStrings )
  84. {
  85. V_SplitString2InPlace( pString, &pSeparator, 1, outStrings );
  86. }