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.

195 lines
2.6 KiB

  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdlib.h>
  4. #include "strlist.h"
  5. //**********************************************************************************
  6. // Implements the StrList, a double-linked list that maintains a list of value
  7. // entries, each assosiated with a unique key
  8. //
  9. elt* CStrList::FindElement ( LPCTSTR key )
  10. {
  11. elt* pelt = NULL;
  12. if (!count)
  13. return NULL;
  14. if (!key)
  15. return NULL;
  16. if (!_tcslen(key))
  17. return NULL;
  18. try
  19. {
  20. Lock();
  21. for ( pelt = head; pelt; pelt = pelt->next )
  22. {
  23. if (!_tcscmp(key, pelt->key)) // found it!
  24. {
  25. Unlock ();
  26. return pelt;
  27. }
  28. }
  29. Unlock ();
  30. }
  31. catch (...)
  32. {
  33. Unlock ();
  34. }
  35. return NULL;
  36. }
  37. bool CStrList::AddValue ( LPCTSTR key, LPCTSTR val )
  38. {
  39. elt *pelt = NULL;
  40. if (!key || !val)
  41. return false;
  42. if (!_tcslen(key))
  43. return false;
  44. try
  45. {
  46. Lock ();
  47. pelt = FindElement (key);
  48. if (pelt)
  49. {
  50. free (pelt->val);
  51. pelt->val = _tcsdup (val);
  52. }
  53. else
  54. {
  55. pelt = new elt;
  56. if ( !pelt )
  57. return false;
  58. pelt->key = _tcsdup (key);
  59. pelt->val = _tcsdup (val);
  60. pelt->next = head;
  61. pelt->prev = NULL;
  62. if (head)
  63. head->prev = pelt;
  64. head = pelt;
  65. count++;
  66. }
  67. Unlock ();
  68. }
  69. catch (...)
  70. {
  71. Unlock ();
  72. return false;
  73. }
  74. return true;
  75. }
  76. LPTSTR CStrList::Lookup ( LPCTSTR key, LPTSTR outBuf )
  77. {
  78. elt *pelt = FindElement (key);
  79. if (!pelt)
  80. return NULL;
  81. _tcscpy (outBuf, pelt->val);
  82. return outBuf;
  83. }
  84. void CStrList::RemoveByKey ( LPCTSTR key )
  85. {
  86. Lock ();
  87. try
  88. {
  89. elt *pelt = FindElement (key);
  90. if (!pelt)
  91. {
  92. Unlock ();
  93. return;
  94. }
  95. if (pelt->prev)
  96. pelt->prev->next = pelt->next;
  97. else
  98. head = pelt->next;
  99. if (pelt->next)
  100. pelt->next->prev = pelt->prev;
  101. count--;
  102. delete pelt;
  103. }
  104. catch (...)
  105. {
  106. }
  107. Unlock ();
  108. }
  109. void CStrList::RemoveAll ()
  110. {
  111. elt *pelt = NULL;
  112. Lock ();
  113. try
  114. {
  115. while (head)
  116. {
  117. pelt = head;
  118. head = head->next;
  119. count--;
  120. delete pelt;
  121. }
  122. }
  123. catch (...)
  124. {
  125. }
  126. Unlock ();
  127. }
  128. LPTSTR CStrList::ConcatKeyValues ( LPCTSTR separator, LPTSTR outBuf )
  129. {
  130. if (!outBuf)
  131. return NULL;
  132. if (!separator)
  133. return NULL;
  134. Lock ();
  135. try
  136. {
  137. _tcscpy (outBuf, _T(""));
  138. for (elt *pelt = head; pelt; pelt = pelt->next)
  139. {
  140. _tcscat (outBuf, pelt->key);
  141. _tcscat (outBuf, _T("=\""));
  142. _tcscat (outBuf, pelt->val);
  143. _tcscat (outBuf, _T("\""));
  144. _tcscat (outBuf, separator);
  145. }
  146. }
  147. catch (...)
  148. {
  149. Unlock ();
  150. return NULL;
  151. }
  152. Unlock ();
  153. return outBuf;
  154. }