1 /*
2  * Fig2dev: Translate Fig code to various Devices
3  * Copyright (c) 1991 by Micah Beck
4  * Parts Copyright (c) 1985-1988 by Supoj Sutanthavibul
5  * Parts Copyright (c) 1989-2015 by Brian V. Smith
6  * Parts Copyright (c) 2015-2020 by Thomas Loimer
7  *
8  * Any party obtaining a copy of these files is granted, free of charge, a
9  * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
10  * nonexclusive right and license to deal in this software and documentation
11  * files (the "Software"), including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense and/or sell copies
13  * of the Software, and to permit persons who receive copies from any such
14  * party to do so, with the only requirement being that the above copyright
15  * and this permission notice remain intact.
16  *
17  */
18 
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include "object.h"
24 #include "free.h"
25 
26 void
free_arc(F_arc ** list)27 free_arc(F_arc **list)
28 {
29 	F_arc	*a, *arc;
30 
31 	for (a = *list; a != NULL;) {
32 	    arc = a;
33 	    a = a->next;
34 	    if (arc->for_arrow) free(arc->for_arrow);
35 	    if (arc->back_arrow) free(arc->back_arrow);
36 	    if (arc->comments) free_comments(arc->comments);
37 	    free(arc);
38 	}
39 	*list = NULL;
40 }
41 
42 void
free_compound(F_compound ** list)43 free_compound(F_compound **list)
44 {
45 	F_compound	*c, *compound;
46 
47 	for (c = *list; c != NULL;) {
48 	    compound = c;
49 	    c = c->next;
50 	    free_arc(&compound->arcs);
51 	    free_compound(&compound->compounds);
52 	    free_ellipse(&compound->ellipses);
53 	    free_line(&compound->lines);
54 	    free_spline(&compound->splines);
55 	    free_text(&compound->texts);
56 	    if (compound->comments) free_comments(compound->comments);
57 	    free(compound);
58 	}
59 	*list = NULL;
60 }
61 
62 void
free_ellipse(F_ellipse ** list)63 free_ellipse(F_ellipse **list)
64 {
65 	F_ellipse	*e, *ellipse;
66 
67 	for (e = *list; e != NULL;) {
68 	    ellipse = e;
69 	    e = e->next;
70 	    if (ellipse->comments) free_comments(ellipse->comments);
71 	    free(ellipse);
72 	}
73 	*list = NULL;
74 }
75 
76 void
free_line(F_line ** list)77 free_line(F_line **list)
78 {
79 	F_line	*l, *line;
80 
81 	for (l = *list; l != NULL;) {
82 	    line = l;
83 	    l = l->next;
84 	    free_linestorage(line);
85 	}
86 	*list = NULL;
87 }
88 
89 void
free_text(F_text ** list)90 free_text(F_text **list)
91 {
92 	F_text	*t, *text;
93 
94 	for (t = *list; t != NULL;) {
95 	    text = t;
96 	    t = t->next;
97 	    free(text->cstring);
98 	    if (text->comments) free_comments(text->comments);
99 	    free(text);
100 	}
101 	*list = NULL;
102 }
103 
104 void
free_spline(F_spline ** list)105 free_spline(F_spline **list)
106 {
107 	F_spline	*s, *spline;
108 
109 	for (s = *list; s != NULL;) {
110 	    spline = s;
111 	    s = s->next;
112 	    free_splinestorage(spline);
113 	}
114 	*list = NULL;
115 }
116 
117 void
free_splinestorage(F_spline * s)118 free_splinestorage(F_spline *s)
119 {
120 	F_point		*p, *q;
121 	F_control	*a, *b;
122 
123 	for (p = s->points; p != NULL; p = q) {
124 	    q = p->next;
125 	    free(p);
126 	}
127 	for (a = s->controls; a != NULL; a = b) {
128 	    b = a->next;
129 	    free(a);
130 	}
131 	if (s->for_arrow) free(s->for_arrow);
132 	if (s->back_arrow) free(s->back_arrow);
133 	if (s->comments) free_comments(s->comments);
134 	free(s);
135 }
136 
137 void
free_linestorage(F_line * l)138 free_linestorage(F_line *l)
139 {
140 	F_point	*p, *q;
141 
142 	for (p = l->points; p != NULL; p = q) {
143 	    q = p->next;
144 	    free(p);
145 	    }
146 	if (l->for_arrow) free(l->for_arrow);
147 	if (l->back_arrow) free(l->back_arrow);
148 	if (l->pic) {
149 		free(l->pic->file);
150 		free(l->pic->bitmap);
151 #ifdef V4_0
152 		free_compound(l->pic->compound);
153 #endif
154 		free(l->pic);
155 	}
156 	if (l->comments) free_comments(l->comments);
157 	free(l);
158 }
159 
160 void
free_comments(F_comment * c)161 free_comments(F_comment *c)
162 {
163 	F_comment	*d, *n;
164 
165 	for (d = c; d != NULL; d = n) {
166 		n = d->next;
167 		free(d);
168 	}
169 }
170