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.

201 lines
4.4 KiB

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