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.

226 lines
4.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: dll-agnostic routines (no dll dependencies here)
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. // Author: Matthew D. Campbell ([email protected]), 2003
  8. #include "cbase.h"
  9. #include <ctype.h>
  10. #include "shared_util.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. static char s_shared_token[ 1500 ];
  14. static char s_shared_quote = '\"';
  15. //--------------------------------------------------------------------------------------------------------------
  16. char * SharedVarArgs(PRINTF_FORMAT_STRING const char *format, ...)
  17. {
  18. va_list argptr;
  19. const int BufLen = 1024;
  20. const int NumBuffers = 4;
  21. static char string[NumBuffers][BufLen];
  22. static int curstring = 0;
  23. curstring = ( curstring + 1 ) % NumBuffers;
  24. va_start (argptr, format);
  25. _vsnprintf( string[curstring], BufLen, format, argptr );
  26. va_end (argptr);
  27. return string[curstring];
  28. }
  29. //--------------------------------------------------------------------------------------------------------------
  30. char * BufPrintf(char *buf, int& len, PRINTF_FORMAT_STRING const char *fmt, ...)
  31. {
  32. if (len <= 0)
  33. return NULL;
  34. va_list argptr;
  35. va_start(argptr, fmt);
  36. _vsnprintf(buf, len, fmt, argptr);
  37. va_end(argptr);
  38. // Make sure the buffer is null-terminated.
  39. buf[ len - 1 ] = 0;
  40. len -= strlen(buf);
  41. return buf + strlen(buf);
  42. }
  43. #ifdef _WIN32
  44. //--------------------------------------------------------------------------------------------------------------
  45. wchar_t * BufWPrintf(wchar_t *buf, int& len, PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
  46. {
  47. if (len <= 0)
  48. return NULL;
  49. va_list argptr;
  50. va_start(argptr, fmt);
  51. _vsnwprintf(buf, len, fmt, argptr);
  52. va_end(argptr);
  53. // Make sure the buffer is null-terminated.
  54. buf[ len - 1 ] = 0;
  55. len -= wcslen(buf);
  56. return buf + wcslen(buf);
  57. }
  58. #endif
  59. //--------------------------------------------------------------------------------------------------------------
  60. #ifdef _WIN32
  61. const wchar_t * NumAsWString( int val )
  62. {
  63. const int BufLen = 16;
  64. static wchar_t buf[BufLen];
  65. int len = BufLen;
  66. BufWPrintf( buf, len, L"%d", val );
  67. return buf;
  68. }
  69. #endif
  70. // dgoodenough - PS3 needs this guy as well.
  71. // PS3_BUILDFIX
  72. #ifdef _PS3
  73. const wchar_t * NumAsWString( int val )
  74. {
  75. const int BufLen = 16;
  76. static wchar_t buf[BufLen];
  77. char szBuf[BufLen];
  78. Q_snprintf(szBuf, BufLen, "%d", val );
  79. szBuf[BufLen - 1] = 0;
  80. for ( int i = 0; i < BufLen; ++i )
  81. {
  82. buf[i] = szBuf[i];
  83. }
  84. return buf;
  85. }
  86. #endif
  87. //--------------------------------------------------------------------------------------------------------------
  88. const char * NumAsString( int val )
  89. {
  90. const int BufLen = 16;
  91. static char buf[BufLen];
  92. int len = BufLen;
  93. BufPrintf( buf, len, "%d", val );
  94. return buf;
  95. }
  96. //--------------------------------------------------------------------------------------------------------
  97. /**
  98. * Returns the token parsed by SharedParse()
  99. */
  100. char *SharedGetToken( void )
  101. {
  102. return s_shared_token;
  103. }
  104. //--------------------------------------------------------------------------------------------------------
  105. /**
  106. * Returns the token parsed by SharedParse()
  107. */
  108. void SharedSetQuoteChar( char c )
  109. {
  110. s_shared_quote = c;
  111. }
  112. //--------------------------------------------------------------------------------------------------------
  113. /**
  114. * Parse a token out of a string
  115. */
  116. const char *SharedParse( const char *data )
  117. {
  118. int c;
  119. int len;
  120. len = 0;
  121. s_shared_token[0] = 0;
  122. if (!data)
  123. return NULL;
  124. // skip whitespace
  125. skipwhite:
  126. while ( (c = *data) <= ' ')
  127. {
  128. if (c == 0)
  129. return NULL; // end of file;
  130. data++;
  131. }
  132. // skip // comments
  133. if (c=='/' && data[1] == '/')
  134. {
  135. while (*data && *data != '\n')
  136. data++;
  137. goto skipwhite;
  138. }
  139. // handle quoted strings specially
  140. if (c == s_shared_quote)
  141. {
  142. data++;
  143. while (1)
  144. {
  145. c = *data++;
  146. if (c==s_shared_quote || !c)
  147. {
  148. s_shared_token[len] = 0;
  149. return data;
  150. }
  151. s_shared_token[len] = c;
  152. len++;
  153. }
  154. }
  155. // parse single characters
  156. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c == ',' )
  157. {
  158. s_shared_token[len] = c;
  159. len++;
  160. s_shared_token[len] = 0;
  161. return data+1;
  162. }
  163. // parse a regular word
  164. do
  165. {
  166. s_shared_token[len] = c;
  167. data++;
  168. len++;
  169. c = *data;
  170. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c == ',' )
  171. break;
  172. } while (c>32);
  173. s_shared_token[len] = 0;
  174. return data;
  175. }
  176. //--------------------------------------------------------------------------------------------------------
  177. /**
  178. * Returns true if additional data is waiting to be processed on this line
  179. */
  180. bool SharedTokenWaiting( const char *buffer )
  181. {
  182. const char *p;
  183. p = buffer;
  184. while ( *p && *p!='\n')
  185. {
  186. if ( !V_isspace( *p ) || V_isalnum( *p ) )
  187. return true;
  188. p++;
  189. }
  190. return false;
  191. }