resource.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* markdown: a C implementation of John Gruber's Markdown markup language.
  2. *
  3. * Copyright (C) 2007 David L Parsons.
  4. * The redistribution terms are provided in the COPYRIGHT file that must
  5. * be distributed with this source code.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <ctype.h>
  13. #include "config.h"
  14. #include "cstring.h"
  15. #include "markdown.h"
  16. #include "amalloc.h"
  17. /* free a (single) line
  18. */
  19. void
  20. ___mkd_freeLine(Line *ptr)
  21. {
  22. DELETE(ptr->text);
  23. free(ptr);
  24. }
  25. /* free a list of lines
  26. */
  27. void
  28. ___mkd_freeLines(Line *p)
  29. {
  30. if (p->next)
  31. ___mkd_freeLines(p->next);
  32. ___mkd_freeLine(p);
  33. }
  34. /* bye bye paragraph.
  35. */
  36. void
  37. ___mkd_freeParagraph(Paragraph *p)
  38. {
  39. if (p->next)
  40. ___mkd_freeParagraph(p->next);
  41. if (p->down)
  42. ___mkd_freeParagraph(p->down);
  43. if (p->text)
  44. ___mkd_freeLines(p->text);
  45. if (p->ident)
  46. free(p->ident);
  47. if (p->lang)
  48. free(p->lang);
  49. free(p);
  50. }
  51. /* bye bye footnote.
  52. */
  53. void
  54. ___mkd_freefootnote(Footnote *f)
  55. {
  56. DELETE(f->tag);
  57. DELETE(f->link);
  58. DELETE(f->title);
  59. if ( f->text) ___mkd_freeParagraph(f->text);
  60. }
  61. /* bye bye footnotes.
  62. */
  63. void
  64. ___mkd_freefootnotes(MMIOT *f)
  65. {
  66. int i;
  67. if ( f->footnotes ) {
  68. for (i=0; i < S(f->footnotes->note); i++)
  69. ___mkd_freefootnote( &T(f->footnotes->note)[i] );
  70. DELETE(f->footnotes->note);
  71. free(f->footnotes);
  72. }
  73. }
  74. /* initialize a new MMIOT
  75. */
  76. void
  77. ___mkd_initmmiot(MMIOT *f, void *footnotes)
  78. {
  79. if ( f ) {
  80. memset(f, 0, sizeof *f);
  81. CREATE(f->in);
  82. CREATE(f->out);
  83. CREATE(f->Q);
  84. if ( footnotes )
  85. f->footnotes = footnotes;
  86. else {
  87. f->footnotes = malloc(sizeof f->footnotes[0]);
  88. CREATE(f->footnotes->note);
  89. }
  90. }
  91. }
  92. /* free the contents of a MMIOT, but leave the object alone.
  93. */
  94. void
  95. ___mkd_freemmiot(MMIOT *f, void *footnotes)
  96. {
  97. if ( f ) {
  98. DELETE(f->in);
  99. DELETE(f->out);
  100. DELETE(f->Q);
  101. if ( f->footnotes != footnotes )
  102. ___mkd_freefootnotes(f);
  103. memset(f, 0, sizeof *f);
  104. }
  105. }
  106. /* free lines up to an barrier.
  107. */
  108. void
  109. ___mkd_freeLineRange(Line *anchor, Line *stop)
  110. {
  111. Line *r = anchor->next;
  112. if ( r != stop ) {
  113. while ( r && (r->next != stop) )
  114. r = r->next;
  115. if ( r ) r->next = 0;
  116. ___mkd_freeLines(anchor->next);
  117. }
  118. anchor->next = 0;
  119. }
  120. /* clean up everything allocated in __mkd_compile()
  121. */
  122. void
  123. mkd_cleanup(Document *doc)
  124. {
  125. if ( doc && (doc->magic == VALID_DOCUMENT) ) {
  126. if ( doc->ctx ) {
  127. ___mkd_freemmiot(doc->ctx, 0);
  128. free(doc->ctx);
  129. }
  130. if ( doc->code) ___mkd_freeParagraph(doc->code);
  131. if ( doc->title) ___mkd_freeLine(doc->title);
  132. if ( doc->author) ___mkd_freeLine(doc->author);
  133. if ( doc->date) ___mkd_freeLine(doc->date);
  134. if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
  135. memset(doc, 0, sizeof doc[0]);
  136. free(doc);
  137. }
  138. }