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.

207 lines
4.3 KiB

  1. //========= Copyright 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(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. V_vsprintf_safe( string[curstring], format, argptr );
  26. va_end (argptr);
  27. return string[curstring];
  28. }
  29. //--------------------------------------------------------------------------------------------------------------
  30. char * BufPrintf(char *buf, int& len, 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. buf[ len - 1 ] = 0;
  38. va_end(argptr);
  39. len -= strlen(buf);
  40. return buf + strlen(buf);
  41. }
  42. //--------------------------------------------------------------------------------------------------------------
  43. wchar_t * BufWPrintf(wchar_t *buf, int& len, const wchar_t *fmt, ...)
  44. {
  45. if (len <= 0)
  46. return NULL;
  47. va_list argptr;
  48. va_start(argptr, fmt);
  49. #ifdef WIN32
  50. _vsnwprintf(buf, len, fmt, argptr);
  51. #else
  52. vswprintf( buf, len, fmt, argptr );
  53. #endif
  54. buf[ len - 1 ] = 0;
  55. va_end(argptr);
  56. len -= wcslen(buf);
  57. return buf + wcslen(buf);
  58. }
  59. //--------------------------------------------------------------------------------------------------------------
  60. const wchar_t * NumAsWString( int val )
  61. {
  62. const int BufLen = 16;
  63. static wchar_t buf[BufLen];
  64. int len = BufLen;
  65. BufWPrintf( buf, len, L"%d", val );
  66. return buf;
  67. }
  68. //--------------------------------------------------------------------------------------------------------------
  69. const char * NumAsString( int val )
  70. {
  71. const int BufLen = 16;
  72. static char buf[BufLen];
  73. int len = BufLen;
  74. BufPrintf( buf, len, "%d", val );
  75. return buf;
  76. }
  77. //--------------------------------------------------------------------------------------------------------
  78. /**
  79. * Returns the token parsed by SharedParse()
  80. */
  81. char *SharedGetToken( void )
  82. {
  83. return s_shared_token;
  84. }
  85. //--------------------------------------------------------------------------------------------------------
  86. /**
  87. * Returns the token parsed by SharedParse()
  88. */
  89. void SharedSetQuoteChar( char c )
  90. {
  91. s_shared_quote = c;
  92. }
  93. //--------------------------------------------------------------------------------------------------------
  94. /**
  95. * Parse a token out of a string
  96. */
  97. const char *SharedParse( const char *data )
  98. {
  99. int c;
  100. int len;
  101. len = 0;
  102. s_shared_token[0] = 0;
  103. if (!data)
  104. return NULL;
  105. // skip whitespace
  106. skipwhite:
  107. while ( (c = *data) <= ' ')
  108. {
  109. if (c == 0)
  110. return NULL; // end of file;
  111. data++;
  112. }
  113. // skip // comments
  114. if (c=='/' && data[1] == '/')
  115. {
  116. while (*data && *data != '\n')
  117. data++;
  118. goto skipwhite;
  119. }
  120. // handle quoted strings specially
  121. if (c == s_shared_quote)
  122. {
  123. data++;
  124. while (1)
  125. {
  126. c = *data++;
  127. if (c==s_shared_quote || !c)
  128. {
  129. s_shared_token[len] = 0;
  130. return data;
  131. }
  132. s_shared_token[len] = c;
  133. len++;
  134. }
  135. }
  136. // parse single characters
  137. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c == ',' )
  138. {
  139. s_shared_token[len] = c;
  140. len++;
  141. s_shared_token[len] = 0;
  142. return data+1;
  143. }
  144. // parse a regular word
  145. do
  146. {
  147. s_shared_token[len] = c;
  148. data++;
  149. len++;
  150. c = *data;
  151. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c == ',' )
  152. break;
  153. } while (c>32);
  154. s_shared_token[len] = 0;
  155. return data;
  156. }
  157. //--------------------------------------------------------------------------------------------------------
  158. /**
  159. * Returns true if additional data is waiting to be processed on this line
  160. */
  161. bool SharedTokenWaiting( const char *buffer )
  162. {
  163. const char *p;
  164. p = buffer;
  165. while ( *p && *p!='\n')
  166. {
  167. if ( !isspace( *p ) || isalnum( *p ) )
  168. return true;
  169. p++;
  170. }
  171. return false;
  172. }