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.

107 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. // A string class that keeps all its memory on the stack and performs no
  8. // memory allocation
  9. //=============================================================================//
  10. #ifndef STACKSTRING_H
  11. #define STACKSTRING_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. template< size_t cubSize >
  16. class CStackString
  17. {
  18. public:
  19. class CStackString() { Reset(); }
  20. class CStackString( const char *pchRHS ) { Set( pchRHS ); }
  21. void Reset() { m_rchBuffer[0] = 0; m_pchEnd = m_rchBuffer; }
  22. void Set( const char *pchValue );
  23. const char *Get() const { return m_rchBuffer; }
  24. static size_t GetCubSize() { return cubSize; }
  25. size_t Length() const { return m_pchEnd - &m_rchBuffer[0]; }
  26. CStackString & operator=( const char *pchValue ) { Set( pchValue ); return *this; }
  27. CStackString & operator+=( const char *pchValue ) { Append( pchValue ); return *this; }
  28. operator const char*() const { return m_rchBuffer; }
  29. void Format( PRINTF_FORMAT_STRING const char *pchFormat, ... );
  30. void AppendFormat( PRINTF_FORMAT_STRING const char *pchFormat, ... );
  31. void AppendFormatV( const char *pchFormat, va_list marker );
  32. void Append( const char *pchValue );
  33. void SetIndent( uint32 unCount, char chIndent = '\t' );
  34. private:
  35. char m_rchBuffer[ cubSize ];
  36. char *m_pchEnd;
  37. };
  38. typedef CStackString<256> CStackString256;
  39. typedef CStackString<512> CStackString512;
  40. typedef CStackString<1024> CStackString1024;
  41. typedef CStackString<8192> CStackStringMax;
  42. template< size_t cubSize >
  43. void CStackString<cubSize>::Set( const char *pchValue )
  44. {
  45. V_strcpy_safe( m_rchBuffer, pchValue );
  46. m_pchEnd = &m_rchBuffer[Q_strlen(m_rchBuffer)];
  47. }
  48. template< size_t cubSize >
  49. void CStackString<cubSize>::AppendFormatV( const char *pchFormat, va_list marker )
  50. {
  51. size_t cubPrinted = Q_vsnprintf( m_pchEnd, GetCubSize() - Length(), pchFormat, marker );
  52. m_pchEnd += cubPrinted;
  53. }
  54. template< size_t cubSize >
  55. void CStackString<cubSize>::Format( PRINTF_FORMAT_STRING const char *pchFormat, ... )
  56. {
  57. va_list marker;
  58. va_start( marker, pchFormat );
  59. size_t cubPrinted = Q_vsnprintf( m_rchBuffer, GetCubSize(), pchFormat, marker );
  60. m_pchEnd = m_rchBuffer + cubPrinted;
  61. va_end( marker );
  62. }
  63. template< size_t cubSize >
  64. void CStackString<cubSize>::AppendFormat( PRINTF_FORMAT_STRING const char *pchFormat, ... )
  65. {
  66. va_list marker;
  67. va_start( marker, pchFormat );
  68. AppendFormatV( pchFormat, marker );
  69. va_end( marker );
  70. }
  71. template< size_t cubSize >
  72. void CStackString<cubSize>::Append( const char *pchValue )
  73. {
  74. Q_strcat( m_rchBuffer, pchValue, GetCubSize() );
  75. m_pchEnd = m_rchBuffer + Q_strlen(m_rchBuffer);
  76. }
  77. template< size_t cubSize >
  78. void CStackString<cubSize>::SetIndent( uint32 unCount, char chIndent )
  79. {
  80. Reset();
  81. for ( uint32 x = 0; x < unCount && x < (GetCubSize()-1); x++ )
  82. {
  83. *m_pchEnd = chIndent;
  84. m_pchEnd++;
  85. }
  86. *m_pchEnd = '\0';
  87. }
  88. #endif