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.

303 lines
7.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Contains lots of stuff that really doesn't fit anywhere else. Including
  4. // the implementations of the various Util:: functions
  5. //
  6. // $Workfile: $
  7. // $Date: $
  8. //
  9. //------------------------------------------------------------------------------------------------------
  10. // $Log: $
  11. //
  12. // $NoKeywords: $
  13. //=============================================================================//
  14. #include "util.h"
  15. #include "TFStatsApplication.h"
  16. #include <stdarg.h>
  17. #include <string.h>
  18. #include <vector>
  19. using namespace std;
  20. //------------------------------------------------------------------------------------------------------
  21. // Function: Util::string2svrID
  22. // Purpose: takes a string and extracts a server assigned ID out of it.
  23. // Input: s - the string from which to extract the server assigned ID
  24. // Output: int
  25. //------------------------------------------------------------------------------------------------------
  26. int Util::string2svrID(string s)
  27. {
  28. const char* text=s.c_str();
  29. const char* read=&text[strlen(text)-1];
  30. while (read != text)
  31. {
  32. if (*read=='<' && *(read+1) != 'W') // if we've found a svrID
  33. break;
  34. read--;
  35. }
  36. if (read==text)
  37. return -1;
  38. int retval=-1;
  39. sscanf(read,"<%i>",&retval);
  40. return retval;
  41. }
  42. //friendly weapon names so users don't have to look at names like "gl_grenade"
  43. map<string,string> Util::frWeapNmTbl;
  44. void Util::initFriendlyWeaponNameTable()
  45. {
  46. frWeapNmTbl["gl_grenade"]="Grenade Launcher";
  47. frWeapNmTbl["pipebomb"]="Pipebombs";
  48. frWeapNmTbl["timer"]="Infection";
  49. frWeapNmTbl["ac"]="Assault Cannon";
  50. frWeapNmTbl["infection"]="Infection";
  51. frWeapNmTbl["flames"]="Flames";
  52. frWeapNmTbl["rocket"]="Rocket Launcher";
  53. frWeapNmTbl["sentrygun"]="Sentry Gun";
  54. frWeapNmTbl["sniperrifle"]="Sniper Rifle";
  55. frWeapNmTbl["headshot"]="Head Shot";
  56. frWeapNmTbl["knife"]="Combat Knife";
  57. frWeapNmTbl["nails"]="Nail Gun";
  58. frWeapNmTbl["axe"]="Crowbar";
  59. frWeapNmTbl["shotgun"]="Shotgun";
  60. frWeapNmTbl["autorifle"]="Auto Rifle";
  61. frWeapNmTbl["supershotgun"]="Super Shotgun";
  62. frWeapNmTbl["supernails"]="Super Nailgun";
  63. frWeapNmTbl["railgun"]="Rail Gun";
  64. frWeapNmTbl["spanner"]="Spanner";
  65. //grenades
  66. frWeapNmTbl["caltrop"]="Caltrops";
  67. frWeapNmTbl["mirvgrenade"]="MIRV Grenade";
  68. frWeapNmTbl["nailgrenade"]="Nail Grenade";
  69. frWeapNmTbl["normalgrenade"]="Hand Grenade";
  70. frWeapNmTbl["gasgrenade"]="Gas Grenade";
  71. frWeapNmTbl["empgrenade"]="EMP Grenade";
  72. }
  73. //------------------------------------------------------------------------------------------------------
  74. // Function: getFriendlyWeaponName
  75. // Purpose: turns a non-friendly weapon name into a friendly one
  76. // Input: s - the non-friendly weapon name which you want to make friendly
  77. // this function returns the non friendly name if the friendly one isn't found
  78. // Output: const string&
  79. //------------------------------------------------------------------------------------------------------
  80. const string& Util::getFriendlyWeaponName(const string& s)
  81. {
  82. if (frWeapNmTbl[s] == "")
  83. return s;
  84. else
  85. return frWeapNmTbl[s];
  86. }
  87. //map of team colors, indexed by team ID
  88. const char* Util::teamcolormap[]=
  89. {
  90. {"blue"},
  91. {"red"},
  92. {"yellow"},
  93. {"green"},
  94. };
  95. //friendly english stuff
  96. char* Util::Months[]=
  97. {
  98. //{""},
  99. {"January"},
  100. {"February"},
  101. {"March"},
  102. {"April"},
  103. {"May"},
  104. {"June"},
  105. {"July"},
  106. {"August"},
  107. {"September"},
  108. {"October"},
  109. {"November"},
  110. {"December"}
  111. };
  112. char* Util::numberSuffixes[]=
  113. {
  114. {"th"},
  115. {"st"},
  116. {"nd"},
  117. {"rd"}
  118. };
  119. char* Util::daysofWeek[]=
  120. {
  121. {"Sunday"},
  122. {"Monday"},
  123. {"Tuesday"},
  124. {"Wednesday"},
  125. {"Thursday"},
  126. {"Friday"},
  127. {"Saturday"}
  128. };
  129. char* Util::ampm[]=
  130. {
  131. {"am"},
  132. {"pm"}
  133. };
  134. //english names, indexed by enumeration player_class in util.h
  135. char* plrClassNames[]={
  136. {"Undefined"},
  137. {"scout"},
  138. {"sniper"},
  139. {"soldier"},
  140. {"demoman"},
  141. {"medic"},
  142. {"hwguy"},
  143. {"pyro"},
  144. {"spy"},
  145. {"engineer"},
  146. {"civilian"},
  147. {"RandomPC"},
  148. {"observer"},
  149. };
  150. #define NUM_CLASSES 12
  151. //------------------------------------------------------------------------------------------------------
  152. // Function: playerClassNameToClassID
  153. // Purpose: determines the classID for the given class Name and returns it
  154. // Input: plrClass - the name of the class.
  155. // Output: player_class
  156. //------------------------------------------------------------------------------------------------------
  157. player_class playerClassNameToClassID(const char* plrClass)
  158. {
  159. for (int i=0;i<NUM_CLASSES;i++)
  160. {
  161. if (stricmp(plrClass,plrClassNames[i])==0)
  162. return (player_class)i;
  163. }
  164. return PC_UNDEFINED;
  165. }
  166. //------------------------------------------------------------------------------------------------------
  167. // Function: Util::time_t2hours
  168. // Purpose: returns how many hours are in the given time
  169. // Input: tmr - the time to convert
  170. // Output: int
  171. //------------------------------------------------------------------------------------------------------
  172. int Util::time_t2hours(time_t tmr)
  173. {
  174. tm* pstart=gmtime(&tmr);
  175. if (!pstart)
  176. return 0;
  177. return pstart->tm_hour;
  178. }
  179. //------------------------------------------------------------------------------------------------------
  180. // Function: Util::time_t2mins
  181. // Purpose: returns how many minutes of the hour are in the given time.
  182. // Input: tmr - the time to convert
  183. // Output: int
  184. //------------------------------------------------------------------------------------------------------
  185. int Util::time_t2mins(time_t tmr)
  186. {
  187. tm* pstart=gmtime(&tmr);
  188. if (!pstart)
  189. return 0;
  190. return pstart->tm_min;
  191. }
  192. //------------------------------------------------------------------------------------------------------
  193. // Function: Util::time_t2secs
  194. // Purpose: returns how many seconds of the minute are in the given time
  195. // Input: tmr - the time to convert
  196. // Output: int
  197. //------------------------------------------------------------------------------------------------------
  198. int Util::time_t2secs(time_t tmr)
  199. {
  200. tm* pstart=gmtime(&tmr);
  201. if (!pstart)
  202. return 0;
  203. return pstart->tm_sec;
  204. }
  205. //------------------------------------------------------------------------------------------------------
  206. // Function: str2lowercase
  207. // Purpose: portable _strlwr. linux doesn't support _strlwr
  208. // Input: out - destination of lower case string
  209. // in - string to lowercasify
  210. //------------------------------------------------------------------------------------------------------
  211. void Util::str2lowercase(char* out , const char* in)
  212. {
  213. while(*in)
  214. {
  215. *(out++)=tolower(*in++);
  216. }
  217. *out=0;
  218. }
  219. //#define _DIRDEBUG
  220. void Util::debug_dir_printf(char* fmt,...)
  221. {
  222. #ifdef _DEBUG
  223. #ifdef _DIRDEBUG
  224. va_list v;
  225. va_start(v,fmt);
  226. vfprintf(stdout,fmt,v);
  227. #endif
  228. #endif
  229. }
  230. const char* Util::makeDurationString(time_t start, time_t end,char* out,char* tostr)
  231. {
  232. //TODO:
  233. //handle case where start and end dates are not the same day
  234. tm* pstart=localtime(&start);
  235. if (!pstart)
  236. {
  237. sprintf(out,"");
  238. return out;
  239. }
  240. int sday=pstart->tm_mday;
  241. int sweekday=pstart->tm_wday;
  242. int smo=pstart->tm_mon;
  243. int syear=pstart->tm_year+1900;
  244. int shour=pstart->tm_hour;
  245. if (pstart->tm_isdst)
  246. shour=(shour+23)%24; //this substracts 1 while accounting for backing up past 0
  247. int smin=pstart->tm_min;
  248. tm* pend=localtime(&end);
  249. if (!pend)
  250. pend=pstart;
  251. int ehour=pend->tm_hour;
  252. int emin=pend->tm_min;
  253. if (pend->tm_isdst)
  254. ehour=(ehour+23)%24; //this substracts 1 while accounting for backing up past 0
  255. char* matchtz=NULL;
  256. matchtz=g_pApp->os->gettzname()[0];
  257. sprintf(out,"%02li:%02li%s%02li:%02li %s, %s %s %li%s %li",shour,smin,tostr,ehour,emin,matchtz, Util::daysofWeek[sweekday],Util::Months[smo],sday,(sday%10)<4?Util::numberSuffixes[sday%10]:"th",syear);
  258. return out;
  259. }