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.

119 lines
2.7 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: VPC
  4. //
  5. //=====================================================================================//
  6. #include "vpc.h"
  7. void CVPC::SetMacro( const char *pName, const char *pValue, bool bSetupDefineInProjectFile )
  8. {
  9. // Setup the macro.
  10. VPCStatus( false, "Set Macro: $%s = %s", pName, pValue );
  11. macro_t *pMacro = FindOrCreateMacro( pName, true, pValue );
  12. pMacro->m_bSetupDefineInProjectFile = bSetupDefineInProjectFile;
  13. pMacro->m_bInternalCreatedMacro = true;
  14. }
  15. //-----------------------------------------------------------------------------
  16. //-----------------------------------------------------------------------------
  17. macro_t *CVPC::FindOrCreateMacro( const char *pName, bool bCreate, const char *pValue )
  18. {
  19. for ( int i = 0; i < m_Macros.Count(); i++ )
  20. {
  21. if ( !V_stricmp( pName, m_Macros[i].name.String() ) )
  22. {
  23. if ( pValue && V_stricmp( pValue, m_Macros[i].value.String() ) )
  24. {
  25. // update
  26. m_Macros[i].value = pValue;
  27. }
  28. return &m_Macros[i];
  29. }
  30. }
  31. if ( !bCreate )
  32. {
  33. return NULL;
  34. }
  35. int index = m_Macros.AddToTail();
  36. m_Macros[index].name = pName;
  37. m_Macros[index].value = pValue;
  38. return &m_Macros[index];
  39. }
  40. int CVPC::GetMacrosMarkedForCompilerDefines( CUtlVector< macro_t* > &macroDefines )
  41. {
  42. macroDefines.Purge();
  43. for ( int i = 0; i < m_Macros.Count(); i++ )
  44. {
  45. if ( m_Macros[i].m_bSetupDefineInProjectFile )
  46. {
  47. macroDefines.AddToTail( &m_Macros[i] );
  48. }
  49. }
  50. return macroDefines.Count();
  51. }
  52. void CVPC::ResolveMacrosInString( char const *pString, char *pOutBuff, int outBuffSize )
  53. {
  54. char macroName[MAX_SYSTOKENCHARS];
  55. char buffer1[MAX_SYSTOKENCHARS];
  56. char buffer2[MAX_SYSTOKENCHARS];
  57. int i;
  58. // iterate and resolve user macros until all macros resolved
  59. strcpy( buffer1, pString );
  60. bool bDone;
  61. do
  62. {
  63. bDone = true;
  64. for ( i=0; i<m_Macros.Count(); i++ )
  65. {
  66. sprintf( macroName, "$%s", m_Macros[i].name.String() );
  67. if ( Sys_ReplaceString( buffer1, macroName, m_Macros[i].value.String(), buffer2, sizeof( buffer2 ) ) )
  68. {
  69. bDone = false;
  70. }
  71. strcpy( buffer1, buffer2 );
  72. }
  73. }
  74. while ( !bDone );
  75. int len = strlen( buffer1 );
  76. if ( outBuffSize < len )
  77. len = outBuffSize;
  78. memcpy( pOutBuff, buffer1, len );
  79. pOutBuff[len] = '\0';
  80. }
  81. void CVPC::RemoveScriptCreatedMacros()
  82. {
  83. for ( int i=0; i < m_Macros.Count(); i++ )
  84. {
  85. if ( !m_Macros[i].m_bInternalCreatedMacro )
  86. {
  87. m_Macros.Remove( i );
  88. --i;
  89. }
  90. }
  91. }
  92. const char *CVPC::GetMacroValue( const char *pName )
  93. {
  94. for ( int i = 0; i < m_Macros.Count(); i++ )
  95. {
  96. if ( !V_stricmp( pName, m_Macros[i].name.String() ) )
  97. {
  98. return m_Macros[i].value.String();
  99. }
  100. }
  101. // not found
  102. return "";
  103. }