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.

102 lines
2.9 KiB

  1. //================ Copyright (c) 1996-2009 Valve Corporation. All Rights Reserved. =================
  2. //
  3. //
  4. //
  5. //==================================================================================================
  6. #include "strtools.h"
  7. #include "utlvector.h"
  8. CSplitString::CSplitString()
  9. {
  10. m_szBuffer = nullptr;
  11. }
  12. CSplitString::CSplitString(const char *pString, const char **pSeparators, int nSeparators)
  13. {
  14. Construct(pString, pSeparators, nSeparators);
  15. };
  16. CSplitString::CSplitString( const char *pString, const char *pSeparator)
  17. {
  18. Construct( pString, &pSeparator, 1 );
  19. }
  20. CSplitString::~CSplitString()
  21. {
  22. if(m_szBuffer)
  23. delete [] m_szBuffer;
  24. }
  25. void CSplitString::Set(const char *pString, const char **pSeparators, int nSeparators)
  26. {
  27. delete[] m_szBuffer;
  28. Construct(pString, pSeparators, nSeparators);
  29. }
  30. void CSplitString::Construct( const char *pString, const char **pSeparators, int nSeparators )
  31. {
  32. //////////////////////////////////////////////////////////////////////////
  33. // make a duplicate of the original string. We'll use pieces of this duplicate to tokenize the string
  34. // and create NULL-terminated tokens of the original string
  35. //
  36. int nOriginalStringLength = V_strlen(pString);
  37. m_szBuffer = new char[nOriginalStringLength + 1];
  38. memcpy(m_szBuffer, pString, nOriginalStringLength + 1);
  39. this->Purge();
  40. const char *pCurPos = pString;
  41. while ( 1 )
  42. {
  43. int iFirstSeparator = -1;
  44. const char *pFirstSeparator = 0;
  45. for ( int i=0; i < nSeparators; i++ )
  46. {
  47. const char *pTest = V_stristr( pCurPos, pSeparators[i] );
  48. if ( pTest && (!pFirstSeparator || pTest < pFirstSeparator) )
  49. {
  50. iFirstSeparator = i;
  51. pFirstSeparator = pTest;
  52. }
  53. }
  54. if ( pFirstSeparator )
  55. {
  56. // Split on this separator and continue on.
  57. int separatorLen = strlen( pSeparators[iFirstSeparator] );
  58. if ( pFirstSeparator > pCurPos )
  59. {
  60. //////////////////////////////////////////////////////////////////////////
  61. /// Cut the token out of the duplicate string
  62. char *pTokenInDuplicate = m_szBuffer + (pCurPos - pString);
  63. int nTokenLength = pFirstSeparator-pCurPos;
  64. Assert(nTokenLength > 0 && !memcmp(pTokenInDuplicate,pCurPos,nTokenLength));
  65. pTokenInDuplicate[nTokenLength] = '\0';
  66. this->AddToTail( pTokenInDuplicate /*AllocString( pCurPos, pFirstSeparator-pCurPos )*/ );
  67. }
  68. pCurPos = pFirstSeparator + separatorLen;
  69. }
  70. else
  71. {
  72. // Copy the rest of the string
  73. if ( int nTokenLength = strlen( pCurPos ) )
  74. {
  75. //////////////////////////////////////////////////////////////////////////
  76. // There's no need to cut this token, because there's no separator after it.
  77. // just add its copy in the buffer to the tail
  78. char *pTokenInDuplicate = m_szBuffer + (pCurPos - pString);
  79. Assert(!memcmp(pTokenInDuplicate, pCurPos, nTokenLength));
  80. this->AddToTail( pTokenInDuplicate/*AllocString( pCurPos, -1 )*/ );
  81. }
  82. return;
  83. }
  84. }
  85. }
  86. void CSplitString::PurgeAndDeleteElements()
  87. {
  88. Purge();
  89. }