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.

160 lines
2.2 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. MyString::MyString()
  4. {
  5. Zero();
  6. *data = L'\0';
  7. m_len = 0;
  8. }
  9. MyString::MyString(wchar_t *str)
  10. {
  11. Zero();
  12. m_len = wcslen(str);
  13. // Since strlen returns the number of characters in str, excluding the terminal NULL,
  14. // and since strncpy does not automatically append the terminal NULL,
  15. // if m_len < MAX_LEN we want to copy m_len + 1 characters because we assume the
  16. // last character of the source string is the terminal NULL. Otherwise we explicitly
  17. // set the last element to the terminal NULL.
  18. if (m_len >= MAX_LEN - 1){
  19. m_len = MAX_LEN - 1;
  20. wcsncpy(data, str, m_len);
  21. data[m_len] = L'\0';
  22. }
  23. else {
  24. wcscpy(data, str);
  25. }
  26. }
  27. MyString::MyString (const MyString& MyStr)
  28. {
  29. Zero();
  30. wcscpy(data, MyStr.data);
  31. //
  32. // We are assuming MyStr is null -terminated
  33. //
  34. m_len = wcslen(MyStr.data);
  35. this->NullTerminate();
  36. }
  37. const MyString& MyString::operator= (PCWSTR lp)
  38. {
  39. Zero();
  40. m_len = wcslen(lp);
  41. if (m_len > MAX_LEN-1)
  42. {
  43. m_len = MAX_LEN-1;
  44. }
  45. wcsncpy(data, lp, m_len);
  46. this->NullTerminate();
  47. return (*this);
  48. }
  49. const MyString& MyString::operator= (const MyString& MyStr)
  50. {
  51. // We assume MyStr is null terminated
  52. Zero();
  53. wcscpy(data, MyStr.data);
  54. m_len = MyStr.m_len;
  55. this->NullTerminate();
  56. return (*this);
  57. }
  58. const wchar_t* MyString::wcharptr()
  59. {
  60. return data;
  61. }
  62. int MyString::len()
  63. {
  64. return m_len;
  65. }
  66. void MyString::append(MyString str)
  67. {
  68. wcsncat(data, str.data, MAX_LEN - m_len - 1);
  69. data[MAX_LEN-1]=L'\0';
  70. m_len = wcslen(data);
  71. }
  72. void
  73. MyString::append(const wchar_t *str)
  74. {
  75. if (str == NULL )
  76. {
  77. return;
  78. }
  79. wcsncat(data, str, MAX_LEN - m_len - 1);
  80. data[MAX_LEN-1]=L'\0';
  81. m_len = wcslen(data);
  82. return ;
  83. }
  84. int compare(MyString firstStr, MyString secondStr)
  85. {
  86. return wcscmp(firstStr.wcharptr(), secondStr.wcharptr());
  87. }
  88. const wchar_t* MyString::c_str() const
  89. {
  90. return (data);
  91. }
  92. void MyString::Zero()
  93. {
  94. UINT i = 0;
  95. m_len = 0;
  96. for (i = 0; i < MAX_LEN; i++)
  97. {
  98. data[i] = 0;
  99. }
  100. }
  101. VOID
  102. MyString::NullTerminate()
  103. {
  104. data[m_len] = L'\0';
  105. }