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.

163 lines
2.2 KiB

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