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.

217 lines
6.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implementation of CLogEventArgument
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //------------------------------------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #pragma warning (disable:4786)
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "argument.h"
  18. #include "memdbg.h"
  19. using namespace std;
  20. //------------------------------------------------------------------------------------------------------
  21. // Function: CLogEventArgument::CLogEventArgument
  22. // Purpose: Constructor that builds the object out of the passed in string of text
  23. // Input: text - text representing the argument
  24. //------------------------------------------------------------------------------------------------------
  25. CLogEventArgument::CLogEventArgument(const char* text)
  26. {
  27. init(text);
  28. }
  29. //------------------------------------------------------------------------------------------------------
  30. // Function: CLogEventArgument::CLogEventArgument
  31. // Purpose: Default constructor
  32. //------------------------------------------------------------------------------------------------------
  33. CLogEventArgument::CLogEventArgument()
  34. :m_ArgText(NULL),m_Valid(false)
  35. {}
  36. //------------------------------------------------------------------------------------------------------
  37. // Function: CLogEventArgument::init
  38. // Purpose: initializes the argument
  39. // Input: text - the text representing the argument
  40. //------------------------------------------------------------------------------------------------------
  41. void CLogEventArgument::init(const char* text)
  42. {
  43. int len=strlen(text);
  44. m_ArgText=new TRACKED char[len+1];
  45. strcpy(m_ArgText,text);
  46. m_Valid=true;
  47. }
  48. char* findStartOfSvrID(char* cs)
  49. {
  50. char* read=&cs[strlen(cs)-1];
  51. while (read != cs)
  52. {
  53. if (*read=='<' && *(read+1) != 'W') // if we've found a svrID
  54. break;
  55. read--;
  56. }
  57. return read;
  58. }
  59. //------------------------------------------------------------------------------------------------------
  60. // Function: CLogEventArgument::asPlayerGetID
  61. // Purpose: treats the argument as a player name, and returns the player ID.
  62. // Note: PlayerName args have this form: "name<pid><WON:wonid>"
  63. // Output: the ID of the player represented by this argument
  64. //------------------------------------------------------------------------------------------------------
  65. int CLogEventArgument::asPlayerGetSvrPID() const
  66. {
  67. char* read=findStartOfSvrID(m_ArgText);
  68. if (read==m_ArgText)
  69. return -1;
  70. int retval=-1;
  71. sscanf(read,"<%i>",&retval);
  72. return retval;
  73. }
  74. /*
  75. PID CLogEventArgument::asPlayerGetPID() const
  76. {
  77. char* openPID=NULL;
  78. int svrPID=INVALID_PID;
  79. if (openPID=strchr(m_ArgText,'<'))
  80. {
  81. openPID++;
  82. sscanf(openPID,"%i",&svrPID);
  83. }
  84. unsigned long wonID;
  85. if (openPID=strstr(m_ArgText,"<WON:"))
  86. {
  87. openPID+=5;
  88. sscanf(openPID,"%li",&wonID);
  89. }
  90. return PID(svrPID,wonID);
  91. }
  92. */
  93. //------------------------------------------------------------------------------------------------------
  94. // Function: CLogEventArgument::asPlayerGetName
  95. // Purpose: treats the argument as a player name, and copies/returns the player name.
  96. // Note: PlayerName args have this form: "name<pid><WONID:wonid>"
  97. // Input: copybuf - the buffer to copy the name into
  98. // Output: char* the pointer to the buffer that the name was copied into
  99. //------------------------------------------------------------------------------------------------------
  100. char* CLogEventArgument::asPlayerGetName(char* copybuf) const
  101. {
  102. char* eon=findStartOfSvrID(m_ArgText);
  103. bool noPID=(eon==m_ArgText);
  104. char old=*eon;
  105. if (!noPID)
  106. *eon=0;
  107. strcpy(copybuf,m_ArgText);
  108. if (!noPID)
  109. *eon=old;
  110. return copybuf;
  111. }
  112. //------------------------------------------------------------------------------------------------------
  113. // Function: CLogEventArgument::asPlayerGetName
  114. // Purpose: an alternate form of the above function that returns the playername
  115. // as a C++ string, rather than buffercopying it around
  116. // Output: string: the player's name
  117. //------------------------------------------------------------------------------------------------------
  118. string CLogEventArgument::asPlayerGetName() const
  119. {
  120. char* eon=findStartOfSvrID(m_ArgText);
  121. bool noPID=(eon==m_ArgText);
  122. char old=*eon;
  123. if (!noPID)
  124. *eon=0;
  125. string s(m_ArgText);
  126. if (!noPID)
  127. *eon=old;
  128. return s;
  129. }
  130. //------------------------------------------------------------------------------------------------------
  131. // Function: CLogEventArgument::asPlayerGetWONID
  132. // Purpose: treats the argument as a player name, and returns the player's wonid
  133. // Note: PlayerName args have this form: "name<pid><WON:wonid>"
  134. // Output: int: the WONID of the player
  135. //------------------------------------------------------------------------------------------------------
  136. unsigned long CLogEventArgument::asPlayerGetWONID() const
  137. {
  138. char* openPID=NULL;
  139. unsigned long retval=INVALID_WONID;
  140. if (openPID=strstr(m_ArgText,"<WON:"))
  141. {
  142. openPID+=5; //move past the <WON: string
  143. sscanf(openPID,"%lu",&retval);
  144. }
  145. return retval;
  146. }
  147. unsigned long CLogEventArgument::asPlayerGetPID() const
  148. {
  149. int svrPID=asPlayerGetSvrPID();
  150. if (pidMap[svrPID]==0 || pidMap[svrPID]==-1)
  151. pidMap[svrPID]=svrPID;
  152. return pidMap[svrPID];
  153. }
  154. //------------------------------------------------------------------------------------------------------
  155. // Function: CLogEventArgument::getFloatValue
  156. // Purpose: treats the argument as a floating point value, and returns it
  157. // Output: double
  158. //------------------------------------------------------------------------------------------------------
  159. double CLogEventArgument::getFloatValue() const
  160. {
  161. return atof(m_ArgText);
  162. }
  163. //------------------------------------------------------------------------------------------------------
  164. // Function: CLogEventArgument::getStringValue
  165. // Purpose: treats the argument as a string and returns a pointer to the argument
  166. // text itself. note the pointer is const, so the argument can't be modified by
  167. // the caller (unless they perform some nefarious casting on the returned pointer)
  168. // Output: const char*
  169. //------------------------------------------------------------------------------------------------------
  170. const char* CLogEventArgument::getStringValue() const
  171. {
  172. return m_ArgText;
  173. }
  174. //------------------------------------------------------------------------------------------------------
  175. // Function: CLogEventArgument::getStringValue
  176. // Purpose: an alternate form of the above that copies the string into a caller
  177. // supplied buffer then returns a pointer to that buffer
  178. // Input: copybuf - the buffer into which the string is to be copied
  179. // Output: char* the pointer to the buffer that the caller passed in
  180. //------------------------------------------------------------------------------------------------------
  181. char* CLogEventArgument::getStringValue(char* copybuf) const
  182. {
  183. strcpy(copybuf,m_ArgText);
  184. return copybuf;
  185. }