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.

204 lines
4.5 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "../game/shared/choreoscene.h"
  7. #include "../game/shared/choreoactor.h"
  8. #include "../game/shared/choreochannel.h"
  9. #include "../game/shared/choreoevent.h"
  10. #include "../game/shared/iscenetokenprocessor.h"
  11. #include "characterset.h"
  12. // NOTE: This has to be the last file included!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose: Helper for parsing scene data file
  16. //-----------------------------------------------------------------------------
  17. class CSceneTokenProcessor : public ISceneTokenProcessor
  18. {
  19. public:
  20. CSceneTokenProcessor();
  21. const char *CurrentToken( void );
  22. bool GetToken( bool crossline );
  23. bool TokenAvailable( void );
  24. void Error( const char *fmt, ... );
  25. void SetBuffer( char *buffer );
  26. private:
  27. const char *ParseNextToken (const char *data);
  28. const char *m_pBuffer;
  29. char m_szToken[ 1024 ];
  30. characterset_t m_BreakSetIncludingColons;
  31. };
  32. CSceneTokenProcessor::CSceneTokenProcessor()
  33. {
  34. CharacterSetBuild( &m_BreakSetIncludingColons, "{}()':" );
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. // Output : const char
  39. //-----------------------------------------------------------------------------
  40. const char *CSceneTokenProcessor::CurrentToken( void )
  41. {
  42. return m_szToken;
  43. }
  44. const char *CSceneTokenProcessor::ParseNextToken (const char *data)
  45. {
  46. unsigned char c;
  47. int len;
  48. characterset_t *breaks;
  49. breaks = &m_BreakSetIncludingColons;
  50. len = 0;
  51. m_szToken[0] = 0;
  52. if (!data)
  53. return NULL;
  54. // skip whitespace
  55. skipwhite:
  56. while ( (c = *data) <= ' ')
  57. {
  58. if (c == 0)
  59. return NULL; // end of file;
  60. data++;
  61. }
  62. // skip // comments
  63. if (c=='/' && data[1] == '/')
  64. {
  65. while (*data && *data != '\n')
  66. data++;
  67. goto skipwhite;
  68. }
  69. // handle quoted strings specially
  70. if (c == '\"')
  71. {
  72. data++;
  73. while (1)
  74. {
  75. c = *data++;
  76. if (c=='\"' || !c)
  77. {
  78. m_szToken[len] = 0;
  79. return data;
  80. }
  81. m_szToken[len] = c;
  82. len++;
  83. }
  84. }
  85. // parse single characters
  86. if ( IN_CHARACTERSET( *breaks, c ) )
  87. {
  88. m_szToken[len] = c;
  89. len++;
  90. m_szToken[len] = 0;
  91. return data+1;
  92. }
  93. // parse a regular word
  94. do
  95. {
  96. m_szToken[len] = c;
  97. data++;
  98. len++;
  99. c = *data;
  100. if ( IN_CHARACTERSET( *breaks, c ) )
  101. break;
  102. } while (c>32);
  103. m_szToken[len] = 0;
  104. return data;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. // Input : crossline -
  109. // Output : Returns true on success, false on failure.
  110. //-----------------------------------------------------------------------------
  111. bool CSceneTokenProcessor::GetToken( bool crossline )
  112. {
  113. // NOTE: crossline is ignored here, may need to implement if needed
  114. m_pBuffer = ParseNextToken( m_pBuffer );
  115. if ( Q_strlen( m_szToken ) >= 0 )
  116. return true;
  117. return false;
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. // Output : Returns true on success, false on failure.
  122. //-----------------------------------------------------------------------------
  123. bool CSceneTokenProcessor::TokenAvailable( void )
  124. {
  125. const char *search_p = m_pBuffer;
  126. while ( *search_p <= 32)
  127. {
  128. if (*search_p == '\n')
  129. return false;
  130. search_p++;
  131. if ( !*search_p )
  132. return false;
  133. }
  134. if (*search_p == ';' || *search_p == '#' || // semicolon and # is comment field
  135. (*search_p == '/' && *((search_p)+1) == '/')) // also make // a comment field
  136. return false;
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose:
  141. // Input : *fmt -
  142. // ... -
  143. //-----------------------------------------------------------------------------
  144. void CSceneTokenProcessor::Error( const char *fmt, ... )
  145. {
  146. char string[ 2048 ];
  147. va_list argptr;
  148. va_start( argptr, fmt );
  149. Q_vsnprintf( string, sizeof(string), fmt, argptr );
  150. va_end( argptr );
  151. Warning( "%s", string );
  152. Assert(0);
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose:
  156. // Input : *buffer -
  157. //-----------------------------------------------------------------------------
  158. void CSceneTokenProcessor::SetBuffer( char *buffer )
  159. {
  160. m_pBuffer = buffer;
  161. }
  162. CSceneTokenProcessor g_TokenProcessor;
  163. ISceneTokenProcessor *GetTokenProcessor()
  164. {
  165. return &g_TokenProcessor;
  166. }
  167. void SetTokenProcessorBuffer( const char *buf )
  168. {
  169. g_TokenProcessor.SetBuffer( (char *)buf );
  170. }