tree.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/tree.h>
  19. GRUB_EXPORT(grub_tree_add_sibling);
  20. GRUB_EXPORT(grub_tree_add_child);
  21. GRUB_EXPORT(grub_tree_remove_node);
  22. GRUB_EXPORT(grub_tree_next_node);
  23. void
  24. grub_tree_add_sibling (grub_tree_t pre, grub_tree_t cur)
  25. {
  26. cur->next = pre->next;
  27. pre->next = cur;
  28. cur->parent = pre->parent;
  29. }
  30. void
  31. grub_tree_add_child (grub_tree_t parent, grub_tree_t cur, int index)
  32. {
  33. grub_tree_t *p, q;
  34. p = &parent->child;
  35. q = *p;
  36. while ((index) && (q))
  37. {
  38. index--;
  39. p = &q->next;
  40. q = *p;
  41. }
  42. cur->next = *p;
  43. *p = cur;
  44. cur->parent = parent;
  45. }
  46. void
  47. grub_tree_remove_node (grub_tree_t cur)
  48. {
  49. grub_tree_t *p, q;
  50. if (! cur->parent)
  51. return;
  52. p = &cur->parent->child;
  53. q = *p;
  54. while (q != cur)
  55. {
  56. p = &q->next;
  57. q = *p;
  58. }
  59. *p = cur->next;
  60. }
  61. void *
  62. grub_tree_next_node (grub_tree_t root, grub_tree_t pre)
  63. {
  64. if (! pre)
  65. return root;
  66. if (pre->child)
  67. return pre->child;
  68. while (1)
  69. {
  70. if (pre == root)
  71. return 0;
  72. if (pre->next)
  73. break;
  74. pre = pre->parent;
  75. }
  76. return pre->next;
  77. }