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.

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