libhighlight.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * Author: g0tsu
  12. * Email: g0tsu at dnmx.0rg
  13. */
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <libhighlight.h>
  22. /*
  23. * A list for tokens
  24. */
  25. hl_node * hl_node_create() {
  26. hl_node * root = (hl_node *)malloc(sizeof(*root));
  27. memset(root, 0, sizeof(*root));
  28. root->root = root;
  29. return root;
  30. }
  31. hl_node * hl_node_insert(hl_node * node) {
  32. hl_node *children = hl_node_create();
  33. children->root = node->root;
  34. children->parent = node;
  35. node->children = children;
  36. return children;
  37. }
  38. hl_node * hl_node_at(hl_node * node, int at) {
  39. hl_node *iter = node->root;
  40. for (int i = 0; i < at; i++) {
  41. if (iter->children)
  42. iter = iter->children;
  43. }
  44. return iter;
  45. }
  46. void hl_node_free(hl_node * root) {
  47. hl_node *iter = root->root;
  48. iter->parent = NULL;
  49. while (iter->children) {
  50. iter = iter->children;
  51. }
  52. while (iter->parent) {
  53. iter = iter->parent;
  54. if (iter->children->text)
  55. free(iter->children->text);
  56. free(iter->children);
  57. }
  58. if (iter->text)
  59. free(iter->text);
  60. free(iter);
  61. }
  62. void hl_root_free(hl_root *root) {
  63. if (root) {
  64. if (root->node->root)
  65. hl_node_free(root->node->root);
  66. free(root);
  67. }
  68. }