Debian Codegraph @

ext/resource.c

raw symbols SCIP
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
14 #include "config.h"
15
16 #include "cstring.h"
17 #include "markdown.h"
18 #include "amalloc.h"
19
20 /* free a (single) line
21 */
22 void
23 ___mkd_freeLine(Line *ptr)
24 {
25 DELETE(ptr->text);
26 free(ptr);
27 }
28
29
30 /* free a list of lines
31 */
32 void
33 ___mkd_freeLines(Line *p)
34 {
35 if (p->next)
36 ___mkd_freeLines(p->next);
37 ___mkd_freeLine(p);
38 }
39
40
41 /* bye bye paragraph.
42 */
43 void
44 ___mkd_freeParagraph(Paragraph *p)
45 {
46 if (p->next)
47 ___mkd_freeParagraph(p->next);
48 if (p->down)
49 ___mkd_freeParagraph(p->down);
50 if (p->text)
51 ___mkd_freeLines(p->text);
52 if (p->ident)
53 free(p->ident);
54 if (p->lang)
55 free(p->lang);
56 free(p);
57 }
58
59
60 /* bye bye footnote.
61 */
62 void
63 ___mkd_freefootnote(Footnote *f)
64 {
65 DELETE(f->tag);
66 DELETE(f->link);
67 DELETE(f->title);
68 if ( f->text) ___mkd_freeParagraph(f->text);
69 }
70
71
72 /* bye bye footnotes.
73 */
74 void
75 ___mkd_freefootnotes(MMIOT *f)
76 {
77 int i;
78
79 if ( f->footnotes ) {
80 for (i=0; i < S(f->footnotes->note); i++)
81 ___mkd_freefootnote( &T(f->footnotes->note)[i] );
82 DELETE(f->footnotes->note);
83 free(f->footnotes);
84 }
85 }
86
87
88 /* initialize a new MMIOT
89 */
90 void
91 ___mkd_initmmiot(MMIOT *f, void *footnotes)
92 {
93 if ( f ) {
94 memset(f, 0, sizeof *f);
95 CREATE(f->in);
96 CREATE(f->out);
97 CREATE(f->Q);
98 if ( footnotes )
99 f->footnotes = footnotes;
100 else {
101 f->footnotes = malloc(sizeof f->footnotes[0]);
102 CREATE(f->footnotes->note);
103 }
104 }
105 }
106
107
108 /* free the contents of a MMIOT, but leave the object alone.
109 */
110 void
111 ___mkd_freemmiot(MMIOT *f, void *footnotes)
112 {
113 if ( f ) {
114 DELETE(f->in);
115 DELETE(f->out);
116 DELETE(f->Q);
117 if ( f->footnotes != footnotes )
118 ___mkd_freefootnotes(f);
119 memset(f, 0, sizeof *f);
120 }
121 }
122
123
124 /* free lines up to an barrier.
125 */
126 void
127 ___mkd_freeLineRange(Line *anchor, Line *stop)
128 {
129 Line *r = anchor->next;
130
131 if ( r != stop ) {
132 while ( r && (r->next != stop) )
133 r = r->next;
134 if ( r ) r->next = 0;
135 ___mkd_freeLines(anchor->next);
136 }
137 anchor->next = 0;
138 }
139
140
141 /* clean up everything allocated in __mkd_compile()
142 */
143 void
144 mkd_cleanup(Document *doc)
145 {
146 if ( doc && (doc->magic == VALID_DOCUMENT) ) {
147 if ( doc->ctx ) {
148 ___mkd_freemmiot(doc->ctx, 0);
149 free(doc->ctx);
150 }
151
152 if ( doc->code) ___mkd_freeParagraph(doc->code);
153 if ( doc->title) ___mkd_freeLine(doc->title);
154 if ( doc->author) ___mkd_freeLine(doc->author);
155 if ( doc->date) ___mkd_freeLine(doc->date);
156 if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
157 memset(doc, 0, sizeof doc[0]);
158 free(doc);
159 }
160 }