-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathresource.c
More file actions
178 lines (153 loc) · 3.14 KB
/
resource.c
File metadata and controls
178 lines (153 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2007 Jessica L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "config.h"
#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"
/* free a (single) line
*/
void
___mkd_freeLine(Line *ptr)
{
if ( ptr->fence_class )
free(ptr->fence_class);
DELETE(ptr->text);
free(ptr);
}
/* free a list of lines
*/
void
___mkd_freeLines(Line *p)
{
if (p->next)
___mkd_freeLines(p->next);
___mkd_freeLine(p);
}
/* bye bye paragraph.
*/
void
___mkd_freeParagraph(Paragraph *p)
{
if (p->next)
___mkd_freeParagraph(p->next);
if (p->down)
___mkd_freeParagraph(p->down);
if (p->text)
___mkd_freeLines(p->text);
if (p->label)
free(p->label);
if (p->ident)
free(p->ident);
if (p->lang)
free(p->lang);
free(p);
}
/* bye bye footnote.
*/
void
___mkd_freefootnote(Footnote *f)
{
DELETE(f->tag);
DELETE(f->link);
DELETE(f->title);
DELETE(f->height);
DELETE(f->width);
DELETE(f->extended_attr);
if ( f->text) ___mkd_freeParagraph(f->text);
}
/* bye bye footnotes.
*/
void
___mkd_freefootnotes(MMIOT *f)
{
int i;
if ( f->footnotes ) {
for (i=0; i < S(f->footnotes->note); i++)
___mkd_freefootnote( &T(f->footnotes->note)[i] );
DELETE(f->footnotes->note);
free(f->footnotes);
}
}
/* initialize a new MMIOT
*/
void
___mkd_initmmiot(MMIOT *f, void *footnotes, mkd_flag_t *flags)
{
if ( f ) {
memset(f, 0, sizeof *f);
CREATE(f->in);
CREATE(f->out);
CREATE(f->Q);
CREATE(f->extratags);
if ( footnotes )
f->footnotes = footnotes;
else {
f->footnotes = calloc(1, sizeof f->footnotes[0]);
CREATE(f->footnotes->note);
}
if ( flags )
COPY_FLAGS(f->flags, *flags);
else
mkd_init_flags(&f->flags);
if ( is_flag_set(&f->flags, MKD_HTML5) )
mkd_add_html5_tags(f);
}
}
/* free the contents of a MMIOT, but leave the object alone.
*/
void
___mkd_freemmiot(MMIOT *f, void *footnotes)
{
if ( f ) {
DELETE(f->in);
DELETE(f->out);
DELETE(f->Q);
DELETE(f->extratags);
if ( f->footnotes != footnotes )
___mkd_freefootnotes(f);
memset(f, 0, sizeof *f);
}
}
/* free lines up to an barrier.
*/
void
___mkd_freeLineRange(Line *anchor, Line *stop)
{
Line *r = anchor->next;
if ( r != stop ) {
while ( r && (r->next != stop) )
r = r->next;
if ( r ) r->next = 0;
___mkd_freeLines(anchor->next);
}
anchor->next = 0;
}
/* clean up everything allocated in __mkd_compile()
*/
void
mkd_cleanup(Document *doc)
{
if ( doc && (doc->magic == VALID_DOCUMENT) ) {
if ( doc->ctx ) {
___mkd_freemmiot(doc->ctx, 0);
free(doc->ctx);
}
if ( doc->code) ___mkd_freeParagraph(doc->code);
if ( doc->title) ___mkd_freeLine(doc->title);
if ( doc->author) ___mkd_freeLine(doc->author);
if ( doc->date) ___mkd_freeLine(doc->date);
if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
memset(doc, 0, sizeof doc[0]);
free(doc);
}
}