Source code of Windows XP (NT5)
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.9 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1997 **/
  4. /**********************************************************************/
  5. /*
  6. string.cxx
  7. This module contains a light weight string class
  8. FILE HISTORY:
  9. 4/8/97 michth created
  10. */
  11. //
  12. // Normal includes only for this module to be active
  13. //
  14. #include "precomp.hxx"
  15. #include "aucommon.hxx"
  16. /*******************************************************************
  17. NAME: MLSZAU::STR
  18. SYNOPSIS: Construct a string object
  19. ENTRY: Optional object initializer
  20. NOTES: If the object is not valid (i.e. !IsValid()) then GetLastError
  21. should be called.
  22. The object is guaranteed to construct successfully if nothing
  23. or NULL is passed as the initializer.
  24. ********************************************************************/
  25. // Inlined in stringau.hxx
  26. VOID
  27. MLSZAU::AuxInit( const LPSTR pInit,
  28. DWORD cbLen )
  29. {
  30. BOOL fRet;
  31. if ( pInit )
  32. {
  33. INT cbCopy;
  34. if (cbLen != 0) {
  35. DBG_ASSERT(cbLen >= 2);
  36. DBG_ASSERT(pInit[cbLen -1] == '\0');
  37. DBG_ASSERT(pInit[cbLen -2] == '\0');
  38. cbCopy = cbLen;
  39. }
  40. else {
  41. LPSTR pszIndex;
  42. for (pszIndex = pInit;
  43. *pszIndex != '\0' || *(pszIndex + 1) != '\0';
  44. pszIndex++) {
  45. }
  46. cbCopy = ((DWORD)(pszIndex - pInit)) + 2;
  47. }
  48. fRet = m_bufAnsi.Resize( cbCopy );
  49. if ( fRet ) {
  50. CopyMemory( m_bufAnsi.QueryPtr(), pInit, cbCopy );
  51. m_cbMultiByteLen = (cbCopy)/sizeof(CHAR);
  52. m_bUnicode = FALSE;
  53. m_bInSync = FALSE;
  54. } else {
  55. m_bIsValid = FALSE;
  56. }
  57. } else {
  58. Reset();
  59. }
  60. return;
  61. } // MLSZAU::AuxInit()
  62. VOID
  63. MLSZAU::AuxInit( const LPWSTR pInit,
  64. DWORD cchLen )
  65. {
  66. BOOL fRet;
  67. if ( pInit )
  68. {
  69. INT cbCopy;
  70. if (cchLen != 0) {
  71. DBG_ASSERT(cchLen >= 2);
  72. DBG_ASSERT(pInit[cchLen -1] == (WCHAR)'\0');
  73. DBG_ASSERT(pInit[cchLen -2] == (WCHAR)'\0');
  74. cbCopy = cchLen * sizeof(WCHAR);
  75. }
  76. else {
  77. LPWSTR pszIndex;
  78. for (pszIndex = pInit;
  79. *pszIndex != '\0' || *(pszIndex + 1) != '\0';
  80. pszIndex++) {
  81. }
  82. cbCopy = ((DIFF(pszIndex - pInit)) + 2) * sizeof(WCHAR);
  83. }
  84. fRet = m_bufUnicode.Resize( cbCopy );
  85. if ( fRet ) {
  86. CopyMemory( m_bufUnicode.QueryPtr(), pInit, cbCopy );
  87. m_cchUnicodeLen = (cbCopy)/sizeof(WCHAR);
  88. m_bUnicode = TRUE;
  89. m_bInSync = FALSE;
  90. } else {
  91. m_bIsValid = FALSE;
  92. }
  93. } else {
  94. Reset();
  95. }
  96. return;
  97. } // MLSZAU::AuxInit()
  98. LPTSTR
  99. MLSZAU::QueryStr(BOOL bUnicode)
  100. {
  101. //
  102. // This routine can fail.
  103. // On failure, return a valid UNICODE or ANSI string
  104. // so clients don't trap.
  105. //
  106. LPTSTR pszReturn = NULL;
  107. int iNewStrLen;
  108. if (m_bIsValid) {
  109. if ((bUnicode != m_bUnicode) &&
  110. (!m_bInSync) &&
  111. ((m_bUnicode && (m_cchUnicodeLen != 0)) ||
  112. (!m_bUnicode && (m_cbMultiByteLen != 0)))) {
  113. //
  114. // Need to Convert First
  115. //
  116. if (bUnicode) {
  117. //
  118. // Convert current string to UNICODE
  119. //
  120. // Conversion routines assume a real string and
  121. // add 1 to length for trailing \0 so subtract
  122. // one from total length.
  123. //
  124. iNewStrLen = ConvertMultiByteToUnicode((LPSTR)m_bufAnsi.QueryPtr(), &m_bufUnicode, m_cbMultiByteLen - 1);
  125. if (STR_CONVERSION_SUCCEEDED(iNewStrLen)) {
  126. m_cchUnicodeLen = iNewStrLen+1;
  127. m_bInSync = TRUE;
  128. }
  129. else {
  130. m_bIsValid = FALSE;
  131. }
  132. }
  133. else {
  134. //
  135. // Convert current string to Ansi
  136. //
  137. iNewStrLen = ConvertUnicodeToMultiByte((LPWSTR)m_bufUnicode.QueryPtr(), &m_bufAnsi, m_cchUnicodeLen - 1);
  138. if (STR_CONVERSION_SUCCEEDED(iNewStrLen)) {
  139. m_cbMultiByteLen = iNewStrLen+1;
  140. m_bInSync = TRUE;
  141. }
  142. else {
  143. m_bIsValid = FALSE;
  144. }
  145. }
  146. }
  147. if (m_bIsValid) {
  148. if (bUnicode) {
  149. pszReturn = (LPTSTR)m_bufUnicode.QueryPtr();
  150. }
  151. else {
  152. pszReturn = (LPTSTR)m_bufAnsi.QueryPtr();
  153. }
  154. }
  155. }
  156. return pszReturn;
  157. }