File indexing completed on 2024-04-21 11:14:27
0001 /* markdown: a C implementation of John Gruber's Markdown markup language. 0002 * 0003 * Copyright (C) 2007 David L Parsons. 0004 * The redistribution terms are provided in the COPYRIGHT file that must 0005 * be distributed with this source code. 0006 */ 0007 #include <stdio.h> 0008 #include <string.h> 0009 #include <stdarg.h> 0010 #include <stdlib.h> 0011 #include <time.h> 0012 #include <ctype.h> 0013 0014 #include "config.h" 0015 0016 #include "cstring.h" 0017 #include "markdown.h" 0018 #include "amalloc.h" 0019 0020 /* free a (single) line 0021 */ 0022 void 0023 ___mkd_freeLine(Line *ptr) 0024 { 0025 DELETE(ptr->text); 0026 free(ptr); 0027 } 0028 0029 0030 /* free a list of lines 0031 */ 0032 void 0033 ___mkd_freeLines(Line *p) 0034 { 0035 if (p->next) 0036 ___mkd_freeLines(p->next); 0037 ___mkd_freeLine(p); 0038 } 0039 0040 0041 /* bye bye paragraph. 0042 */ 0043 void 0044 ___mkd_freeParagraph(Paragraph *p) 0045 { 0046 if (p->next) 0047 ___mkd_freeParagraph(p->next); 0048 if (p->down) 0049 ___mkd_freeParagraph(p->down); 0050 if (p->text) 0051 ___mkd_freeLines(p->text); 0052 if (p->ident) 0053 free(p->ident); 0054 if (p->lang) 0055 free(p->lang); 0056 free(p); 0057 } 0058 0059 0060 /* bye bye footnote. 0061 */ 0062 void 0063 ___mkd_freefootnote(Footnote *f) 0064 { 0065 DELETE(f->tag); 0066 DELETE(f->link); 0067 DELETE(f->title); 0068 if ( f->text) ___mkd_freeParagraph(f->text); 0069 } 0070 0071 0072 /* bye bye footnotes. 0073 */ 0074 void 0075 ___mkd_freefootnotes(MMIOT *f) 0076 { 0077 int i; 0078 0079 if ( f->footnotes ) { 0080 for (i=0; i < S(f->footnotes->note); i++) 0081 ___mkd_freefootnote( &T(f->footnotes->note)[i] ); 0082 DELETE(f->footnotes->note); 0083 free(f->footnotes); 0084 } 0085 } 0086 0087 0088 /* initialize a new MMIOT 0089 */ 0090 void 0091 ___mkd_initmmiot(MMIOT *f, void *footnotes) 0092 { 0093 if ( f ) { 0094 memset(f, 0, sizeof *f); 0095 CREATE(f->in); 0096 CREATE(f->out); 0097 CREATE(f->Q); 0098 if ( footnotes ) 0099 f->footnotes = footnotes; 0100 else { 0101 f->footnotes = malloc(sizeof f->footnotes[0]); 0102 CREATE(f->footnotes->note); 0103 } 0104 } 0105 } 0106 0107 0108 /* free the contents of a MMIOT, but leave the object alone. 0109 */ 0110 void 0111 ___mkd_freemmiot(MMIOT *f, void *footnotes) 0112 { 0113 if ( f ) { 0114 DELETE(f->in); 0115 DELETE(f->out); 0116 DELETE(f->Q); 0117 if ( f->footnotes != footnotes ) 0118 ___mkd_freefootnotes(f); 0119 memset(f, 0, sizeof *f); 0120 } 0121 } 0122 0123 0124 /* free lines up to an barrier. 0125 */ 0126 void 0127 ___mkd_freeLineRange(Line *anchor, Line *stop) 0128 { 0129 Line *r = anchor->next; 0130 0131 if ( r != stop ) { 0132 while ( r && (r->next != stop) ) 0133 r = r->next; 0134 if ( r ) r->next = 0; 0135 ___mkd_freeLines(anchor->next); 0136 } 0137 anchor->next = 0; 0138 } 0139 0140 0141 /* clean up everything allocated in __mkd_compile() 0142 */ 0143 void 0144 mkd_cleanup(Document *doc) 0145 { 0146 if ( doc && (doc->magic == VALID_DOCUMENT) ) { 0147 if ( doc->ctx ) { 0148 ___mkd_freemmiot(doc->ctx, 0); 0149 free(doc->ctx); 0150 } 0151 0152 if ( doc->code) ___mkd_freeParagraph(doc->code); 0153 if ( doc->title) ___mkd_freeLine(doc->title); 0154 if ( doc->author) ___mkd_freeLine(doc->author); 0155 if ( doc->date) ___mkd_freeLine(doc->date); 0156 if ( T(doc->content) ) ___mkd_freeLines(T(doc->content)); 0157 memset(doc, 0, sizeof doc[0]); 0158 free(doc); 0159 } 0160 }