Leaked source code of windows server 2003
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.

143 lines
3.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1996-1998, Microsoft Corporation.
  4. //
  5. // File: string.hxx
  6. //
  7. // Contents: Yet another string class
  8. //
  9. // History: 96/Jan/3 DwightKr Created
  10. //
  11. //----------------------------------------------------------------------------
  12. #pragma once
  13. #include <ci64.hxx>
  14. //+---------------------------------------------------------------------------
  15. //
  16. // Class: CVirtualString
  17. //
  18. // Purpose: Maintains a WCHAR string buffer as a virtual allocation
  19. // and grows & commits it as necessary. This class should
  20. // be used whenever you need a string buffer which will be
  21. // slowly grow by appending new strings to it ... such as with
  22. // the construction of query results in a HTML page.
  23. //
  24. // History: 96/Jan/23 DwightKr Created
  25. //
  26. //----------------------------------------------------------------------------
  27. class CVirtualString
  28. {
  29. public:
  30. CVirtualString( unsigned cwcBuffer = 2048 );
  31. ~CVirtualString();
  32. WCHAR const * Get() const
  33. {
  34. Win4Assert( _wcsEnd <= _pwcLastCommitted );
  35. *_wcsEnd = 0;
  36. return _wcsString;
  37. }
  38. WCHAR * GetPointer() const
  39. {
  40. Win4Assert( _wcsEnd <= _pwcLastCommitted );
  41. *_wcsEnd = 0;
  42. return _wcsString;
  43. }
  44. void StrCat( WCHAR const * wcsString )
  45. {
  46. StrCat( wcsString, wcslen( wcsString ) );
  47. }
  48. void StrCat( WCHAR const * wcsString, ULONG cwcValue )
  49. {
  50. Win4Assert( ( 0 != wcsString ) || ( 0 == cwcValue ) );
  51. if ( ( _wcsEnd + cwcValue ) >= _pwcLastCommitted )
  52. GrowBuffer( cwcValue + 1 );
  53. RtlCopyMemory( _wcsEnd, wcsString, cwcValue * sizeof WCHAR );
  54. _wcsEnd += cwcValue;
  55. Win4Assert( (_wcsEnd - _wcsString) < (int) _cwcBuffer );
  56. }
  57. void CharCat( WCHAR wcChar )
  58. {
  59. Win4Assert( 0 != wcChar );
  60. Win4Assert( _wcsEnd <= _pwcLastCommitted );
  61. if ( _wcsEnd != _pwcLastCommitted )
  62. {
  63. // duplicate the assignment to avoid a branch
  64. *_wcsEnd++ = wcChar;
  65. Win4Assert( (_wcsEnd - _wcsString) < (int) _cwcBuffer );
  66. return;
  67. }
  68. GrowBuffer( 1 );
  69. *_wcsEnd++ = wcChar;
  70. Win4Assert( (_wcsEnd - _wcsString) < (int) _cwcBuffer );
  71. }
  72. WCHAR * StrDup() const
  73. {
  74. WCHAR * wcsString = new WCHAR[ StrLen() + 1 ];
  75. RtlCopyMemory( wcsString,
  76. _wcsString,
  77. StrLen() * sizeof(WCHAR) );
  78. wcsString[ StrLen() ] = 0;
  79. return wcsString;
  80. }
  81. unsigned StrLen() const
  82. {
  83. return CiPtrToUint( _wcsEnd - _wcsString );
  84. }
  85. void Empty()
  86. {
  87. Win4Assert( 0 != _wcsString );
  88. Win4Assert( _wcsEnd <= _pwcLastCommitted );
  89. _wcsEnd = _wcsString;
  90. *_wcsEnd = 0;
  91. }
  92. private:
  93. void GrowBuffer( ULONG cwcToGrow );
  94. WCHAR * _wcsString; // Start of the string
  95. WCHAR * _wcsEnd; // Next place to strcat new data
  96. WCHAR * _pwcLastCommitted; // Pointer to last committed WCHAR
  97. unsigned _cwcBuffer; // Total size of virtual alloc'ed memory
  98. };
  99. void URLEscapeW( WCHAR const * wcsString,
  100. CVirtualString & StrResult,
  101. ULONG ulCodepage,
  102. BOOL fConvertSpaceToPlus = FALSE );
  103. void URLEscapeMToW( BYTE const * psz,
  104. unsigned cc,
  105. CVirtualString & StrResult,
  106. BOOL fConvertSpaceToPlus = FALSE );