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.

150 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. /*
  3. ** The copyright to the contents herein is the property of Valve, L.L.C.
  4. ** The contents may be used and/or copied only with the written permission of
  5. ** Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. ** the agreement/contract under which the contents have been supplied.
  7. **
  8. *******************************************************************************
  9. **
  10. ** Contents:
  11. **
  12. ** TokenLine.cpp: implementation of the TokenLine class.
  13. **
  14. ******************************************************************************/
  15. #include "TokenLine.h"
  16. #include <string.h>
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. TokenLine::~TokenLine()
  21. {
  22. }
  23. bool TokenLine::SetLine(const char * newLine)
  24. {
  25. m_tokenNumber = 0;
  26. if (!newLine || ( strlen(newLine) >= (MAX_LINE_CHARS-1) ) )
  27. {
  28. memset( m_fullLine, 0, MAX_LINE_CHARS );
  29. memset( m_tokenBuffer, 0, MAX_LINE_CHARS );
  30. return false;
  31. }
  32. strncpy( m_fullLine, newLine, MAX_LINE_CHARS-1 );
  33. m_fullLine[ MAX_LINE_CHARS-1 ] = '\0';
  34. strncpy( m_tokenBuffer, newLine, MAX_LINE_CHARS-1 );
  35. m_tokenBuffer[ MAX_LINE_CHARS-1 ] = '\0';
  36. // parse tokens
  37. char * charPointer = m_tokenBuffer;
  38. while (*charPointer && (m_tokenNumber < MAX_LINE_TOKENS))
  39. {
  40. while (*charPointer && ((*charPointer <= 32) || (*charPointer > 126)))
  41. charPointer++;
  42. if (*charPointer)
  43. {
  44. m_token[m_tokenNumber] = charPointer;
  45. // special treatment for quotes
  46. if (*charPointer == '\"')
  47. {
  48. charPointer++;
  49. m_token[m_tokenNumber] = charPointer;
  50. while (*charPointer && (*charPointer != '\"') )
  51. charPointer++;
  52. }
  53. else
  54. {
  55. m_token[m_tokenNumber] = charPointer;
  56. while (*charPointer && ((*charPointer > 32) && (*charPointer <= 126)))
  57. charPointer++;
  58. }
  59. m_tokenNumber++;
  60. if (*charPointer)
  61. {
  62. *charPointer=0;
  63. charPointer++;
  64. }
  65. }
  66. }
  67. return (m_tokenNumber != MAX_LINE_TOKENS);
  68. }
  69. char * TokenLine::GetLine()
  70. {
  71. return m_fullLine;
  72. }
  73. char * TokenLine::GetToken(int i)
  74. {
  75. if (i >= m_tokenNumber)
  76. return NULL;
  77. return m_token[i];
  78. }
  79. // if the given parm is not present return NULL
  80. // otherwise return the address of the following token, or an empty string
  81. char* TokenLine::CheckToken(char * parm)
  82. {
  83. for (int i = 0 ; i < m_tokenNumber; i ++)
  84. {
  85. if (!m_token[i])
  86. continue;
  87. if ( !strcmp (parm,m_token[i]) )
  88. {
  89. char * ret = m_token[i+1];
  90. // if this token doesn't exist, since index i was the last
  91. // return an empty string
  92. if ( (i+1) == m_tokenNumber ) ret = "";
  93. return ret;
  94. }
  95. }
  96. return NULL;
  97. }
  98. int TokenLine::CountToken()
  99. {
  100. int c = 0;
  101. for (int i = 0 ; i < m_tokenNumber; i ++)
  102. {
  103. if (m_token[i])
  104. c++;
  105. }
  106. return c;
  107. }
  108. char* TokenLine::GetRestOfLine(int i)
  109. {
  110. if (i >= m_tokenNumber)
  111. return NULL;
  112. return m_fullLine + (m_token[i] - m_tokenBuffer);
  113. }
  114. TokenLine::TokenLine(char * string)
  115. {
  116. SetLine(string);
  117. }
  118. TokenLine::TokenLine()
  119. {
  120. }