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.

474 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. keytree.c
  5. Abstract:
  6. functions handling the operation of the treeview
  7. that displays the keys in a memdb tree in memdbe.exe
  8. Author:
  9. Matthew Vanderzee (mvander) 13-Aug-1999
  10. Revision History:
  11. --*/
  12. #include "pch.h"
  13. #include "dbeditp.h"
  14. //
  15. // controls in display
  16. //
  17. extern HWND g_hTreeKey;
  18. //
  19. // when we put items in the treeview, instead of just inserting
  20. // them, we create a growlist which we fill with relevant data.
  21. // then we go through that and filter if necessary, and then
  22. // we put the remaining stuff in the tree.
  23. //
  24. INT g_LastItem = -1;
  25. CHAR g_LastItemName[MEMDB_MAX] = "";
  26. GROWLIST g_AddList = INIT_GROWLIST;
  27. typedef struct {
  28. INT ParentListIndex;
  29. UINT KeyIndex;
  30. HTREEITEM TreeItem;
  31. BYTE Flags;
  32. CHAR Name[];
  33. } ADDLISTITEM, *PADDLISTITEM;
  34. #define ADDLISTFLAG_ENDPOINT 0x01
  35. #define ADDLISTFLAG_KEEP 0x02
  36. extern BOOL g_ParsePaths = TRUE;
  37. CHAR g_KeyPiece[MEMDB_MAX] = "";
  38. CHAR g_FilterPattern[MEMDB_MAX] = "";
  39. //
  40. // functions we need from keytree.c
  41. //
  42. extern BOOL
  43. KeyTreeGetNameOfItem (
  44. HTREEITEM hItem,
  45. PSTR Buffer
  46. );
  47. extern HTREEITEM
  48. KeyTreeFindChildItem (
  49. HTREEITEM hItem,
  50. PSTR Str
  51. );
  52. #define ISDRIVELETTER(Str) ((Str) && (Str)[1]==':' && ((Str)[2]=='\0' || (Str)[2]=='\\'))
  53. PSTR
  54. GetPieceOfKey (
  55. PSTR KeyPtr,
  56. PSTR PieceBuf
  57. )
  58. {
  59. PSTR Cur;
  60. if (!KeyPtr || (*KeyPtr=='\0') || (*KeyPtr=='\\')) {
  61. return NULL;
  62. }
  63. while ((*KeyPtr!='\0') && (*KeyPtr!='\\')) {
  64. *(PieceBuf++) = *(KeyPtr++);
  65. }
  66. *PieceBuf = '\0';
  67. if (*KeyPtr == '\\') {
  68. KeyPtr++;
  69. }
  70. return KeyPtr;
  71. }
  72. HTREEITEM
  73. pKeyAddEnumFirstChild (
  74. HTREEITEM hParent
  75. )
  76. {
  77. if (hParent == NULL) {
  78. return TreeView_GetRoot (g_hTreeKey);
  79. }
  80. return TreeView_GetChild (g_hTreeKey, hParent);
  81. }
  82. HTREEITEM
  83. pKeyAddEnumNextChild (
  84. HTREEITEM hPrevChild
  85. )
  86. {
  87. return TreeView_GetNextSibling (g_hTreeKey, hPrevChild);
  88. }
  89. VOID
  90. KeyAddClear (
  91. VOID
  92. )
  93. {
  94. g_LastItem = -1;
  95. g_LastItemName[0] = '\0';
  96. }
  97. HTREEITEM
  98. KeyAddItem (
  99. PSTR ItemName,
  100. HTREEITEM Parent,
  101. KEYHANDLE Index
  102. )
  103. {
  104. HTREEITEM Item;
  105. TVINSERTSTRUCT tvis;
  106. tvis.hParent = Parent;
  107. tvis.hInsertAfter = TVI_LAST;
  108. tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
  109. tvis.item.pszText = ItemName;
  110. tvis.item.iImage = 0;
  111. tvis.item.iSelectedImage = 1;
  112. tvis.item.lParam = Index;
  113. Item = TreeView_InsertItem (g_hTreeKey, &tvis);
  114. return Item;
  115. }
  116. INT
  117. pKeyAddItemToList (
  118. PSTR ItemName,
  119. INT ParentListIndex,
  120. KEYHANDLE Index,
  121. BOOL EndPoint
  122. )
  123. {
  124. PADDLISTITEM pItem;
  125. ADDLISTITEM ali;
  126. ali.TreeItem = NULL;
  127. ali.KeyIndex = Index;
  128. ali.ParentListIndex = ParentListIndex;
  129. ali.Flags = EndPoint ? ADDLISTFLAG_ENDPOINT : 0;
  130. if (!(pItem = (PADDLISTITEM) GlAppend (
  131. &g_AddList,
  132. (PBYTE)&ali,
  133. sizeof(ADDLISTITEM) + CharCountA (ItemName) + 1
  134. )))
  135. {
  136. DEBUGMSG ((DBG_ERROR, "Could not add item because GrowListAppend failed"));
  137. return -1;
  138. }
  139. StringCopyA (pItem->Name, ItemName);
  140. return GlGetSize (&g_AddList) - 1;
  141. }
  142. INT
  143. pKeyAddKeyToList (
  144. PSTR Name,
  145. KEYHANDLE Index,
  146. BOOL EndPoint
  147. )
  148. {
  149. PADDLISTITEM ali;
  150. INT Len;
  151. PSTR Next, NameLeft;
  152. INT ItemIndex;
  153. NameLeft = Name;
  154. while (g_LastItem >= 0) {
  155. Len = CharCountA (g_LastItemName);
  156. if (StringIMatchCharCountA (g_LastItemName, Name, Len) && (Name[Len]=='\\')) {
  157. //
  158. // we have found the parent of the new item
  159. //
  160. NameLeft = Name + Len + 1;
  161. break;
  162. }
  163. ali = (PADDLISTITEM) GlGetItem (&g_AddList, g_LastItem);
  164. if (((g_LastItem = ali->ParentListIndex) < 0) ||
  165. !(Next = _mbsrchr (g_LastItemName, '\\')))
  166. {
  167. g_LastItem = -1;
  168. g_LastItemName[0] = '\0';
  169. NameLeft = Name;
  170. break;
  171. }
  172. *Next = '\0';
  173. }
  174. if (ISDRIVELETTER (NameLeft) && g_ParsePaths)
  175. {
  176. if (EndPoint) {
  177. return pKeyAddItemToList (NameLeft, g_LastItem, Index, TRUE);
  178. } else {
  179. return -1;
  180. }
  181. }
  182. ItemIndex = pKeyAddItemToList (NameLeft, g_LastItem, Index, EndPoint);
  183. if (g_LastItem >= 0) {
  184. StringCatA (g_LastItemName, "\\");
  185. StringCatA (g_LastItemName, NameLeft);
  186. } else {
  187. StringCopyA (g_LastItemName, NameLeft);
  188. }
  189. g_LastItem = ItemIndex;
  190. return ItemIndex;
  191. }
  192. VOID
  193. pKeyAddKeepListIndex (
  194. INT Index
  195. )
  196. {
  197. PADDLISTITEM ali;
  198. if (Index < 0) {
  199. return;
  200. }
  201. ali = (PADDLISTITEM) GlGetItem (&g_AddList, Index);
  202. ali->Flags |= ADDLISTFLAG_KEEP;
  203. //
  204. // recurse up tree, marking all parent index items to keep them.
  205. //
  206. pKeyAddKeepListIndex (ali->ParentListIndex);
  207. }
  208. BOOL
  209. pKeyAddApplyFilterToList (
  210. VOID
  211. )
  212. {
  213. PCTSTR key;
  214. INT i, count;
  215. PADDLISTITEM ali;
  216. PPARSEDPATTERN parsedPattern;
  217. if (g_FilterPattern[0] == 0) {
  218. return TRUE;
  219. }
  220. parsedPattern = CreateParsedPattern (g_FilterPattern);
  221. count = GlGetSize (&g_AddList);
  222. for (i=0; i < count; i++) {
  223. ali = (PADDLISTITEM) GlGetItem (&g_AddList, i);
  224. if (ali->Flags & ADDLISTFLAG_ENDPOINT) {
  225. key = MemDbGetKeyFromHandle (ali->KeyIndex, 0);
  226. if (key) {
  227. if (TestParsedPattern (parsedPattern, key)) {
  228. //
  229. // we have found an endpoint that matches the pattern, so
  230. // mark it and all its parents to keep them.
  231. //
  232. pKeyAddKeepListIndex (i);
  233. }
  234. MemDbReleaseMemory (key);
  235. }
  236. }
  237. }
  238. DestroyParsedPattern (parsedPattern);
  239. return TRUE;
  240. }
  241. BOOL
  242. pKeyAddList (
  243. VOID
  244. )
  245. {
  246. PADDLISTITEM ali, temp;
  247. HTREEITEM hParent;
  248. INT i, count;
  249. BOOL NoFilter;
  250. count = GlGetSize (&g_AddList);
  251. NoFilter = (g_FilterPattern[0] == '\0');
  252. for (i=0; i<count; i++) {
  253. ali = (PADDLISTITEM) GlGetItem (&g_AddList, i);
  254. if (ali->ParentListIndex >= 0) {
  255. //
  256. // get the index of the additem that is the parent of this
  257. // item, and then get the treeitem handle from that.
  258. //
  259. temp = (PADDLISTITEM) GlGetItem (&g_AddList, ali->ParentListIndex);
  260. hParent = temp->TreeItem;
  261. } else {
  262. hParent = NULL;
  263. }
  264. if (NoFilter || (ali->Flags & ADDLISTFLAG_KEEP)) {
  265. if (!(ali->TreeItem = KeyAddItem (ali->Name, hParent, ali->KeyIndex))) {
  266. DEBUGMSG ((DBG_ERROR, "Could not add item!"));
  267. return FALSE;
  268. }
  269. }
  270. }
  271. return TRUE;
  272. }
  273. BOOL
  274. KeyAddSubLevels (
  275. HTREEITEM ParentItem
  276. )
  277. {
  278. HTREEITEM hItem;
  279. MEMDB_ENUM e;
  280. TCHAR Key[MEMDB_MAX];
  281. g_LastItem = -1;
  282. ZeroMemory (&g_AddList, sizeof (g_AddList));
  283. if (ParentItem) {
  284. KeyTreeGetNameOfItem (ParentItem, Key);
  285. StringCat (Key, TEXT("\\*"));
  286. } else {
  287. StringCopy (Key, TEXT("*"));
  288. }
  289. if (MemDbEnumFirst (&e, Key, ENUMFLAG_ALL, 0, ENUMLEVEL_ALLLEVELS)) {
  290. do {
  291. pKeyAddKeyToList (e.FullKeyName, e.KeyHandle, e.EndPoint);
  292. } while (MemDbEnumNext (&e));
  293. }
  294. pKeyAddApplyFilterToList ();
  295. pKeyAddList ();
  296. GlFree (&g_AddList);
  297. ZeroMemory (&g_AddList, sizeof (g_AddList));
  298. return TRUE;
  299. }
  300. VOID
  301. KeyAddSetFilterPattern (
  302. PSTR Pattern
  303. )
  304. {
  305. if (Pattern) {
  306. StringCopyA (g_FilterPattern, Pattern);
  307. } else {
  308. g_FilterPattern[0] = '\0';
  309. }
  310. }
  311. HTREEITEM
  312. KeyAddCreateItem (
  313. PSTR Key
  314. )
  315. {
  316. BOOL Created = FALSE;
  317. CHAR NewKey[MEMDB_MAX];
  318. PSTR Next, End;
  319. KEYHANDLE Index;
  320. HTREEITEM hParent = NULL, hItem;
  321. NewKey[0] = '\0';
  322. while (Next = GetPieceOfKey (Key, g_KeyPiece)) {
  323. if (NewKey[0] != '\0') {
  324. StringCatA (NewKey, "\\");
  325. }
  326. StringCat (NewKey, g_KeyPiece);
  327. if (!(hItem = KeyTreeFindChildItem (hParent, g_KeyPiece))) {
  328. Created = TRUE;
  329. if (Next) {
  330. Index = MemDbAddKey (NewKey);
  331. if (!Index) {
  332. return NULL;
  333. }
  334. } else {
  335. Index = MemDbAddKey (NewKey);
  336. if (!Index) {
  337. return NULL;
  338. }
  339. }
  340. if (!(hItem = KeyAddItem (g_KeyPiece, hParent, Index))) {
  341. return NULL;
  342. }
  343. }
  344. hParent = hItem;
  345. Key = Next;
  346. }
  347. if (!Created) {
  348. MessageBox (NULL, "Key already exists", "MemDb Editor", MB_OK|MB_ICONEXCLAMATION);
  349. }
  350. return hParent;
  351. }